1 | import { redBright, dim } from 'colorette'
|
2 | import { execa, execaCommand } 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 | let loopIntervalId
|
60 |
|
61 | async function loop() {
|
62 | if (ctx.errors.size > 0) {
|
63 | clearInterval(loopIntervalId)
|
64 |
|
65 | const childPids = await pidTree(execaChildProcess.pid)
|
66 | for (const pid of childPids) {
|
67 | process.kill(pid)
|
68 | }
|
69 |
|
70 |
|
71 |
|
72 | execaChildProcess.kill()
|
73 | }
|
74 | }
|
75 |
|
76 | loopIntervalId = setInterval(loop, ERROR_CHECK_INTERVAL)
|
77 |
|
78 | return () => {
|
79 | clearInterval(loopIntervalId)
|
80 | }
|
81 | }
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 | const makeErr = (command, result, ctx) => {
|
97 | ctx.errors.add(TaskError)
|
98 | handleOutput(command, result, ctx, true)
|
99 | const tag = getTag(result)
|
100 | return new Error(`${redBright(command)} ${dim(`[${tag}]`)}`)
|
101 | }
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 | export const resolveTaskFn = ({
|
117 | command,
|
118 | cwd = process.cwd(),
|
119 | files,
|
120 | gitDir,
|
121 | isFn,
|
122 | shell = false,
|
123 | verbose = false,
|
124 | }) => {
|
125 | const [cmd, ...args] = parseArgsStringToArgv(command)
|
126 | debugLog('cmd:', cmd)
|
127 | debugLog('args:', args)
|
128 |
|
129 | const execaOptions = {
|
130 |
|
131 |
|
132 | cwd: /^git(\.exe)?/i.test(cmd) ? gitDir : cwd,
|
133 | preferLocal: true,
|
134 | reject: false,
|
135 | shell,
|
136 | }
|
137 |
|
138 | debugLog('execaOptions:', execaOptions)
|
139 |
|
140 | return async (ctx = getInitialState()) => {
|
141 | const execaChildProcess = shell
|
142 | ? execaCommand(isFn ? command : `${command} ${files.join(' ')}`, execaOptions)
|
143 | : execa(cmd, isFn ? args : args.concat(files), execaOptions)
|
144 |
|
145 | const quitInterruptCheck = interruptExecutionOnError(ctx, execaChildProcess)
|
146 | const result = await execaChildProcess
|
147 | quitInterruptCheck()
|
148 |
|
149 | if (result.failed || result.killed || result.signal != null) {
|
150 | throw makeErr(command, result, ctx)
|
151 | }
|
152 |
|
153 | if (verbose) {
|
154 | handleOutput(command, result, ctx)
|
155 | }
|
156 | }
|
157 | }
|