1 | import debug from 'debug'
|
2 | import inspect from 'object-inspect'
|
3 |
|
4 | import { loadConfig } from './loadConfig.js'
|
5 | import { PREVENTED_EMPTY_COMMIT, GIT_ERROR, RESTORE_STASH_EXAMPLE } from './messages.js'
|
6 | import { printTaskOutput } from './printTaskOutput.js'
|
7 | import { runAll } from './runAll.js'
|
8 | import {
|
9 | ApplyEmptyCommitError,
|
10 | ConfigNotFoundError,
|
11 | GetBackupStashError,
|
12 | GitError,
|
13 | } from './symbols.js'
|
14 | import { validateConfig } from './validateConfig.js'
|
15 | import { validateOptions } from './validateOptions.js'
|
16 |
|
17 | const debugLog = debug('lint-staged')
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 | const lintStaged = async (
|
43 | {
|
44 | allowEmpty = false,
|
45 | concurrent = true,
|
46 | config: configObject,
|
47 | configPath,
|
48 | cwd = process.cwd(),
|
49 | debug = false,
|
50 | maxArgLength,
|
51 | quiet = false,
|
52 | relative = false,
|
53 | shell = false,
|
54 | stash = true,
|
55 | verbose = false,
|
56 | } = {},
|
57 | logger = console
|
58 | ) => {
|
59 | await validateOptions({ shell }, logger)
|
60 |
|
61 | debugLog('Loading config using `lilconfig`')
|
62 |
|
63 | const resolved = configObject
|
64 | ? { config: configObject, filepath: '(input)' }
|
65 | : await loadConfig(configPath)
|
66 |
|
67 | if (!resolved) {
|
68 | logger.error(`${ConfigNotFoundError.message}.`)
|
69 | throw ConfigNotFoundError
|
70 | }
|
71 |
|
72 | debugLog('Successfully loaded config from `%s`:\n%O', resolved.filepath, resolved.config)
|
73 |
|
74 | const config = validateConfig(resolved.config, logger)
|
75 |
|
76 | if (debug) {
|
77 |
|
78 | logger.log('Running lint-staged with the following config:')
|
79 | logger.log(inspect(config, { indent: 2 }))
|
80 | } else {
|
81 |
|
82 |
|
83 | debugLog('lint-staged config:\n%O', config)
|
84 | }
|
85 |
|
86 |
|
87 | debugLog('Unset GIT_LITERAL_PATHSPECS (was `%s`)', process.env.GIT_LITERAL_PATHSPECS)
|
88 | delete process.env.GIT_LITERAL_PATHSPECS
|
89 |
|
90 | try {
|
91 | const ctx = await runAll(
|
92 | {
|
93 | allowEmpty,
|
94 | concurrent,
|
95 | config,
|
96 | cwd,
|
97 | debug,
|
98 | maxArgLength,
|
99 | quiet,
|
100 | relative,
|
101 | shell,
|
102 | stash,
|
103 | verbose,
|
104 | },
|
105 | logger
|
106 | )
|
107 | debugLog('Tasks were executed successfully!')
|
108 | printTaskOutput(ctx, logger)
|
109 | return true
|
110 | } catch (runAllError) {
|
111 | if (runAllError && runAllError.ctx && runAllError.ctx.errors) {
|
112 | const { ctx } = runAllError
|
113 | if (ctx.errors.has(ApplyEmptyCommitError)) {
|
114 | logger.warn(PREVENTED_EMPTY_COMMIT)
|
115 | } else if (ctx.errors.has(GitError) && !ctx.errors.has(GetBackupStashError)) {
|
116 | logger.error(GIT_ERROR)
|
117 | if (ctx.shouldBackup) {
|
118 |
|
119 | logger.error(RESTORE_STASH_EXAMPLE)
|
120 | }
|
121 | }
|
122 |
|
123 | printTaskOutput(ctx, logger)
|
124 | return false
|
125 | }
|
126 |
|
127 |
|
128 | throw runAllError
|
129 | }
|
130 | }
|
131 |
|
132 | export default lintStaged
|