-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
58 lines (50 loc) · 1.05 KB
/
index.js
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
52
53
54
55
56
57
58
/**
*
*
* Author: Saad Irfan
* GitHub: msaaddev
* Twitter: https://twitter.com/msaaddev
*/
const { exec } = require('child_process');
const handleError = require('node-cli-handle-error');
/**
*
*
* @param {cmd} - commands to run
* @returns {Promise} - a promise that runs the command
*/
async function promise(cmd) {
return new Promise((resolve, reject) => {
exec(cmd, (error, stdout, stderr) => {
if (error) {
handleError(error);
reject();
}
resolve();
});
});
}
module.exports = async (opt = {}) => {
const defaultOptions = {
path: ``,
cmd: ``
};
const options = { ...defaultOptions, ...opt };
const { path, cmd } = options;
// changes directory if a path exists
path !== `` ? process.chdir(path) : null;
// checks if there is one command or array of commands
if (typeof cmd !== 'object') {
return promise(cmd);
} else {
for (let i = 0; i < cmd.length; i++) {
await promise(cmd[i]);
let j = i + 1;
if (j === cmd.length) {
return new Promise((resolve, reject) => {
resolve();
});
}
}
}
};