Skip to content

Commit 94cc772

Browse files
feat: optional skip-dev yarn support
1 parent a43b379 commit 94cc772

File tree

11 files changed

+107
-7
lines changed

11 files changed

+107
-7
lines changed

lib/audit.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,8 @@ const PARTIAL_RETRY_ERROR_MSG = {
1414

1515
function audit(pm, config, reporter) {
1616
const auditor = pm === "npm" ? npmAuditer : yarnAuditer;
17-
const {
18-
"pass-enoaudit": passENoAudit,
19-
"retry-count": maxRetryCount,
20-
} = config;
17+
const { "pass-enoaudit": passENoAudit, "retry-count": maxRetryCount } =
18+
config;
2119

2220
async function run(attempt = 0) {
2321
try {

lib/yarn-auditer.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,18 @@ function yarnAuditSupportsRegistry(yarnVersion) {
4949
* `registry`: the registry to resolve packages by name and version.
5050
* `show-not-found`: show allowlisted advisories that are not found.
5151
* `levels`: the vulnerability levels to fail on, if `moderate` is set `true`, `high` and `critical` should be as well.
52+
* `skip-dev`: skip devDependencies, defaults to false
5253
* `_yarn`: a path to yarn, uses yarn from PATH if not specified.
5354
* @returns {Promise<any>} Returns the audit report summary on resolve, `Error` on rejection.
5455
*/
5556
async function audit(config, reporter = reportAudit) {
56-
const { levels, registry, "report-type": reportType, _yarn } = config;
57+
const {
58+
levels,
59+
registry,
60+
"report-type": reportType,
61+
"skip-dev": skipDev,
62+
_yarn,
63+
} = config;
5764
const yarnExec = _yarn || "yarn";
5865
let missingLockFile = false;
5966
const model = new Model(config);
@@ -170,8 +177,10 @@ async function audit(config, reporter = reportAudit) {
170177
}
171178
const options = { cwd: config.directory };
172179
const args = isYarnClassic
173-
? ["audit", "--json"]
174-
: ["npm", "audit", "--all", "--recursive", "--json"];
180+
? ["audit", "--json"].concat(skipDev ? ["--groups", "dependencies"] : [])
181+
: ["npm", "audit", "--recursive", "--json"].concat(
182+
skipDev ? ["--environment", "production"] : ["--all"]
183+
);
175184
if (registry) {
176185
const auditRegistrySupported = yarnAuditSupportsRegistry(yarnVersion);
177186
if (auditRegistrySupported) {

test/yarn-auditer.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ function config(additions) {
2121
directory: "./",
2222
registry: undefined,
2323
"pass-enoaudit": false,
24+
"skip-dev": false,
2425
};
2526
return { ...defaultConfig, ...additions };
2627
}
@@ -255,6 +256,19 @@ describe("yarn-auditer", function testYarnAuditer() {
255256
);
256257
}
257258
);
259+
it("reports summary with no vulnerabilities when critical devDependency and skip-dev is true", async () => {
260+
const summary = await audit(
261+
config({
262+
directory: testDir(
263+
canRunYarnBerry ? "yarn-berry-skip-dev" : "yarn-skip-dev"
264+
),
265+
"skip-dev": true,
266+
"report-type": "important",
267+
}),
268+
(_summary) => _summary
269+
);
270+
expect(summary).to.eql(summaryWithDefault());
271+
});
258272
// it('prints unexpected https://registry.yarnpkg.com 503 error message', () => {
259273
// const directory = testDir('yarn-503');
260274
// const errorMessagePath = path.resolve(directory, 'error-message');
1.17 KB
Binary file not shown.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Rather than have a bunch of yarn-berry.cjs of different versions,
2+
// we can specify a single yarn-berry.cjs and require the file for each package.
3+
module.exports = require("../../../yarn-berry.cjs");

test/yarn-berry-skip-dev/.yarnrc.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
yarnPath: ".yarn/releases/yarn-berry.cjs"

test/yarn-berry-skip-dev/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Yarn Berry tests
2+
3+
When creating Yarn Berry tests, there are several files and folders that may generate that are not necessary for auditing using `yarn npm audit --all --recursive --json`.
4+
5+
- .pnp.js
6+
7+
- .yarn/cache
8+
9+
Consider manually deleting them before committing.
10+
11+
Also, the `.yarn/releases/yarn-berry.cjs` file in each project re-exports the `yarn-berry.cjs` file at the root of tests.
12+
Re-exporting the file reduces duplication and version mismatching for tests.
13+
Currently, this project is set up to use the latest version v2.4.0 (at the time of writing this, Dec 6th, 2020).

test/yarn-berry-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-yarn-berry-skip-dev",
3+
"description": "Test package.json with critical devDependency",
4+
"dependencies": {
5+
"node-noop": "1.0.0"
6+
},
7+
"devDependencies": {
8+
"open": "0.0.5"
9+
}
10+
}

test/yarn-berry-skip-dev/yarn.lock

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# This file is generated by running "yarn install" inside your project.
2+
# Manual changes might be lost - proceed with caution!
3+
4+
__metadata:
5+
version: 4
6+
cacheKey: 7
7+
8+
"audit-ci-yarn-berry-skip-dev@workspace:.":
9+
version: 0.0.0-use.local
10+
resolution: "audit-ci-yarn-berry-skip-dev@workspace:."
11+
dependencies:
12+
node-noop: 1.0.0
13+
open: 0.0.5
14+
languageName: unknown
15+
linkType: soft
16+
17+
"node-noop@npm:1.0.0":
18+
version: 1.0.0
19+
resolution: "node-noop@npm:1.0.0"
20+
checksum: 33331046468af72c22553cee2b754851897fa26c36393017ad3dcfbcd28b705e573a71ae7abe18a8f357fa6fd9a3b3ab3aefb52f373b368ec4a5be40b530e269
21+
languageName: node
22+
linkType: hard
23+
24+
"open@npm:0.0.5":
25+
version: 0.0.5
26+
resolution: "open@npm:0.0.5"
27+
checksum: 5c974432a245cad8ecf3c10529fc1bce29118ee73cb71dd89bbe1dc89b453b944edd4a5e42aa56915a27d5419c7b29bfb4782f1fc336a863452d8051ec3e00af
28+
languageName: node
29+
linkType: hard

test/yarn-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-yarn-skip-dev",
3+
"description": "Test package.json with critical devDependency",
4+
"dependencies": {
5+
"node-noop": "1.0.0"
6+
},
7+
"devDependencies": {
8+
"open": "0.0.5"
9+
}
10+
}

test/yarn-skip-dev/yarn.lock

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
2+
# yarn lockfile v1
3+
4+
5+
6+
version "1.0.0"
7+
resolved "https://registry.yarnpkg.com/node-noop/-/node-noop-1.0.0.tgz#47a3e7d80cffaa6458364bd22ed85cab3307be79"
8+
integrity sha1-R6Pn2Az/qmRYNkvSLthcqzMHvnk=
9+
10+
11+
version "0.0.5"
12+
resolved "https://registry.yarnpkg.com/open/-/open-0.0.5.tgz#42c3e18ec95466b6bf0dc42f3a2945c3f0cad8fc"
13+
integrity sha1-QsPhjslUZra/DcQvOilFw/DK2Pw=

0 commit comments

Comments
 (0)