Skip to content

Commit 09844ca

Browse files
committedJul 16, 2023
feat(dependencies): update listr2@6.6.0
1 parent 26dea68 commit 09844ca

8 files changed

+251
-410
lines changed
 

‎lib/getRenderer.js

+47-14
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,61 @@
1-
const getMainRendererOptions = ({ debug, quiet }, env) => {
2-
if (quiet) return { renderer: 'silent' }
1+
import { EOL } from 'node:os'
2+
import { Writable } from 'node:stream'
3+
4+
import { ListrLogger, ProcessOutput } from 'listr2'
5+
6+
const EOLRegex = new RegExp(EOL + '$')
7+
8+
const bindLogger = (consoleLogMethod) =>
9+
new Writable({
10+
write: function (chunk, encoding, next) {
11+
consoleLogMethod(chunk.toString().replace(EOLRegex, ''))
12+
next()
13+
},
14+
})
15+
16+
const getMainRendererOptions = ({ debug, quiet }, logger, env) => {
17+
if (quiet) {
18+
return {
19+
renderer: 'silent',
20+
}
21+
}
22+
23+
if (env.NODE_ENV === 'test') {
24+
return {
25+
renderer: 'test',
26+
rendererOptions: {
27+
logger: new ListrLogger({
28+
processOutput: new ProcessOutput(bindLogger(logger.log), bindLogger(logger.error)),
29+
}),
30+
},
31+
}
32+
}
33+
334
// Better support for dumb terminals: https://en.wikipedia.org/wiki/Computer_terminal#Dumb_terminals
4-
const isDumbTerminal = env.TERM === 'dumb'
5-
if (debug || isDumbTerminal || env.NODE_ENV === 'test') return { renderer: 'verbose' }
6-
return { renderer: 'update', rendererOptions: { dateFormat: false } }
7-
}
35+
if (debug || env.TERM === 'dumb') {
36+
return {
37+
renderer: 'verbose',
38+
}
39+
}
840

9-
const getFallbackRenderer = ({ renderer }, { FORCE_COLOR }) => {
10-
if (renderer === 'silent') {
11-
return 'silent'
41+
return {
42+
renderer: 'update',
1243
}
44+
}
1345

14-
// If colors are being forced, then also force non-fallback rendering
15-
if (Number(FORCE_COLOR) > 0) {
46+
const getFallbackRenderer = ({ renderer }, { FORCE_COLOR }) => {
47+
if (renderer === 'silent' || renderer === 'test' || Number(FORCE_COLOR) > 0) {
1648
return renderer
1749
}
1850

1951
return 'verbose'
2052
}
2153

22-
export const getRenderer = (options, env = process.env) => {
23-
const mainRendererOptions = getMainRendererOptions(options, env)
54+
export const getRenderer = (options, logger, env = process.env) => {
55+
const mainRendererOptions = getMainRendererOptions(options, logger, env)
56+
2457
return {
2558
...mainRendererOptions,
26-
nonTTYRenderer: getFallbackRenderer(mainRendererOptions, env),
59+
fallbackRenderer: getFallbackRenderer(mainRendererOptions, env),
2760
}
2861
}

‎lib/runAll.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ export const runAll = async (
151151
ctx,
152152
exitOnError: false,
153153
registerSignalListeners: false,
154-
...getRenderer({ debug, quiet }),
154+
...getRenderer({ debug, quiet }, logger),
155155
}
156156

157157
const listrTasks = []

‎package-lock.json

+153-296
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
"debug": "4.3.4",
4040
"execa": "7.1.1",
4141
"lilconfig": "2.1.0",
42-
"listr2": "5.0.8",
42+
"listr2": "6.6.0",
4343
"micromatch": "4.0.5",
4444
"normalize-path": "3.0.0",
4545
"object-inspect": "1.12.3",

‎test/integration/partially-staged-changes.test.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ describe('lint-staged', () => {
2929

3030
const output = await gitCommit()
3131

32-
expect(output).toMatch('[SUCCESS] Hiding unstaged changes to partially staged files...')
33-
expect(output).toMatch('[SUCCESS] Applying modifications from tasks...')
34-
expect(output).toMatch('[SUCCESS] Restoring unstaged changes to partially staged files...')
32+
expect(output).toMatch('Hiding unstaged changes to partially staged files...')
33+
expect(output).toMatch('Applying modifications from tasks...')
34+
expect(output).toMatch('Restoring unstaged changes to partially staged files...')
3535

3636
// Nothing is wrong, so a new commit is created and file is pretty
3737
expect(await execGit(['rev-list', '--count', 'HEAD'])).toEqual('2')

‎test/unit/getRenderer.spec.js

+19-25
Original file line numberDiff line numberDiff line change
@@ -2,60 +2,54 @@ import { getRenderer } from '../../lib/getRenderer.js'
22

33
describe('getRenderer', () => {
44
it('should return silent renderers when quiet', () => {
5-
expect(getRenderer({ quiet: true }, {})).toEqual({
5+
expect(getRenderer({ quiet: true }, console, {})).toEqual({
66
renderer: 'silent',
7-
nonTTYRenderer: 'silent',
7+
fallbackRenderer: 'silent',
88
})
99
})
1010

11-
it('should return verbose renderers when NODE_ENV=test', () => {
12-
expect(getRenderer({}, { NODE_ENV: 'test' })).toEqual({
13-
renderer: 'verbose',
14-
nonTTYRenderer: 'verbose',
11+
it('should return test renderers when NODE_ENV=test', () => {
12+
expect(getRenderer({}, console, { NODE_ENV: 'test' })).toEqual({
13+
renderer: 'test',
14+
fallbackRenderer: 'test',
15+
rendererOptions: {
16+
logger: expect.any(Object),
17+
},
1518
})
1619
})
1720

1821
it('should return test renderers when TERM=dumb', () => {
19-
expect(getRenderer({}, { TERM: 'dumb' })).toEqual({
22+
expect(getRenderer({}, console, { TERM: 'dumb' })).toEqual({
2023
renderer: 'verbose',
21-
nonTTYRenderer: 'verbose',
24+
fallbackRenderer: 'verbose',
2225
})
2326
})
2427

2528
it('should return verbose renderers when debug', () => {
26-
expect(getRenderer({ debug: true }, {})).toEqual({
29+
expect(getRenderer({ debug: true }, console, {})).toEqual({
2730
renderer: 'verbose',
28-
nonTTYRenderer: 'verbose',
31+
fallbackRenderer: 'verbose',
2932
})
3033
})
3134

3235
it('should return update main renderer and verbose fallback renderer by default', () => {
33-
expect(getRenderer({}, {})).toEqual({
36+
expect(getRenderer({}, console, {})).toEqual({
3437
renderer: 'update',
35-
rendererOptions: {
36-
dateFormat: false,
37-
},
38-
nonTTYRenderer: 'verbose',
38+
fallbackRenderer: 'verbose',
3939
})
4040
})
4141

4242
it('should return update main renderer and verbose fallback renderer when colors are not forced', () => {
43-
expect(getRenderer({}, { FORCE_COLOR: '0' })).toEqual({
43+
expect(getRenderer({}, console, { FORCE_COLOR: '0' })).toEqual({
4444
renderer: 'update',
45-
rendererOptions: {
46-
dateFormat: false,
47-
},
48-
nonTTYRenderer: 'verbose',
45+
fallbackRenderer: 'verbose',
4946
})
5047
})
5148

5249
it('should return update renderers when colors are forced', () => {
53-
expect(getRenderer({}, { FORCE_COLOR: '1' })).toEqual({
50+
expect(getRenderer({}, console, { FORCE_COLOR: '1' })).toEqual({
5451
renderer: 'update',
55-
rendererOptions: {
56-
dateFormat: false,
57-
},
58-
nonTTYRenderer: 'update',
52+
fallbackRenderer: 'update',
5953
})
6054
})
6155
})

‎test/unit/index2.spec.js

+20-3
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ describe('lintStaged', () => {
6161
"shouldBackup": true,
6262
},
6363
"exitOnError": false,
64-
"nonTTYRenderer": "silent",
64+
"fallbackRenderer": "silent",
6565
"registerSignalListeners": false,
6666
"renderer": "silent",
6767
}
@@ -94,9 +94,26 @@ describe('lintStaged', () => {
9494
"shouldBackup": true,
9595
},
9696
"exitOnError": false,
97-
"nonTTYRenderer": "verbose",
97+
"fallbackRenderer": "test",
9898
"registerSignalListeners": false,
99-
"renderer": "verbose",
99+
"renderer": "test",
100+
"rendererOptions": {
101+
"logger": ListrLogger {
102+
"applyFormat": [MockFunction],
103+
"fields": [MockFunction],
104+
"format": [MockFunction],
105+
"icon": [MockFunction],
106+
"log": [MockFunction],
107+
"prefix": [MockFunction],
108+
"spacing": [MockFunction],
109+
"splat": [MockFunction],
110+
"style": [MockFunction],
111+
"suffix": [MockFunction],
112+
"toStderr": [MockFunction],
113+
"toStdout": [MockFunction],
114+
"wrap": [MockFunction],
115+
},
116+
},
100117
}
101118
`)
102119
})

‎test/unit/runAll.spec.js

+7-67
Original file line numberDiff line numberDiff line change
@@ -162,23 +162,7 @@ describe('runAll', () => {
162162
expect.assertions(1)
163163
getStagedFiles.mockImplementationOnce(async () => ['sample.js'])
164164
await runAll({ configObject: { '*.js': ['echo "sample"'] }, configPath })
165-
expect(console.printHistory()).toMatchInlineSnapshot(`
166-
"
167-
LOG [STARTED] Preparing lint-staged...
168-
LOG [SUCCESS] Preparing lint-staged...
169-
LOG [STARTED] Running tasks for staged files...
170-
LOG [STARTED] Config object — 1 file
171-
LOG [STARTED] *.js — 1 file
172-
LOG [STARTED] echo "sample"
173-
LOG [SUCCESS] echo "sample"
174-
LOG [SUCCESS] *.js — 1 file
175-
LOG [SUCCESS] Config object — 1 file
176-
LOG [SUCCESS] Running tasks for staged files...
177-
LOG [STARTED] Applying modifications from tasks...
178-
LOG [SUCCESS] Applying modifications from tasks...
179-
LOG [STARTED] Cleaning up temporary files...
180-
LOG [SUCCESS] Cleaning up temporary files..."
181-
`)
165+
expect(console.printHistory()).toMatch(/"data":"COMPLETED".*Running tasks for staged files/)
182166
})
183167

184168
it('should skip tasks if previous git error', async () => {
@@ -188,27 +172,15 @@ describe('runAll', () => {
188172
...jest.requireActual('../../lib/gitWorkflow.js'),
189173
prepare: (ctx) => {
190174
ctx.errors.add(GitError)
191-
throw new Error('test')
175+
throw new Error('test error')
192176
},
193177
}))
194178

195179
await expect(
196180
runAll({ configObject: { '*.js': ['echo "sample"'] }, configPath })
197181
).rejects.toThrowErrorMatchingInlineSnapshot(`"lint-staged failed"`)
198182

199-
expect(console.printHistory()).toMatchInlineSnapshot(`
200-
"
201-
LOG [STARTED] Preparing lint-staged...
202-
ERROR [FAILED] test
203-
LOG [STARTED] Running tasks for staged files...
204-
INFO [SKIPPED] Running tasks for staged files...
205-
LOG [STARTED] Applying modifications from tasks...
206-
INFO [SKIPPED]
207-
[SKIPPED] ✖ lint-staged failed due to a git error.
208-
LOG [STARTED] Cleaning up temporary files...
209-
INFO [SKIPPED]
210-
[SKIPPED] ✖ lint-staged failed due to a git error."
211-
`)
183+
expect(console.printHistory()).toMatch(/"data":"SKIPPED".*Running tasks for staged files/)
212184
})
213185

214186
it('should skip applying unstaged modifications if there are errors during linting', async () => {
@@ -228,24 +200,7 @@ describe('runAll', () => {
228200
runAll({ configObject: { '*.js': ['echo "sample"'] }, configPath })
229201
).rejects.toThrowErrorMatchingInlineSnapshot(`"lint-staged failed"`)
230202

231-
expect(console.printHistory()).toMatchInlineSnapshot(`
232-
"
233-
LOG [STARTED] Preparing lint-staged...
234-
LOG [SUCCESS] Preparing lint-staged...
235-
LOG [STARTED] Running tasks for staged files...
236-
LOG [STARTED] Config object — 1 file
237-
LOG [STARTED] *.js — 1 file
238-
LOG [STARTED] echo "sample"
239-
ERROR [FAILED] echo "sample" [1]
240-
ERROR [FAILED] echo "sample" [1]
241-
ERROR [FAILED] echo "sample" [1]
242-
LOG [STARTED] Applying modifications from tasks...
243-
INFO [SKIPPED] Skipped because of errors from tasks.
244-
LOG [STARTED] Reverting to original state because of errors...
245-
LOG [SUCCESS] Reverting to original state because of errors...
246-
LOG [STARTED] Cleaning up temporary files...
247-
LOG [SUCCESS] Cleaning up temporary files..."
248-
`)
203+
expect(console.printHistory()).toMatch(/"data":"SKIPPED".*Applying modifications from tasks/)
249204
})
250205

251206
it('should skip tasks and restore state if terminated', async () => {
@@ -267,24 +222,9 @@ describe('runAll', () => {
267222
runAll({ configObject: { '*.js': ['echo "sample"'] }, configPath })
268223
).rejects.toThrowErrorMatchingInlineSnapshot(`"lint-staged failed"`)
269224

270-
expect(console.printHistory()).toMatchInlineSnapshot(`
271-
"
272-
LOG [STARTED] Preparing lint-staged...
273-
LOG [SUCCESS] Preparing lint-staged...
274-
LOG [STARTED] Running tasks for staged files...
275-
LOG [STARTED] Config object — 1 file
276-
LOG [STARTED] *.js — 1 file
277-
LOG [STARTED] echo "sample"
278-
ERROR [FAILED] echo "sample" [KILLED]
279-
ERROR [FAILED] echo "sample" [KILLED]
280-
ERROR [FAILED] echo "sample" [KILLED]
281-
LOG [STARTED] Applying modifications from tasks...
282-
INFO [SKIPPED] Skipped because of errors from tasks.
283-
LOG [STARTED] Reverting to original state because of errors...
284-
LOG [SUCCESS] Reverting to original state because of errors...
285-
LOG [STARTED] Cleaning up temporary files...
286-
LOG [SUCCESS] Cleaning up temporary files..."
287-
`)
225+
expect(console.printHistory()).toMatch(
226+
/"data":"COMPLETED".*Reverting to original state because of errors/
227+
)
288228
})
289229

290230
it('should resolve matched files to cwd when using relative option', async () => {

0 commit comments

Comments
 (0)
Please sign in to comment.