1 | import path from 'node:path'
|
2 |
|
3 | import debug from 'debug'
|
4 | import micromatch from 'micromatch'
|
5 | import normalize from 'normalize-path'
|
6 |
|
7 | const debugLog = debug('lint-staged:generateTasks')
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | export const generateTasks = ({ config, cwd = process.cwd(), files, relative = false }) => {
|
20 | debugLog('Generating linter tasks')
|
21 |
|
22 | const relativeFiles = files.map((file) => normalize(path.relative(cwd, file)))
|
23 |
|
24 | return Object.entries(config).map(([pattern, commands]) => {
|
25 | const isParentDirPattern = pattern.startsWith('../')
|
26 |
|
27 |
|
28 |
|
29 | const filteredFiles = relativeFiles.filter((file) => {
|
30 | if (isParentDirPattern) return true
|
31 | return !file.startsWith('..') && !path.isAbsolute(file)
|
32 | })
|
33 |
|
34 | const matches = micromatch(filteredFiles, pattern, {
|
35 | cwd,
|
36 | dot: true,
|
37 |
|
38 |
|
39 |
|
40 | matchBase: !pattern.includes('/'),
|
41 | posixSlashes: true,
|
42 | strictBrackets: true,
|
43 | })
|
44 |
|
45 | const fileList = matches.map((file) => normalize(relative ? file : path.resolve(cwd, file)))
|
46 |
|
47 | const task = { pattern, commands, fileList }
|
48 | debugLog('Generated task: \n%O', task)
|
49 |
|
50 | return task
|
51 | })
|
52 | }
|