-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
98 lines (90 loc) · 2.74 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
const { existsSync } = require('fs');
const fs = require('fs').promises;
const path = require('path');
const glob = require('glob');
const chokidar = require('chokidar');
const tasks = {};
function globPromise (src) {
src = src.replace(/\\/g, '/');
return new Promise((resolve, reject) => {
glob(src, (error, files) => {
if (error) {
reject(error);
}
resolve(files);
})
});
}
module.exports = {
task(name, definition) {
if (Array.isArray(definition)) {
tasks[name] = this.parallel(definition);
}
tasks[name] = definition;
},
series() {
const subtasks = arguments;
return async () => {
for(let i = 0; i < subtasks.length; i++) {
if (typeof subtasks[i] === "string") {
await this.run(subtasks[i]);
} else {
await subtasks[i]();
}
}
}
},
parallel() {
const subtasks = arguments;
return async () => {
const promises = [];
for(let i = 0; i < subtasks.length; i++) {
if (typeof subtasks[i] === "string") {
promises.push(this.run(subtasks[i]))
} else {
promises.push(subtasks[i]());
}
}
await Promise.all(promises);
}
},
run: async (name) => {
if (!(name in tasks)) {
console.log(`Error task ${name} not found...`);
return;
}
console.log(`Starting ${name}...`);
try {
await tasks[name]();
} catch(ex) {
console.log(name);
console.log(tasks[name]);
console.error(ex);
}
console.log(`Finished ${name}...`);
},
copy: async (src, dest, base) => {
src = path.resolve(src);
dest = path.resolve(dest);
base = base ? base.replace(/\\/g, '/') : base;
var files = await globPromise(src);
for (let i = 0; i < files.length; i++) {
const f = files[i];
const newPath = path.join(dest, (base ? f.replace(base, '') : path.basename(f)));
const stat = await fs.lstat(f);
if (stat.isDirectory()) {
await fs.mkdir(newPath, { recursive: true });
} else {
const parent = path.dirname(newPath);
if (!existsSync(parent)) {
await fs.mkdir(parent, { recursive: true });
}
await fs.copyFile(f, newPath);
}
}
},
watch: (path, callback) => {
chokidar.watch(path).on('change', callback);
},
glob: globPromise,
}