1 | 'use strict'
|
2 |
|
3 | const dedent = require('dedent')
|
4 | const { cosmiconfig } = require('cosmiconfig')
|
5 | const stringifyObject = require('stringify-object')
|
6 | const printErrors = require('./printErrors')
|
7 | const runAll = require('./runAll')
|
8 | const validateConfig = require('./validateConfig')
|
9 |
|
10 | const debugLog = require('debug')('lint-staged')
|
11 |
|
12 | const errConfigNotFound = new Error('Config could not be found')
|
13 |
|
14 | function resolveConfig(configPath) {
|
15 | try {
|
16 | return require.resolve(configPath)
|
17 | } catch {
|
18 | return configPath
|
19 | }
|
20 | }
|
21 |
|
22 | function loadConfig(configPath) {
|
23 | const explorer = cosmiconfig('lint-staged', {
|
24 | searchPlaces: [
|
25 | 'package.json',
|
26 | '.lintstagedrc',
|
27 | '.lintstagedrc.json',
|
28 | '.lintstagedrc.yaml',
|
29 | '.lintstagedrc.yml',
|
30 | '.lintstagedrc.js',
|
31 | 'lint-staged.config.js'
|
32 | ]
|
33 | })
|
34 |
|
35 | return configPath ? explorer.load(resolveConfig(configPath)) : explorer.search()
|
36 | }
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 | module.exports = async function lintStaged(
|
60 | {
|
61 | allowEmpty = false,
|
62 | concurrent = true,
|
63 | config: configObject,
|
64 | configPath,
|
65 | maxArgLength,
|
66 | relative = false,
|
67 | shell = false,
|
68 | quiet = false,
|
69 | debug = false
|
70 | } = {},
|
71 | logger = console
|
72 | ) {
|
73 | try {
|
74 | debugLog('Loading config using `cosmiconfig`')
|
75 |
|
76 | const resolved = configObject
|
77 | ? { config: configObject, filepath: '(input)' }
|
78 | : await loadConfig(configPath)
|
79 | if (resolved == null) throw errConfigNotFound
|
80 |
|
81 | debugLog('Successfully loaded config from `%s`:\n%O', resolved.filepath, resolved.config)
|
82 |
|
83 |
|
84 | const config = validateConfig(resolved.config)
|
85 | if (debug) {
|
86 |
|
87 | logger.log('Running lint-staged with the following config:')
|
88 | logger.log(stringifyObject(config, { indent: ' ' }))
|
89 | } else {
|
90 |
|
91 |
|
92 | debugLog('lint-staged config:\n%O', config)
|
93 | }
|
94 |
|
95 | try {
|
96 | await runAll(
|
97 | { allowEmpty, concurrent, config, debug, maxArgLength, quiet, relative, shell },
|
98 | logger
|
99 | )
|
100 | debugLog('tasks were executed successfully!')
|
101 | return true
|
102 | } catch (runAllError) {
|
103 | printErrors(runAllError, logger)
|
104 | return false
|
105 | }
|
106 | } catch (lintStagedError) {
|
107 | if (lintStagedError === errConfigNotFound) {
|
108 | logger.error(`${lintStagedError.message}.`)
|
109 | } else {
|
110 |
|
111 | logger.error(dedent`
|
112 | Could not parse lint-staged config.
|
113 |
|
114 | ${lintStagedError}
|
115 | `)
|
116 | }
|
117 | logger.error()
|
118 |
|
119 | logger.error(dedent`
|
120 | Please make sure you have created it correctly.
|
121 | See https://github.com/okonet/lint-staged#configuration.
|
122 | `)
|
123 |
|
124 | throw lintStagedError
|
125 | }
|
126 | }
|