Skip to content

Commit

Permalink
Fix progress and refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
SteezCram committed Nov 5, 2022
1 parent 39ebd3d commit 0788cb8
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 52 deletions.
80 changes: 31 additions & 49 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand All @@ -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));
}
});
Expand All @@ -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));
}
});
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down

0 comments on commit 0788cb8

Please sign in to comment.