Skip to content

Commit 8376f07

Browse files
committed
Sketch of lockfile generation.
1 parent 43ffc65 commit 8376f07

File tree

3 files changed

+99
-10
lines changed

3 files changed

+99
-10
lines changed

lib/broccoli/fastboot-config.js

+91-10
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33

44
const fs = require('fs');
55
const fmt = require('util').format;
6-
const uniq = require('ember-cli-lodash-subset').uniq;
76
const merge = require('ember-cli-lodash-subset').merge;
87
const md5Hex = require('md5-hex');
98
const path = require('path');
109
const Plugin = require('broccoli-plugin');
10+
const child_process = require('child_process');
11+
const rimraf = require('rimraf');
1112

1213
const stringify = require('json-stable-stringify');
1314

@@ -47,13 +48,15 @@ module.exports = class FastBootConfig extends Plugin {
4748
* and write it to `package.json`.
4849
*/
4950
build() {
50-
this.buildConfig();
51-
this.buildDependencies();
52-
this.buildManifest();
53-
this.buildHostWhitelist();
54-
55-
let outputPath = path.join(this.outputPath, 'package.json');
56-
this.writeFileIfContentChanged(outputPath, this.toJSONString());
51+
return Promise.all([
52+
this.buildConfig(),
53+
this.buildDependencies(),
54+
this.buildManifest(),
55+
this.buildHostWhitelist()
56+
]).then(() => {
57+
let packageOutputPath = path.join(this.outputPath, 'package.json');
58+
this.writeFileIfContentChanged(packageOutputPath, this.toJSONString());
59+
});
5760
}
5861

5962
writeFileIfContentChanged(outputPath, content) {
@@ -124,8 +127,86 @@ module.exports = class FastBootConfig extends Plugin {
124127
});
125128
}
126129

127-
this.dependencies = dependencies;
128-
this.moduleWhitelist = uniq(moduleWhitelist);
130+
return this.updateDependencies(dependencies, moduleWhitelist);
131+
}
132+
133+
updateDependencies(dependencies, moduleWhitelist) {
134+
if (this.dependenciesChanged(dependencies)) {
135+
return this.getPackageLock(dependencies).then(() => {
136+
this.dependencies = dependencies;
137+
this.moduleWhitelist = moduleWhitelist;
138+
});
139+
} else {
140+
this.dependencies = dependencies;
141+
this.moduleWhitelist = moduleWhitelist;
142+
}
143+
}
144+
145+
dependenciesChanged(dependencies) {
146+
return stringify(dependencies) !== stringify(this.dependencies);
147+
}
148+
149+
getPackageLock(dependencies) {
150+
let packagePath = path.join(this.project.root, 'package.json');
151+
let lockfilePath = path.join(this.project.root, 'package-lock.json');
152+
let tmpFolder = path.join(this.outputPath, 'package-lock-generator');
153+
154+
fs.mkdirSync(tmpFolder);
155+
fs.symlinkSync(packagePath, path.join(tmpFolder, 'package.json'));
156+
fs.symlinkSync(lockfilePath, path.join(tmpFolder, 'package-lock.json'));
157+
158+
const firstInstall = new Promise((resolve, reject) => {
159+
this.ui.writeLine('FastBoot: Building node module cache.');
160+
child_process.exec('npm install --cache=tmp', { cwd: tmpFolder }, (error) => {
161+
if (error) {
162+
reject(error);
163+
} else {
164+
resolve();
165+
}
166+
});
167+
})
168+
169+
return firstInstall
170+
.then(() => {
171+
this.ui.writeLine('FastBoot: Generating lockfile.');
172+
173+
fs.unlinkSync(path.join(tmpFolder, 'package.json'));
174+
fs.unlinkSync(path.join(tmpFolder, 'package-lock.json'));
175+
176+
fs.writeFileSync(path.join(tmpFolder, 'package.json'), stringify({ dependencies }));
177+
178+
const secondInstall = new Promise((resolve, reject) => {
179+
child_process.exec('npm install --cache=tmp --offline', { cwd: tmpFolder }, (error) => {
180+
if (error) {
181+
reject(error);
182+
} else {
183+
resolve();
184+
}
185+
});
186+
});
187+
188+
return secondInstall;
189+
})
190+
.then(() => {
191+
this.ui.writeLine('FastBoot: Removing node module cache.');
192+
193+
fs.renameSync(
194+
path.join(tmpFolder, 'package-lock.json'),
195+
path.join(this.outputPath, 'package-lock.json')
196+
);
197+
198+
const removeTmpFolder = new Promise((resolve, reject) => {
199+
rimraf(tmpFolder, (error) => {
200+
if (error) {
201+
reject(error);
202+
} else {
203+
resolve();
204+
}
205+
});
206+
});
207+
208+
return removeTmpFolder;
209+
});
129210
}
130211

131212
updateFastBootManifest(manifest) {

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"fs-extra": "^7.0.0",
3636
"json-stable-stringify": "^1.0.1",
3737
"md5-hex": "^2.0.0",
38+
"rimraf": "^2.6.3",
3839
"silent-error": "^1.1.0"
3940
},
4041
"devDependencies": {

yarn.lock

+7
Original file line numberDiff line numberDiff line change
@@ -6154,6 +6154,13 @@ rimraf@^2.2.8, rimraf@^2.3.4, rimraf@^2.4.3, rimraf@^2.4.4, rimraf@^2.5.3, rimra
61546154
dependencies:
61556155
glob "^7.0.5"
61566156

6157+
rimraf@^2.6.3:
6158+
version "2.6.3"
6159+
resolved "https://artifacts.apple.com/api/npm/npm-private/rimraf/-/rimraf-2.6.3.tgz#b2d104fe0d8fb27cf9e0a1cda8262dd3833c6cab"
6160+
integrity sha1-stEE/g2Psnz54KHNqCYt04M8bKs=
6161+
dependencies:
6162+
glob "^7.1.3"
6163+
61576164
rimraf@~2.2.6:
61586165
version "2.2.8"
61596166
resolved "https://registry.yarnpkg.com/rimraf/-/rimraf-2.2.8.tgz#e439be2aaee327321952730f99a8929e4fc50582"

0 commit comments

Comments
 (0)