-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
gulputils.mjs
47 lines (42 loc) · 1.35 KB
/
gulputils.mjs
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
import gulp from 'gulp';
const { series, parallel } = gulp;
import { Transform } from 'stream';
import asyncDone from 'async-done';
import child_process from 'child_process';
export function makePromise(obj) {
if (obj.then instanceof Function) {
return obj; // Already a then-able, just return
}
return new Promise((resolve, reject) => {
asyncDone(obj, (err, result) => {
err ? reject(err) : resolve(result)
})
});
}
export function runParallel(...tasks) {
return makePromise(parallel(...tasks));
}
export function runSeries(...tasks) {
return makePromise(series(...tasks));
}
export function execTask(command, options = {}) {
const task = async () =>
new Promise((resolve, reject) => {
child_process.exec(command, options, (err, stdout, stderr) => {
if (stdout) console.log(`${command}: ${stdout.trim()}`);
if (stderr) console.warn(`${command}: ${stderr.trim()}`);
err ? reject(err) : resolve(stdout);
});
});
task.displayName = `Exec \`${command}\``;
return task;
}
export function contentTransform(fn) {
return new Transform({
objectMode: true,
transform(file, enc, cb) {
file.contents = Buffer.from(fn(file.contents, file, enc));
cb(null, file);
}
});
}