2
2
3
3
exports = module . exports = { } ;
4
4
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
+
5
42
exports . getQueue = function ( line , tasks ) {
6
43
var queue = { found : [ ] , notFound : [ ] } ;
7
44
8
45
while ( line . length ) {
9
46
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 ) ;
13
48
14
- if ( task . match ) {
49
+ if ( task && task . match ) {
15
50
queue . found . push ( task . match ) ;
51
+ line = line . slice ( task . match . length ) . trim ( ) ;
16
52
} else {
17
53
queue . notFound . push ( name ) ;
54
+ line = line . slice ( name . length ) . trim ( ) ;
18
55
}
19
-
20
- line = task . notFound . trim ( ) ;
21
56
}
22
57
23
58
return queue ;
@@ -34,58 +69,20 @@ exports.completer = function(line, instances){
34
69
35
70
instances . forEach ( function ( instance ) {
36
71
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 ) ;
41
80
} ) ;
42
81
43
- var hits = completion . filter ( function ( elem ) {
82
+ var hits = exports . uniq ( completion ) . filter ( function ( elem ) {
44
83
return ! elem . indexOf ( line ) ;
45
84
} ) ;
46
85
47
86
// TODO: add async path completion (nodejs.org/api/readline.html)
48
87
return [ hits . length ? hits : completion , line ] ;
49
88
} ;
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