-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
51 lines (48 loc) · 1.52 KB
/
Copy pathindex.js
File metadata and controls
51 lines (48 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
const { exec, execSync } = require('child_process');
/**
* Run a command asynchronously.
* @param {string} command - Command to run.
* @param {Object} [options] - Exec options.
* @param {function} [callback] - Optional callback(err, stdout, stderr).
* @returns {Promise<{ stdout: string, stderr: string }>}
*/
function run(command, options, callback) {
// options is optional
if (typeof options === 'function') {
callback = options;
options = undefined;
}
return new Promise((resolve, reject) => {
exec(command, options, (err, stdout, stderr) => {
if (callback) callback(err, stdout, stderr);
if (err) {
// Still resolve with output, make error information available
return reject({err, stdout, stderr});
}
resolve({ stdout, stderr });
});
});
}
/**
* Run a command synchronously.
* @param {string} command - Command to run.
* @param {Object} [options] - ExecSync options.
* @returns {{ stdout: string, stderr: string, error: Error|null }}
*/
function runSync(command, options) {
try {
const stdout = execSync(command, { ...options }).toString();
return { stdout, stderr: '', error: null };
} catch (error) {
// Include any partial output available
return {
stdout: error.stdout ? error.stdout.toString() : '',
stderr: error.stderr ? error.stderr.toString() : '',
error
};
}
}
module.exports = {
run,
runSync
};