1 | 'use strict'
|
2 |
|
3 | const dedent = require('dedent')
|
4 | const { cosmiconfig } = require('cosmiconfig')
|
5 | const debugLog = require('debug')('lint-staged')
|
6 | const stringifyObject = require('stringify-object')
|
7 |
|
8 | const { PREVENTED_EMPTY_COMMIT, GIT_ERROR, RESTORE_STASH_EXAMPLE } = require('./messages')
|
9 | const printTaskOutput = require('./printTaskOutput')
|
10 | const runAll = require('./runAll')
|
11 | const { ApplyEmptyCommitError, GetBackupStashError, GitError } = require('./symbols')
|
12 | const formatConfig = require('./formatConfig')
|
13 | const validateConfig = require('./validateConfig')
|
14 |
|
15 | const errConfigNotFound = new Error('Config could not be found')
|
16 |
|
17 | function resolveConfig(configPath) {
|
18 | try {
|
19 | return require.resolve(configPath)
|
20 | } catch {
|
21 | return configPath
|
22 | }
|
23 | }
|
24 |
|
25 | function loadConfig(configPath) {
|
26 | const explorer = cosmiconfig('lint-staged', {
|
27 | searchPlaces: [
|
28 | 'package.json',
|
29 | '.lintstagedrc',
|
30 | '.lintstagedrc.json',
|
31 | '.lintstagedrc.yaml',
|
32 | '.lintstagedrc.yml',
|
33 | '.lintstagedrc.js',
|
34 | '.lintstagedrc.cjs',
|
35 | 'lint-staged.config.js',
|
36 | 'lint-staged.config.cjs',
|
37 | ],
|
38 | })
|
39 |
|
40 | return configPath ? explorer.load(resolveConfig(configPath)) : explorer.search()
|
41 | }
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 | module.exports = async function lintStaged(
|
67 | {
|
68 | allowEmpty = false,
|
69 | concurrent = true,
|
70 | config: configObject,
|
71 | configPath,
|
72 | cwd = process.cwd(),
|
73 | debug = false,
|
74 | maxArgLength,
|
75 | quiet = false,
|
76 | relative = false,
|
77 | shell = false,
|
78 | stash = true,
|
79 | verbose = false,
|
80 | } = {},
|
81 | logger = console
|
82 | ) {
|
83 | try {
|
84 | debugLog('Loading config using `cosmiconfig`')
|
85 |
|
86 | const resolved = configObject
|
87 | ? { config: configObject, filepath: '(input)' }
|
88 | : await loadConfig(configPath)
|
89 | if (resolved == null) throw errConfigNotFound
|
90 |
|
91 | debugLog('Successfully loaded config from `%s`:\n%O', resolved.filepath, resolved.config)
|
92 |
|
93 |
|
94 | const formattedConfig = formatConfig(resolved.config)
|
95 | const config = validateConfig(formattedConfig)
|
96 | if (debug) {
|
97 |
|
98 | logger.log('Running lint-staged with the following config:')
|
99 | logger.log(stringifyObject(config, { indent: ' ' }))
|
100 | } else {
|
101 |
|
102 |
|
103 | debugLog('lint-staged config:\n%O', config)
|
104 | }
|
105 |
|
106 |
|
107 | debugLog('Unset GIT_LITERAL_PATHSPECS (was `%s`)', process.env.GIT_LITERAL_PATHSPECS)
|
108 | delete process.env.GIT_LITERAL_PATHSPECS
|
109 |
|
110 | try {
|
111 | const ctx = await runAll(
|
112 | {
|
113 | allowEmpty,
|
114 | concurrent,
|
115 | config,
|
116 | cwd,
|
117 | debug,
|
118 | maxArgLength,
|
119 | quiet,
|
120 | relative,
|
121 | shell,
|
122 | stash,
|
123 | verbose,
|
124 | },
|
125 | logger
|
126 | )
|
127 | debugLog('Tasks were executed successfully!')
|
128 | printTaskOutput(ctx, logger)
|
129 | return true
|
130 | } catch (runAllError) {
|
131 | const { ctx } = runAllError
|
132 | if (ctx.errors.has(ApplyEmptyCommitError)) {
|
133 | logger.warn(PREVENTED_EMPTY_COMMIT)
|
134 | } else if (ctx.errors.has(GitError) && !ctx.errors.has(GetBackupStashError)) {
|
135 | logger.error(GIT_ERROR)
|
136 | if (ctx.shouldBackup) {
|
137 |
|
138 | logger.error(RESTORE_STASH_EXAMPLE)
|
139 | }
|
140 | }
|
141 |
|
142 | printTaskOutput(ctx, logger)
|
143 | return false
|
144 | }
|
145 | } catch (lintStagedError) {
|
146 | if (lintStagedError === errConfigNotFound) {
|
147 | logger.error(`${lintStagedError.message}.`)
|
148 | } else {
|
149 |
|
150 | logger.error(dedent`
|
151 | Could not parse lint-staged config.
|
152 |
|
153 | ${lintStagedError}
|
154 | `)
|
155 | }
|
156 | logger.error()
|
157 |
|
158 | logger.error(dedent`
|
159 | Please make sure you have created it correctly.
|
160 | See https://github.com/okonet/lint-staged#configuration.
|
161 | `)
|
162 |
|
163 | throw lintStagedError
|
164 | }
|
165 | }
|