/
generateTasks.js
58 lines (50 loc) · 1.82 KB
/
generateTasks.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
'use strict'
const micromatch = require('micromatch')
const normalize = require('normalize-path')
const path = require('path')
const debug = require('debug')('lint-staged:gen-tasks')
/**
* Generates all task commands, and filelist
*
* @param {object} options
* @param {Object} [options.config] - Task configuration
* @param {Object} [options.cwd] - Current working directory
* @param {boolean} [options.gitDir] - Git root directory
* @param {boolean} [options.files] - Staged filepaths
* @param {boolean} [options.relative] - Whether filepaths to should be relative to gitDir
*/
module.exports = function generateTasks({
config,
cwd = process.cwd(),
gitDir,
files,
relative = false
}) {
debug('Generating linter tasks')
const absoluteFiles = files.map(file => normalize(path.resolve(gitDir, file)))
const relativeFiles = absoluteFiles.map(file => normalize(path.relative(cwd, file)))
return Object.entries(config).map(([pattern, commands]) => {
const isParentDirPattern = pattern.startsWith('../')
const fileList = micromatch(
relativeFiles
// Only worry about children of the CWD unless the pattern explicitly
// specifies that it concerns a parent directory.
.filter(file => {
if (isParentDirPattern) return true
return !file.startsWith('..') && !path.isAbsolute(file)
}),
pattern,
{
cwd,
dot: true,
// If pattern doesn't look like a path, enable `matchBase` to
// match against filenames in every directory. This makes `*.js`
// match both `test.js` and `subdirectory/test.js`.
matchBase: !pattern.includes('/')
}
).map(file => normalize(relative ? file : path.resolve(cwd, file)))
const task = { pattern, commands, fileList }
debug('Generated task: \n%O', task)
return task
})
}