-
Notifications
You must be signed in to change notification settings - Fork 159
Expand file tree
/
Copy pathfastboot-config.js
More file actions
310 lines (254 loc) · 8.46 KB
/
fastboot-config.js
File metadata and controls
310 lines (254 loc) · 8.46 KB
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/* eslint-env node */
'use strict';
const fs = require('fs');
const fmt = require('util').format;
const merge = require('ember-cli-lodash-subset').merge;
const md5Hex = require('md5-hex');
const path = require('path');
const Plugin = require('broccoli-plugin');
const child_process = require('child_process');
const rimraf = require('rimraf');
const stringify = require('json-stable-stringify');
const LATEST_SCHEMA_VERSION = 3;
module.exports = class FastBootConfig extends Plugin {
constructor(inputNode, options) {
super([inputNode], {
annotation: 'Generate: FastBoot package.json',
persistentOutput: true
});
this.project = options.project;
this.name = options.name;
this.ui = options.ui;
this.fastbootAppConfig = options.fastbootAppConfig;
this.outputPaths = options.outputPaths;
this.appName = options.appConfig.modulePrefix;
const appConfigModule = `${this.appName}`;
this.fastbootConfig = {};
this.fastbootConfig[appConfigModule] = options.appConfig;
this._fileToChecksumMap = {};
if (this.fastbootAppConfig && this.fastbootAppConfig.htmlFile) {
this.htmlFile = this.fastbootAppConfig.htmlFile;
} else {
this.htmlFile = 'index.html';
}
}
/**
* The main hook called by Broccoli Plugin. Used to build or
* rebuild the tree. In this case, we generate the configuration
* and write it to `package.json`.
*/
build() {
return Promise.all([
this.buildConfig(),
this.buildDependencies(),
this.buildManifest(),
this.buildHostWhitelist()
]).then(() => {
let packageOutputPath = path.join(this.outputPath, 'package.json');
this.writeFileIfContentChanged(packageOutputPath, this.toJSONString());
});
}
writeFileIfContentChanged(outputPath, content) {
let previous = this._fileToChecksumMap[outputPath];
let next = md5Hex(content);
if (previous !== next) {
fs.writeFileSync(outputPath, content);
this._fileToChecksumMap[outputPath] = next; // update map
}
}
buildConfig() {
// we only walk the host app's addons to grab the config since ideally
// addons that have dependency on other addons would never define
// this advance hook.
this.project.addons.forEach((addon) => {
if (addon.fastbootConfigTree) {
let configFromAddon = addon.fastbootConfigTree();
if (!configFromAddon) {
throw new Error('`fastbootConfigTree` requires a map to be returned');
}
merge(this.fastbootConfig, configFromAddon);
}
});
}
buildDependencies() {
let dependencies = {};
let moduleWhitelist = [];
let ui = this.ui;
eachAddonPackage(this.project, pkg => {
let deps = getFastBootDependencies(pkg, ui);
if (deps) {
deps.forEach(dep => {
let version = getDependencyVersion(pkg, dep);
if (dep in dependencies) {
version = dependencies[dep];
ui.writeLine(fmt("Duplicate FastBoot dependency %s. Versions may mismatch. Using range %s.", dep, version), ui.WARNING);
return;
}
moduleWhitelist.push(dep);
if (version) {
dependencies[dep] = version;
}
});
}
});
let pkg = this.project.pkg;
let projectDeps = pkg.fastbootDependencies;
if (projectDeps) {
projectDeps.forEach(dep => {
moduleWhitelist.push(dep);
let version = pkg.dependencies && pkg.dependencies[dep];
if (version) {
dependencies[dep] = version;
}
});
}
return this.updateDependencies(dependencies, moduleWhitelist);
}
updateDependencies(dependencies, moduleWhitelist) {
if (this.dependenciesChanged(dependencies)) {
return this.getPackageLock(dependencies).then(() => {
this.dependencies = dependencies;
this.moduleWhitelist = moduleWhitelist;
});
} else {
this.dependencies = dependencies;
this.moduleWhitelist = moduleWhitelist;
}
}
dependenciesChanged(dependencies) {
return stringify(dependencies) !== stringify(this.dependencies);
}
getPackageLock(dependencies) {
let packagePath = path.join(this.project.root, 'package.json');
let lockfilePath = path.join(this.project.root, 'package-lock.json');
let tmpFolder = path.join(this.outputPath, 'package-lock-generator');
fs.mkdirSync(tmpFolder);
fs.symlinkSync(packagePath, path.join(tmpFolder, 'package.json'));
fs.symlinkSync(lockfilePath, path.join(tmpFolder, 'package-lock.json'));
const firstInstall = new Promise((resolve, reject) => {
this.ui.writeLine('FastBoot: Building node module cache.');
child_process.exec('npm install --cache=tmp', { cwd: tmpFolder }, (error) => {
if (error) {
reject(error);
} else {
resolve();
}
});
})
return firstInstall
.then(() => {
this.ui.writeLine('FastBoot: Generating lockfile.');
fs.unlinkSync(path.join(tmpFolder, 'package.json'));
fs.unlinkSync(path.join(tmpFolder, 'package-lock.json'));
fs.writeFileSync(path.join(tmpFolder, 'package.json'), stringify({ dependencies }));
const secondInstall = new Promise((resolve, reject) => {
child_process.exec('npm install --cache=tmp --offline', { cwd: tmpFolder }, (error) => {
if (error) {
reject(error);
} else {
resolve();
}
});
});
return secondInstall;
})
.then(() => {
this.ui.writeLine('FastBoot: Removing node module cache.');
fs.renameSync(
path.join(tmpFolder, 'package-lock.json'),
path.join(this.outputPath, 'package-lock.json')
);
fs.renameSync(
path.join(tmpFolder, 'node_modules'),
path.join(this.outputPath, 'node_modules')
);
const removeTmpFolder = new Promise((resolve, reject) => {
rimraf(tmpFolder, (error) => {
if (error) {
reject(error);
} else {
resolve();
}
});
});
return removeTmpFolder;
});
}
updateFastBootManifest(manifest) {
this.project.addons.forEach(addon =>{
if (addon.updateFastBootManifest) {
manifest = addon.updateFastBootManifest(manifest);
if (!manifest) {
throw new Error(`${addon.name} did not return the updated manifest from updateFastBootManifest hook.`);
}
}
});
return manifest;
}
buildManifest() {
function stripLeadingSlash(filePath) {
return filePath.replace(/^\//, '');
}
let appFilePath = stripLeadingSlash(this.outputPaths.app.js);
let appFastbootFilePath = appFilePath.replace(/\.js$/, '') + '-fastboot.js';
let vendorFilePath = stripLeadingSlash(this.outputPaths.vendor.js);
let manifest = {
appFiles: [appFilePath, appFastbootFilePath],
vendorFiles: [vendorFilePath],
htmlFile: this.htmlFile
};
this.manifest = this.updateFastBootManifest(manifest);
}
buildHostWhitelist() {
if (this.fastbootAppConfig) {
this.hostWhitelist = this.fastbootAppConfig.hostWhitelist;
}
}
toJSONString() {
return stringify({
dependencies: this.dependencies,
fastboot: {
moduleWhitelist: this.moduleWhitelist,
schemaVersion: LATEST_SCHEMA_VERSION,
manifest: this.manifest,
hostWhitelist: this.normalizeHostWhitelist(),
config: this.fastbootConfig,
appName: this.appName,
}
}, null, 2);
}
normalizeHostWhitelist() {
if (!this.hostWhitelist) {
return;
}
return this.hostWhitelist.map(function(entry) {
// Is a regex
if (entry.source) {
return '/' + entry.source + '/';
} else {
return entry;
}
});
}
}
function eachAddonPackage(project, cb) {
project.addons.map(addon => cb(addon.pkg));
}
function getFastBootDependencies(pkg, ui) {
let addon = pkg['ember-addon'];
if (!addon) {
return addon;
}
let deps = addon.fastBootDependencies;
if (deps) {
ui.writeDeprecateLine('ember-addon.fastBootDependencies has been replaced with ember-addon.fastbootDependencies [addon: ' + pkg.name + ']');
return deps;
}
return addon.fastbootDependencies;
}
function getDependencyVersion(pkg, dep) {
if (!pkg.dependencies) {
throw new Error(fmt("Could not find FastBoot dependency '%s' in %s/package.json dependencies.", dep, pkg.name));
}
return pkg.dependencies[dep];
}