UNPKG

1.64 kBJavaScriptView Raw
1'use strict'
2
3const path = require('path')
4const micromatch = require('micromatch')
5const pathIsInside = require('path-is-inside')
6const { getConfig } = require('./getConfig')
7const resolveGitDir = require('./resolveGitDir')
8
9const debug = require('debug')('lint-staged:gen-tasks')
10
11module.exports = function generateTasks(config, stagedRelFiles) {
12 debug('Generating linter tasks')
13
14 const normalizedConfig = getConfig(config) // Ensure we have a normalized 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 // Only worry about children of the CWD unless the pattern explicitly
28 // specifies that it concerns a parent directory.
29 .filter(file => isParentDirPattern || pathIsInside(file, cwd))
30 // Make the paths relative to CWD for filtering
31 .map(file => path.relative(cwd, file)),
32 pattern,
33 {
34 ...globOptions,
35 ignore
36 }
37 ).map(file => {
38 // if you set relative option, then the file path will be relative to your package.json
39 if (config.relative) {
40 return path.normalize(file)
41 }
42 // Return absolute path after the filter is run
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}