/
runAll.js
221 lines (194 loc) · 7.28 KB
/
runAll.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
'use strict'
/** @typedef {import('./index').Logger} Logger */
const chalk = require('chalk')
const Listr = require('listr')
const symbols = require('log-symbols')
const chunkFiles = require('./chunkFiles')
const generateTasks = require('./generateTasks')
const getStagedFiles = require('./getStagedFiles')
const GitWorkflow = require('./gitWorkflow')
const makeCmdTasks = require('./makeCmdTasks')
const resolveGitRepo = require('./resolveGitRepo')
const debugLog = require('debug')('lint-staged:run')
const getRenderer = ({ debug, quiet }) => {
if (quiet) return 'silent'
// Better support for dumb terminals: https://en.wikipedia.org/wiki/Computer_terminal#Dumb_terminals
const isDumbTerminal = process.env.TERM === 'dumb'
if (debug || isDumbTerminal) return 'verbose'
return 'update'
}
/**
* Executes all tasks and either resolves or rejects the promise
*
* @param {object} options
* @param {Object} [options.allowEmpty] - Allow empty commits when tasks revert all staged changes
* @param {Object} [options.config] - Task configuration
* @param {Object} [options.cwd] - Current working directory
* @param {number} [options.maxArgLength] - Maximum argument string length
* @param {boolean} [options.relative] - Pass relative filepaths to tasks
* @param {boolean} [options.shell] - Skip parsing of tasks for better shell support
* @param {boolean} [options.quiet] - Disable lint-staged’s own console output
* @param {boolean} [options.debug] - Enable debug mode
* @param {boolean | number} [options.concurrent] - The number of tasks to run concurrently, or false to run tasks serially
* @param {Logger} logger
* @returns {Promise}
*/
module.exports = async function runAll(
{
allowEmpty = false,
config,
cwd = process.cwd(),
debug = false,
maxArgLength,
quiet = false,
relative = false,
shell = false,
concurrent = true
},
logger = console
) {
debugLog('Running all linter scripts')
const { gitDir, gitConfigDir } = await resolveGitRepo(cwd)
if (!gitDir) throw new Error('Current directory is not a git directory!')
const files = await getStagedFiles({ cwd: gitDir })
if (!files) throw new Error('Unable to get staged files!')
debugLog('Loaded list of staged files in git:\n%O', files)
// If there are no files avoid executing any lint-staged logic
if (files.length === 0) {
logger.log('No staged files found.')
return 'No tasks to run.'
}
const stagedFileChunks = chunkFiles({ files, gitDir, maxArgLength, relative })
const chunkCount = stagedFileChunks.length
if (chunkCount > 1) debugLog(`Chunked staged files into ${chunkCount} part`, chunkCount)
// lint-staged 10 will automatically add modifications to index
// Warn user when their command includes `git add`
let hasDeprecatedGitAdd = false
const listrOptions = {
dateFormat: false,
exitOnError: false,
renderer: getRenderer({ debug, quiet })
}
const listrTasks = []
for (const [index, files] of stagedFileChunks.entries()) {
const chunkTasks = generateTasks({ config, cwd, gitDir, files, relative })
const chunkListrTasks = []
for (const task of chunkTasks) {
const subTasks = await makeCmdTasks({
commands: task.commands,
files: task.fileList,
gitDir,
shell
})
if (subTasks.some(subTask => subTask.command === 'git add')) {
hasDeprecatedGitAdd = true
}
chunkListrTasks.push({
title: `Running tasks for ${task.pattern}`,
task: async () =>
new Listr(subTasks, {
// In sub-tasks we don't want to run concurrently
// and we want to abort on errors
dateFormat: false,
concurrent: false,
exitOnError: true
}),
skip: () => {
// Skip task when no files matched
if (task.fileList.length === 0) {
return `No staged files match ${task.pattern}`
}
return false
}
})
}
listrTasks.push({
// No need to show number of task chunks when there's only one
title:
chunkCount > 1 ? `Running tasks (chunk ${index + 1}/${chunkCount})...` : 'Running tasks...',
task: () => new Listr(chunkListrTasks, { ...listrOptions, concurrent }),
skip: (ctx = {}) => {
// Skip if the first step (backup) failed
if (ctx.gitError) return 'Skipped because of previous git error.'
// Skip chunk when no every task is skipped (due to no matches)
if (chunkListrTasks.every(task => task.skip())) return 'No tasks to run.'
return false
}
})
}
if (hasDeprecatedGitAdd) {
logger.warn(`${symbols.warning} ${chalk.yellow(
`Some of your tasks use \`git add\` command. Please remove it from the config since all modifications made by tasks will be automatically added to the git commit index.`
)}
`)
}
// If all of the configured tasks should be skipped
// avoid executing any lint-staged logic
if (listrTasks.every(task => task.skip())) {
logger.log('No staged files match any of provided globs.')
return 'No tasks to run.'
}
const git = new GitWorkflow({ allowEmpty, gitConfigDir, gitDir, stagedFileChunks })
// Running git reset or dropping the backup stash should be skipped
// when there are git errors NOT related to applying unstaged modifications.
// In the latter case, the original state is restored.
const cleanupNotSafe = ctx =>
ctx.gitError &&
!ctx.gitApplyEmptyCommit &&
!ctx.gitApplyModificationsError &&
'Skipped because of previous git error.'
const runner = new Listr(
[
{
title: 'Preparing...',
task: ctx => git.stashBackup(ctx)
},
...listrTasks,
{
title: 'Applying modifications...',
skip: ctx => {
if (ctx.gitError) return 'Skipped because of previous git error.'
if (ctx.taskError) return 'Skipped because of errors from tasks.'
},
task: ctx => git.applyModifications(ctx)
},
{
title: 'Reverting to original state...',
enabled: ctx => ctx.taskError || ctx.gitApplyEmptyCommit || ctx.gitApplyModificationsError,
skip: cleanupNotSafe,
task: ctx => git.restoreOriginalState(ctx)
},
{
title: 'Cleaning up...',
skip: cleanupNotSafe,
task: ctx => git.dropBackup(ctx)
}
],
listrOptions
)
try {
await runner.run({})
} catch (error) {
if (error.context.gitApplyEmptyCommit) {
logger.warn(`
${symbols.warning} ${chalk.yellow(`lint-staged prevented an empty git commit.
Use the --allow-empty option to continue, or check your task configuration`)}
`)
} else if (error.context.gitError && !error.context.gitGetBackupStashError) {
logger.error(`\n ${symbols.error} ${chalk.red(`lint-staged failed due to a git error.`)}`)
if (error.context.emptyGitRepo) {
logger.error(
`\n The initial commit is needed for lint-staged to work.
Please use the --no-verify flag to skip running lint-staged.`
)
} else {
// No sense to show this if the backup stash itself is missing.
logger.error(` Any lost modifications can be restored from a git stash:
> git stash list
stash@{0}: On master: automatic lint-staged backup
> git stash pop stash@{0}\n`)
}
}
throw error
}
}