Skip to content

Commit a43b379

Browse files
feat: optional skip-dev npm support
1 parent be22cc9 commit a43b379

File tree

9 files changed

+105
-0
lines changed

9 files changed

+105
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ A config file can manage auditing preferences `audit-ci`. The config file's keys
129129
"show-not-found": <boolean>, // [Optional] defaults `true`
130130
"registry": <string>, // [Optional] defaults `undefined`
131131
"retry-count": <number>, // [Optional] defaults 5
132+
"skip-dev": <boolean>, // [Optional] defaults `false`
132133
"advisories": <number[]>, // [Deprecated, optional] defaults `[]`
133134
"path-whitelist": <string[]>, // [Deprecated, optional] defaults `[]`
134135
"whitelist": <string[]> // [Deprecated, optional] defaults `[]`

lib/audit-ci.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,11 @@ const { argv } = yargs
100100
"Pass if no audit is performed due to the registry returning ENOAUDIT",
101101
type: "boolean",
102102
},
103+
"skip-dev": {
104+
default: false,
105+
describe: "Skip devDependencies",
106+
type: "boolean",
107+
},
103108
advisories: {
104109
default: [],
105110
describe:

lib/npm-auditer.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ async function runNpmAudit(config) {
2020
if (registry) {
2121
args.push("--registry", registry);
2222
}
23+
if (config["skip-dev"]) {
24+
args.push("--production");
25+
}
2326
const options = { cwd: directory };
2427
await runProgram(npmExec, args, options, outListener, errListener);
2528
if (stderrBuffer.length) {
@@ -85,6 +88,7 @@ function report(parsedOutput, config, reporter) {
8588
* `registry`: the registry to resolve packages by name and version.
8689
* `show-not-found`: show allowlisted advisories that are not found.
8790
* `levels`: the vulnerability levels to fail on, if `moderate` is set `true`, `high` and `critical` should be as well.
91+
* `skip-dev`: skip devDependencies, defaults to false
8892
* `_npm`: a path to npm, uses npm from PATH if not specified.
8993
* @returns {Promise<any>} Returns the audit report summary on resolve, `Error` on rejection.
9094
*/

test/npm-auditer.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const reportNpmModerateSeverity = require("./npm-moderate/npm-output.json");
99
const reportNpmAllowlistedPath = require("./npm-allowlisted-path/npm-output.json");
1010
const reportNpmLow = require("./npm-low/npm-output.json");
1111
const reportNpmNone = require("./npm-none/npm-output.json");
12+
const reportNpmSkipDev = require("./npm-skip-dev/npm-output.json");
1213

1314
// To modify what slow times are, need to use
1415
// function() {} instead of () => {}
@@ -265,6 +266,18 @@ describe("npm-auditer", function testNpmAuditer() {
265266
done();
266267
});
267268
});
269+
it("reports summary with no vulnerabilities when critical devDependency and skip-dev is true", () => {
270+
const summary = report(
271+
reportNpmSkipDev,
272+
config({
273+
directory: testDir("npm-skip-dev"),
274+
"skip-dev": true,
275+
"report-type": "important",
276+
}),
277+
(_summary) => _summary
278+
);
279+
expect(summary).to.eql(summaryWithDefault());
280+
});
268281
// it("fails errors with code ENOAUDIT on a valid site with no audit", (done) => {
269282
// audit(
270283
// config({

test/npm-skip-dev/npm-output.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"actions": [],
3+
"advisories": {},
4+
"muted": [],
5+
"metadata": {
6+
"vulnerabilities": {
7+
"info": 0,
8+
"low": 0,
9+
"moderate": 0,
10+
"high": 0,
11+
"critical": 0
12+
},
13+
"dependencies": 1,
14+
"devDependencies": 0,
15+
"optionalDependencies": 0,
16+
"totalDependencies": 1
17+
},
18+
"runId": "cf8267d6-1ce5-44eb-9320-003467502021"
19+
}

test/npm-skip-dev/npm7-output.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"auditReportVersion": 2,
3+
"vulnerabilities": {},
4+
"metadata": {
5+
"vulnerabilities": {
6+
"info": 0,
7+
"low": 0,
8+
"moderate": 0,
9+
"high": 0,
10+
"critical": 0,
11+
"total": 0
12+
},
13+
"dependencies": {
14+
"prod": 2,
15+
"dev": 1,
16+
"optional": 0,
17+
"peer": 0,
18+
"peerOptional": 0,
19+
"total": 2
20+
}
21+
}
22+
}

test/npm-skip-dev/package-lock.json

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/npm-skip-dev/package.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "audit-ci-npm-skip-dev",
3+
"description": "Test package.json with critical vulnerability in devDependencies",
4+
"dependencies": {
5+
"node-noop": "1.0.0"
6+
},
7+
"devDependencies": {
8+
"open": "0.0.5"
9+
}
10+
}

test/npm7-auditer.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const reportNpmModerateSeverity = require("./npm-moderate/npm7-output.json");
99
const reportNpmAllowlistedPath = require("./npm-allowlisted-path/npm7-output.json");
1010
const reportNpmLow = require("./npm-low/npm7-output.json");
1111
const reportNpmNone = require("./npm-none/npm7-output.json");
12+
const reportNpmSkipDev = require("./npm-skip-dev/npm-output.json");
1213

1314
describe("npm7-auditer", function testNpm7Auditer() {
1415
it("prints full report with critical severity", () => {
@@ -263,4 +264,16 @@ describe("npm7-auditer", function testNpm7Auditer() {
263264
done();
264265
});
265266
});
267+
it("reports summary with no vulnerabilities when critical devDependency and skip-dev is true", () => {
268+
const summary = report(
269+
reportNpmSkipDev,
270+
config({
271+
directory: testDir("npm-skip-dev"),
272+
"skip-dev": true,
273+
"report-type": "important",
274+
}),
275+
(_summary) => _summary
276+
);
277+
expect(summary).to.eql(summaryWithDefault());
278+
});
266279
});

0 commit comments

Comments
 (0)