Skip to content

Commit c863fee

Browse files
authored
BREAKING: Require Node 18, support ESM and CJS (#319)
1 parent 168a597 commit c863fee

37 files changed

+4618
-2995
lines changed

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
test/yarn-berry.cjs
22
dist
3+
coverage

.eslintrc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"mocha": true
88
},
99
"parserOptions": {
10-
"ecmaVersion": 2019
10+
"ecmaVersion": 2021
1111
},
1212
"extends": [
1313
"eslint:recommended",

.github/workflows/build.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@ jobs:
88
runs-on: ubuntu-latest
99
strategy:
1010
matrix:
11-
node: ["16", "18", "20"]
11+
node: ["18", "20", "21"]
1212
name: Build and test with Node ${{ matrix.node }}
1313
steps:
14-
- uses: actions/checkout@v3
15-
- uses: actions/setup-node@v3
14+
- uses: actions/checkout@v4
15+
- uses: actions/setup-node@v4
1616
with:
1717
node-version: ${{ matrix.node }}
1818
registry-url: https://registry.npmjs.org/
1919
- run: npm ci
2020
- run: npm run build
2121
# Have audit-ci run audit-ci to audit itself :)
2222
- run: node ./dist/bin.js --config ./audit-ci.jsonc
23-
- run: npm test
23+
- run: npm run coverage
2424
- run: npm run lint
2525
- run: npm run format -- --check

.github/workflows/codeql-analysis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ jobs:
3939

4040
steps:
4141
- name: Checkout repository
42-
uses: actions/checkout@v3
42+
uses: actions/checkout@v4
4343

4444
# Initializes the CodeQL tools for scanning.
4545
- name: Initialize CodeQL

.github/workflows/npmpublish.yml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,13 @@ jobs:
1111
test-and-publish:
1212
runs-on: ubuntu-latest
1313
steps:
14-
- uses: actions/checkout@v3
15-
- uses: actions/setup-node@v3
14+
- uses: actions/checkout@v4
15+
- uses: actions/setup-node@v4
1616
with:
17-
node-version: "16"
1817
registry-url: https://registry.npmjs.org/
1918
- run: npm ci
2019
- run: npm run build
21-
- run: npm test
20+
- run: npm run coverage
2221
- run: npm run lint
2322
- run: npm publish
2423
env:

.mocharc.json

Lines changed: 0 additions & 14 deletions
This file was deleted.

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
test/yarn-berry.cjs
2+
coverage
23
**/pnpm-lock.yaml
34
**/*-output.json
45
**/.pnp.cjs

.travis.yml

Lines changed: 0 additions & 26 deletions
This file was deleted.

README.md

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# audit-ci
22

33
[![npm version](https://badge.fury.io/js/audit-ci.svg)](https://badge.fury.io/js/audit-ci)
4-
[![Build Status](https://app.travis-ci.com/IBM/audit-ci.svg?branch=main)](https://app.travis-ci.com/github/IBM/audit-ci)
54
[![CircleCI](https://circleci.com/gh/IBM/audit-ci/tree/main.svg?style=svg)](https://circleci.com/gh/IBM/audit-ci/tree/main)
65
[![GitHub CI](https://github.com/IBM/audit-ci/actions/workflows/build.yml/badge.svg)](https://github.com/IBM/audit-ci/actions/workflows/build.yml)
76
[![CodeQL](https://github.com/IBM/audit-ci/actions/workflows/codeql-analysis.yml/badge.svg)](https://github.com/IBM/audit-ci/actions/workflows/codeql-analysis.yml)
@@ -14,7 +13,7 @@ threshold while ignoring allowlisted advisories.
1413
1514
## Requirements
1615

17-
- Node >=12.9.0 (Yarn Berry requires Node >=12.13.0)
16+
- Node >=16
1817
- _(Optional)_ Yarn ^1.12.3 || Yarn >=2.4.0
1918
- _(Optional)_ PNPM >=4.3.0
2019

@@ -28,9 +27,9 @@ _(Recommended)_ Install `audit-ci` during your CI environment using `npx`, `yarn
2827

2928
```sh
3029
# Use the option for your project's package manager, pinning to a major version to avoid breaking changes
31-
npx audit-ci@^6 --config ./audit-ci.jsonc
32-
yarn dlx audit-ci@^6 --config ./audit-ci.jsonc
33-
pnpm dlx audit-ci@^6 --config ./audit-ci.jsonc
30+
npx audit-ci@^7 --config ./audit-ci.jsonc
31+
yarn dlx audit-ci@^7 --config ./audit-ci.jsonc
32+
pnpm dlx audit-ci@^7 --config ./audit-ci.jsonc
3433
```
3534

3635
Alternatively, `audit-ci` can be installed as a devDependency.
@@ -197,7 +196,7 @@ You can also use an object notation ([NSPRecord](#nsprecord-fields)) in which yo
197196
steps:
198197
- uses: actions/checkout@v2
199198
- name: Audit for vulnerabilities
200-
run: npx audit-ci@^6 --config ./audit-ci.jsonc
199+
run: npx audit-ci@^7 --config ./audit-ci.jsonc
201200
```
202201
203202
_(Recommended)_ Run `audit-ci` immediately after checking out the git repository to reduce the risk of executing a `postinstall` script from a compromised NPM package.
@@ -223,7 +222,7 @@ steps:
223222
# command: if [[ ! -z $CIRCLE_PULL_REQUEST ]] ; then npx audit-ci --config ./audit-ci.jsonc ; fi
224223
- run:
225224
name: run-audit-ci
226-
command: npx audit-ci@^6 --config ./audit-ci.jsonc
225+
command: npx audit-ci@^7 --config ./audit-ci.jsonc
227226
- run:
228227
name: install-npm
229228
command: "npm install --no-audit"
@@ -237,14 +236,14 @@ Auditing only on PR builds is [recommended](#qa)
237236
scripts:
238237
# This script should be the first that runs to reduce the risk of
239238
# executing a script from a compromised NPM package.
240-
- if [ "${TRAVIS_PULL_REQUEST}" != "false" ]; then npx audit-ci@^6 --config ./audit-ci.jsonc; fi
239+
- if [ "${TRAVIS_PULL_REQUEST}" != "false" ]; then npx audit-ci@^7 --config ./audit-ci.jsonc; fi
241240
```
242241
243242
For `Travis-CI` not using PR builds:
244243

245244
```yml
246245
scripts:
247-
- npx audit-ci@^6 --config ./audit-ci.jsonc
246+
- npx audit-ci@^7 --config ./audit-ci.jsonc
248247
```
249248

250249
## Options
@@ -349,7 +348,7 @@ With a `JSON5` config file:
349348
Or, with the CLI with `yarn dlx`:
350349

351350
```sh
352-
yarn dlx audit-ci@^6 -l -a "GHSA-38f5-ghc2-fcmv" lodash base64url --show-found false
351+
yarn dlx audit-ci@^7 -l -a "GHSA-38f5-ghc2-fcmv" lodash base64url --show-found false
353352
```
354353

355354
### Prevents build with critical vulnerabilities showing the full report
@@ -367,7 +366,7 @@ With a `JSONC` config file:
367366
Or, with the CLI with `pnpm dlx`:
368367

369368
```sh
370-
pnpm dlx audit-ci@^6 --critical --report-type full
369+
pnpm dlx audit-ci@^7 --critical --report-type full
371370
```
372371

373372
### Continues build regardless of vulnerabilities, but show the summary report
@@ -384,7 +383,7 @@ With a `JSONC` config file:
384383
Or, with the CLI:
385384

386385
```sh
387-
npx audit-ci@^6 --report-type summary
386+
npx audit-ci@^7 --report-type summary
388387
```
389388

390389
### Pass additional args to Yarn Berry to exclude a certain package from audit
@@ -401,7 +400,7 @@ With a `JSONC` config file, in a project on Yarn Berry v3.3.0 or later:
401400
Or, with the CLI:
402401

403402
```sh
404-
npx audit-ci@^6 --extra-args '\--exclude' example
403+
npx audit-ci@^7 --extra-args '\--exclude' example
405404
```
406405

407406
### Example config file and different directory usage
@@ -428,7 +427,7 @@ npx audit-ci@^6 --extra-args '\--exclude' example
428427
```
429428

430429
```sh
431-
npx audit-ci@^6 --directory test/npm-config-file --config test/npm-config-file/audit-ci.jsonc
430+
npx audit-ci@^7 --directory test/npm-config-file --config test/npm-config-file/audit-ci.jsonc
432431
```
433432

434433
#### test/pnpm-config-file/audit-ci.json5
@@ -449,7 +448,7 @@ npx audit-ci@^6 --directory test/npm-config-file --config test/npm-config-file/a
449448
```
450449

451450
```sh
452-
npx audit-ci@^6 --directory test/pnpm-config-file --config test/pnpm-config-file/audit-ci.json5
451+
npx audit-ci@^7 --directory test/pnpm-config-file --config test/pnpm-config-file/audit-ci.json5
453452
```
454453

455454
## Codemod

docs/schema.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* This file generates the `schema.json` file. */
22

3-
import { type NSPRecord } from "../lib/nsp-record";
3+
import { type NSPRecord } from "../lib/nsp-record.js";
44

55
export interface Schema {
66
/** @default https://github.com/IBM/audit-ci/raw/main/docs/schema.json */

lib/allowlist.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import type { GitHubAdvisoryId } from "audit-types";
2-
import { isGitHubAdvisoryId } from "./common";
2+
import { isGitHubAdvisoryId } from "./common.js";
33
import {
44
type NSPContent,
55
type NSPRecord,
66
type GitHubNSPRecord,
77
getAllowlistId,
88
isNSPRecordActive,
9-
} from "./nsp-record";
9+
} from "./nsp-record.js";
1010

1111
export type AllowlistRecord = string | NSPRecord;
1212

@@ -24,7 +24,7 @@ const DEFAULT_NSP_CONTENT: Readonly<NSPContent> = {
2424
* @returns Normalized NSPRecord object.
2525
*/
2626
export function normalizeAllowlistRecord(
27-
recordOrId: AllowlistRecord
27+
recordOrId: AllowlistRecord,
2828
): NSPRecord {
2929
return typeof recordOrId === "string"
3030
? {
@@ -40,7 +40,7 @@ export function normalizeAllowlistRecord(
4040
* @returns An array of NSPRecords with duplicates removed.
4141
*/
4242
export function dedupeAllowlistRecords(
43-
recordsOrIds: AllowlistRecord[]
43+
recordsOrIds: AllowlistRecord[],
4444
): NSPRecord[] {
4545
const map = new Map<string, NSPRecord>();
4646
for (const recordOrId of recordsOrIds) {
@@ -78,7 +78,7 @@ class Allowlist {
7878
for (const allowlist of input) {
7979
if (typeof allowlist === "number") {
8080
throw new TypeError(
81-
"Unsupported number as allowlist. Perform codemod to update config to use GitHub advisory as identifiers: https://github.com/quinnturner/audit-ci-codemod with `npx @quinnturner/audit-ci-codemod`. See also: https://github.com/IBM/audit-ci/pull/217"
81+
"Unsupported number as allowlist. Perform codemod to update config to use GitHub advisory as identifiers: https://github.com/quinnturner/audit-ci-codemod with `npx @quinnturner/audit-ci-codemod`. See also: https://github.com/IBM/audit-ci/pull/217",
8282
);
8383
}
8484

@@ -106,7 +106,7 @@ class Allowlist {
106106
}
107107

108108
static mapConfigToAllowlist(
109-
config: Readonly<{ allowlist: AllowlistRecord[] }>
109+
config: Readonly<{ allowlist: AllowlistRecord[] }>,
110110
) {
111111
const { allowlist } = config;
112112
const deduplicatedAllowlist = dedupeAllowlistRecords(allowlist || []);

lib/audit-ci.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import audit from "./audit";
2-
import { green, red } from "./colors";
3-
import { runYargs } from "./config";
1+
import audit from "./audit.js";
2+
import { green, red } from "./colors.js";
3+
import { runYargs } from "./config.js";
44

55
/**
66
* Runs the audit-ci CLI.

lib/audit.ts

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { yellow } from "./colors";
2-
import { ReportConfig } from "./common";
3-
import type { AuditCiFullConfig } from "./config";
4-
import type { Summary } from "./model";
5-
import * as npmAuditer from "./npm-auditer";
6-
import * as pnpmAuditer from "./pnpm-auditer";
7-
import * as yarnAuditer from "./yarn-auditer";
1+
import { yellow } from "./colors.js";
2+
import { ReportConfig } from "./common.js";
3+
import type { AuditCiFullConfig } from "./config.js";
4+
import type { Summary } from "./model.js";
5+
import * as npmAuditer from "./npm-auditer.js";
6+
import * as pnpmAuditer from "./pnpm-auditer.js";
7+
import * as yarnAuditer from "./yarn-auditer.js";
88

99
const PARTIAL_RETRY_ERROR_MSG = {
1010
// The three ENOAUDIT error messages for NPM are:
@@ -19,23 +19,27 @@ const PARTIAL_RETRY_ERROR_MSG = {
1919
} as const;
2020

2121
function getAuditor(
22-
packageManager: "npm" | "yarn" | "pnpm"
22+
packageManager: "npm" | "yarn" | "pnpm",
2323
): typeof yarnAuditer | typeof npmAuditer | typeof pnpmAuditer {
2424
switch (packageManager) {
25-
case "yarn":
25+
case "yarn": {
2626
return yarnAuditer;
27-
case "npm":
27+
}
28+
case "npm": {
2829
return npmAuditer;
29-
case "pnpm":
30+
}
31+
case "pnpm": {
3032
return pnpmAuditer;
31-
default:
33+
}
34+
default: {
3235
throw new Error(`Invalid package manager: ${packageManager}`);
36+
}
3337
}
3438
}
3539

3640
async function audit(
3741
config: AuditCiFullConfig,
38-
reporter?: (summary: Summary, config: ReportConfig) => Summary
42+
reporter?: (summary: Summary, config: ReportConfig) => Summary,
3943
) {
4044
const {
4145
"pass-enoaudit": passENoAudit,
@@ -49,12 +53,15 @@ async function audit(
4953
try {
5054
const result = await auditor.auditWithFullConfig(config, reporter);
5155
return result;
52-
} catch (error: any) {
53-
const message = error.message || error;
56+
} catch (error: unknown) {
57+
const message =
58+
error && typeof error === "object" && "message" in error
59+
? error.message
60+
: error;
5461
const isRetryableMessage =
5562
typeof message === "string" &&
5663
PARTIAL_RETRY_ERROR_MSG[packageManager].some((retryErrorMessage) =>
57-
message.includes(retryErrorMessage)
64+
message.includes(retryErrorMessage),
5865
);
5966
const shouldRetry = attempt < maxRetryCount && isRetryableMessage;
6067
if (shouldRetry) {
@@ -67,7 +74,7 @@ async function audit(
6774
if (shouldPassWithoutAuditing) {
6875
console.warn(
6976
yellow,
70-
`ACTION RECOMMENDED: An audit could not performed due to ${maxRetryCount} audits that resulted in ENOAUDIT. Perform an audit manually and verify that no significant vulnerabilities exist before merging.`
77+
`ACTION RECOMMENDED: An audit could not performed due to ${maxRetryCount} audits that resulted in ENOAUDIT. Perform an audit manually and verify that no significant vulnerabilities exist before merging.`,
7178
);
7279
return;
7380
}

lib/bin.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env node
2-
import { runAuditCi } from "./audit-ci";
2+
import { runAuditCi } from "./audit-ci.js";
33

4+
// eslint-disable-next-line unicorn/prefer-top-level-await
45
runAuditCi().catch((error) => {
56
console.error(error);
67
process.exit(1);

0 commit comments

Comments
 (0)