UNPKG

1.12 kBJavaScriptView Raw
1'use strict'
2
3const resolveTaskFn = require('./resolveTaskFn')
4
5const debug = require('debug')('lint-staged:make-cmd-tasks')
6
7/**
8 * Creates and returns an array of listr tasks which map to the given commands.
9 *
10 * @param {Array<string|Function>|string|Function} commands
11 * @param {Boolean} shell
12 * @param {Array<string>} pathsToLint
13 */
14module.exports = async function makeCmdTasks(commands, shell, gitDir, pathsToLint) {
15 debug('Creating listr tasks for commands %o', commands)
16 const commandsArray = Array.isArray(commands) ? commands : [commands]
17
18 return commandsArray.reduce((tasks, command) => {
19 // linter function may return array of commands that already include `pathsToLit`
20 const isFn = typeof command === 'function'
21 const resolved = isFn ? command(pathsToLint) : command
22 const linters = Array.isArray(resolved) ? resolved : [resolved] // Wrap non-array linter as array
23
24 linters.forEach(linter => {
25 const task = {
26 title: linter,
27 task: resolveTaskFn({ gitDir, isFn, linter, pathsToLint, shell })
28 }
29
30 tasks.push(task)
31 })
32
33 return tasks
34 }, [])
35}