-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathgulp.util.js
80 lines (71 loc) · 1.62 KB
/
gulp.util.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
/*
* Aikar's Minecraft Timings Parser
*
* Written by Aikar <[email protected]>
* http://aikar.co
* http://starlis.com
*
* @license MIT
*/
const fs = require('fs');
const path = require('path');
const ini = require('ini');
const merge = require('ordered-merge-stream');
const gulp = require('gulp');
const gutil = require('gulp-util');
const cp = require("child_process");
// =====================
function exec(arg) {
const result = cp.execSync(arg).toString();
result.split("\n").forEach(function (line) {
line = line.trim();
if (!line) return;
gutil.log('[' + arg + '] ' + line);
});
}
module.exports = {
exec: exec,
scheduleTask: scheduleTask,
quote: quote,
readIni: readIni,
copyFile: copyFile,
ini: ini,
merge: merge,
};
// =====================
const scheduledTasks = {};
function scheduleTask(task, time) {
if (scheduledTasks[task]) {
clearTimeout(scheduledTasks[task]);
}
scheduledTasks[task] = setTimeout(function () {
delete scheduledTasks[task];
gulp.start(task);
}, time);
}
// =====================
function quote(str) {
return (str + '').replace(/[.?*+^$[\]\\(){}|-]/g, "\\$&");
}
// =====================
function readIni(file) {
return ini.parse(fs.readFileSync(file, 'utf-8')) || {};
}
// =====================
function copyFile(source, target, cb) {
let cbCalled = false;
const rd = fs.createReadStream(source);
rd.on("error", done);
const wr = fs.createWriteStream(target);
wr.on("error", done);
wr.on("close", function (ex) {
done();
});
rd.pipe(wr);
function done(err) {
if (!cbCalled) {
cb && cb(err);
cbCalled = true;
}
}
}