-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
154 lines (139 loc) · 3.75 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
147
148
149
150
151
152
153
154
/**
* @author Prashanth.Kumar
*/
var Gaze = require('gaze').Gaze;
var chalk = require('chalk');
var tasks = {};
exports = module.exports = buildfriend = {};
buildfriend.tasks = tasks;
buildfriend.seq = [];
buildfriend.task = function(name, callback) {
var args = [].slice.call(arguments, 0);
switch(args.length) {
case 0:
console.log(chalk.red('task method requires at least two parameters'));
break;
case 1:
console.log(chalk.red('task method requires at least two parameters'));
break;
case 2:
tasks[name] = {
cb : callback,
deps : []
};
break;
case 3:
if ( typeof args[0] !== 'string') {
console.log(chalk.red('task name should be a string'));
}
if ( typeof args[1] == 'object' && typeof args[2] == 'function' && typeof args[1].length !== 'undefined') {
tasks[args[0]] = {
cb : args[2],
deps : []
};
for (var i = 0; i < args[1].length; i++) {
tasks[args[0]].deps[i] = args[1][i];
}
} else {
console.log(chalk.red('Give proper parameters to task method'));
}
break;
default:
console.log(chalk.red('Give proper parameters to task method'));
}
};
buildfriend.watch = function(glob, tasklist, cbFunction) {
buildfriend.task('watch', tasklist, function() {
fileWatcher()(cbFunction, tasklist, glob);
});
};
buildfriend.start = function(task) {
buildfriend.seq = [];
if (tasks.hasOwnProperty(task)) {
this.walkTree(task, buildfriend.seq);
if (!(tasks[task].deps.length === 0)) {
buildfriend.seq.push(task);
}
run(buildfriend.seq)();
} else {
console.log(chalk.red('task ' + task + ' Not found'));
}
};
buildfriend.walkTree = function(task, seq) {
if (tasks[task].deps.length === 0) {
seq.push(task);
if (seq.length > 1) {
var swap = seq[seq.length - 1];
seq[seq.length - 1] = seq[seq.length - 2];
seq[seq.length - 2] = swap;
}
return task;
} else {
for (var i = 0; i < tasks[task].deps.length; ++i) {
if (tasks[tasks[task].deps[i]].deps.length > 0) {
seq.push(tasks[task].deps[i]);
}
this.walkTree(tasks[task].deps[i], seq);
}
}
}
function run(taskArray) {
var i = -1, length = taskArray.length;
function start() {++i;
if (i < length) {
console.time(chalk.green(taskArray[i] + ' task completed in '));
var rTask = tasks[taskArray[i]].cb();
if ( typeof rTask == 'object') {
if ( typeof rTask.on == 'function' && typeof rTask.resume == 'function') {
rTask.resume();
rTask.on('end', function() {
console.timeEnd(chalk.green(taskArray[i] + ' task completed in '));
start();
});
} else {
console.timeEnd(chalk.green(taskArray[i] + ' task completed in '));
start();
}
} else {
console.timeEnd(chalk.green(taskArray[i] + ' task completed in '));
start();
}
}
}
return start;
}
function fileWatcher() {
function watchdog(cbFunction, tasklist, glob) {
if ( typeof glob == 'string' && typeof tasklist == 'object' && typeof cbFunction == 'function' && tasklist.length >= 0) {
var gaze = new Gaze(glob);
gaze.on('changed', function(filepath) {
if (filepath.charAt(filepath.length - 1) !== '~') {
gaze.close();
buildfriend.start('watch');
cbFunction('changed', filepath);
}
});
gaze.on('added', function(filepath) {
if (filepath.charAt(filepath.length - 1) !== '~') {
gaze.close();
buildfriend.start('watch');
cbFunction('added', filepath);
}
});
gaze.on('deleted', function(filepath) {
if (filepath.charAt(filepath.length - 1) !== '~') {
gaze.close();
buildfriend.start('watch');
cbFunction('deleted', filepath);
}
});
gaze.on('error', function(error) {
gaze.close();
console.log(chalk.red(error.errno));
});
} else {
console.log(chalk.red('Pass proper parameters to watch function'));
}
}
return watchdog;
}