1 |
|
2 |
|
3 | import path from 'path'
|
4 |
|
5 | import { dim } from 'colorette'
|
6 | import debug from 'debug'
|
7 | import { Listr } from 'listr2'
|
8 | import normalize from 'normalize-path'
|
9 |
|
10 | import { chunkFiles } from './chunkFiles.js'
|
11 | import { execGit } from './execGit.js'
|
12 | import { generateTasks } from './generateTasks.js'
|
13 | import { getRenderer } from './getRenderer.js'
|
14 | import { getStagedFiles } from './getStagedFiles.js'
|
15 | import { GitWorkflow } from './gitWorkflow.js'
|
16 | import { groupFilesByConfig } from './groupFilesByConfig.js'
|
17 | import { makeCmdTasks } from './makeCmdTasks.js'
|
18 | import {
|
19 | DEPRECATED_GIT_ADD,
|
20 | FAILED_GET_STAGED_FILES,
|
21 | NOT_GIT_REPO,
|
22 | NO_STAGED_FILES,
|
23 | NO_TASKS,
|
24 | SKIPPED_GIT_ERROR,
|
25 | skippingBackup,
|
26 | } from './messages.js'
|
27 | import { resolveGitRepo } from './resolveGitRepo.js'
|
28 | import {
|
29 | applyModificationsSkipped,
|
30 | cleanupEnabled,
|
31 | cleanupSkipped,
|
32 | getInitialState,
|
33 | hasPartiallyStagedFiles,
|
34 | restoreOriginalStateEnabled,
|
35 | restoreOriginalStateSkipped,
|
36 | restoreUnstagedChangesSkipped,
|
37 | } from './state.js'
|
38 | import { GitRepoError, GetStagedFilesError, GitError, ConfigNotFoundError } from './symbols.js'
|
39 | import { ConfigObjectSymbol, searchConfigs } from './searchConfigs.js'
|
40 |
|
41 | const debugLog = debug('lint-staged:runAll')
|
42 |
|
43 | const createError = (ctx) => Object.assign(new Error('lint-staged failed'), { ctx })
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 | export const runAll = async (
|
65 | {
|
66 | allowEmpty = false,
|
67 | concurrent = true,
|
68 | configObject,
|
69 | configPath,
|
70 | cwd,
|
71 | debug = false,
|
72 | maxArgLength,
|
73 | quiet = false,
|
74 | relative = false,
|
75 | shell = false,
|
76 | stash = true,
|
77 | verbose = false,
|
78 | },
|
79 | logger = console
|
80 | ) => {
|
81 | debugLog('Running all linter scripts...')
|
82 |
|
83 |
|
84 | const hasExplicitCwd = !!cwd
|
85 | cwd = hasExplicitCwd ? path.resolve(cwd) : process.cwd()
|
86 | debugLog('Using working directory `%s`', cwd)
|
87 |
|
88 | const ctx = getInitialState({ quiet })
|
89 |
|
90 | const { gitDir, gitConfigDir } = await resolveGitRepo(cwd)
|
91 | if (!gitDir) {
|
92 | if (!quiet) ctx.output.push(NOT_GIT_REPO)
|
93 | ctx.errors.add(GitRepoError)
|
94 | throw createError(ctx)
|
95 | }
|
96 |
|
97 |
|
98 |
|
99 | const hasInitialCommit = await execGit(['log', '-1'], { cwd: gitDir })
|
100 | .then(() => true)
|
101 | .catch(() => false)
|
102 |
|
103 |
|
104 | ctx.shouldBackup = hasInitialCommit && stash
|
105 | if (!ctx.shouldBackup) {
|
106 | logger.warn(skippingBackup(hasInitialCommit))
|
107 | }
|
108 |
|
109 | const files = await getStagedFiles({ cwd: gitDir })
|
110 | if (!files) {
|
111 | if (!quiet) ctx.output.push(FAILED_GET_STAGED_FILES)
|
112 | ctx.errors.add(GetStagedFilesError)
|
113 | throw createError(ctx, GetStagedFilesError)
|
114 | }
|
115 | debugLog('Loaded list of staged files in git:\n%O', files)
|
116 |
|
117 |
|
118 | if (files.length === 0) {
|
119 | if (!quiet) ctx.output.push(NO_STAGED_FILES)
|
120 | return ctx
|
121 | }
|
122 |
|
123 | const foundConfigs = await searchConfigs({ configObject, configPath, cwd, gitDir }, logger)
|
124 | const numberOfConfigs = Reflect.ownKeys(foundConfigs).length
|
125 |
|
126 |
|
127 | if (numberOfConfigs === 0) {
|
128 | ctx.errors.add(ConfigNotFoundError)
|
129 | throw createError(ctx, ConfigNotFoundError)
|
130 | }
|
131 |
|
132 | const filesByConfig = await groupFilesByConfig({ configs: foundConfigs, files })
|
133 |
|
134 | const hasMultipleConfigs = numberOfConfigs > 1
|
135 |
|
136 |
|
137 |
|
138 | let hasDeprecatedGitAdd = false
|
139 |
|
140 | const listrOptions = {
|
141 | ctx,
|
142 | exitOnError: false,
|
143 | registerSignalListeners: false,
|
144 | ...getRenderer({ debug, quiet }),
|
145 | }
|
146 |
|
147 | const listrTasks = []
|
148 |
|
149 |
|
150 | const matchedFiles = new Set()
|
151 |
|
152 | for (const configPath of Reflect.ownKeys(filesByConfig)) {
|
153 | const { config, files } = filesByConfig[configPath]
|
154 |
|
155 | const configName =
|
156 | configPath === ConfigObjectSymbol
|
157 | ? 'Config object'
|
158 | : normalize(path.relative(cwd, configPath))
|
159 |
|
160 | const stagedFileChunks = chunkFiles({ baseDir: gitDir, files, maxArgLength, relative })
|
161 |
|
162 |
|
163 |
|
164 |
|
165 | const groupCwd = hasMultipleConfigs && !hasExplicitCwd ? path.dirname(configPath) : cwd
|
166 |
|
167 | const chunkCount = stagedFileChunks.length
|
168 | if (chunkCount > 1) {
|
169 | debugLog('Chunked staged files from `%s` into %d part', configPath, chunkCount)
|
170 | }
|
171 |
|
172 | for (const [index, files] of stagedFileChunks.entries()) {
|
173 | const chunkListrTasks = await Promise.all(
|
174 | generateTasks({ config, cwd: groupCwd, files, relative }).map((task) =>
|
175 | makeCmdTasks({
|
176 | commands: task.commands,
|
177 | cwd: groupCwd,
|
178 | files: task.fileList,
|
179 | gitDir,
|
180 | renderer: listrOptions.renderer,
|
181 | shell,
|
182 | verbose,
|
183 | }).then((subTasks) => {
|
184 |
|
185 | task.fileList.forEach((file) => {
|
186 |
|
187 |
|
188 |
|
189 | const normalizedFile = path.isAbsolute(file)
|
190 | ? file
|
191 | : normalize(path.join(groupCwd, file))
|
192 |
|
193 | matchedFiles.add(normalizedFile)
|
194 | })
|
195 |
|
196 | hasDeprecatedGitAdd =
|
197 | hasDeprecatedGitAdd || subTasks.some((subTask) => subTask.command === 'git add')
|
198 |
|
199 | const fileCount = task.fileList.length
|
200 |
|
201 | return {
|
202 | title: `${task.pattern}${dim(` — ${fileCount} ${fileCount > 1 ? 'files' : 'file'}`)}`,
|
203 | task: async () =>
|
204 | new Listr(subTasks, {
|
205 |
|
206 |
|
207 | ...listrOptions,
|
208 | concurrent: false,
|
209 | exitOnError: true,
|
210 | }),
|
211 | skip: () => {
|
212 |
|
213 | if (fileCount === 0) {
|
214 | return `${task.pattern}${dim(' — no files')}`
|
215 | }
|
216 | return false
|
217 | },
|
218 | }
|
219 | })
|
220 | )
|
221 | )
|
222 |
|
223 | listrTasks.push({
|
224 | title:
|
225 | `${configName}${dim(` — ${files.length} ${files.length > 1 ? 'files' : 'file'}`)}` +
|
226 | (chunkCount > 1 ? dim(` (chunk ${index + 1}/${chunkCount})...`) : ''),
|
227 | task: () => new Listr(chunkListrTasks, { ...listrOptions, concurrent, exitOnError: true }),
|
228 | skip: () => {
|
229 |
|
230 | if (ctx.errors.has(GitError)) return SKIPPED_GIT_ERROR
|
231 |
|
232 | if (chunkListrTasks.every((task) => task.skip())) {
|
233 | return `${configName}${dim(' — no tasks to run')}`
|
234 | }
|
235 | return false
|
236 | },
|
237 | })
|
238 | }
|
239 | }
|
240 |
|
241 | if (hasDeprecatedGitAdd) {
|
242 | logger.warn(DEPRECATED_GIT_ADD)
|
243 | }
|
244 |
|
245 |
|
246 |
|
247 | if (listrTasks.every((task) => task.skip())) {
|
248 | if (!quiet) ctx.output.push(NO_TASKS)
|
249 | return ctx
|
250 | }
|
251 |
|
252 |
|
253 | const matchedFileChunks = chunkFiles({
|
254 |
|
255 | baseDir: cwd,
|
256 | files: Array.from(matchedFiles),
|
257 | maxArgLength,
|
258 | relative: false,
|
259 | })
|
260 |
|
261 | const git = new GitWorkflow({ allowEmpty, gitConfigDir, gitDir, matchedFileChunks })
|
262 |
|
263 | const runner = new Listr(
|
264 | [
|
265 | {
|
266 | title: 'Preparing lint-staged...',
|
267 | task: (ctx) => git.prepare(ctx),
|
268 | },
|
269 | {
|
270 | title: 'Hiding unstaged changes to partially staged files...',
|
271 | task: (ctx) => git.hideUnstagedChanges(ctx),
|
272 | enabled: hasPartiallyStagedFiles,
|
273 | },
|
274 | {
|
275 | title: `Running tasks for staged files...`,
|
276 | task: () => new Listr(listrTasks, { ...listrOptions, concurrent }),
|
277 | skip: () => listrTasks.every((task) => task.skip()),
|
278 | },
|
279 | {
|
280 | title: 'Applying modifications from tasks...',
|
281 | task: (ctx) => git.applyModifications(ctx),
|
282 | skip: applyModificationsSkipped,
|
283 | },
|
284 | {
|
285 | title: 'Restoring unstaged changes to partially staged files...',
|
286 | task: (ctx) => git.restoreUnstagedChanges(ctx),
|
287 | enabled: hasPartiallyStagedFiles,
|
288 | skip: restoreUnstagedChangesSkipped,
|
289 | },
|
290 | {
|
291 | title: 'Reverting to original state because of errors...',
|
292 | task: (ctx) => git.restoreOriginalState(ctx),
|
293 | enabled: restoreOriginalStateEnabled,
|
294 | skip: restoreOriginalStateSkipped,
|
295 | },
|
296 | {
|
297 | title: 'Cleaning up temporary files...',
|
298 | task: (ctx) => git.cleanup(ctx),
|
299 | enabled: cleanupEnabled,
|
300 | skip: cleanupSkipped,
|
301 | },
|
302 | ],
|
303 | listrOptions
|
304 | )
|
305 |
|
306 | await runner.run()
|
307 |
|
308 | if (ctx.errors.size > 0) {
|
309 | throw createError(ctx)
|
310 | }
|
311 |
|
312 | return ctx
|
313 | }
|