Skip to content

Commit

Permalink
Use package-lock.json to install packages if there is one available. (#…
Browse files Browse the repository at this point in the history
…246)

* Use package-lock.json to install packages if there is one available.

* Pre-create composite package.json to use lock accordingly

* Fixed unit tests

* Increase coverage
  • Loading branch information
HyperBrain authored Oct 3, 2017
1 parent 251a30e commit 85b60bf
Show file tree
Hide file tree
Showing 6 changed files with 251 additions and 137 deletions.
54 changes: 40 additions & 14 deletions lib/packExternalModules.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const _ = require('lodash');
const path = require('path');
const childProcess = require('child_process');
const fse = require('fs-extra');
const npm = require('npm-programmatic');
const isBuiltinModule = require('is-builtin-module');

function getProdModules(externalModules, packagePath, dependencyGraph) {
Expand Down Expand Up @@ -193,22 +192,49 @@ module.exports = {
// (1.a) Install all needed modules
const compositeModulePath = path.join(this.webpackOutputPath, 'dependencies');
const compositePackageJson = path.join(compositeModulePath, 'package.json');
this.serverless.utils.writeFileSync(compositePackageJson, '{}');

this.serverless.cli.log('Packing external modules: ' + compositeModules.join(', '));
// (1.a.1) Create a package.json
const compositePackage = {
name: this.serverless.service.service,
version: '1.0.0',
description: `Packaged externals for ${this.serverless.service.service}`,
private: true
};
_.forEach(compositeModules, compositeModule => {
const splitModule = _.split(compositeModule, '@');
// If we have a scoped module we have to re-add the @
if (_.startsWith(compositeModule, '@')) {
splitModule.splice(0, 1);
splitModule[0] = '@' + splitModule[0];
}
const moduleVersion = _.join(_.tail(splitModule), '@');
_.set(compositePackage, `dependencies.${_.first(splitModule)}`, moduleVersion);
});
this.serverless.utils.writeFileSync(compositePackageJson, JSON.stringify(compositePackage, null, 2));

return new BbPromise((resolve, reject) => {
// (1.a.2) Copy package-lock.json if it exists, to prevent unwanted upgrades
const packageLockPath = path.join(path.dirname(packageJsonPath), 'package-lock.json');
return BbPromise.fromCallback(cb => fse.pathExists(packageLockPath, cb))
.then(exists => {
if (exists) {
this.serverless.cli.log('Package lock found - Using locked versions');
return BbPromise.fromCallback(cb => fse.copy(packageLockPath, path.join(compositeModulePath, 'package-lock.json'), cb))
.catch(err => this.serverless.cli.log(`Warning: Could not copy lock file: ${err.message}`));
}
return BbPromise.resolve();
})
.then(() => {
const start = _.now();
npm.install(compositeModules, {
cwd: compositeModulePath,
maxBuffer: this.serverless.service.custom.packExternalModulesMaxBuffer || 200 * 1024,
save: true
}).then(() => {
this.options.verbose && this.serverless.cli.log(`Package took [${_.now() - start} ms]`); // eslint-disable-line promise/always-return
resolve(stats.stats);
}).catch(e => {
reject(e);
});
this.serverless.cli.log('Packing external modules: ' + compositeModules.join(', '));
return BbPromise.fromCallback(cb => {
childProcess.exec('npm install', {
cwd: compositeModulePath,
maxBuffer: this.serverless.service.custom.packExternalModulesMaxBuffer || 200 * 1024,
encoding: 'utf8'
}, cb);
})
.then(() => this.options.verbose && this.serverless.cli.log(`Package took [${_.now() - start} ms]`))
.return(stats.stats);
})
.mapSeries(compileStats => {
const modulePath = compileStats.compilation.compiler.outputPath;
Expand Down
8 changes: 0 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
"glob": "^7.1.2",
"is-builtin-module": "^1.0.0",
"lodash": "^4.17.4",
"npm-programmatic": "^0.0.7",
"semver": "^5.4.1",
"ts-node": "^3.2.0"
},
Expand Down
1 change: 1 addition & 0 deletions tests/mocks/fs-extra.mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
module.exports.create = sandbox => {
const fsExtraMock = {
copy: sandbox.stub().yields(),
pathExists: sandbox.stub().yields()
};

return fsExtraMock;
Expand Down
13 changes: 0 additions & 13 deletions tests/mocks/npm-programmatic.mock.js

This file was deleted.

Loading

0 comments on commit 85b60bf

Please sign in to comment.