1 | import cliTruncate from 'cli-truncate'
|
2 | import debug from 'debug'
|
3 |
|
4 | import { configurationError } from './messages.js'
|
5 | import { resolveTaskFn } from './resolveTaskFn.js'
|
6 |
|
7 | const debugLog = debug('lint-staged:makeCmdTasks')
|
8 |
|
9 | const STDOUT_COLUMNS_DEFAULT = 80
|
10 |
|
11 | const listrPrefixLength = {
|
12 | update: ` X `.length,
|
13 | verbose: `[STARTED] `.length,
|
14 | }
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | const getTitleLength = (renderer, columns = process.stdout.columns) => {
|
22 | const prefixLength = listrPrefixLength[renderer] || 0
|
23 | return (columns || STDOUT_COLUMNS_DEFAULT) - prefixLength
|
24 | }
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 | export const makeCmdTasks = async ({ commands, files, gitDir, renderer, shell, verbose }) => {
|
38 | debugLog('Creating listr tasks for commands %o', commands)
|
39 | const commandArray = Array.isArray(commands) ? commands : [commands]
|
40 | const cmdTasks = []
|
41 |
|
42 | for (const cmd of commandArray) {
|
43 |
|
44 | const isFn = typeof cmd === 'function'
|
45 | const resolved = isFn ? await cmd(files) : cmd
|
46 |
|
47 | const resolvedArray = Array.isArray(resolved) ? resolved : [resolved]
|
48 |
|
49 | for (const command of resolvedArray) {
|
50 |
|
51 |
|
52 | if (isFn && typeof command !== 'string') {
|
53 | throw new Error(
|
54 | configurationError(
|
55 | '[Function]',
|
56 | 'Function task should return a string or an array of strings',
|
57 | resolved
|
58 | )
|
59 | )
|
60 | }
|
61 |
|
62 |
|
63 | const title = cliTruncate(command, getTitleLength(renderer))
|
64 | const task = resolveTaskFn({ command, files, gitDir, isFn, shell, verbose })
|
65 | cmdTasks.push({ title, command, task })
|
66 | }
|
67 | }
|
68 |
|
69 | return cmdTasks
|
70 | }
|