Skip to content

Commit

Permalink
fix: update npm build command
Browse files Browse the repository at this point in the history
  • Loading branch information
jsetton committed Dec 15, 2022
1 parent 5fa9b7a commit 969c750
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
3 changes: 2 additions & 1 deletion lib/builtins/build-flows/abstract-build-flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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 });
}

/**
Expand Down
10 changes: 9 additions & 1 deletion lib/builtins/build-flows/nodejs-npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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;
24 changes: 22 additions & 2 deletions test/unit/builtins/build-flows/nodejs-npm-test.js
Original file line number Diff line number Diff line change
@@ -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');
Expand All @@ -9,6 +10,7 @@ describe('NodeJsNpmBuildFlow test', () => {
let execStub;
let debugStub;
let createZipStub;
let lockFileStub;
beforeEach(() => {
config = {
cwd: 'cwd',
Expand All @@ -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) => {
Expand All @@ -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();
});
Expand All @@ -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();
});
});
Expand Down

0 comments on commit 969c750

Please sign in to comment.