/
runAll.js
184 lines (158 loc) · 5.21 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
'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 resolveGitDir = require('./resolveGitDir')
const debugLog = require('debug')('lint-staged:run')
/**
* Executes all tasks and either resolves or rejects the promise
*
* @param {object} options
* @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 {Logger} logger
* @returns {Promise}
*/
module.exports = async function runAll(
{
config,
cwd = process.cwd(),
debug = false,
maxArgLength,
quiet = false,
relative = false,
shell = false
},
logger = console
) {
debugLog('Running all linter scripts')
const gitDir = await resolveGitDir({ cwd })
if (!gitDir) {
throw new Error('Current directory is not a git directory!')
}
debugLog('Resolved git directory to be `%s`', gitDir)
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)
const chunkedFiles = chunkFiles({ files, gitDir, maxArgLength, relative })
const chunkCount = chunkedFiles.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,
renderer: (quiet && 'silent') || (debug && 'verbose') || 'update'
}
const listrTasks = []
for (const [index, files] of chunkedFiles.entries()) {
const chunkTasks = generateTasks({ config, cwd, gitDir, files, relative })
const chunkListrTasks = chunkTasks.map(task => {
const subTasks = makeCmdTasks({
commands: task.commands,
files: task.fileList,
gitDir,
shell
})
if (subTasks.some(subTask => subTask.command.includes('git add'))) {
hasDeprecatedGitAdd = true
}
return {
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: () => {
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}/${chunkCount})...` : 'Running tasks...',
task: () =>
new Listr(chunkListrTasks, { ...listrOptions, concurrent: false, exitOnError: false }),
skip: () => {
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(gitDir)
const runner = new Listr(
[
{
title: 'Preparing...',
task: () => git.stashBackup()
},
...listrTasks,
{
title: 'Applying modifications...',
skip: ctx => ctx.hasErrors && 'Skipped because of errors from tasks',
task: () => git.applyModifications()
},
{
title: 'Reverting to original state...',
enabled: ctx => ctx.hasErrors,
task: () => git.restoreOriginalState()
},
{
title: 'Cleaning up...',
task: () => git.dropBackup()
}
],
listrOptions
)
try {
await runner.run()
} catch (error) {
if (error.message.includes('Another git process seems to be running in this repository')) {
logger.error(`
${symbols.error} ${chalk.red(`lint-staged failed due to a git 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}`)}
`)
}
throw error
}
}