Skip to content

Commit 9e7f5ac

Browse files
committed
feat!: require passing in chalk instance
BREAKING CHANGES: libnpmexec no longer accepts a color property and instead requires passing in a chalk instance. To replicate the old behavior of `color: false` use `chalk: new Chalk({ level: 0 })`.
1 parent 5dbbe60 commit 9e7f5ac

File tree

4 files changed

+40
-47
lines changed

4 files changed

+40
-47
lines changed

workspaces/libnpmexec/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ await libexec({
3131
- `call`: An alternative command to run when using `packages` option **String**, defaults to empty string.
3232
- `cache`: The path location to where the npm cache folder is placed **String**
3333
- `npxCache`: The path location to where the npx cache folder is placed **String**
34-
- `color`: Output should use color? **Boolean**, defaults to `false`
34+
- `chalk`: Chalk instance to use for colors? **Required**
3535
- `localBin`: Location to the `node_modules/.bin` folder of the local project to start scanning for bin files **String**, defaults to `./node_modules/.bin`. **libexec** will walk up the directory structure looking for `node_modules/.bin` folders in parent folders that might satisfy the current `arg` and will use that bin if found.
3636
- `locationMsg`: Overrides "at location" message when entering interactive mode **String**
3737
- `globalBin`: Location to the global space bin folder, same as: `$(npm bin -g)` **String**, defaults to empty string.

workspaces/libnpmexec/lib/run-script.js

+4-13
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
1-
const chalk = require('chalk')
21
const ciInfo = require('ci-info')
32
const runScript = require('@npmcli/run-script')
43
const readPackageJson = require('read-package-json-fast')
54
const npmlog = require('npmlog')
65
const log = require('proc-log')
76
const noTTY = require('./no-tty.js')
87

9-
const nocolor = {
10-
reset: s => s,
11-
bold: s => s,
12-
dim: s => s,
13-
}
14-
158
const run = async ({
169
args,
1710
call,
@@ -25,8 +18,6 @@ const run = async ({
2518
}) => {
2619
// turn list of args into command string
2720
const script = call || args.shift() || scriptShell
28-
const color = !!flatOptions.color
29-
const colorize = color ? chalk : nocolor
3021

3122
// do the fakey runScript dance
3223
// still should work if no package.json in cwd
@@ -49,14 +40,14 @@ const run = async ({
4940
return log.warn('exec', 'Interactive mode disabled in CI environment')
5041
}
5142

52-
locationMsg = locationMsg || ` at location:\n${colorize.dim(runPath)}`
43+
locationMsg = locationMsg || ` at location:\n${flatOptions.chalk.dim(runPath)}`
5344

5445
output(`${
55-
colorize.reset('\nEntering npm script environment')
46+
flatOptions.chalk.reset('\nEntering npm script environment')
5647
}${
57-
colorize.reset(locationMsg)
48+
flatOptions.chalk.reset(locationMsg)
5849
}${
59-
colorize.bold('\nType \'exit\' or ^D when finished\n')
50+
flatOptions.chalk.bold('\nType \'exit\' or ^D when finished\n')
6051
}`)
6152
}
6253
}

workspaces/libnpmexec/package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
"template-oss-apply": "template-oss-apply --force"
4343
},
4444
"tap": {
45-
"color": true,
4645
"files": "test/*.js",
4746
"nyc-arg": [
4847
"--exclude",
@@ -54,6 +53,7 @@
5453
"@npmcli/mock-registry": "^1.0.0",
5554
"@npmcli/template-oss": "4.14.1",
5655
"bin-links": "^4.0.1",
56+
"chalk": "^5.2.0",
5757
"just-extend": "^6.2.0",
5858
"just-safe-set": "^4.2.1",
5959
"minify-registry-metadata": "^3.0.0",
@@ -62,7 +62,6 @@
6262
"dependencies": {
6363
"@npmcli/arborist": "^6.2.9",
6464
"@npmcli/run-script": "^6.0.0",
65-
"chalk": "^4.1.0",
6665
"ci-info": "^3.7.1",
6766
"npm-package-arg": "^10.1.0",
6867
"npmlog": "^7.0.1",
+34-31
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,30 @@
11
const t = require('tap')
22

3-
const baseOpts = {
4-
args: [],
5-
call: '',
6-
color: false,
7-
flatOptions: {},
8-
path: '',
9-
runPath: '',
10-
shell: process.platform === 'win32'
11-
? process.env.ComSpec || 'cmd'
12-
: process.env.SHELL || 'sh',
3+
const mockRunScript = async (t, mocks, { level = 0 } = {}) => {
4+
const runScript = t.mock('../lib/run-script.js', mocks)
5+
const { Chalk } = await import('chalk')
6+
return (opts) => runScript({
7+
args: [],
8+
call: '',
9+
path: '',
10+
runPath: '',
11+
shell: process.platform === 'win32'
12+
? process.env.ComSpec || 'cmd'
13+
: process.env.SHELL || 'sh',
14+
...opts,
15+
flatOptions: { chalk: new Chalk({ level }) },
16+
})
1317
}
1418

15-
t.test('disable, enable log progress', t => {
19+
t.test('disable, enable log progress', async t => {
1620
t.plan(3)
1721

1822
const path = t.testdir({
1923
'package.json': JSON.stringify({
2024
name: 'pkg',
2125
}),
2226
})
23-
const runScript = t.mock('../lib/run-script.js', {
27+
const runScript = await mockRunScript(t, {
2428
'ci-info': { isCI: false },
2529
'@npmcli/run-script': async () => {
2630
t.ok('should call run-script')
@@ -36,53 +40,53 @@ t.test('disable, enable log progress', t => {
3640
},
3741
})
3842

39-
runScript({
40-
...baseOpts,
41-
path,
42-
})
43+
await runScript({ path })
4344
})
4445

45-
t.test('no package.json', t => {
46+
t.test('no package.json', async t => {
4647
t.plan(1)
4748

48-
const runScript = t.mock('../lib/run-script.js', {
49+
const path = t.testdir({
50+
'package.json': JSON.stringify({
51+
name: 'pkg',
52+
}),
53+
})
54+
const runScript = await mockRunScript(t, {
4955
'ci-info': { isCI: false },
5056
'@npmcli/run-script': async () => {
5157
t.ok('should call run-script')
5258
},
5359
'../lib/no-tty.js': () => false,
5460
})
5561

56-
runScript(baseOpts)
62+
await runScript({ path })
5763
})
5864

5965
t.test('colorized interactive mode msg', async t => {
6066
t.plan(2)
6167

62-
const runScript = t.mock('../lib/run-script.js', {
68+
const runScript = await mockRunScript(t, {
6369
'ci-info': { isCI: false },
6470
'@npmcli/run-script': async () => {
6571
t.ok('should call run-script')
6672
},
6773
'../lib/no-tty.js': () => false,
68-
})
74+
}, { level: 3 })
6975

7076
const OUTPUT = []
7177
await runScript({
72-
...baseOpts,
7378
output: msg => {
7479
OUTPUT.push(msg)
7580
},
7681
runPath: '/foo/',
77-
flatOptions: { color: true },
7882
})
7983
t.matchSnapshot(OUTPUT.join('\n'), 'should print colorized output')
8084
})
8185

8286
t.test('no color interactive mode msg', async t => {
8387
t.plan(2)
8488

85-
const runScript = t.mock('../lib/run-script.js', {
89+
const runScript = await mockRunScript(t, {
8690
'ci-info': { isCI: false },
8791
'@npmcli/run-script': async () => {
8892
t.ok('should call run-script')
@@ -92,7 +96,6 @@ t.test('no color interactive mode msg', async t => {
9296

9397
const OUTPUT = []
9498
await runScript({
95-
...baseOpts,
9699
output: msg => {
97100
OUTPUT.push(msg)
98101
},
@@ -101,24 +104,24 @@ t.test('no color interactive mode msg', async t => {
101104
t.matchSnapshot(OUTPUT.join('\n'), 'should print non-colorized output')
102105
})
103106

104-
t.test('no tty', t => {
107+
t.test('no tty', async t => {
105108
t.plan(1)
106109

107-
const runScript = t.mock('../lib/run-script.js', {
110+
const runScript = await mockRunScript(t, {
108111
'ci-info': { isCI: false },
109112
'@npmcli/run-script': async () => {
110113
t.ok('should call run-script')
111114
},
112115
'../lib/no-tty.js': () => true,
113116
})
114117

115-
runScript(baseOpts)
118+
await runScript()
116119
})
117120

118-
t.test('ci env', t => {
121+
t.test('ci env', async t => {
119122
t.plan(2)
120123

121-
const runScript = t.mock('../lib/run-script.js', {
124+
const runScript = await mockRunScript(t, {
122125
'ci-info': { isCI: true },
123126
'@npmcli/run-script': async () => {
124127
throw new Error('should not call run-script')
@@ -136,5 +139,5 @@ t.test('ci env', t => {
136139
},
137140
})
138141

139-
runScript({ ...baseOpts })
142+
await runScript()
140143
})

0 commit comments

Comments
 (0)