@vbarbarosh/node-helpers 2.4.0 → 2.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/progress.js +37 -0
- package/src/shell_json.js +25 -0
package/package.json
CHANGED
package/src/progress.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
// - update progress by delta
|
|
2
|
+
// - update progress by replacing total amount
|
|
3
|
+
// - render eta
|
|
4
|
+
// - render items/second
|
|
5
|
+
// - render percentage
|
|
6
|
+
// - time spent
|
|
7
|
+
function progress(total)
|
|
8
|
+
{
|
|
9
|
+
let done = 0;
|
|
10
|
+
const time0 = Date.now();
|
|
11
|
+
const out = {
|
|
12
|
+
done: 0,
|
|
13
|
+
total,
|
|
14
|
+
eta: null,
|
|
15
|
+
rate: null,
|
|
16
|
+
duration: 0,
|
|
17
|
+
progress: null,
|
|
18
|
+
percentage: null,
|
|
19
|
+
add: function (delta = 0) {
|
|
20
|
+
out.done += delta;
|
|
21
|
+
out.refresh();
|
|
22
|
+
},
|
|
23
|
+
update: function (done) {
|
|
24
|
+
out.done = done;
|
|
25
|
+
out.refresh();
|
|
26
|
+
},
|
|
27
|
+
refresh: function () {
|
|
28
|
+
out.duration = (Date.now() - time0)/1000;
|
|
29
|
+
out.rate = out.done/out.duration;
|
|
30
|
+
out.percentage = !out.total ? null : out.done/out.total;
|
|
31
|
+
out.eta = !out.total ? null : (out.total - out.done)/out.rate;
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
return out;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
module.exports = progress;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
const child_process = require('child_process');
|
|
2
|
+
|
|
3
|
+
function shell_json(args, options)
|
|
4
|
+
{
|
|
5
|
+
return new Promise(function (resolve, reject) {
|
|
6
|
+
child_process.execFile(args[0], args.slice(1), options, function (error, stdout, stderr) {
|
|
7
|
+
if (error) {
|
|
8
|
+
reject(error);
|
|
9
|
+
}
|
|
10
|
+
else if (stderr) {
|
|
11
|
+
reject(new Error(`Process terminated with the following stderr:\n\n${stderr}`));
|
|
12
|
+
}
|
|
13
|
+
else {
|
|
14
|
+
try {
|
|
15
|
+
resolve(JSON.parse(stdout));
|
|
16
|
+
}
|
|
17
|
+
catch (error) {
|
|
18
|
+
reject(error);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
module.exports = shell_json;
|