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