22
33exports = 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+
542exports . 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:' + ( / s t a r t / . 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 ( / w a t c h / 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- } ;
0 commit comments