From 969c750435f71c526cec128471be39fa8732ddd8 Mon Sep 17 00:00:00 2001 From: jsetton Date: Thu, 15 Dec 2022 10:33:10 -0500 Subject: [PATCH] fix: update npm build command --- .../build-flows/abstract-build-flow.js | 3 ++- lib/builtins/build-flows/nodejs-npm.js | 10 +++++++- .../builtins/build-flows/nodejs-npm-test.js | 24 +++++++++++++++++-- 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/lib/builtins/build-flows/abstract-build-flow.js b/lib/builtins/build-flows/abstract-build-flow.js index db5a4d31..2056ba47 100644 --- a/lib/builtins/build-flows/abstract-build-flow.js +++ b/lib/builtins/build-flows/abstract-build-flow.js @@ -17,6 +17,7 @@ class AbstractBuildFlow { this.cwd = cwd; this.src = src; this.buildFile = buildFile; + this.env = process.env; this.stdio = 'inherit'; this.doDebug = !!doDebug; this.isWindows = process.platform === 'win32'; @@ -76,7 +77,7 @@ class AbstractBuildFlow { * @param {String} cmd command */ execCommand(cmd) { - childProcess.execSync(cmd, { cwd: this.cwd, stdio: this.stdio }); + childProcess.execSync(cmd, { cwd: this.cwd, env: this.env, stdio: this.stdio }); } /** diff --git a/lib/builtins/build-flows/nodejs-npm.js b/lib/builtins/build-flows/nodejs-npm.js index 7d9d262c..d7715170 100644 --- a/lib/builtins/build-flows/nodejs-npm.js +++ b/lib/builtins/build-flows/nodejs-npm.js @@ -9,6 +9,8 @@ class NodeJsNpmBuildFlow extends AbstractBuildFlow { */ static get manifest() { return 'package.json'; } + static get _lockFiles() { return ['package-lock.json', 'npm-shrinkwrap.json']; } + /** * Returns true if the build flow can handle the build */ @@ -33,11 +35,17 @@ class NodeJsNpmBuildFlow extends AbstractBuildFlow { * @param {Function} callback */ execute(callback) { + const installCmd = this._hasLockFile() ? 'ci' : 'install'; const quietFlag = this.doDebug ? '' : ' --quiet'; + this.env.NODE_ENV = 'production'; this.debug(`Installing NodeJS dependencies based on the ${NodeJsNpmBuildFlow.manifest}.`); - this.execCommand(`npm install --production${quietFlag}`); + this.execCommand(`npm ${installCmd}${quietFlag}`); this.createZip(callback); } + + _hasLockFile() { + return NodeJsNpmBuildFlow._lockFiles.some(file => fs.existsSync(path.join(this.src, file))); + } } module.exports = NodeJsNpmBuildFlow; diff --git a/test/unit/builtins/build-flows/nodejs-npm-test.js b/test/unit/builtins/build-flows/nodejs-npm-test.js index a082d1f0..5bfb6f5f 100644 --- a/test/unit/builtins/build-flows/nodejs-npm-test.js +++ b/test/unit/builtins/build-flows/nodejs-npm-test.js @@ -1,5 +1,6 @@ const { expect } = require('chai'); const sinon = require('sinon'); +const fs = require('fs-extra'); const AbstractBuildFlow = require('@src/builtins/build-flows/abstract-build-flow'); const NodeJsNpmBuildFlow = require('@src/builtins/build-flows/nodejs-npm'); @@ -9,6 +10,7 @@ describe('NodeJsNpmBuildFlow test', () => { let execStub; let debugStub; let createZipStub; + let lockFileStub; beforeEach(() => { config = { cwd: 'cwd', @@ -19,6 +21,7 @@ describe('NodeJsNpmBuildFlow test', () => { execStub = sinon.stub(AbstractBuildFlow.prototype, 'execCommand'); debugStub = sinon.stub(AbstractBuildFlow.prototype, 'debug'); createZipStub = sinon.stub(AbstractBuildFlow.prototype, 'createZip').yields(); + lockFileStub = sinon.stub(fs, 'existsSync').returns(false); }); describe('# inspect correctness of execute', () => { it('| should execute commands', (done) => { @@ -27,7 +30,22 @@ describe('NodeJsNpmBuildFlow test', () => { buildFlow.execute((err, res) => { expect(err).eql(undefined); expect(res).eql(undefined); - expect(execStub.args[0][0]).eql('npm install --production --quiet'); + expect(buildFlow.env.NODE_ENV).eql('production'); + expect(execStub.args[0][0]).eql('npm install --quiet'); + expect(createZipStub.callCount).eql(1); + done(); + }); + }); + + it('| should execute commands with package lock file', (done) => { + lockFileStub.returns(true); + const buildFlow = new NodeJsNpmBuildFlow(config); + + buildFlow.execute((err, res) => { + expect(err).eql(undefined); + expect(res).eql(undefined); + expect(buildFlow.env.NODE_ENV).eql('production'); + expect(execStub.args[0][0]).eql('npm ci --quiet'); expect(createZipStub.callCount).eql(1); done(); }); @@ -40,8 +58,10 @@ describe('NodeJsNpmBuildFlow test', () => { buildFlow.execute((err, res) => { expect(err).eql(undefined); expect(res).eql(undefined); - expect(execStub.args[0][0]).eql('npm install --production'); + expect(buildFlow.env.NODE_ENV).eql('production'); + expect(execStub.args[0][0]).eql('npm install'); expect(debugStub.args[0][0]).eql('Installing NodeJS dependencies based on the package.json.'); + expect(createZipStub.callCount).eql(1); done(); }); });