1
1
import { pathToFileURL } from 'url'
2
2
3
+ import debug from 'debug'
3
4
import { lilconfig } from 'lilconfig'
4
5
import YAML from 'yaml'
5
6
7
+ const debugLog = debug ( 'lint-staged:loadConfig' )
8
+
6
9
/**
7
10
* The list of files `lint-staged` will read configuration
8
11
* from, in the declared order.
@@ -22,13 +25,7 @@ const searchPlaces = [
22
25
]
23
26
24
27
/** exported for tests */
25
- export const dynamicImport = ( path ) =>
26
- import ( pathToFileURL ( path ) )
27
- . then ( ( module ) => module . default )
28
- . catch ( ( error ) => {
29
- console . error ( error )
30
- throw error
31
- } )
28
+ export const dynamicImport = ( path ) => import ( pathToFileURL ( path ) ) . then ( ( module ) => module . default )
32
29
33
30
const jsonParse = ( path , content ) => JSON . parse ( content )
34
31
@@ -60,14 +57,30 @@ const resolveConfig = (configPath) => {
60
57
61
58
/**
62
59
* @param {string } [configPath]
60
+ * @param {Logger } [logger]
63
61
*/
64
- export const loadConfig = async ( configPath ) => {
65
- const explorer = lilconfig ( 'lint-staged' , { searchPlaces, loaders } )
66
- const result = await ( configPath ? explorer . load ( resolveConfig ( configPath ) ) : explorer . search ( ) )
67
- if ( ! result ) return null
62
+ export const loadConfig = async ( configPath , logger ) => {
63
+ try {
64
+ if ( configPath ) {
65
+ debugLog ( 'Loading configuration from `%s`...' , configPath )
66
+ } else {
67
+ debugLog ( 'Searching for configuration...' )
68
+ }
69
+
70
+ const explorer = lilconfig ( 'lint-staged' , { searchPlaces, loaders } )
68
71
69
- const { config, filepath } = result
72
+ const result = await ( configPath ? explorer . load ( resolveConfig ( configPath ) ) : explorer . search ( ) )
73
+ if ( ! result ) return null
70
74
71
- // config is a promise when using the `dynamicImport` loader
72
- return { config : await config , filepath }
75
+ // config is a promise when using the `dynamicImport` loader
76
+ const config = await result . config
77
+
78
+ debugLog ( 'Successfully loaded config from `%s`:\n%O' , result . filepath , config )
79
+
80
+ return config
81
+ } catch ( error ) {
82
+ debugLog ( 'Failed to load configuration from `%s`' , configPath )
83
+ logger . error ( error )
84
+ return null
85
+ }
73
86
}
0 commit comments