Skip to content

Commit 8820f03

Browse files
Fix getting npm version through CLI
Update the logic to get the npm version from the npm CLI to use the synchronous version of `exec`, `execSync`, to ensure the output can be read correctly. In the previous implementation the output would not be read correctly, leading to the comparison at `handleInput.ts:14` always evaluating to false and `'--omit=dev'` always being used.
1 parent adf6857 commit 8820f03

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

src/utils/npm.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
import { exec } from 'child_process';
2-
import { Readable } from 'stream';
1+
import { execSync } from 'child_process';
32

43
/**
54
* Get the current npm version
65
* @return {String} The npm version
76
*/
87
export function getNpmVersion(): string {
9-
const version = exec('npm --version');
10-
return (version.stdout as Readable).toString();
8+
const version = execSync('npm --version');
9+
return version.toString();
1110
}

test/handlers/flags.test.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import sinon from 'sinon';
22
import { expect } from 'chai';
3+
import * as semver from 'semver';
34
import { CommandOptions } from '../../src/types';
45
import handleInput from '../../src/handlers/handleInput';
6+
import { getNpmVersion } from '../../src/utils/npm';
57

68
describe('Flags', () => {
79
describe('default', () => {
@@ -92,7 +94,9 @@ describe('Flags', () => {
9294
it('should be able to set production mode from the command flag correctly', () => {
9395
const callbackStub = sinon.stub();
9496
const options = { production: true };
95-
const auditCommand = 'npm audit --omit=dev';
97+
const npmVersion = getNpmVersion();
98+
const flag = semver.satisfies(npmVersion, '<=8.13.2') ? '--production' : '--omit=dev';
99+
const auditCommand = `npm audit ${flag}`;
96100
const auditLevel = 'info';
97101
const exceptionIds: string[] = [];
98102

0 commit comments

Comments
 (0)