-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
executable file
·99 lines (84 loc) · 2.41 KB
/
gulpfile.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
#!/usr/bin/env node
'use strict';
// @note We are avoiding ES6 here.
var xIsJsFile = /\.js$/i;
var fs = require('fs');
var gulp = require('gulp');
var tasks = createTasks('./gulp');
/**
* Get a tree structure from a directory with given root. Returns required files if files is .js.
*
* @param {string} root The starting directory.
* @returns {Object} The structure.
*/
function tree(root) {
var structure = {};
var _files = fs.readdirSync(root);
for(var i=0; i<_files.length; i++) {
if ((_files[i] !== '.') && (_files[i] !== '..')) {
var stats = fs.statSync(root + '/' + _files[i]);
if (stats.isDirectory()) {
structure[_files[i]] = tree(root + '/' + _files[i]);
} else if (stats.isFile() && xIsJsFile.test(_files[i])) {
structure[_files[i]] = require(root + '/' + _files[i])
}
}
}
return structure;
}
/**
* Add an id to a parent to get a new full id.
*
* @param {string} parent The parent id.
* @param {string} id The id.
* @returns {string} The full id.
*/
function parentId(parent, id) {
return parent + id.replace(xIsJsFile, '');
}
/**
* Given a tree structure, create an object of tasks-ids against task objects.
*
* @param {Object} tree The directory tree from tree().
* @param {string} [parent=""] The current parent id.
* @param {Object} [tasks={}] The task object.
* @returns {Object} The flat object, tasks.
*/
function _createTasks(tree, parent, tasks) {
parent = parent || "";
tasks = tasks || {};
for (var id in tree) {
if (tree[id].deps && tree[id].fn) {
tasks[parentId(parent, id)] = tree[id];
} else {
_createTasks(tree[id], parentId(parent, id) + ':', tasks)
}
}
return tasks;
}
/**
* Get the a tasks object from the given directory path.
*
* @param {string} root The path to scan.
* @returns {Object} The tasks object created by _createTasks().
*/
function createTasks(root) {
return _createTasks(tree(root));
}
/**
* Create a gulp task for the given id.
*
* @param {string} taskId Task id to lookup in tasks and assign.
* @returns {Function} The gulp function.
*/
function createTask(taskId) {
return function (done) {
var stream = tasks[taskId].fn(gulp, done);
if (stream) {
if (stream.on) stream.on('end', done);
if (stream.then) stream.then(done);
}
};
}
for (var taskId in tasks) gulp.task(taskId, tasks[taskId].deps, createTask(taskId));
if (!module.parent && (process.argv.length > 2)) gulp.start([process.argv[2]]);