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