1 | import path from '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(([rawPattern, commands]) => {
|
25 | let pattern = rawPattern
|
26 |
|
27 | const isParentDirPattern = pattern.startsWith('../')
|
28 |
|
29 |
|
30 |
|
31 | const filteredFiles = relativeFiles.filter((file) => {
|
32 | if (isParentDirPattern) return true
|
33 | return !file.startsWith('..') && !path.isAbsolute(file)
|
34 | })
|
35 |
|
36 | const matches = micromatch(filteredFiles, pattern, {
|
37 | cwd,
|
38 | dot: true,
|
39 |
|
40 |
|
41 |
|
42 | matchBase: !pattern.includes('/'),
|
43 | strictBrackets: true,
|
44 | })
|
45 |
|
46 | const fileList = matches.map((file) => normalize(relative ? file : path.resolve(cwd, file)))
|
47 |
|
48 | const task = { pattern, commands, fileList }
|
49 | debugLog('Generated task: \n%O', task)
|
50 |
|
51 | return task
|
52 | })
|
53 | }
|