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
*
@@ -20,14 +25,20 @@ export const getConfigGroups = async (
20
25
{ configObject, configPath, cwd, files } ,
21
26
logger = console
22
27
) => {
28
+ debugLog ( 'Grouping configuration files...' )
29
+
23
30
// Return explicit config object from js API
24
31
if ( configObject ) {
32
+ debugLog ( 'Using single direct configuration object...' )
33
+
25
34
const config = validateConfig ( configObject , 'config object' , logger )
26
35
return { '' : { config, files } }
27
36
}
28
37
29
38
// Use only explicit config path instead of discovering multiple
30
39
if ( configPath ) {
40
+ debugLog ( 'Using single configuration path...' )
41
+
31
42
const { config, filepath } = await loadConfig ( { configPath } , logger )
32
43
33
44
if ( ! config ) {
@@ -39,6 +50,8 @@ export const getConfigGroups = async (
39
50
return { [ configPath ] : { config : validatedConfig , files } }
40
51
}
41
52
53
+ debugLog ( 'Grouping staged files by their directories...' )
54
+
42
55
// Group files by their base directory
43
56
const filesByDir = files . reduce ( ( acc , file ) => {
44
57
const dir = path . normalize ( path . dirname ( file ) )
@@ -52,18 +65,29 @@ export const getConfigGroups = async (
52
65
return acc
53
66
} , { } )
54
67
68
+ debugLog ( 'Grouped staged files into %d directories:' , Object . keys ( filesByDir ) . length )
69
+ debugLog ( objectInspect ( filesByDir , { indent : 2 } ) )
70
+
55
71
// Group files by their discovered config
56
72
// { '.lintstagedrc.json': { config: {...}, files: [...] } }
57
73
const configGroups = { }
58
74
75
+ debugLog ( 'Searching config files...' )
76
+
59
77
const searchConfig = async ( cwd , files = [ ] ) => {
60
78
const { config, filepath } = await loadConfig ( { cwd } , logger )
61
- if ( ! config ) return
79
+ if ( ! config ) {
80
+ debugLog ( 'Found no config from "%s"!' , cwd )
81
+ return
82
+ }
62
83
63
84
if ( filepath in configGroups ) {
85
+ debugLog ( 'Found existing config "%s" from "%s"!' , filepath , cwd )
64
86
// Re-use cached config and skip validation
65
87
configGroups [ filepath ] . files . push ( ...files )
66
88
} else {
89
+ debugLog ( 'Found new config "%s" from "%s"!' , filepath , cwd )
90
+
67
91
const validatedConfig = validateConfig ( config , filepath , logger )
68
92
configGroups [ filepath ] = { config : validatedConfig , files }
69
93
}
@@ -77,9 +101,12 @@ export const getConfigGroups = async (
77
101
78
102
// Throw if no configurations were found
79
103
if ( Object . keys ( configGroups ) . length === 0 ) {
104
+ debugLog ( 'Found no config groups!' )
80
105
logger . error ( `${ ConfigNotFoundError . message } .` )
81
106
throw ConfigNotFoundError
82
107
}
83
108
109
+ debugLog ( 'Grouped staged files into %d groups!' , Object . keys ( configGroups ) . length )
110
+
84
111
return configGroups
85
112
}
0 commit comments