Skip to content

Commit

Permalink
Sketch of lockfile generation.
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanhammond committed Mar 14, 2019
1 parent 43ffc65 commit 8376f07
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 10 deletions.
101 changes: 91 additions & 10 deletions lib/broccoli/fastboot-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@

const fs = require('fs');
const fmt = require('util').format;
const uniq = require('ember-cli-lodash-subset').uniq;
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');

Expand Down Expand Up @@ -47,13 +48,15 @@ module.exports = class FastBootConfig extends Plugin {
* and write it to `package.json`.
*/
build() {
this.buildConfig();
this.buildDependencies();
this.buildManifest();
this.buildHostWhitelist();

let outputPath = path.join(this.outputPath, 'package.json');
this.writeFileIfContentChanged(outputPath, this.toJSONString());
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) {
Expand Down Expand Up @@ -124,8 +127,86 @@ module.exports = class FastBootConfig extends Plugin {
});
}

this.dependencies = dependencies;
this.moduleWhitelist = uniq(moduleWhitelist);
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')
);

const removeTmpFolder = new Promise((resolve, reject) => {
rimraf(tmpFolder, (error) => {
if (error) {
reject(error);
} else {
resolve();
}
});
});

return removeTmpFolder;
});
}

updateFastBootManifest(manifest) {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"fs-extra": "^7.0.0",
"json-stable-stringify": "^1.0.1",
"md5-hex": "^2.0.0",
"rimraf": "^2.6.3",
"silent-error": "^1.1.0"
},
"devDependencies": {
Expand Down
7 changes: 7 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
dependencies:
glob "^7.0.5"

rimraf@^2.6.3:
version "2.6.3"
resolved "https://artifacts.apple.com/api/npm/npm-private/rimraf/-/rimraf-2.6.3.tgz#b2d104fe0d8fb27cf9e0a1cda8262dd3833c6cab"
integrity sha1-stEE/g2Psnz54KHNqCYt04M8bKs=
dependencies:
glob "^7.1.3"

rimraf@~2.2.6:
version "2.2.8"
resolved "https://registry.yarnpkg.com/rimraf/-/rimraf-2.2.8.tgz#e439be2aaee327321952730f99a8929e4fc50582"
Expand Down

0 comments on commit 8376f07

Please sign in to comment.