-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
84 lines (67 loc) · 2.05 KB
/
index.js
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
module.exports = function(gulp, config) {
var groups, task, resolveDependency;
// get gulp if not provided
if (typeof gulp === 'undefined') {
gulp = require('gulp');
}
// seed config
config = config || {};
config.separator = config.separator || '/';
config.current = config.current || './';
config.parent = config.parent || '../';
// console.log(' Gulp tasks:');
// var indents = [' '];
groups = [];
task = gulp.task;
resolveDependency = function(dep, ns) {
if (dep.indexOf(config.current) === 0) {
// current token is used
ns.pop();
ns.push(dep.slice(config.current.length));
return ns.join(config.separator);
} else if (dep.indexOf(config.parent) === 0) {
// parent token is used
ns.pop();
dep = resolveDependency(dep.slice(config.parent.length), ns);
return resolveDependency(config.current + dep, ns);
} else {
// ordinary dependency
return dep;
}
};
gulp.group = function(name, define) {
var subTasks, group;
group = new String(name);
group.deps = [];
groups.push(group);
// console.log(indents.join('') + '├─┬ %s', group);
// indents.push('│ ');
subTasks = [];
define();
groups.pop();
gulp.task(group.toString(), group.deps.map(function(dep) {
return config.current + name + config.separator + dep;
}));
// console.log(indents.join('') + '└ ..');
// indents.pop();
};
gulp.task = function(name, deps, func) {
var resolve, ns;
// make ns a shallow copy of groups
ns = groups.slice();
ns.push(name);
if (Array.isArray(deps)) {
// console.log(indents.join('') + '├── %s [%s]', name, deps);
deps = deps.map(function(dep, idx, arr) {
return resolveDependency(dep.toString(), ns.slice());
}, this);
} else {
// console.log(indents.join('') + '├── %s', name);
}
if (groups.length > 0) {
groups[groups.length-1].deps.push(name);
}
task.call(gulp, ns.join(config.separator), deps, func);
}
return gulp;
};