-
-
Notifications
You must be signed in to change notification settings - Fork 412
/
runAll.js
271 lines (238 loc) · 8.63 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
'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'
}
const MESSAGES = {
TASK_ERROR: 'Skipped because of errors from tasks.',
GIT_ERROR: 'Skipped because of previous git error.'
}
const shouldSkipApplyModifications = ctx => {
// Should be skipped in case of git errors
if (ctx.gitError) {
return MESSAGES.GIT_ERROR
}
// Should be skipped when tasks fail
if (ctx.taskError) {
return MESSAGES.TASK_ERROR
}
}
const shouldSkipRevert = ctx => {
// Should be skipped in case of unknown git errors
if (ctx.gitError && !ctx.gitApplyEmptyCommitError && !ctx.gitRestoreUnstagedChangesError) {
return MESSAGES.GIT_ERROR
}
}
const shouldSkipCleanup = ctx => {
// Should be skipped in case of unknown git errors
if (ctx.gitError && !ctx.gitApplyEmptyCommitError && !ctx.gitRestoreUnstagedChangesError) {
return MESSAGES.GIT_ERROR
}
// Should be skipped when reverting to original state fails
if (ctx.gitRestoreOriginalStateError) {
return MESSAGES.GIT_ERROR
}
}
/**
* 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 {boolean | number} [options.concurrent] - The number of tasks to run concurrently, or false to run tasks serially
* @param {Object} [options.config] - Task configuration
* @param {Object} [options.cwd] - Current working directory
* @param {boolean} [options.debug] - Enable debug mode
* @param {number} [options.maxArgLength] - Maximum argument string length
* @param {boolean} [options.quiet] - Disable lint-staged’s own console output
* @param {boolean} [options.relative] - Pass relative filepaths to tasks
* @param {boolean} [options.shell] - Skip parsing of tasks for better shell support
* @param {boolean} [options.stash] - Enable the backup stash, and revert in case of errors
* @param {Logger} logger
* @returns {Promise}
*/
const runAll = async (
{
allowEmpty = false,
concurrent = true,
config,
cwd = process.cwd(),
debug = false,
maxArgLength,
quiet = false,
relative = false,
shell = false,
stash = true
},
logger = console
) => {
debugLog('Running all linter scripts')
if (!stash) {
logger.warn(
`${symbols.warning} ${chalk.yellow('Skipping backup because `--no-stash` was used.')}`
)
}
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) {
return logger.log(`${symbols.info} No staged files found.`)
}
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
})
hasDeprecatedGitAdd = subTasks.some(subTask => subTask.command === 'git add')
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 MESSAGES.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 })
const runner = new Listr(
[
{
title: 'Preparing...',
task: ctx => git.prepare(ctx, stash)
},
{
title: 'Hiding unstaged changes to partially staged files...',
task: ctx => git.hideUnstagedChanges(ctx),
enabled: ctx => ctx.hasPartiallyStagedFiles
},
...listrTasks,
{
title: 'Applying modifications...',
task: ctx => git.applyModifications(ctx),
// Always apply back unstaged modifications when skipping backup
skip: ctx => stash && shouldSkipApplyModifications(ctx)
},
{
title: 'Restoring unstaged changes to partially staged files...',
task: ctx => git.restoreUnstagedChanges(ctx),
enabled: ctx => ctx.hasPartiallyStagedFiles,
skip: shouldSkipApplyModifications
},
{
title: 'Reverting to original state because of errors...',
task: ctx => git.restoreOriginalState(ctx),
enabled: ctx =>
stash &&
(ctx.taskError || ctx.gitApplyEmptyCommitError || ctx.gitRestoreUnstagedChangesError),
skip: shouldSkipRevert
},
{
title: 'Cleaning up...',
task: ctx => git.cleanup(ctx),
enabled: () => stash,
skip: shouldSkipCleanup
}
],
listrOptions
)
try {
await runner.run({})
} catch (error) {
if (error.context.gitApplyEmptyCommitError) {
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 if (stash) {
// 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 apply --index stash@{0}\n`)
}
}
throw error
}
}
module.exports = runAll
module.exports.shouldSkip = {
shouldSkipApplyModifications,
shouldSkipRevert,
shouldSkipCleanup
}