-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.js
146 lines (111 loc) · 3.04 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
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
module.exports = gulpRequireTasks;
const path = require('path');
const requireDirectory = require('require-directory');
const DEFAULT_OPTIONS = {
path: process.cwd() + '/gulp-tasks',
separator: ':',
passGulp: true,
passCallback: true,
gulp: null
};
function gulpRequireTasks (options) {
options = Object.assign({}, DEFAULT_OPTIONS, options);
const gulp = options.gulp || require('gulp');
// Recursively visiting all modules in the specified directory
// and registering Gulp tasks.
requireDirectory(module, options.path, {
visit: moduleVisitor
});
/**
* Registers the specified module. Task name is deducted from the specified path.
*
* @param {object|function} module
* @param {string} modulePath
*/
function moduleVisitor (module, modulePath) {
module = normalizeModule(module);
const taskName = taskNameFromPath(modulePath);
if (module.dep) {
console.warn(
'Usage of "module.dep" property is deprecated and will be removed in next major version. ' +
'Use "deps" instead.'
);
}
gulp.task(
taskName,
// @todo: deprecate `module.dep` in 2.0.0
module.deps || module.dep || [],
module.nativeTask || taskFunction
);
/**
* Wrapper around user task function.
* It passes special arguments to the user function according
* to the configuration.
*
* @param {function} callback
*
* @returns {*}
*/
function taskFunction (callback) {
if ('function' !== typeof module.fn) {
callback();
return;
}
let args = [];
// @deprecated
// @todo: remove this in 2.0.0
if (options.arguments) {
console.warn(
'Usage of "arguments" option is deprecated and will be removed in next major version. ' +
'Use globals or module imports instead.'
);
args = Array.from(options.arguments);
}
if (options.passGulp) {
args.unshift(gulp);
}
if (options.passCallback) {
args.push(callback);
}
return module.fn.apply(module, args);
}
/**
* Deducts task name from the specified module path.
*
* @returns {string}
*/
function taskNameFromPath (modulePath) {
const relativePath = path.relative(options.path, modulePath);
// Registering root index.js as a default task.
if ('index.js' === relativePath) {
return 'default';
}
const pathInfo = path.parse(relativePath);
const taskNameParts = [];
if (pathInfo.dir) {
taskNameParts.push.apply(taskNameParts, pathInfo.dir.split(path.sep));
}
if ('index' !== pathInfo.name) {
taskNameParts.push(pathInfo.name);
}
return taskNameParts.join(options.separator);
}
}
}
/**
* Normalizes module definition.
*
* @param {function|object} module
*
* @returns {object}
*/
function normalizeModule (module) {
if ('function' === typeof module) {
return {
fn: module,
deps: []
};
} else {
return module;
}
}