lint-staged 12.2.1 → 12.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/getConfigGroups.js +48 -16
- package/lib/runAll.js +1 -1
- package/package.json +1 -1
package/lib/getConfigGroups.js
CHANGED
|
@@ -2,10 +2,15 @@
|
|
|
2
2
|
|
|
3
3
|
import path from 'path'
|
|
4
4
|
|
|
5
|
+
import debug from 'debug'
|
|
6
|
+
import objectInspect from 'object-inspect'
|
|
7
|
+
|
|
5
8
|
import { loadConfig } from './loadConfig.js'
|
|
6
9
|
import { ConfigNotFoundError } from './symbols.js'
|
|
7
10
|
import { validateConfig } from './validateConfig.js'
|
|
8
11
|
|
|
12
|
+
const debugLog = debug('lint-staged:getConfigGroups')
|
|
13
|
+
|
|
9
14
|
/**
|
|
10
15
|
* Return matched files grouped by their configuration.
|
|
11
16
|
*
|
|
@@ -13,17 +18,27 @@ import { validateConfig } from './validateConfig.js'
|
|
|
13
18
|
* @param {Object} [options.configObject] - Explicit config object from the js API
|
|
14
19
|
* @param {string} [options.configPath] - Explicit path to a config file
|
|
15
20
|
* @param {string} [options.cwd] - Current working directory
|
|
21
|
+
* @param {string} [options.files] - List of staged files
|
|
16
22
|
* @param {Logger} logger
|
|
17
23
|
*/
|
|
18
|
-
export const getConfigGroups = async (
|
|
24
|
+
export const getConfigGroups = async (
|
|
25
|
+
{ configObject, configPath, cwd, files },
|
|
26
|
+
logger = console
|
|
27
|
+
) => {
|
|
28
|
+
debugLog('Grouping configuration files...')
|
|
29
|
+
|
|
19
30
|
// Return explicit config object from js API
|
|
20
31
|
if (configObject) {
|
|
32
|
+
debugLog('Using single direct configuration object...')
|
|
33
|
+
|
|
21
34
|
const config = validateConfig(configObject, 'config object', logger)
|
|
22
35
|
return { '': { config, files } }
|
|
23
36
|
}
|
|
24
37
|
|
|
25
38
|
// Use only explicit config path instead of discovering multiple
|
|
26
39
|
if (configPath) {
|
|
40
|
+
debugLog('Using single configuration path...')
|
|
41
|
+
|
|
27
42
|
const { config, filepath } = await loadConfig({ configPath }, logger)
|
|
28
43
|
|
|
29
44
|
if (!config) {
|
|
@@ -35,6 +50,8 @@ export const getConfigGroups = async ({ configObject, configPath, files }, logge
|
|
|
35
50
|
return { [configPath]: { config: validatedConfig, files } }
|
|
36
51
|
}
|
|
37
52
|
|
|
53
|
+
debugLog('Grouping staged files by their directories...')
|
|
54
|
+
|
|
38
55
|
// Group files by their base directory
|
|
39
56
|
const filesByDir = files.reduce((acc, file) => {
|
|
40
57
|
const dir = path.normalize(path.dirname(file))
|
|
@@ -48,33 +65,48 @@ export const getConfigGroups = async ({ configObject, configPath, files }, logge
|
|
|
48
65
|
return acc
|
|
49
66
|
}, {})
|
|
50
67
|
|
|
68
|
+
debugLog('Grouped staged files into %d directories:', Object.keys(filesByDir).length)
|
|
69
|
+
debugLog(objectInspect(filesByDir, { indent: 2 }))
|
|
70
|
+
|
|
51
71
|
// Group files by their discovered config
|
|
52
72
|
// { '.lintstagedrc.json': { config: {...}, files: [...] } }
|
|
53
73
|
const configGroups = {}
|
|
54
74
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
75
|
+
debugLog('Searching config files...')
|
|
76
|
+
|
|
77
|
+
const searchConfig = async (cwd, files = []) => {
|
|
78
|
+
const { config, filepath } = await loadConfig({ cwd }, logger)
|
|
79
|
+
if (!config) {
|
|
80
|
+
debugLog('Found no config from "%s"!', cwd)
|
|
81
|
+
return
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
if (filepath in configGroups) {
|
|
85
|
+
debugLog('Found existing config "%s" from "%s"!', filepath, cwd)
|
|
86
|
+
// Re-use cached config and skip validation
|
|
87
|
+
configGroups[filepath].files.push(...files)
|
|
88
|
+
} else {
|
|
89
|
+
debugLog('Found new config "%s" from "%s"!', filepath, cwd)
|
|
60
90
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
}
|
|
91
|
+
const validatedConfig = validateConfig(config, filepath, logger)
|
|
92
|
+
configGroups[filepath] = { config: validatedConfig, files }
|
|
93
|
+
}
|
|
94
|
+
}
|
|
66
95
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
)
|
|
96
|
+
// Start by searching from cwd
|
|
97
|
+
await searchConfig(cwd)
|
|
98
|
+
|
|
99
|
+
// Discover configs from the base directory of each file
|
|
100
|
+
await Promise.all(Object.entries(filesByDir).map(([dir, files]) => searchConfig(dir, files)))
|
|
72
101
|
|
|
73
102
|
// Throw if no configurations were found
|
|
74
103
|
if (Object.keys(configGroups).length === 0) {
|
|
104
|
+
debugLog('Found no config groups!')
|
|
75
105
|
logger.error(`${ConfigNotFoundError.message}.`)
|
|
76
106
|
throw ConfigNotFoundError
|
|
77
107
|
}
|
|
78
108
|
|
|
109
|
+
debugLog('Grouped staged files into %d groups!', Object.keys(configGroups).length)
|
|
110
|
+
|
|
79
111
|
return configGroups
|
|
80
112
|
}
|
package/lib/runAll.js
CHANGED
|
@@ -114,7 +114,7 @@ export const runAll = async (
|
|
|
114
114
|
return ctx
|
|
115
115
|
}
|
|
116
116
|
|
|
117
|
-
const configGroups = await getConfigGroups({ configObject, configPath, files }, logger)
|
|
117
|
+
const configGroups = await getConfigGroups({ configObject, configPath, cwd, files }, logger)
|
|
118
118
|
|
|
119
119
|
// lint-staged 10 will automatically add modifications to index
|
|
120
120
|
// Warn user when their command includes `git add`
|