1 | import { redBright, dim } from 'colorette'
|
2 | import execa from 'execa'
|
3 | import debug from 'debug'
|
4 | import { parseArgsStringToArgv } from 'string-argv'
|
5 | import pidTree from 'pidtree'
|
6 |
|
7 | import { error, info } from './figures.js'
|
8 | import { getInitialState } from './state.js'
|
9 | import { TaskError } from './symbols.js'
|
10 |
|
11 | const ERROR_CHECK_INTERVAL = 200
|
12 |
|
13 | const debugLog = debug('lint-staged:resolveTaskFn')
|
14 |
|
15 | const getTag = ({ code, killed, signal }) => (killed && 'KILLED') || signal || code || 'FAILED'
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 | const handleOutput = (command, result, ctx, isError = false) => {
|
31 | const { stderr, stdout } = result
|
32 | const hasOutput = !!stderr || !!stdout
|
33 |
|
34 | if (hasOutput) {
|
35 | const outputTitle = isError ? redBright(`${error} ${command}:`) : `${info} ${command}:`
|
36 | const output = []
|
37 | .concat(ctx.quiet ? [] : ['', outputTitle])
|
38 | .concat(stderr ? stderr : [])
|
39 | .concat(stdout ? stdout : [])
|
40 | ctx.output.push(output.join('\n'))
|
41 | } else if (isError) {
|
42 |
|
43 | const tag = getTag(result)
|
44 | const message = redBright(`\n${error} ${command} failed without output (${tag}).`)
|
45 | if (!ctx.quiet) ctx.output.push(message)
|
46 | }
|
47 | }
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 | const interruptExecutionOnError = (ctx, execaChildProcess) => {
|
59 | async function loop() {
|
60 | if (ctx.errors.size > 0) {
|
61 | const ids = await pidTree(execaChildProcess.pid)
|
62 | ids.forEach(process.kill)
|
63 |
|
64 |
|
65 |
|
66 | execaChildProcess.kill()
|
67 | }
|
68 | }
|
69 |
|
70 | const loopIntervalId = setInterval(loop, ERROR_CHECK_INTERVAL)
|
71 |
|
72 | return () => {
|
73 | clearInterval(loopIntervalId)
|
74 | }
|
75 | }
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 | const makeErr = (command, result, ctx) => {
|
91 | ctx.errors.add(TaskError)
|
92 | handleOutput(command, result, ctx, true)
|
93 | const tag = getTag(result)
|
94 | return new Error(`${redBright(command)} ${dim(`[${tag}]`)}`)
|
95 | }
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 | export const resolveTaskFn = ({
|
111 | command,
|
112 | cwd = process.cwd(),
|
113 | files,
|
114 | gitDir,
|
115 | isFn,
|
116 | shell = false,
|
117 | verbose = false,
|
118 | }) => {
|
119 | const [cmd, ...args] = parseArgsStringToArgv(command)
|
120 | debugLog('cmd:', cmd)
|
121 | debugLog('args:', args)
|
122 |
|
123 | const execaOptions = {
|
124 |
|
125 |
|
126 | cwd: /^git(\.exe)?/i.test(cmd) ? gitDir : cwd,
|
127 | preferLocal: true,
|
128 | reject: false,
|
129 | shell,
|
130 | }
|
131 |
|
132 | debugLog('execaOptions:', execaOptions)
|
133 |
|
134 | return async (ctx = getInitialState()) => {
|
135 | const execaChildProcess = shell
|
136 | ? execa.command(isFn ? command : `${command} ${files.join(' ')}`, execaOptions)
|
137 | : execa(cmd, isFn ? args : args.concat(files), execaOptions)
|
138 |
|
139 | const quitInterruptCheck = interruptExecutionOnError(ctx, execaChildProcess)
|
140 | const result = await execaChildProcess
|
141 | quitInterruptCheck()
|
142 |
|
143 | if (result.failed || result.killed || result.signal != null) {
|
144 | throw makeErr(command, result, ctx)
|
145 | }
|
146 |
|
147 | if (verbose) {
|
148 | handleOutput(command, result, ctx)
|
149 | }
|
150 | }
|
151 | }
|