|
2 | 2 |
|
3 | 3 | var util = require('./lib/util');
|
4 | 4 |
|
5 |
| -exports = module.exports = gulpRepl; |
6 |
| -exports.instances = []; |
7 |
| - |
8 |
| -/** |
9 |
| - * create a readline interface |
10 |
| -**/ |
11 |
| -var repl = require('readline').createInterface({ |
12 |
| - input: process.stdin, |
13 |
| - output: process.stdout, |
14 |
| - completer: function onCompletion(line){ |
15 |
| - return util.completer(line, exports.instances); |
| 5 | +var repl = null; |
| 6 | +var instances = []; |
| 7 | + |
| 8 | +// add the instance to the repl and start it |
| 9 | +exports = module.exports = function (_gulp_) { |
| 10 | + return exports.start(_gulp_); |
| 11 | +}; |
| 12 | + |
| 13 | +// get the instance properties used by the REPL |
| 14 | +exports.get = function (gulp) { |
| 15 | + if (!arguments.length) { |
| 16 | + return instances.concat(); |
16 | 17 | }
|
17 |
| -}); |
18 |
| - |
19 |
| -/** |
20 |
| - * queue tasks when line is not empty |
21 |
| -**/ |
22 |
| -repl.on('line', function onLine(input){ |
23 |
| - var line = input.trim(); |
24 |
| - if(!line){ return repl.prompt(); } |
25 |
| - |
26 |
| - var queue = { |
27 |
| - found: [], |
28 |
| - notFound: line.split(/[ ]+/) |
29 |
| - }; |
30 |
| - |
31 |
| - exports.instances.forEach(function(inst){ |
32 |
| - var tasks = util.getQueue(queue.notFound.join(' '), inst.tasks); |
33 |
| - if(tasks.found.length){ |
34 |
| - queue.found.push({ |
35 |
| - inst: inst, |
36 |
| - tasks: tasks.found |
37 |
| - }); |
| 18 | + |
| 19 | + var length = instances.length; |
| 20 | + |
| 21 | + for (var index = 0; index < length; ++index) { |
| 22 | + var instance = instances[index] || {}; |
| 23 | + if (instance.gulp === gulp) { |
| 24 | + return instance; |
38 | 25 | }
|
39 |
| - queue.notFound = tasks.notFound; |
40 |
| - }); |
| 26 | + } |
41 | 27 |
|
42 |
| - if(queue.notFound.length){ |
43 |
| - var plural = queue.notFound.length > 1; |
| 28 | + return null; |
| 29 | +}; |
44 | 30 |
|
45 |
| - console.log(' `%s` task%s %s not defined yet', |
46 |
| - queue.notFound.join(', '), |
47 |
| - plural ? 's' : '', |
48 |
| - plural ? 'are' : 'is' |
49 |
| - ); |
| 31 | +// add the given instance to the REPL lookup |
| 32 | +exports.add = function (_gulp_) { |
| 33 | + if (_gulp_ && !this.get(_gulp_)) { |
| 34 | + var gulp = util.getGulp(_gulp_); |
50 | 35 |
|
51 |
| - return repl.prompt(); |
| 36 | + instances.push({ |
| 37 | + gulp: gulp, |
| 38 | + index: instances.length, |
| 39 | + tasks: util.getTasks(gulp), |
| 40 | + runner: gulp.start || gulp.parallel |
| 41 | + }); |
52 | 42 | }
|
| 43 | + return this; |
| 44 | +}; |
| 45 | + |
| 46 | +// reset the instances array |
| 47 | +exports.reset = function () { |
| 48 | + instances = []; |
| 49 | + return this; |
| 50 | +}; |
| 51 | + |
| 52 | +// remove the instance from the instances array |
| 53 | +exports.remove = function (_gulp_) { |
| 54 | + var instance = this.get(_gulp_); |
| 55 | + if (instance) { |
| 56 | + instances.splice(instance.index, 1); |
| 57 | + } |
| 58 | + return this; |
| 59 | +}; |
| 60 | + |
| 61 | +// create a readline instance if there is none |
| 62 | +exports.start = function (_gulp_) { |
| 63 | + exports.add(_gulp_); |
53 | 64 |
|
54 |
| - queue.found.forEach(function(found){ |
55 |
| - var result = found.inst.runner.apply(found.inst.gulp, found.tasks); |
56 |
| - if(typeof result === 'function'){ |
57 |
| - result(); // gulp#4.0 |
| 65 | + // only create one repl listening on stdin |
| 66 | + if (repl) { return repl; } |
| 67 | + |
| 68 | + repl = require('readline').createInterface({ |
| 69 | + input: process.stdin, |
| 70 | + output: process.stdout, |
| 71 | + completer: function onComplete (line) { |
| 72 | + return util.completer(line, instances); |
| 73 | + } |
| 74 | + }); |
| 75 | + |
| 76 | + // queue tasks when line is not empty |
| 77 | + repl.on('line', function onLine (input) { |
| 78 | + var line = input.trim(); |
| 79 | + if (!line) { return repl.prompt(); } |
| 80 | + |
| 81 | + var queue = { |
| 82 | + found: [], |
| 83 | + notFound: line.split(/[ ]+/) |
| 84 | + }; |
| 85 | + |
| 86 | + instances.forEach(function (inst) { |
| 87 | + var tasks = util.getQueue(queue.notFound.join(' '), inst.tasks); |
| 88 | + if (tasks.found.length) { |
| 89 | + queue.found.push({ inst: inst, tasks: tasks.found }); |
| 90 | + } |
| 91 | + queue.notFound = tasks.notFound; |
| 92 | + }); |
| 93 | + |
| 94 | + if (queue.notFound.length) { |
| 95 | + console.log(' `%s` not found', queue.notFound.join(' ')); |
| 96 | + return repl.prompt(); |
58 | 97 | }
|
| 98 | + |
| 99 | + queue.found.forEach(function (found) { |
| 100 | + var result = found.inst.runner.apply(found.inst.gulp, found.tasks); |
| 101 | + if (typeof result === 'function') { |
| 102 | + result(); // gulp#4.0 |
| 103 | + } |
| 104 | + }); |
| 105 | + |
| 106 | + return this; |
59 | 107 | });
|
60 |
| -}); |
61 |
| - |
62 |
| -/** |
63 |
| - * exit on SIGINT with a timestamp |
64 |
| -**/ |
65 |
| -repl.on('SIGINT', function onSIGINT(){ |
66 |
| - process.stdout.write('\n'); |
67 |
| - console.log(new Date()); |
68 |
| - process.exit(0); |
69 |
| -}); |
70 |
| - |
71 |
| -/** |
72 |
| - * add the given gulp instance to the instances array |
73 |
| -**/ |
74 |
| -function gulpRepl(_gulp_){ |
75 |
| - var gulp = util.getGulp(_gulp_); |
76 |
| - |
77 |
| - var inInstances = Boolean( |
78 |
| - exports.instances.filter(function(instance){ |
79 |
| - return instance.gulp === gulp; |
80 |
| - }).length |
81 |
| - ); |
82 |
| - if(inInstances){ return repl; } |
83 |
| - |
84 |
| - exports.instances.push({ |
85 |
| - gulp: gulp, |
86 |
| - tasks: util.getTasks(gulp), |
87 |
| - runner: gulp.start || gulp.parallel |
| 108 | + |
| 109 | + // exit on SIGINT |
| 110 | + repl.on('SIGINT', function onSIGINT () { |
| 111 | + process.stdout.write('\n'); |
| 112 | + process.exit(0); |
88 | 113 | });
|
89 | 114 |
|
90 | 115 | return repl;
|
91 |
| -} |
| 116 | +}; |
0 commit comments