Skip to content

Commit

Permalink
Fix getting npm version through CLI
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ericcornelissen committed Aug 29, 2024
1 parent adf6857 commit 8820f03
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
7 changes: 3 additions & 4 deletions src/utils/npm.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { exec } from 'child_process';
import { Readable } from 'stream';
import { execSync } from 'child_process';

/**
* Get the current npm version
* @return {String} The npm version
*/
export function getNpmVersion(): string {
const version = exec('npm --version');
return (version.stdout as Readable).toString();
const version = execSync('npm --version');
return version.toString();
}
6 changes: 5 additions & 1 deletion test/handlers/flags.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import sinon from 'sinon';
import { expect } from 'chai';
import * as semver from 'semver';
import { CommandOptions } from '../../src/types';
import handleInput from '../../src/handlers/handleInput';
import { getNpmVersion } from '../../src/utils/npm';

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

Expand Down

0 comments on commit 8820f03

Please sign in to comment.