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 { getConfigGroups } from './getConfigGroups.js'
|
14 | import { getRenderer } from './getRenderer.js'
|
15 | import { getStagedFiles } from './getStagedFiles.js'
|
16 | import { GitWorkflow } from './gitWorkflow.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 { 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 configGroups = await getConfigGroups({ configObject, configPath, cwd, files }, logger)
|
124 |
|
125 | const hasExplicitConfig = configObject || configPath
|
126 | const foundConfigs = hasExplicitConfig ? null : await searchConfigs(gitDir, logger)
|
127 | const numberOfConfigs = hasExplicitConfig ? 1 : Object.keys(foundConfigs).length
|
128 |
|
129 |
|
130 | if (numberOfConfigs === 0) {
|
131 | ctx.errors.add(ConfigNotFoundError)
|
132 | throw createError(ctx, ConfigNotFoundError)
|
133 | }
|
134 |
|
135 | debugLog('Found %d configs:\n%O', numberOfConfigs, foundConfigs)
|
136 |
|
137 | const hasMultipleConfigs = numberOfConfigs > 1
|
138 |
|
139 |
|
140 |
|
141 | let hasDeprecatedGitAdd = false
|
142 |
|
143 | const listrOptions = {
|
144 | ctx,
|
145 | exitOnError: false,
|
146 | nonTTYRenderer: 'verbose',
|
147 | registerSignalListeners: false,
|
148 | ...getRenderer({ debug, quiet }),
|
149 | }
|
150 |
|
151 | const listrTasks = []
|
152 |
|
153 |
|
154 | const matchedFiles = new Set()
|
155 |
|
156 | for (const [configPath, { config, files }] of Object.entries(configGroups)) {
|
157 | const relativeConfig = normalize(path.relative(cwd, configPath))
|
158 | const stagedFileChunks = chunkFiles({ baseDir: gitDir, files, maxArgLength, relative })
|
159 |
|
160 |
|
161 |
|
162 |
|
163 | const groupCwd = hasMultipleConfigs && !hasExplicitCwd ? path.dirname(configPath) : cwd
|
164 |
|
165 | const chunkCount = stagedFileChunks.length
|
166 | if (chunkCount > 1) {
|
167 | debugLog('Chunked staged files from `%s` into %d part', configPath, chunkCount)
|
168 | }
|
169 |
|
170 | for (const [index, files] of stagedFileChunks.entries()) {
|
171 | const chunkListrTasks = await Promise.all(
|
172 | generateTasks({ config, cwd: groupCwd, files, relative }).map((task) =>
|
173 | makeCmdTasks({
|
174 | commands: task.commands,
|
175 | cwd: groupCwd,
|
176 | files: task.fileList,
|
177 | gitDir,
|
178 | renderer: listrOptions.renderer,
|
179 | shell,
|
180 | verbose,
|
181 | }).then((subTasks) => {
|
182 |
|
183 | task.fileList.forEach((file) => {
|
184 |
|
185 |
|
186 |
|
187 | const normalizedFile = path.isAbsolute(file)
|
188 | ? file
|
189 | : normalize(path.join(groupCwd, file))
|
190 |
|
191 | matchedFiles.add(normalizedFile)
|
192 | })
|
193 |
|
194 | hasDeprecatedGitAdd =
|
195 | hasDeprecatedGitAdd || subTasks.some((subTask) => subTask.command === 'git add')
|
196 |
|
197 | const fileCount = task.fileList.length
|
198 |
|
199 | return {
|
200 | title: `${task.pattern}${dim(` — ${fileCount} ${fileCount > 1 ? 'files' : 'file'}`)}`,
|
201 | task: async () =>
|
202 | new Listr(subTasks, {
|
203 |
|
204 |
|
205 | ...listrOptions,
|
206 | concurrent: false,
|
207 | exitOnError: true,
|
208 | }),
|
209 | skip: () => {
|
210 |
|
211 | if (fileCount === 0) {
|
212 | return `${task.pattern}${dim(' — no files')}`
|
213 | }
|
214 | return false
|
215 | },
|
216 | }
|
217 | })
|
218 | )
|
219 | )
|
220 |
|
221 | listrTasks.push({
|
222 | title:
|
223 | `${relativeConfig}${dim(` — ${files.length} ${files.length > 1 ? 'files' : 'file'}`)}` +
|
224 | (chunkCount > 1 ? dim(` (chunk ${index + 1}/${chunkCount})...`) : ''),
|
225 | task: () => new Listr(chunkListrTasks, { ...listrOptions, concurrent, exitOnError: true }),
|
226 | skip: () => {
|
227 |
|
228 | if (ctx.errors.has(GitError)) return SKIPPED_GIT_ERROR
|
229 |
|
230 | if (chunkListrTasks.every((task) => task.skip())) {
|
231 | return `${relativeConfig}${dim(' — no tasks to run')}`
|
232 | }
|
233 | return false
|
234 | },
|
235 | })
|
236 | }
|
237 | }
|
238 |
|
239 | if (hasDeprecatedGitAdd) {
|
240 | logger.warn(DEPRECATED_GIT_ADD)
|
241 | }
|
242 |
|
243 |
|
244 |
|
245 | if (listrTasks.every((task) => task.skip())) {
|
246 | if (!quiet) ctx.output.push(NO_TASKS)
|
247 | return ctx
|
248 | }
|
249 |
|
250 |
|
251 | const matchedFileChunks = chunkFiles({
|
252 |
|
253 | baseDir: cwd,
|
254 | files: Array.from(matchedFiles),
|
255 | maxArgLength,
|
256 | relative: false,
|
257 | })
|
258 |
|
259 | const git = new GitWorkflow({ allowEmpty, gitConfigDir, gitDir, matchedFileChunks })
|
260 |
|
261 | const runner = new Listr(
|
262 | [
|
263 | {
|
264 | title: 'Preparing lint-staged...',
|
265 | task: (ctx) => git.prepare(ctx),
|
266 | },
|
267 | {
|
268 | title: 'Hiding unstaged changes to partially staged files...',
|
269 | task: (ctx) => git.hideUnstagedChanges(ctx),
|
270 | enabled: hasPartiallyStagedFiles,
|
271 | },
|
272 | {
|
273 | title: `Running tasks for staged files...`,
|
274 | task: () => new Listr(listrTasks, { ...listrOptions, concurrent }),
|
275 | skip: () => listrTasks.every((task) => task.skip()),
|
276 | },
|
277 | {
|
278 | title: 'Applying modifications from tasks...',
|
279 | task: (ctx) => git.applyModifications(ctx),
|
280 | skip: applyModificationsSkipped,
|
281 | },
|
282 | {
|
283 | title: 'Restoring unstaged changes to partially staged files...',
|
284 | task: (ctx) => git.restoreUnstagedChanges(ctx),
|
285 | enabled: hasPartiallyStagedFiles,
|
286 | skip: restoreUnstagedChangesSkipped,
|
287 | },
|
288 | {
|
289 | title: 'Reverting to original state because of errors...',
|
290 | task: (ctx) => git.restoreOriginalState(ctx),
|
291 | enabled: restoreOriginalStateEnabled,
|
292 | skip: restoreOriginalStateSkipped,
|
293 | },
|
294 | {
|
295 | title: 'Cleaning up temporary files...',
|
296 | task: (ctx) => git.cleanup(ctx),
|
297 | enabled: cleanupEnabled,
|
298 | skip: cleanupSkipped,
|
299 | },
|
300 | ],
|
301 | listrOptions
|
302 | )
|
303 |
|
304 | await runner.run()
|
305 |
|
306 | if (ctx.errors.size > 0) {
|
307 | throw createError(ctx)
|
308 | }
|
309 |
|
310 | return ctx
|
311 | }
|