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