forked from aikar/timings
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulp.util.js
88 lines (78 loc) · 1.81 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
81
82
83
84
85
86
87
88
/*
* Aikar's Minecraft Timings Parser
*
* Written by Aikar <aikar@aikar.co>
* http://aikar.co
* http://starlis.com
*
* @license MIT
*/
var _ = global._ = require('lodash');
var fs = global.fs = require('fs');
var path = global.path = require('path');
var cp = global.cp = require('child_process');
var ini = require('ini');
_.mixin(require("underscore.string").exports());
var merge = require('ordered-merge-stream');
var insert = require('gulp-insert');
var gulp = require('gulp');
module.exports = {
exec: exec,
scheduleTask: scheduleTask,
quote: quote,
readIni: readIni,
copyFile: copyFile,
ini: ini,
merge: merge,
wrapJS: wrapJS,
};
// =====================
function wrapJS() {
return insert.wrap('(function($) {', "\n})(jQuery);");
}
// =====================
var exec = function (arg) {
var result = require('execSync').exec(arg);
_.forEach(result.stdout.split("\n"), function (line) {
line = line.trim();
if (!line) return;
gutil.log('[' + arg + '] ' + line);
});
};
// =====================
var 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) {
var cbCalled = false;
var rd = fs.createReadStream(source);
rd.on("error", done);
var 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;
}
}
}