11import { pathToFileURL } from 'url'
22
3+ import debug from 'debug'
34import { lilconfig } from 'lilconfig'
45import YAML from 'yaml'
56
7+ const debugLog = debug ( 'lint-staged:loadConfig' )
8+
69/**
710 * The list of files `lint-staged` will read configuration
811 * from, in the declared order.
@@ -22,13 +25,7 @@ const searchPlaces = [
2225]
2326
2427/** 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 )
3229
3330const jsonParse = ( path , content ) => JSON . parse ( content )
3431
@@ -60,14 +57,30 @@ const resolveConfig = (configPath) => {
6057
6158/**
6259 * @param {string } [configPath]
60+ * @param {Logger } [logger]
6361 */
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 } )
6871
69- const { config, filepath } = result
72+ const result = await ( configPath ? explorer . load ( resolveConfig ( configPath ) ) : explorer . search ( ) )
73+ if ( ! result ) return null
7074
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+ }
7386}
0 commit comments