-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
106 lines (80 loc) · 3.36 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
const chalk = require('chalk');
const debug = require('debug')('meta-exec');
const cp = require('child_process');
const { getFileLocation } = require('get-meta-file');
const fileLocation = getFileLocation() || '';
const metaDir = fileLocation.replace('.meta', '');
module.exports = function (options, cb, errorCb) {
if (options.stdio === undefined) options.stdio = [0, 1, 2];
if (options.suppressLogging === undefined) options.suppressLogging = false;
if (options.suppressLogging) options.stdio[1] = 'ignore';
options.cmd = options.cmd || options.command;
options.dir = options.dir || process.cwd();
options.displayDir = options.displayDir || options.dir.replace(metaDir, '');
options.parallel = options.parallel || false;
debug(`executing command ${options.cmd} in dir ${options.dir}`);
var code = null;
const execPromise = options.parallel
? new Promise((resolve, reject) => {
cp.exec(
options.cmd,
{
cwd: options.dir,
env: process.env,
stdio: options.stdio,
},
(err, stdout, stderr) => {
if (err) return reject(err);
if (err) {
let errorMessage = `${chalk.red(options.displayDir)} '${options.cmd}' exited with code: ${err}`;
if (errorCb) errorCb(new Error(errorMessage));
if (!options.suppressLogging) console.error(errorMessage);
if (cb) return cb(null, { err: errorMessage });
return;
}
if (!options.suppressLogging) console.log(`\n${chalk.cyan(options.displayDir)}:`);
stdout.length && console.log(stdout.trim());
stderr.length && console.log(stderr.trim());
let success = chalk.green(`${options.displayDir} ✓`);
if (!options.suppressLogging) console.log(success);
return resolve(err ? 1 : 0);
}
);
})
: new Promise((resolve, reject) => {
if (!options.suppressLogging) console.log(`\n${chalk.cyan(options.displayDir)}:`);
try {
const code = cp.execSync(options.cmd, {
cwd: options.dir,
env: process.env,
stdio: options.stdio,
});
if (code) {
let errorMessage = `${chalk.red(options.displayDir)} '${options.cmd}' exited with code: ${code}`;
if (errorCb) errorCb(new Error(errorMessage));
if (!options.suppressLogging) console.error(errorMessage);
if (cb) return cb(null, { err: errorMessage });
return;
}
let success = chalk.green(`${options.displayDir} ✓`);
if (!options.suppressLogging) console.log(success);
return resolve(success);
} catch (err) {
if (errorCb) errorCb(err);
// if there is no error callback, we're just going to forward the output
let errorMessage = `${chalk.red(options.displayDir)}: command '${
options.cmd
}' exited with error: ${err.toString()}`;
if (!options.suppressLogging) console.error(errorMessage);
if (cb) return cb(null, { error: errorMessage });
return reject(1);
}
});
execPromise
.then((success) => {
if (cb) return cb(null, { output: success });
})
.catch((errorMessage) => {
if (cb) return cb(null, { error: errorMessage });
});
};