forked from microsoft/pxt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjakeutil.js
60 lines (52 loc) · 1.67 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
"use strict";
var child_process = require("child_process");
var fs = require("fs");
var util = require("util");
// 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 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 catFiles(out, files, pref) {
if (pref == null) pref = '"use strict";'
file(out, files, function () {
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 cmdIn(task, dir, cmd) {
console.log(`[${task.name}] cd ${dir}; ${cmd}`)
child_process.exec(cmd, { cwd: dir }, execCallback(task))
}
exports.execCallback = execCallback;
exports.expand = expand;
exports.catFiles = catFiles;
exports.cmdIn = cmdIn;