diff --git a/index.js b/index.js index 90e6c12..dcbcb43 100644 --- a/index.js +++ b/index.js @@ -32,6 +32,10 @@ module.exports.compress = function (algorithm, parameters, callback = undefined, throw 'Parameters cannot be undefined or null'; if (parameters.dir && parameters.files) throw 'Cannot use dir and files property at the same time'; + if (!parameters.destination && parameters.files) + throw 'Cannot use files property without destination property'; + if (!parameters.destination && parameters.dir) + parameters.destination = path.dirname(parameters.dir); algorithm = (algorithm === '' || algorithm === null || algorithm === undefined) ? '7z' : algorithm; @@ -47,33 +51,21 @@ module.exports.compress = function (algorithm, parameters, callback = undefined, const dllPath = sevenZipBin.path7za; - if (callback) + return new Promise((resolve, reject) => { - const sevenZipProcess = child_process.execFile(dllPath, buildCommandArgs('compress', parameters, algorithm), {shell: true, detached: false}, (error, stdout, stderr) => { - callback(error); - }); - - let send = false; - sevenZipProcess.stdout.on('data', (data) => + const sevenZipProcess = child_process.execFile(dllPath, buildCommandArgs('compress', parameters, algorithm), { shell: true, detached: false }, (error, stdout, stderr) => { - if (data.includes('1%')) { - send = true; - } - else if (data.includes('99%')) { - send = false; + if (progressCallback) { + progressCallback({ + progress: 100, + fileProcessed: '' + }); } - - if (send) { - progressCallback(parseProgress(data)); - } - }); - return; - } - - return new Promise((resolve, reject) => - { - const sevenZipProcess = child_process.execFile(dllPath, buildCommandArgs('compress', parameters, algorithm), {shell: true, detached: false}, (error, stdout, stderr) => { - resolve(error); + + if (callback) callback(error); + + if (error) reject(error); + else resolve(); }); let send = false; @@ -83,7 +75,7 @@ module.exports.compress = function (algorithm, parameters, callback = undefined, else if (data.includes('99%')) send = false; - if (send && progressCallback !== undefined && progressCallback !== null) { + if (progressCallback && send) { progressCallback(parseProgress(data)); } }); @@ -108,42 +100,32 @@ module.exports.extract = function (algorithm, parameters, callback = undefined, const dllPath = sevenZipBin.path7za; - if (callback) + return new Promise((resolve, reject) => { - const sevenZipProcess = child_process.execFile(dllPath, buildCommandArgs('extract', parameters), {shell: true, detached: false}, (error, stdout, stderr) => { - callback(error); - }); - - let send = false; - sevenZipProcess.stdout.on('data', (data) => { - if (data.includes('1%')) { - send = true; - } - else if (data.includes('99%')) { - send = false; + const sevenZipProcess = child_process.execFile(dllPath, buildCommandArgs('extract', parameters), { shell: true, detached: false }, (error, stdout, stderr) => + { + if (progressCallback) { + progressCallback({ + progress: 100, + fileProcessed: '' + }); } - if (send && progressCallback !== undefined && progressCallback !== null) { - progressCallback(parseProgress(data)); - } - }); - return; - } + if (callback) callback(error); - return new Promise((resolve, reject) => - { - const sevenZipProcess = child_process.execFile(dllPath, buildCommandArgs('extract', parameters), {shell: true, detached: false}, (error, stdout, stderr) => { - resolve(error); + if (error) reject(error); + else resolve(); }); let send = false; - sevenZipProcess.stdout.on('data', (data) => { + sevenZipProcess.stdout.on('data', (data) => + { if (data.includes('1%')) send = true; else if (data.includes('99%')) send = false; - if (send) { + if (progressCallback && send) { progressCallback(parseProgress(data)); } }); diff --git a/package-lock.json b/package-lock.json index df7df05..a295558 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@steezcram/sevenzip", - "version": "1.1.4", + "version": "1.1.5", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@steezcram/sevenzip", - "version": "1.1.4", + "version": "1.1.5", "license": "MIT", "dependencies": { "7zip-bin": "^5.2.0" diff --git a/package.json b/package.json index c9e705b..0ca90dc 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@steezcram/sevenzip", - "version": "1.1.5", + "version": "1.1.6", "description": "7-Zip wrapper around 7za", "homepage": "https://github.com/SteezCram/sevenzip", "repository": {