forked from microsoft/pxt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jakeutil.js
145 lines (131 loc) · 3.83 KB
/
jakeutil.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
"use strict";
var child_process = require("child_process");
var fs = require("fs");
var util = require("util");
var path = require("path");
// for use with child_process.exec/execFile
function execCallback(task) {
return function (error, stdout, stderr) {
if (stdout) console.log(stdout.toString().replace(/\n$/, ""));
if (stderr) console.error(stderr.toString().replace(/\n$/, ""));
if (error) {
console.error(error);
task.fail(error);
}
else task.complete();
}
}
function mkdirP(thePath) {
if (thePath == ".") return;
if (!fs.existsSync(thePath)) {
mkdirP(path.dirname(thePath))
fs.mkdirSync(thePath)
}
}
function cpR(src, trg) {
let p = path.dirname(trg)
mkdirP(p)
jake.cpR(src, trg)
}
function expand1(dirs) {
if (!Array.isArray(dirs))
dirs = [dirs]
let r = []
dirs.forEach(dir =>
fs.readdirSync(dir).forEach(f => r.push(dir + "/" + f)))
return r
}
function expand(dir, ext) {
function expandCore(dir) {
if (Array.isArray(dir)) {
let r = []
dir.forEach(f => expandCore(f).forEach(x => r.push(x)))
return r
}
if (fs.existsSync(dir) && fs.statSync(dir).isDirectory())
return expandCore(fs.readdirSync(dir).map(f => dir + "/" + f))
else {
if (ext && dir.slice(-ext.length) != ext)
return []
return [dir]
}
}
var res = expandCore(dir)
//console.log("expand:", dir, res)
return res
}
function cat(out, files, pref) {
if (pref == null) pref = '"use strict";'
console.log("[cat] " + out + " <- " + files.join(" "))
let cont = files.map(f => fs.readFileSync(f, "utf8").replace(/\r/g, ""))
cont.unshift(pref)
fs.writeFileSync(out, cont.join("\n"))
}
function catFiles(out, files, pref, addDep) {
file(out, files.concat(addDep || []), function () {
cat(out, files, pref)
})
}
function cmdsIn(task, cmds) {
let num = cmds.length
cmds.forEach(obj => {
console.log(`[${task.name}] cd ${obj.dir}; ${obj.cmd} ${obj.args.join(" ")}`)
let ch = child_process.spawn(obj.cmd, obj.args, {
cwd: obj.dir,
env: process.env,
stdio: "inherit"
})
ch.on('close', (code) => {
if (code != 0)
task.fail();
else {
if (--num == 0)
task.complete();
}
});
})
}
function cmdIn(task, dir, cmd) {
console.log(`[${task.name}] cd ${dir}; ${cmd}`)
let args = cmd.split(/\s+/)
let ch = child_process.spawn(args[0], args.slice(1), {
cwd: dir,
env: process.env,
stdio: "inherit"
})
ch.on('close', (code) => {
if (code != 0)
task.fail();
else task.complete();
});
}
// strpSrcMap strips out the sourceMappingURL= from each of the files in dir (recursively)
function strpSrcMap(task, dir) {
fs.readdir(dir, (err, files) => {
if (err) {
console.error(err);
task.fail(err);
}
files.forEach((file, index) => {
file = path.resolve(dir, file) + '';
let isDirectory = fs.statSync(file).isDirectory();
if (isDirectory) {
strpSrcMap(this, file);
} else {
let fileContents = fs.readFileSync(file, "utf8");
fileContents = fileContents.replace(/\/\/# sourceMappingURL=.*/gi, '')
fs.writeFileSync(file, fileContents)
}
});
});
}
exports.execCallback = execCallback;
exports.expand = expand;
exports.expand1 = expand1;
exports.cat = cat;
exports.catFiles = catFiles;
exports.cmdIn = cmdIn;
exports.cmdsIn = cmdsIn;
exports.cpR = cpR;
exports.mkdirP = mkdirP;
exports.strpSrcMap = strpSrcMap;