Skip to content

Commit cce9809

Browse files
committed
fix: prevent Listr from hiding git add warning
1 parent c7d0592 commit cce9809

File tree

6 files changed

+101
-118
lines changed

6 files changed

+101
-118
lines changed

lib/generateTasks.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ const debug = require('debug')('lint-staged:gen-tasks')
1515
* @param {boolean} [options.gitDir] - Git root directory
1616
* @param {boolean} [options.files] - Staged filepaths
1717
* @param {boolean} [options.relative] - Whether filepaths to should be relative to gitDir
18-
* @returns {Promise}
1918
*/
2019
module.exports = function generateTasks({
2120
config,

lib/makeCmdTasks.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@ const debug = require('debug')('lint-staged:make-cmd-tasks')
1111
* @param {Array<string|Function>|string|Function} options.commands
1212
* @param {Array<string>} options.files
1313
* @param {string} options.gitDir
14-
* @param {Function} [options.logger]
1514
* @param {Boolean} shell
1615
*/
17-
module.exports = async function makeCmdTasks({ commands, files, gitDir, logger, shell }) {
16+
module.exports = function makeCmdTasks({ commands, files, gitDir, shell }) {
1817
debug('Creating listr tasks for commands %o', commands)
1918
const commandsArray = Array.isArray(commands) ? commands : [commands]
2019

@@ -42,8 +41,11 @@ module.exports = async function makeCmdTasks({ commands, files, gitDir, logger,
4241
title = mockCommands[i].replace(/\[file\].*\[file\]/, '[file]')
4342
}
4443

45-
const task = { title, task: resolveTaskFn({ command, files, gitDir, isFn, logger, shell }) }
46-
tasks.push(task)
44+
tasks.push({
45+
title,
46+
command,
47+
task: resolveTaskFn({ command, files, gitDir, isFn, shell })
48+
})
4749
})
4850

4951
return tasks

lib/resolveTaskFn.js

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -62,30 +62,14 @@ function makeErr(linter, result, context = {}) {
6262
* @param {String} options.gitDir - Current git repo path
6363
* @param {Boolean} options.isFn - Whether the linter task is a function
6464
* @param {Array<string>} options.files — Filepaths to run the linter task against
65-
* @param {Function} [options.logger]
6665
* @param {Boolean} [options.relative] — Whether the filepaths should be relative
6766
* @param {Boolean} [options.shell] — Whether to skip parsing linter task for better shell support
6867
* @returns {function(): Promise<Array<string>>}
6968
*/
70-
module.exports = function resolveTaskFn({
71-
command,
72-
files,
73-
gitDir,
74-
isFn,
75-
logger,
76-
relative,
77-
shell = false
78-
}) {
69+
module.exports = function resolveTaskFn({ command, files, gitDir, isFn, relative, shell = false }) {
7970
const cmd = isFn ? command : `${command} ${files.join(' ')}`
8071
debug('cmd:', cmd)
8172

82-
if (logger && cmd.includes('git add')) {
83-
logger.warn(`
84-
${symbols.warning} ${chalk.yellow(
85-
`Detected a task using \`git add\`. Lint-staged version 10 will automatically add any task modifications to the git index, and you should remove this command.`
86-
)}`)
87-
}
88-
8973
const execaOptions = { preferLocal: true, reject: false, shell }
9074
if (relative) {
9175
execaOptions.cwd = process.cwd()

lib/runAll.js

Lines changed: 64 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -68,75 +68,89 @@ module.exports = async function runAll(
6868
`)
6969
}
7070

71-
const tasks = generateTasks({ config, cwd, gitDir, files, relative }).map(task => ({
72-
title: `Running tasks for ${task.pattern}`,
73-
task: async () =>
74-
new Listr(
75-
await makeCmdTasks({
76-
commands: task.commands,
77-
files: task.fileList,
78-
gitDir,
79-
logger,
80-
shell
81-
}),
82-
{
71+
const tasks = generateTasks({ config, cwd, gitDir, files, relative })
72+
73+
// lint-staged 10 will automatically add modifications to index
74+
// Warn user when their command includes `git add`
75+
let hasDeprecatedGitAdd = false
76+
77+
const listrTasks = tasks.map(task => {
78+
const subTasks = makeCmdTasks({ commands: task.commands, files: task.fileList, gitDir, shell })
79+
80+
if (subTasks.some(subTask => subTask.command.includes('git add'))) {
81+
hasDeprecatedGitAdd = true
82+
}
83+
84+
return {
85+
title: `Running tasks for ${task.pattern}`,
86+
task: async () =>
87+
new Listr(subTasks, {
8388
// In sub-tasks we don't want to run concurrently
8489
// and we want to abort on errors
8590
dateFormat: false,
8691
concurrent: false,
8792
exitOnError: true
93+
}),
94+
skip: () => {
95+
if (task.fileList.length === 0) {
96+
return `No staged files match ${task.pattern}`
8897
}
89-
),
90-
skip: () => {
91-
if (task.fileList.length === 0) {
92-
return `No staged files match ${task.pattern}`
98+
return false
9399
}
94-
return false
95100
}
96-
}))
101+
})
97102

98-
const listrOptions = {
99-
dateFormat: false,
100-
renderer: (quiet && 'silent') || (debug && 'verbose') || 'update'
103+
if (hasDeprecatedGitAdd) {
104+
logger.warn(`${symbols.warning} ${chalk.yellow(
105+
`Detected a task using \`git add\`. Lint-staged version 10 will automatically add any task modifications to the git index, and you should remove this command.`
106+
)}
107+
`)
101108
}
102109

103-
// If all of the configured "linters" should be skipped
110+
// If all of the configured tasks should be skipped
104111
// avoid executing any lint-staged logic
105-
if (tasks.every(task => task.skip())) {
112+
if (listrTasks.every(task => task.skip())) {
106113
logger.log('No staged files match any of provided globs.')
107114
return 'No tasks to run.'
108115
}
109116

117+
const listrOptions = {
118+
dateFormat: false,
119+
renderer: (quiet && 'silent') || (debug && 'verbose') || 'update'
120+
}
121+
110122
const git = new GitWorkflow(gitDir)
111123

124+
const runner = new Listr(
125+
[
126+
{
127+
title: 'Preparing...',
128+
task: () => git.stashBackup()
129+
},
130+
{
131+
title: 'Running tasks...',
132+
task: () => new Listr(listrTasks, { ...listrOptions, concurrent: true, exitOnError: false })
133+
},
134+
{
135+
title: 'Applying modifications...',
136+
skip: ctx => ctx.hasErrors && 'Skipped because of errors from tasks',
137+
task: () => git.applyModifications()
138+
},
139+
{
140+
title: 'Reverting to original state...',
141+
enabled: ctx => ctx.hasErrors,
142+
task: () => git.restoreOriginalState()
143+
},
144+
{
145+
title: 'Cleaning up...',
146+
task: () => git.dropBackup()
147+
}
148+
],
149+
listrOptions
150+
)
151+
112152
try {
113-
await new Listr(
114-
[
115-
{
116-
title: 'Preparing...',
117-
task: () => git.stashBackup()
118-
},
119-
{
120-
title: 'Running tasks...',
121-
task: () => new Listr(tasks, { ...listrOptions, concurrent: true, exitOnError: false })
122-
},
123-
{
124-
title: 'Applying modifications...',
125-
skip: ctx => ctx.hasErrors && 'Skipped because of errors from tasks',
126-
task: () => git.applyModifications()
127-
},
128-
{
129-
title: 'Reverting to original state...',
130-
enabled: ctx => ctx.hasErrors,
131-
task: () => git.restoreOriginalState()
132-
},
133-
{
134-
title: 'Cleaning up...',
135-
task: () => git.dropBackup()
136-
}
137-
],
138-
listrOptions
139-
).run()
153+
await runner.run()
140154
} catch (error) {
141155
if (error.message.includes('Another git process seems to be running in this repository')) {
142156
logger.error(`

test/resolveTaskFn.spec.js

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import execa from 'execa'
2-
import makeConsoleMock from 'consolemock'
32

43
import resolveTaskFn from '../lib/resolveTaskFn'
54

@@ -201,19 +200,4 @@ describe('resolveTaskFn', () => {
201200
expect(context.hasErrors).toEqual(true)
202201
}
203202
})
204-
205-
it('should warn when tasks include git add', async () => {
206-
const logger = makeConsoleMock()
207-
await resolveTaskFn({
208-
...defaultOpts,
209-
command: 'git add',
210-
logger,
211-
relative: true
212-
})
213-
expect(logger.printHistory()).toMatchInlineSnapshot(`
214-
"
215-
WARN
216-
‼ Detected a task using \`git add\`. Lint-staged version 10 will automatically add any task modifications to the git index, and you should remove this command."
217-
`)
218-
})
219203
})

test/runAll.unmocked.spec.js

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -340,8 +340,8 @@ describe('runAll', () => {
340340
expect(error.message).toMatch('Another git process seems to be running in this repository')
341341
expect(console.printHistory()).toMatchInlineSnapshot(`
342342
"
343-
WARN
344-
‼ Detected a task using \`git add\`. Lint-staged version 10 will automatically add any task modifications to the git index, and you should remove this command.
343+
WARN ‼ Detected a task using \`git add\`. Lint-staged version 10 will automatically add any task modifications to the git index, and you should remove this command.
344+
345345
ERROR
346346
× lint-staged failed due to a git error.
347347
Any lost modifications can be restored from a git stash:
@@ -360,17 +360,17 @@ describe('runAll', () => {
360360
// But local modifications are gone
361361
expect(await execGit(['diff'])).not.toEqual(diff)
362362
expect(await execGit(['diff'])).toMatchInlineSnapshot(`
363-
"diff --git a/test.js b/test.js
364-
index f80f875..1c5643c 100644
365-
--- a/test.js
366-
+++ b/test.js
367-
@@ -1,3 +1,3 @@
368-
module.exports = {
369-
- 'foo': 'bar',
370-
-}
371-
+ foo: \\"bar\\"
372-
+};"
373-
`)
363+
"diff --git a/test.js b/test.js
364+
index f80f875..1c5643c 100644
365+
--- a/test.js
366+
+++ b/test.js
367+
@@ -1,3 +1,3 @@
368+
module.exports = {
369+
- 'foo': 'bar',
370+
-}
371+
+ foo: \\"bar\\"
372+
+};"
373+
`)
374374

375375
expect(await readFile('test.js')).not.toEqual(testJsFileUgly + appended)
376376
expect(await readFile('test.js')).toEqual(testJsFilePretty)
@@ -424,13 +424,13 @@ describe('runAll', () => {
424424
}
425425

426426
expect(await readFile('test.js')).toMatchInlineSnapshot(`
427-
"<<<<<<< HEAD
428-
module.exports = \\"foo\\";
429-
=======
430-
module.exports = \\"bar\\";
431-
>>>>>>> branch-b
432-
"
433-
`)
427+
"<<<<<<< HEAD
428+
module.exports = \\"foo\\";
429+
=======
430+
module.exports = \\"bar\\";
431+
>>>>>>> branch-b
432+
"
433+
`)
434434

435435
// Fix conflict and commit using lint-staged
436436
await writeFile('test.js', fileInBranchB)
@@ -444,12 +444,12 @@ describe('runAll', () => {
444444
// Nothing is wrong, so a new commit is created and file is pretty
445445
expect(await execGit(['rev-list', '--count', 'HEAD'])).toEqual('4')
446446
expect(await execGit(['log', '-1', '--pretty=%B'])).toMatchInlineSnapshot(`
447-
"Merge branch 'branch-b'
447+
"Merge branch 'branch-b'
448448
449-
# Conflicts:
450-
# test.js
451-
"
452-
`)
449+
# Conflicts:
450+
# test.js
451+
"
452+
`)
453453
expect(await readFile('test.js')).toEqual(fileInBranchBFixed)
454454
})
455455

@@ -490,13 +490,13 @@ describe('runAll', () => {
490490
expect(await execGit(['rev-list', '--count', 'HEAD'])).toEqual('1')
491491
expect(await execGit(['log', '-1', '--pretty=%B'])).toMatch('initial commit')
492492
expect(await readFile('README.md')).toMatchInlineSnapshot(`
493-
"# Test
493+
"# Test
494494
495-
## Amended
495+
## Amended
496496
497-
## Edited
498-
"
499-
`)
497+
## Edited
498+
"
499+
`)
500500
expect(await readFile('test-untracked.js')).toEqual(testJsFilePretty)
501501
const status = await execGit(['status'])
502502
expect(status).toMatch('modified: README.md')

0 commit comments

Comments
 (0)