-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
113 lines (91 loc) · 2.43 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
111
112
113
'use strict';
var util = require('./lib/util');
var repl = null;
var instances = [];
exports = module.exports = {};
// get the instance properties used by the REPL
exports.get = function (gulp) {
if (!arguments.length) {
return instances.concat();
}
var length = instances.length;
for (var index = 0; index < length; ++index) {
var instance = instances[index] || {};
if (instance.gulp === gulp) {
return instance;
}
}
return null;
};
// add the given instance to the REPL lookup
exports.add = function (_gulp_) {
if (_gulp_ && !this.get(_gulp_)) {
var gulp = util.getGulp(_gulp_);
instances.push({
gulp: gulp,
index: instances.length,
tasks: util.getTasks(gulp),
runner: gulp.start || gulp.parallel
});
}
return this;
};
// reset the instances array
exports.reset = function () {
instances = [];
return this;
};
// remove the instance from the instances array
exports.remove = function (_gulp_) {
var instance = this.get(_gulp_);
if (instance) {
instances.splice(instance.index, 1);
}
return this;
};
// create a readline instance if there is none
exports.start = function (_gulp_) {
this.add(_gulp_);
// only create one repl listening on stdin
if (repl) { return repl; }
repl = require('readline').createInterface({
input: process.stdin,
output: process.stdout,
completer: function onComplete (line) {
return util.completer(line, instances);
}
});
// queue tasks when line is not empty
repl.on('line', function onLine (input) {
var line = input.trim();
if (!line) { return repl.prompt(); }
var queue = {
found: [],
notFound: line.split(/[ ]+/)
};
instances.forEach(function (inst) {
var tasks = util.getQueue(queue.notFound.join(' '), inst.tasks);
if (tasks.found.length) {
queue.found.push({ inst: inst, tasks: tasks.found });
}
queue.notFound = tasks.notFound;
});
if (queue.notFound.length) {
console.log(' `%s` not found', queue.notFound.join(' '));
return repl.prompt();
}
queue.found.forEach(function (found) {
var result = found.inst.runner.apply(found.inst.gulp, found.tasks);
if (typeof result === 'function') {
result(); // gulp#4.0
}
});
return this;
});
// exit on SIGINT
repl.on('SIGINT', function onSIGINT () {
process.stdout.write('\n');
process.exit(0);
});
return repl;
};