Skip to content

Commit 9c0db7c

Browse files
committed
Merge branch 'next_release'
2 parents d925dbc + cee91d8 commit 9c0db7c

File tree

3 files changed

+66
-98
lines changed

3 files changed

+66
-98
lines changed

index.js

Lines changed: 13 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ exports.instances = [];
1111
var repl = require('readline').createInterface({
1212
input: process.stdin,
1313
output: process.stdout,
14-
completer: function(line){
14+
completer: function onCompletion(line){
1515
return util.completer(line, exports.instances);
1616
}
1717
});
@@ -46,21 +46,20 @@ repl.on('line', function onLine(input){
4646
plural ? 's' : '',
4747
plural ? 'are' : 'is'
4848
);
49-
return repl.prompt();
49+
} else {
50+
queue.found.forEach(function(found){
51+
var result = found.inst.runner.apply(found.inst.gulp, found.tasks);
52+
if(typeof result === 'function'){
53+
result(); // gulp#4.0
54+
}
55+
});
5056
}
51-
52-
queue.found.forEach(function(found){
53-
var result = found.inst.runner.apply(found.inst.gulp, found.tasks);
54-
if(typeof result === 'function'){
55-
result(); // gulp#4.0
56-
}
57-
});
5857
});
5958

6059
/**
6160
* exit on SIGINT with a timestamp
6261
**/
63-
repl.on('SIGINT', function(){
62+
repl.on('SIGINT', function onSIGINT(){
6463
process.stdout.write('\n');
6564
console.log(new Date());
6665
process.exit(0);
@@ -69,16 +68,8 @@ repl.on('SIGINT', function(){
6968
/**
7069
* add the given gulp instance to the instances array
7170
**/
72-
function gulpRepl(gulp){
73-
if(!gulp || typeof gulp.task !== 'function'){
74-
try {
75-
gulp = require('gulp');
76-
} catch (err){
77-
console.log('gulp is not installed locally');
78-
console.log('try `npm install gulp`');
79-
process.exit(1);
80-
}
81-
}
71+
function gulpRepl(_gulp_){
72+
var gulp = util.getGulp(_gulp_);
8273

8374
var inInstances = Boolean(
8475
exports.instances.filter(function(instance){
@@ -87,34 +78,11 @@ function gulpRepl(gulp){
8778
);
8879
if(inInstances){ return repl; }
8980

90-
var tasks = {
91-
obj: (
92-
(gulp._registry && gulp._registry._tasks) || // gulp#4
93-
(gulp.tasks && gulp.tasks.store) || // gulp-runtime
94-
gulp.tasks // gulp#3
95-
)
96-
};
97-
98-
if(typeof (gulp.tasks && gulp.tasks.get) === 'function'){
99-
tasks.get = function(name, line){
100-
return gulp.tasks.get(line);
101-
};
102-
} else {
103-
tasks.get = function(name, line){
104-
return tasks.obj[name] && {
105-
match: name,
106-
notFound: line.slice(name.length)
107-
};
108-
};
109-
}
110-
11181
exports.instances.push({
11282
gulp: gulp,
113-
tasks: tasks,
114-
runner: gulp.parallel || gulp.start
83+
tasks: util.getTasks(gulp),
84+
runner: gulp.start
11585
});
11686

117-
util.waitToPrompt(gulp, repl);
118-
11987
return repl;
12088
}

lib/util.js

Lines changed: 50 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,57 @@
22

33
exports = module.exports = {};
44

5+
exports.uniq = require('lodash.uniq');
6+
7+
exports.getGulp = function(gulp){
8+
if(!gulp || typeof gulp.task !== 'function'){
9+
try {
10+
gulp = require('gulp');
11+
} catch (err){
12+
console.log('gulp is not installed locally');
13+
console.log('try `npm install gulp`');
14+
process.exit(1);
15+
}
16+
}
17+
return gulp;
18+
};
19+
20+
exports.getTasks = function(gulp){
21+
var tasks = {
22+
obj: (
23+
(gulp._registry && gulp._registry._tasks) || // gulp#4
24+
(gulp.tasks && gulp.tasks.store) || // gulp-runtime
25+
gulp.tasks // gulp#3
26+
)
27+
};
28+
29+
if(typeof (gulp.tasks && gulp.tasks.get) === 'function'){
30+
tasks.get = function(name, line){
31+
return gulp.tasks.get(line);
32+
};
33+
} else {
34+
tasks.get = function(name){
35+
return tasks.obj[name] && {match: name};
36+
};
37+
}
38+
39+
return tasks;
40+
};
41+
542
exports.getQueue = function(line, tasks){
643
var queue = {found: [], notFound: []};
744

845
while(line.length){
946
var name = /(\S+)/.exec(line).pop();
10-
var task = tasks.get(name, line) || {
11-
notFound: line.slice(name.length)
12-
};
47+
var task = tasks.get(name, line);
1348

14-
if(task.match){
49+
if(task && task.match){
1550
queue.found.push(task.match);
51+
line = line.slice(task.match.length).trim();
1652
} else {
1753
queue.notFound.push(name);
54+
line = line.slice(name.length).trim();
1855
}
19-
20-
line = task.notFound.trim();
2156
}
2257

2358
return queue;
@@ -34,58 +69,20 @@ exports.completer = function(line, instances){
3469

3570
instances.forEach(function (instance){
3671
var matches = exports.getQueue(line, instance.tasks);
37-
completion.push.apply(completion, matches.found.length
38-
? matches.found
39-
: Object.keys(instance.tasks.obj)
40-
);
72+
if(!matches.found.length){
73+
Object.keys(instance.tasks.obj).forEach(function(name){
74+
matches.found.push.apply(matches.found,
75+
(name.match(/\(([^(]+)\)/) || [name]).pop().split('|')
76+
);
77+
});
78+
}
79+
completion.push.apply(completion, matches.found);
4180
});
4281

43-
var hits = completion.filter(function(elem){
82+
var hits = exports.uniq(completion).filter(function(elem){
4483
return !elem.indexOf(line);
4584
});
4685

4786
// TODO: add async path completion (nodejs.org/api/readline.html)
4887
return [hits.length ? hits : completion, line];
4988
};
50-
51-
exports.waitToPrompt = function(gulp, repl){
52-
var events = [
53-
'start', 'task_start',
54-
'err', 'stop', 'error', 'task_err', 'task_stop', 'task_not_found'
55-
];
56-
57-
if(typeof gulp.on === 'function'){
58-
events.forEach(function(eventName){
59-
var replEventName = 'task:' + (/start/.test(eventName) ?
60-
'start' : 'ended'
61-
);
62-
gulp.on(eventName, function(ev){
63-
repl.emit(replEventName, ev);
64-
});
65-
});
66-
}
67-
68-
var queue = [];
69-
70-
repl.on('task:start', function start(ev){
71-
var taskName = ev && (ev.task || ev.name);
72-
if(!taskName){ return; }
73-
if(/watch/i.test(taskName)){
74-
repl.emit('task:ended', ev);
75-
} else {
76-
queue.push(taskName);
77-
}
78-
});
79-
80-
repl.on('task:ended', function ended(ev){
81-
var taskName = ev && (ev.task || ev.name);
82-
var index = queue.indexOf(taskName);
83-
if(index > -1){ queue.splice(index, 1); }
84-
if(queue.length){ return; }
85-
setTimeout(function(){
86-
if(!queue.length){
87-
repl.prompt();
88-
}
89-
}, 10);
90-
});
91-
};

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,8 @@
2121
"scripts": {
2222
"lint": "jshint lib index.js --exclude ./node_modules",
2323
"test": "npm run-script lint && mocha test"
24+
},
25+
"dependencies": {
26+
"lodash.uniq": "^4.2.1"
2427
}
2528
}

0 commit comments

Comments
 (0)