This repository has been archived by the owner on Apr 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
110 lines (90 loc) · 2.31 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
107
108
109
110
var child_process = require('child_process');
var Promise = require('lie');
var type = require('typelib');
var c = require('chalk');
var defaults = {
tracking : false, // show start and end of command
verbose : true // write executed commands to console
};
var cmdspawn = function (conf) {
var conf = type.merge({}, defaults, conf);
var cmd = function (the_command) {
var args = Array.prototype.slice.call(arguments);
if (args.length > 1) {
the_command = args.join(' ');
}
else if (the_command instanceof Array) {
the_command = the_command.join(' ');
}
return new Promise(function (resolve, reject) {
if (conf.tracking) {
console.log(c.blue('start') + ' ' + the_command);
}
else if (conf.verbose) {
console.log(' ' + the_command);
}
var instructions = the_command.split(' ');
var command = instructions.shift();
var instance = child_process.spawn(command, instructions);
instance.stdout.on('data', function (output) {
process.stdout.write(output.toString());
});
instance.stderr.on('data', function (err) {
process.stderr.write(err.toString());
});
instance.on('close', function (code) {
if (conf.tracking) {
console.log(' ' + c.blue('end') + ' ' + the_command);
}
if (code == 0) {
resolve();
}
else { // code != 0
reject(new Error('command ' + command + ' exited with code ' + code));
}
});
instance.on('error', function (err) {
console.log('failed calling ' + command + '; potentially unrecognized by system');
reject(err);
})
});
};
cmd.fin = function (cb) {
return function () {
cb(null);
console.log("\n");
};
};
cmd.noop = function () {};
// promise error reporting
cmd.err = function (reason) {
if (reason) {
if (reason.stack) {
console.log('');
console.log(c.red.dim('error detected'));
console.log(reason.stack);
}
else { // no error stack
console.log('');
console.log(c.red.dim(reason))
}
}
return Promise.reject();
};
// no error messages for error
cmd.silent = function (reason) {
return Promise.reject();
};
// functor to be used in promise chaining
cmd.f = function (command) {
return function () {
return cmd(command);
};
};
return cmd;
}
// export dependencies
cmdspawn.lib = {
Promise: Promise
}
module.exports = cmdspawn;