forked from yyx990803/shell-task
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask.js
More file actions
170 lines (148 loc) · 2.96 KB
/
task.js
File metadata and controls
170 lines (148 loc) · 2.96 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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
var spawn = require('child_process').spawn
/**
* Task constructor.
*
* @constructor
* @param {String} cmd - initial command
* @param {Object} [opts] - options to be passed to child
* processes
*/
function Task (cmd, opts) {
this.opts = opts || {
stdio: 'inherit'
}
this.queue = []
this.then(cmd)
}
/**
* Queue an additional step.
*
* @param {String|Function} cmd
* @return {Task}
*/
Task.prototype.then = function (cmd) {
if (typeof cmd === 'string') {
cmd = splitCmd(cmd)
}
this.queue.push(cmd)
return this
}
/**
* Run all the queued steps.
*
* @param {Function} [cb]
*/
Task.prototype.run = function (cb) {
this.cb = cb || defaultCb
run(this)
}
/**
* The actual queue runner.
*
* @param {Task} task
*/
function run (task) {
var step = task.queue.shift()
if (typeof step === 'function') {
log('function: ' + (step.name || 'anonymous') + '()', 32)
try {
step(next)
} catch (e) {
handle(e)
}
} else {
log(step.raw)
if (isSleep(step)) {
return setTimeout(next, +step.args[0])
}
var child = spawn(step.cmd, step.args, task.opts)
child.once('error', handle)
child.once('exit', next)
}
/**
* Continue running the next task if no error.
*
* @param {Number} code - exit code of last task
*/
function next (code) {
if (typeof code === 'number' && code !== 0) {
handle(new Error('process exited with unexpected code: ' + code))
} else {
if (task.queue.length) {
run(task)
} else {
task.cb()
}
}
}
/**
* Handle errors.
*
* @param {Error} err
*/
function handle (err) {
task.cb(err, next)
}
}
/**
* Split a command string into the command and a list of args.
*
* @param {String} cmd
* @return {Object}
*/
function splitCmd (cmd) {
var split = cmd.match(/(?:"[^"]*"|'[^']*'|[^\s"']+)+/g)
return {
cmd: split[0].trim(),
args: split.slice(1).map(processArg),
raw: cmd
}
}
/**
* Process a single argument, strip whitespaces and quotes
*
* @param {String} str
* @return {String}
*/
function processArg (str) {
str = str.trim()
var a = str.charCodeAt(0)
var b = str.charCodeAt(str.length - 1)
return a === b && (a === 0x22 || a === 0x27)
? str.slice(1, -1)
: str
}
/**
* Check if the command is a sleep command...
* If yes we just simulate it.
*
* @param {Object} step
* @return {Boolean}
*/
function isSleep (step) {
return step.cmd === 'sleep' &&
step.args[0] == +step.args[0]
}
/**
* The default callback if no callback if provided to run()
*
* @param {Error} [err]
*/
function defaultCb (err) {
if (err) {
log('Task failed.', 31)
log(err.toString(), 31)
} else {
log('Task completed.', 32)
}
}
/**
* Colorful log utility.
*
* @param {String} str
* @param {Number} colorCode
*/
function log (str, colorCode) {
console.log('\x1B[' + (colorCode || 36) + 'm> ' + str + '\x1B[39m')
}
module.exports = Task