From 03a592f76d4228080b1f58ed0d073d434d74c5bb Mon Sep 17 00:00:00 2001 From: techno-express Date: Mon, 21 Dec 2020 21:10:47 -0500 Subject: [PATCH] use `spawning` for child process, update version --- package-lock.json | 2 +- package.json | 2 +- util/run.mjs | 48 +++++++++++++++++++++++++---------------------- 3 files changed, 28 insertions(+), 24 deletions(-) diff --git a/package-lock.json b/package-lock.json index 0ea9bf3..6d9270f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "node-7z-archive", - "version": "0.9.11", + "version": "1.0.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 75dcfb8..e21faf6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "node-7z-archive", - "version": "0.9.11", + "version": "1.0.0", "description": "ESM front-end to 7-Zip, featuring alternative full 7z CLI tools, binaries for Linux, Windows, Mac OSX, seamlessly create 7zip SFX self extracting archives targeting different platforms.", "type": "module", "main": "lib/index.mjs", diff --git a/util/run.mjs b/util/run.mjs index bd4ada9..d4d42ea 100644 --- a/util/run.mjs +++ b/util/run.mjs @@ -2,7 +2,7 @@ import { EOL } from 'os'; -import { spawn } from 'child_process'; +import { spawning } from 'node-sys'; import when from 'when'; import { normalize, @@ -92,34 +92,38 @@ export default function (binary = '7z', command = null, switches = {}, override // error's message. Otherwise progress with stdout message. let err; let reg = new RegExp('Error:(' + EOL + '|)?(.*)', 'i'); + let onprogress = (object) => { + progress(object.output); + return args; + }; + + let onerror = (data) => { + let res = reg.exec(data); + if (res) { + err = new Error(res[2].substr(0, res[2].length - 1)); + return err; + } + }; + let res = { cmd: cmd, args: args, options: { - stdio: 'pipe' + stdio: 'pipe', + onprogress: onprogress, + onerror: onerror } }; - //console.log('>> ', res.cmd, res.args.join(' '), res.options,' <<'); - let run = spawn(res.cmd, res.args, res.options); - run.stderr.on('data', function (data) { - let res = reg.exec(data.toString()); - if (res) { - err = new Error(res[2].substr(0, res[2].length - 1)); - } - }); - run.stdout.on('data', function (data) { - return progress(data.toString()); - }); - run.on('error', function (err) { - reject(err); - }); - run.on('close', function (code) { - if (code === 0) { - return fulfill(args); - } - return reject(err, code); - }); + spawning(res.cmd, res.args, res.options) + .then((data) => { + if (data === args) + return fulfill(args); + return reject(err); + }) + .catch((err) => { + return reject(err); + }); }); };