-
Notifications
You must be signed in to change notification settings - Fork 135
/
BuildSubprojects.js
273 lines (236 loc) · 8.45 KB
/
BuildSubprojects.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
const Promise = require('bluebird');
const { spawn } = require('child_process');
const copyfiles = require('copyfiles');
const fs = require('fs');
const fsP = require('fs').promises;
const glob = require('glob');
const minimist = require('minimist');
const path = require('path');
const rimraf = require('rimraf');
const vm = require('vm');
const projectGroups = JSON.parse(fs.readFileSync('./BuildSubprojects.json'));
const packageJSON = JSON.parse(fs.readFileSync('./package.json'));
const npmcli = process.platform === 'win32' ? 'npm.cmd' : 'npm';
const yarncli = process.platform === 'win32' ? 'yarn.cmd' : 'yarn';
const useYarn = true;
//const rebuild = path.join('node_modules', '.bin', process.platform === 'win32' ? 'electron-rebuild.cmd' : 'electron-rebuild');
const globOptions = { };
const copyfilesAsync = Promise.promisify(copyfiles);
const rimrafAsync = Promise.promisify(rimraf);
const globAsync = Promise.promisify(glob);
class Unchanged extends Error {
constructor() {
super('No changes');
}
}
class ConditionNotMet extends Error {
constructor() {
super('Condition not met');
}
}
class ProcessFeedback {
constructor(id) {
this.id = id;
this.output = [];
}
finish(desc, code) {
if (code !== 0) {
this.output.forEach((line) => console.log(line));
}
console.log(`(${this.id}) ${desc} finished with code ${code}`);
}
log(data) {
if (data === undefined) {
return;
}
let lines = data.split(/[\r\n\t]+/);
lines.forEach((line) => {
if (line.length > 0) {
this.output.push(`-- ${line}`);
}
});
}
err(data) {
if (data === undefined) {
return;
}
let lines = data.split(/[\r\n\t]+/);
lines.forEach((line) => {
if (line.length > 0) {
this.output.push(`xx ${line}`);
}
});
}
}
function spawnAsync(exe, args, options, out) {
return new Promise((resolve, reject) => {
let desc = `${options.cwd || '.'}/${exe} ${args.join(' ')}`;
out.log('started: ' + desc);
try {
let proc = spawn(exe, args, { ...options, shell: true });
proc.stdout.on('data', (data) => out.log(data.toString()));
proc.stderr.on('data', (data) => out.err(data.toString()));
proc.on('error', (err) => reject(err));
proc.on('close', (code) => {
out.finish(desc, code);
if (code === 0) {
resolve();
} else {
reject(new Error(`${args} failed with code ${code}`));
}
});
} catch (err) {
out.err(`failed to spawn ${desc}: ${err.message}`);
reject(err);
}
});
}
let nextId = 0;
function getId() {
return nextId++;
}
function npm(command, args, options, out) {
if (!useYarn && (command === 'add')) {
command = 'install';
}
return spawnAsync(useYarn ? yarncli : npmcli, [command, ...args, '--mutex', 'file'], options, out);
}
function changes(basePath, patterns, force) {
if ((patterns === undefined) || force) {
return Promise.resolve();
}
// glob all the patterns, then map all the files to their last modified time,
// then get the newest of those modified times
return Promise.reduce(patterns,
(total, pattern) =>
globAsync(path.join(basePath, pattern), globOptions)
.then((files) => [].concat(total, files)),
[])
.map((filePath) => fsP.stat(filePath).then((stat) => stat.mtime.getTime()))
.then((fileTimes) => Math.max(...fileTimes));
}
function format(fmt, parameters) {
return fmt.replace(/{([a-zA-Z_]+)}/g, (match, key) => {
return typeof parameters[key] !== 'undefined' ? parameters[key] : match;
});
}
function processModule(project, buildType, feedback) {
let options = {};
let modulePath;
if (buildType !== 'out') {
options.cwd = path.join(__dirname, buildType);
modulePath = path.join(buildType, 'node_modules', project.module);
} else {
options.cwd = __dirname;
modulePath = path.join('node_modules', project.module);
}
let build = project.build !== undefined && project.build !== false
? npm('install', [], { cwd: project.path }, feedback)
.then(() => npm('run', [typeof project.build === 'string' ? project.build : 'build'], { cwd: project.path }, feedback))
: Promise.resolve();
return build
.then(() => rimrafAsync(modulePath))
.then(() => npm('add', [project.module], options, feedback));
}
async function updateSourceMap(filePath) {
let dat = await fs.promises.readFile(filePath, { encoding: 'utf8' });
const modPath = path.basename(path.dirname(filePath));
dat = dat.replace(/\/\/# sourceMappingURL=([a-z\-.]+\.js\.map)$/,
`//# sourceMappingURL=bundledPlugins/${modPath}/$1`);
await fs.promises.writeFile(filePath, dat);
}
function processCustom(project, buildType, feedback, noparallel) {
const start = Date.now();
let instArgs = noparallel ? ['--network-concurrency', '1'] : [];
let res = npm('install', instArgs, { cwd: project.path }, feedback)
.then(() => npm('run', [typeof project.build === 'string' ? project.build : 'build'], { cwd: project.path }, feedback));
if (project.copyTo !== undefined) {
const source = path.join(project.path, 'dist', '**', '*');
const output = format(project.copyTo, { BUILD_DIR: buildType });
feedback.log('copying files', source, output);
res = res
.then(() => copyfilesAsync([source, output], project.depth || 3))
.then(() => updateSourceMap(path.join(output, 'index.js')));
}
res = res.then(() => {
console.log(project.path, 'took', (Date.now() - start) / 1000, 's');
})
return res;
}
function evalCondition(condition, context) {
if (condition === undefined) {
return true;
}
const script = new vm.Script(condition);
return script.runInNewContext({ ... context, process });
}
function processProject(project, buildType, feedback, noparallel) {
if (!evalCondition(project.condition, { buildType })) {
return Promise.reject(new ConditionNotMet());
}
if (project.type === 'install-module') {
return processModule(project, buildType, feedback);
} else if (project.type === 'build-copy') {
return processCustom(project, buildType, feedback, noparallel);
// } else if (project.type === 'electron-rebuild') {
// return processRebuild(project, buildType, feedback);
}
if (project.type.startsWith('_')) {
return Promise.resolve();
}
return Promise.reject(new Error('invalid project descriptor ' + project.toString()));
}
function main(args) {
if (args.length === 0) {
console.error('No command line parameters specified');
return Promise.reject(1);
}
const globalFeedback = new ProcessFeedback('global');
const buildType = args._[0];
process.env.TARGET_ENV = (buildType === 'app')
? 'production' : 'development';
const buildStateName = `./BuildState_${buildType}.json`;
let buildState;
try {
buildState = JSON.parse(fs.readFileSync(buildStateName));
} catch (err) {
buildState = {};
}
let failed = false;
// the projects file contains groups of projects
// each group is processed in parallel
return Promise.each(projectGroups, (projects) => Promise.map(projects, (project) => {
if ((project.variant !== undefined) && (buildType !== 'out') && (process.env.VORTEX_VARIANT !== project.variant)) {
return Promise.resolve();
}
let feedback = new ProcessFeedback(project.name);
return changes(project.path || '.', project.sources, args.f || (buildState[project.name] === undefined))
.then((lastChange) => {
if ((lastChange !== undefined) && (lastChange < buildState[project.name])) {
return Promise.reject(new Unchanged());
}
return processProject(project, buildType, feedback, args.noparallel);
})
.then(() => {
buildState[project.name] = Date.now();
return fsP.writeFile(buildStateName, JSON.stringify(buildState, undefined, 2));
})
.catch((err) => {
if (err instanceof Unchanged) {
console.log('nothing to do', project.name);
} else if (err instanceof ConditionNotMet) {
console.log('condition wasn\'t met', project.name);
} else {
console.error('failed ', project.name, err);
failed = true;
}
})
;
}, { concurrency: 1 }))
.then(() => failed ? 1 : 0);
}
const args = minimist(process.argv.slice(2));
main(args)
// just run a second time, to repeat all failed builds
.then(() => main(args))
.then(res => process.exit(res));