1 | 'use strict'
|
2 |
|
3 | const chalk = require('chalk')
|
4 | const dedent = require('dedent')
|
5 | const execa = require('execa')
|
6 | const symbols = require('log-symbols')
|
7 | const stringArgv = require('string-argv')
|
8 |
|
9 | const debug = require('debug')('lint-staged:task')
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | const execLinter = (cmd, args, execaOptions) => {
|
21 | debug('cmd:', cmd)
|
22 | if (args) debug('args:', args)
|
23 | debug('execaOptions:', execaOptions)
|
24 |
|
25 | return args ? execa(cmd, args, execaOptions) : execa(cmd, execaOptions)
|
26 | }
|
27 |
|
28 | const successMsg = linter => `${symbols.success} ${linter} passed!`
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 | function throwError(message) {
|
40 | const err = new Error()
|
41 | err.privateMsg = `\n\n\n${message}`
|
42 | return err
|
43 | }
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 | function makeErr(linter, result, context = {}) {
|
59 |
|
60 | context.hasErrors = true
|
61 | const { stdout, stderr, killed, signal } = result
|
62 | if (killed || (signal && signal !== '')) {
|
63 | return throwError(
|
64 | `${symbols.warning} ${chalk.yellow(`${linter} was terminated with ${signal}`)}`
|
65 | )
|
66 | }
|
67 | return throwError(dedent`${symbols.error} ${chalk.redBright(
|
68 | `${linter} found some errors. Please fix them and try committing again.`
|
69 | )}
|
70 | ${stdout}
|
71 | ${stderr}
|
72 | `)
|
73 | }
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 | module.exports = function resolveTaskFn({ gitDir, isFn, linter, pathsToLint, shell = false }) {
|
88 | const execaOptions = { preferLocal: true, reject: false, shell }
|
89 |
|
90 | let cmd
|
91 | let args
|
92 |
|
93 | if (shell) {
|
94 | execaOptions.shell = true
|
95 |
|
96 |
|
97 | cmd = isFn ? linter : `${linter} ${pathsToLint.join(' ')}`
|
98 | } else {
|
99 | const [parsedCmd, ...parsedArgs] = stringArgv.parseArgsStringToArgv(linter)
|
100 | cmd = parsedCmd
|
101 | args = isFn ? parsedArgs : parsedArgs.concat(pathsToLint)
|
102 | }
|
103 |
|
104 |
|
105 |
|
106 | if (/^git(\.exe)?/i.test(linter) && gitDir !== process.cwd()) {
|
107 | execaOptions.cwd = gitDir
|
108 | }
|
109 |
|
110 | return ctx =>
|
111 | execLinter(cmd, args, execaOptions).then(result => {
|
112 | if (result.failed || result.killed || result.signal != null) {
|
113 | throw makeErr(linter, result, ctx)
|
114 | }
|
115 |
|
116 | return successMsg(linter)
|
117 | })
|
118 | }
|