Skip to content

Commit 83c6acf

Browse files
committed
Replace chalk with built-in styleText
1 parent 2354f8c commit 83c6acf

11 files changed

Lines changed: 37 additions & 47 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ provided directly.
5959
| option | values | default | description |
6060
| :--- | :--- | :--- |:--- |
6161
| reporter | `install`, `detail`, `json`, `quiet` | `install` | specify which output format you want to use |
62-
| chalk   | `Chalk` instance  | required  | a Chalk instance to use for colorizing strings. use `new chalk.Instance({ level: 0 })` for no colors |
62+
| color | `true`, `false` | `false` | indicates if ANSI color escapes should be used to colorize the report |
6363
| unicode  | `true`, `false`                  | `true` | indicates if unicode characters should be used|
6464
| indent   | Number or String                | `2` | indentation for `'json'` report|
6565
| auditLevel | 'info', 'low', 'moderate', 'high', 'critical', 'none' | `low` (ie, exit 0 if only `info` advisories are found) | level of vulnerability that will trigger a non-zero exit code (set to 'none' to always exit with a 0 status code) |

lib/colors.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
1-
module.exports = (chalk) => {
2-
const green = s => chalk.green.bold(s)
3-
const red = s => chalk.red.bold(s)
4-
const magenta = s => chalk.magenta.bold(s)
5-
const yellow = s => chalk.yellow.bold(s)
6-
const white = s => chalk.bold(s)
1+
'use strict'
2+
3+
const { styleText } = require('node:util')
4+
5+
module.exports = (color) => {
6+
// chalk coerced non-string input (eg numbers) to strings, but styleText
7+
// throws on it, so coerce here to preserve the previous behavior
8+
const style = (format, s) => color ? styleText(format, String(s), { validateStream: false }) : s
9+
const green = s => style(['green', 'bold'], s)
10+
const red = s => style(['red', 'bold'], s)
11+
const magenta = s => style(['magenta', 'bold'], s)
12+
const yellow = s => style(['yellow', 'bold'], s)
13+
const white = s => style('bold', s)
714
const severity = (sev, s) => sev.toLowerCase() === 'moderate' ? yellow(s || sev)
815
: sev.toLowerCase() === 'high' ? red(s || sev)
916
: sev.toLowerCase() === 'critical' ? magenta(s || sev)
1017
: white(s || sev)
11-
const dim = s => chalk.dim(s)
18+
const dim = s => style('dim', s)
1219

1320
return {
1421
dim,

lib/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const exitCode = require('./exit-code.js')
1212
module.exports = Object.assign((data, options = {}) => {
1313
const {
1414
reporter = 'install',
15-
chalk,
15+
color,
1616
unicode = true,
1717
indent = 2,
1818
} = options
@@ -35,7 +35,7 @@ module.exports = Object.assign((data, options = {}) => {
3535
}
3636

3737
return {
38-
report: reporters[reporter](data, { chalk, unicode, indent }),
38+
report: reporters[reporter](data, { color, unicode, indent }),
3939
exitCode: exitCode(data, auditLevel),
4040
}
4141
}, { reporters })

lib/reporters/detail.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
const colors = require('../colors.js')
44
const install = require('./install.js')
55

6-
module.exports = (data, { chalk }) => {
7-
const summary = install.summary(data, { chalk })
6+
module.exports = (data, { color }) => {
7+
const summary = install.summary(data, { color })
88
const none = data.metadata.vulnerabilities.total === 0
9-
return none ? summary : fullReport(data, { chalk, summary })
9+
return none ? summary : fullReport(data, { color, summary })
1010
}
1111

12-
const fullReport = (data, { chalk, summary }) => {
13-
const c = colors(chalk)
12+
const fullReport = (data, { color, summary }) => {
13+
const c = colors(color)
1414
const output = [c.white('# npm audit report'), '']
1515

1616
const printed = new Set()

lib/reporters/install.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const colors = require('../colors.js')
22

3-
const calculate = (data, { chalk }) => {
4-
const c = colors(chalk)
3+
const calculate = (data, { color }) => {
4+
const c = colors(color)
55
const output = []
66
const { metadata: { vulnerabilities } } = data
77
const vulnCount = vulnerabilities.total

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
"devDependencies": {
3333
"@npmcli/eslint-config": "^7.0.0",
3434
"@npmcli/template-oss": "5.1.1",
35-
"chalk": "^5.2.0",
3635
"tap": "^16.0.0"
3736
},
3837
"directories": {

test/colors.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
const t = require('tap')
2-
const chalk = require('./fixtures/chalk.js')
32
const colors = require('../lib/colors.js')
43

54
t.formatSnapshot = ({ report, exitCode }) => `${report}\nexitCode=${exitCode}`
65

76
t.test('with colors', async t => {
8-
const { color } = await chalk()
9-
const c = colors(color)
7+
const c = colors(true)
108
t.not(c.green('x'), 'x')
119
t.not(c.red('x'), 'x')
1210
t.not(c.magenta('x'), 'x')
@@ -24,8 +22,7 @@ t.test('with colors', async t => {
2422
})
2523

2624
t.test('without colors', async t => {
27-
const { noColor } = await chalk()
28-
const c = colors(noColor)
25+
const c = colors(false)
2926
t.equal(c.green('x'), 'x')
3027
t.equal(c.red('x'), 'x')
3128
t.equal(c.magenta('x'), 'x')

test/fixtures/chalk.js

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

test/index.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ const t = require('tap')
22
const nar = require('../')
33
nar.reporters.fake = (data, options) => ({ data, options })
44
const fixture = require('./fixtures/index.js')
5-
const chalk = require('./fixtures/chalk.js')
65

76
t.equal(nar.reporters.install, require('../lib/reporters/install.js'))
87
t.equal(nar.reporters.detail, require('../lib/reporters/detail.js'))
@@ -25,7 +24,7 @@ t.strictSame(nar({ foo: 'bar', metadata }, fake), {
2524
report: {
2625
data: { foo: 'bar', metadata },
2726
options: {
28-
chalk: undefined,
27+
color: undefined,
2928
unicode: true,
3029
indent: 2,
3130
},
@@ -37,7 +36,7 @@ t.strictSame(nar({ foo: 'bar', toJSON: () => ({ bar: 'baz', metadata }) }, fake)
3736
report: {
3837
data: { bar: 'baz', metadata },
3938
options: {
40-
chalk: undefined,
39+
color: undefined,
4140
unicode: true,
4241
indent: 2,
4342
},
@@ -49,7 +48,7 @@ t.strictSame(nar({ foo: 'bar', auditLevel: null, metadata: highMeta }, fake), {
4948
report: {
5049
data: { foo: 'bar', auditLevel: null, metadata: highMeta },
5150
options: {
52-
chalk: undefined,
51+
color: undefined,
5352
unicode: true,
5453
indent: 2,
5554
},
@@ -58,9 +57,8 @@ t.strictSame(nar({ foo: 'bar', auditLevel: null, metadata: highMeta }, fake), {
5857

5958
t.test('install is default reporter', async t => {
6059
const fix = fixture('one-vuln')
61-
const { color } = await chalk()
62-
t.strictSame(nar(fix, { chalk: color }).report, nar.reporters.install(fix, {
63-
chalk: color,
60+
t.strictSame(nar(fix, { color: true }).report, nar.reporters.install(fix, {
61+
color: true,
6462
unicode: true,
6563
indent: 2,
6664
}))

test/reporters/detail.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
const t = require('tap')
22
const fixture = require('../fixtures/index.js')
3-
const chalk = require('../fixtures/chalk')
43
const Report = require('../..')
54

65
t.formatSnapshot = ({ report, exitCode }) => `${report}\nexitCode=${exitCode}`
76

87
for (const f of fixture.files) {
98
t.test(f, async t => {
109
const data = fixture(f)
11-
const { color, noColor } = await chalk()
12-
t.matchSnapshot(Report(data, { reporter: 'detail', chalk: color }), 'default settings')
13-
t.matchSnapshot(Report(data, { reporter: 'detail', chalk: noColor }), 'no color')
10+
t.matchSnapshot(Report(data, { reporter: 'detail', color: true }), 'default settings')
11+
t.matchSnapshot(Report(data, { reporter: 'detail', color: false }), 'no color')
1412
t.end()
1513
})
1614
}

0 commit comments

Comments
 (0)