Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use "--omit=dev" internally on newer npm version #86

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions package-lock.json

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

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@
"commander": "^8.0.0",
"dayjs": "^1.10.6",
"lodash.get": "^4.4.2",
"semver": "^7.3.8",
"table": "^6.7.1"
},
"devDependencies": {
"@types/chai": "^4.2.19",
"@types/lodash.get": "^4.4.6",
"@types/mocha": "^8.2.3",
"@types/node": "^16.0.0",
"@types/semver": "^7.3.13",
"@types/sinon": "^10.0.2",
"@typescript-eslint/eslint-plugin": "^4.28.2",
"@typescript-eslint/parser": "^4.28.2",
Expand Down
17 changes: 16 additions & 1 deletion src/handlers/handleInput.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
import get from 'lodash.get';
import semver from 'semver';
import { AuditLevel, CommandOptions } from 'src/types';
import { getNpmVersion } from '../utils/npm';
import { readFile } from '../utils/file';
import { getExceptionsIds } from '../utils/vulnerability';

/**
* Get the `npm audit` flag to audit only production dependencies.
* @return {String} The flag.
*/
function getProductionOnlyOption() {
const npmVersion = getNpmVersion();
if (semver.satisfies(npmVersion, '<=8.13.2')) {
return '--production';
} else {
return '--omit=dev';
}
}

/**
* Handle user's input
* @param {Object} options User's command options or flags
Expand All @@ -13,7 +28,7 @@ export default function handleInput(options: CommandOptions, fn: (T1: string, T2
const auditCommand: string = [
'npm audit',
// flags
get(options, 'production') ? '--production' : '',
get(options, 'production') ? getProductionOnlyOption() : '',
get(options, 'registry') ? `--registry=${options.registry}` : '',
]
.filter(Boolean)
Expand Down
11 changes: 11 additions & 0 deletions src/utils/npm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { exec } from 'child_process';
import { Readable } from 'stream';

/**
* 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();
}
2 changes: 1 addition & 1 deletion test/handlers/flags.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ 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 --production';
const auditCommand = 'npm audit --omit=dev';
const auditLevel = 'info';
const exceptionIds: string[] = [];

Expand Down