Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
289 lines (230 loc) · 6.64 KB
/
Copy pathindex.js
File metadata and controls
289 lines (230 loc) · 6.64 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
'use strict';
var Parth = require('parth');
var Runtime = require('runtime');
var vinylFS = require('vinyl-fs')
var cli = require('./lib/cli');
var util = require('./lib/util');
var __slice = Array.prototype.slice;
var Gulp = module.exports = Runtime.createClass({
src: vinylFS.src,
dest: vinylFS.dest,
create: function Gulp(props){
Gulp.super_.call(this, props);
this.tasks = new Parth({defaultRE: /\S+/});
this.log = this.log === void 0 || this.log;
this.gulpfile = util.getGulpFile();
if(this.repl){
this.repl = require('gulp-repl')(this);
util.log('REPL enabled');
}
if(this.log){
util.log('Using gulpfile', util.format.path(this.gulpfile));
}
// adds cli tasks and run the cli for this instance
cli(this);
}
});
/**
* gulp.task
**/
Gulp.prototype.task = function(name, deps, handle){
handle = util.type(handle || deps || name).function || '';
deps = util.type(deps).array || util.type(name).array;
name = util.type(name).string || util.type(handle.name).string;
if(name && !deps && !handle){
return (this.tasks.get(name) || {fn: null}).fn;
}
if(handle && handle.name !== name){
handle.displayName = name;
}
if(name && deps && handle){
var tasks = deps.concat(handle);
var composer = this.series.apply(this, tasks);
this.tasks.set(name, {name: name, fn: composer});
} else if(name && deps){
var composer = this.stack.apply(this, deps);
this.tasks.set(name, {name: name, fn: composer});
} else if(name){
this.tasks.set(name, {name: name, fn: handle});
}
return this;
};
/**
* gulp.watch
**/
Gulp.prototype.watch = function(glob, opt, handle) {
var fn = util.type(handle || opt).function;
var tasks = util.type(opt).array;
if(tasks){
var composer = this.stack.apply(this, tasks);
return vinylFS.watch(glob, function(/* arguments */){
var args = __slice.call(arguments);
composer.apply(null, fn ? args.concat(fn) : args);
});
}
return vinylFS.watch(glob, opt, fn);
};
/**
gulp.tree
**/
Gulp.prototype.tree = function(options){
options = options || {};
var depth = options.depth === void 0 || options.depth;
if(depth && typeof depth !== 'number'){
depth = 1;
}
var self = this;
var tree = {label: options.label || '', nodes: []};
if(!(this instanceof Runtime.Stack)){
var tasks = Object.keys(this.tasks.store).filter(function(task){
return !/^\:cli/.test(task);
});
if(options.simple){ return tasks; }
tasks.forEach(function(name){
var task = self.tasks.store[name];
var node = {label: task.name};
if(task.fn.stack instanceof Runtime.Stack){
node = task.fn.stack.tree({
host: task,
depth: depth < options.depth ? depth + 1 : false
});
}
tree.nodes.push(node);
});
return tree;
}
var sites = this.length ? this : this.reduce();
sites.forEach(function(task){
if(!task || !task.fn){ return; }
var node = task;
if(task.fn.stack instanceof Runtime.Stack){
node = task.fn.stack.tree({
host: task,
depth: depth < options.depth && (depth + 1) || false
});
}
tree.label += (tree.label && ', ' + node.label) || node.label;
tree.nodes.push(node);
});
if(options.host && options.host.name){
tree.label = options.host.name;
tree.nodes = tree.nodes.filter(function(node){
return node.label !== tree.label;
});
tree.label = util.chalk.underline(tree.label);
}
return tree;
};
/**
maps all the arguments of gulp.stack to functions
**/
Gulp.prototype.reduceStack = function(stack, site){
var task = typeof site === 'function'
? {fn: site}
: this.tasks.get(site);
if(!task){
util.log('Task `%s` is not defined yet',
util.format.task(site)
);
util.log('Not sure how to do that? See %s',
util.format.link(util.docs.task)
);
if(util.isFromCLI(this)){
return process.exit(1);
}
throw new Error(
'task `' + util.format.task(site) + '` is not defined yet'
);
}
stack.push(task);
if(task.fn.stack instanceof Runtime.Stack){
task.mode = task.fn.stack.wait ? 'series' : 'parallel';
} else {
task.label = task.name || task.fn.displayName || task.fn.name;
}
};
/**
logging
**/
Gulp.prototype.onHandleStart = function(task, stack){
if(!this.log || (task.params && task.params.cli)){
return;
}
if(!stack.time){
stack.time = process.hrtime();
stack.deep = stack.length > 1;
stack.mode = stack.wait ? 'series' : 'parallel';
stack.label = stack.tree().label;
if(stack.deep && !stack.host){
util.log('Start', util.format.task(stack));
}
}
if(task.mode){
task.label = task.fn.stack.tree().label;
}
if(task.mode || (!stack.deep && !stack.host)){
util.log('Start', util.format.task(task));
}
task.time = process.hrtime();
};
Gulp.prototype.onHandleEnd = function(task, stack){
if(this.log && !(task.params && task.params.cli)){
if(!task.mode){
util.log(' %s took %s',
util.format.task(task),
util.format.time(task.time)
);
}
if(!stack.host && stack.end){
util.log('Ended %s after %s',
util.format.task(stack.deep ? stack : task),
util.format.time(stack.time)
);
}
}
if(this.repl && stack.end && !stack.host){
var self = this;
clearTimeout(this.timeout);
this._timeout = setTimeout(function (){
self.repl.prompt();
}, 250);
}
};
/**
error handling
**/
Gulp.prototype.onHandleError = function(error, site, stack){
util.log('%s threw an error after %s',
util.format.error(site.label || stack.tree().label),
util.format.time(site.time)
);
if(!this.repl){ throw err; }
util.log('at %s',
util.format.path(error.stack.match(/\/[^)]+/).pop())
);
};
/**
With some sugar on top please
**/
Gulp.prototype.start = function(/* arguments */){
var args = null;
var tasks = arguments;
if(util.type(arguments[0]).array){
args = __slice.call(arguments, 1);
tasks = arguments[0];
}
var composer = this.stack.apply(this, tasks.length ? tasks : ['default']);
return composer.apply(this, args);
};
Gulp.prototype.series = function(/* arguments */){
var args = __slice.call(arguments);
var props = util.type(args[args.length - 1]).plainObject && args.pop() || {};
var tasks = args.concat(util.merge(props, {wait: true}));
return this.stack.apply(this, tasks);
};
Gulp.prototype.parallel = function(/* arguments */){
var args = __slice.call(arguments);
var props = util.type(args[args.length - 1]).plainObject && args.pop() || {};
var tasks = args.concat(util.merge(props, {wait: false}));
return this.stack.apply(this, tasks);
};