Skip to content

Sketch of lockfile generation. #671

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ module.exports = {
throw new SilentError("This version of ember-cli-fastboot requires a newer version of broccoli-asset-rev");
}

// Prevent fingerprinting node_modules
app.options.fingerprint = app.options.fingerprint || {};
app.options.fingerprint.exclude = app.options.fingerprint.exclude || [];
app.options.fingerprint.exclude.push('node_modules/');

// set autoRun to false since we will conditionally include creating app when app files
// is eval'd in app-boot
app.options.autoRun = false;
Expand Down
106 changes: 96 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,91 @@ 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')
);

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) {
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 @@ -6156,6 +6156,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