#!/usr/bin/env node // SPDX-License-Identifier: AGPL-3.0-or-later /** ---------------------------------------------------------------------- * libtmcsplit * ---------------------------------------------------------------------- * Copyright © * Pellegrino Prevete * 2024, 2025, 2026 * * All rights reserved * ---------------------------------------------------------------------- * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ /* eslint no-unused-vars: [ "error", { "vars": "local" } ] */ const _libcrash = await import( "libcrash-js"); /* global _basename */ const _basename = _libcrash._basename; /* global _dirname */ const _dirname = _libcrash._dirname; /* global _ls */ const _ls = _libcrash._ls; /* global _msg_error */ const _msg_error = _libcrash._msg_error; /* global _msg_info */ const _msg_info = _libcrash._msg_info; /* global _mv */ const _mv = _libcrash._mv; /* global _require */ const _require = _libcrash._require; const _split_file_module = await _require( "split-file"); /* global _split_file */ const _split_file = _split_file_module._split_file; /* global _split_file_by_size */ const _split_file_by_size = _split_file_module._split_file_by_size; async function _output_files_get( _input_file, _output_prefix) { let _file, _mv_args, _output_file, _output_files, _part_no; _output_files = []; const _file_name = _basename( _input_file); const _pattern = `${_file_name}.sf-part` const _dir = _dirname( _input_file); const _dir_content = _ls( _dir); for ( _file of _dir_content ) { if ( _file.startsWith( _pattern)) { _part_no = _file.replace( _pattern, ""); _output_file = `${_output_prefix}${_part_no}`; _mv_args = [ `${_input_file}.sf-part${_part_no}`, _output_file ]; _mv.apply( null, _mv_args); _output_files.push( _output_file); } } return _output_files; } async function _split( _input_file, _output_prefix, _chunks_amount, _size_max) { let _msg, _output_files; const _chunks_amount_not_number = typeof( _chunks_amount) !== "number"; if ( _chunks_amount_not_number ) { _msg = "Chunks amount must be a number."; _msg_error( _msg, 1); } _msg = `Splitting '${_input_file}' ` + `into '${_chunks_amount}' of size '${_size_max}' ` + `into files with prefix '${_output_prefix}':`; _msg_info( _msg); _size_max = Number( _size_max); if ( _chunks_amount > 0 ) { await _split_file( _input_file, _chunks_amount); } else if ( _size_max > 0 ) { await _split_file_by_size( _input_file, _size_max); } _output_files = _output_files_get( _input_file, _output_prefix); return _output_files; } // ES exports export { _split }; // Common Javascript exports // module.exports = { // _split: // _split // };