-
-
Notifications
You must be signed in to change notification settings - Fork 411
/
makeCmdTasks.js
58 lines (48 loc) · 1.85 KB
/
makeCmdTasks.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 resolveTaskFn = require('./resolveTaskFn')
const { createError } = require('./validateConfig')
const debug = require('debug')('lint-staged:make-cmd-tasks')
/**
* Creates and returns an array of listr tasks which map to the given commands.
*
* @param {object} options
* @param {Array<string|Function>|string|Function} options.commands
* @param {Array<string>} options.files
* @param {string} options.gitDir
* @param {Boolean} shell
*/
module.exports = async function makeCmdTasks({ commands, files, gitDir, shell }) {
debug('Creating listr tasks for commands %o', commands)
const commandArray = Array.isArray(commands) ? commands : [commands]
const cmdTasks = []
for (const cmd of commandArray) {
// command function may return array of commands that already include `stagedFiles`
const isFn = typeof cmd === 'function'
const resolved = isFn ? await cmd(files) : cmd
const resolvedArray = Array.isArray(resolved) ? resolved : [resolved] // Wrap non-array command as array
for (const command of resolvedArray) {
let title = isFn ? '[Function]' : command
if (isFn) {
// If the function linter didn't return string | string[] it won't work
// Do the validation here instead of `validateConfig` to skip evaluating the function multiple times
if (typeof command !== 'string') {
throw new Error(
createError(
title,
'Function task should return a string or an array of strings',
resolved
)
)
}
const [startOfFn] = command.split(' ')
title += ` ${startOfFn} ...` // Append function name, like `[Function] eslint ...`
}
cmdTasks.push({
title,
command,
task: resolveTaskFn({ command, files, gitDir, isFn, shell })
})
}
}
return cmdTasks
}