-
Notifications
You must be signed in to change notification settings - Fork 136
/
InstallAssets.js
105 lines (91 loc) · 2.71 KB
/
InstallAssets.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
'use strict';
const Promise = require('bluebird');
const fs = require('fs-extra');
const path = require('path');
const glob = require('glob');
const exec = require('child_process').exec;
const data = require('./InstallAssets.json');
const globOptions = { matchBase: true, globstar: true };
if (process.argv.length < 3) {
process.exit(1);
}
const tgt = process.argv[2];
let childProcesses = [];
let copies = -1;
let status = 0;
// run other, independend commands concurrently to speed things up.
for (let spawn of data.spawn) {
if (spawn.target.indexOf(tgt) === -1) {
continue;
}
const cmdline = spawn.executable + ' ' + spawn.arguments.join(' ');
const child = exec(spawn.executable + ' ' + spawn.arguments.join(' '), {
stdio: [0, 1, 2],
env: Object.assign({}, process.env, spawn.env)
});
console.log('spawned', cmdline);
child.stdout.on('data', (output) => {
console.log(spawn.executable, output);
});
child.stderr.on('data', (output) => {
console.log('Error:', spawn.executable, output);
});
child.on('close', (code) => {
if (code !== 0) {
status = 1;
}
console.log('finished', spawn.executable, code);
});
childProcesses.push(spawn.executable);
child.on('exit', () => {
childProcesses = childProcesses.filter((proc) => proc !== spawn.executable);
});
}
function waitForProcesses() {
let resolve;
const cb = () => {
if ((childProcesses.length > 0) || (copies !== 0)) {
setTimeout(cb, 100);
} else {
resolve();
}
}
return new Promise((resolveIn, reject) => {
resolve = resolveIn;
setTimeout(cb, 100);
});
}
// copy files
Promise.mapSeries(data.copy, file => {
if (file.target.indexOf(tgt) === -1) {
return;
}
return new Promise((resolve, reject) => {
glob(file.srcPath, globOptions, (globErr, files) => {
copies = copies === -1 ? files.length : copies += files.length;
if (globErr !== null) {
reject(new Error('glob failed: ' + globErr));
}
resolve(files);
});
})
.then(files => Promise.map(files, (globResult) => {
let globTarget = path.join(...globResult.split(/[\/\\]/).slice(file.skipPaths));
if (file.rename) {
globTarget = path.join(path.dirname(globTarget), file.rename);
}
const targetFile = path.join(tgt, file.outPath, globTarget);
return fs.ensureDir(path.dirname(targetFile))
.then(() => fs.copy(globResult, targetFile))
.then(() => {
console.log('copied', globResult, targetFile);
})
.catch((copyErr) => {
console.log('failed to copy', globResult, targetFile, copyErr);
})
.finally(() => {
--copies;
});
}));
})
.then(() => waitForProcesses());