UNPKG

node-7z-threetwo

Version:

A CommonJs and ESM frontend to 7-Zip, downloads binaries in for Linux, Windows, and Mac OSX, with methods to create SFX self extracting 7z archives targeting different platforms.

57 lines (48 loc) 1.83 kB
'use strict'; const when = require('when'), u = { files: require('../util/files'), replaceNativeSeparator: require('../util/replaceNativeSeparator'), run: require('../util/run'), switches: require('../util/switches') }; /** * Add content to an archive. * @promise Add * @param archive {string} Path to the archive. * @param files {string|array} Files to add. * @param options {Object} An object of acceptable 7-zip switch options. * @resolve {array} Arguments passed to the child-process. * @progress {array} Listed files and directories. * @reject {Error} The error as issued by 7-Zip. */ module.exports = function (archive, files, options, override = false) { return when.promise(function (resolve, reject, progress) { // Convert array of files into a string if needed. files = u.files(files); // Create a string that can be parsed by `run`. let command = '7za a "' + archive + '" ' + files; // Start the command u.run(command, options, override) // When a stdout is emitted, parse each line and search for a pattern. When // the pattern is found, extract the file (or directory) name from it and // pass it to an array. Finally returns this array in the progress function. .progress(function (data) { let entries = []; data.split('\n').forEach(function (line) { if (line.substr(0, 1) === '+') { entries.push(u.replaceNativeSeparator(line.substr(2, line.length))); } }); return progress(entries); }) // When all is done resolve the Promise. .then(function (args) { return resolve(args); }) // Catch the error and pass it to the reject function of the Promise. .catch(function (err) { return reject(err); }); }); };