Skip to content

Commit e7b6412

Browse files
committedNov 22, 2021
fix: improve error logging in loadConfig
1 parent 0082ec2 commit e7b6412

File tree

4 files changed

+33
-37
lines changed

4 files changed

+33
-37
lines changed
 

‎lib/index.js

+3-9
Original file line numberDiff line numberDiff line change
@@ -58,20 +58,14 @@ const lintStaged = async (
5858
) => {
5959
await validateOptions({ shell }, logger)
6060

61-
debugLog('Loading config using `lilconfig`')
61+
const inputConfig = configObject || (await loadConfig(configPath, logger))
6262

63-
const resolved = configObject
64-
? { config: configObject, filepath: '(input)' }
65-
: await loadConfig(configPath)
66-
67-
if (!resolved) {
63+
if (!inputConfig) {
6864
logger.error(`${ConfigNotFoundError.message}.`)
6965
throw ConfigNotFoundError
7066
}
7167

72-
debugLog('Successfully loaded config from `%s`:\n%O', resolved.filepath, resolved.config)
73-
74-
const config = validateConfig(resolved.config, logger)
68+
const config = validateConfig(inputConfig, logger)
7569

7670
if (debug) {
7771
// Log using logger to be able to test through `consolemock`.

‎lib/loadConfig.js

+27-14
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import { pathToFileURL } from 'url'
22

3+
import debug from 'debug'
34
import { lilconfig } from 'lilconfig'
45
import 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

3330
const 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
}

‎test/index.spec.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ describe('lintStaged', () => {
327327
})
328328

329329
it('should print helpful error message when explicit config file is not found', async () => {
330-
expect.assertions(2)
330+
expect.assertions(3)
331331

332332
const nonExistentConfig = 'fake-config-file.yml'
333333

@@ -343,6 +343,7 @@ describe('lintStaged', () => {
343343
lintStaged({ configPath: nonExistentConfig, quiet: true }, logger)
344344
).rejects.toThrowError()
345345

346-
expect(logger.printHistory()).toMatchInlineSnapshot(`""`)
346+
expect(logger.printHistory()).toMatch('ENOENT')
347+
expect(logger.printHistory()).toMatch('Configuration could not be found')
347348
})
348349
})

‎test/loadConfig.spec.js

-12
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,6 @@
1-
import makeConsoleMock from 'consolemock'
2-
31
import { dynamicImport } from '../lib/loadConfig.js'
42

53
describe('dynamicImport', () => {
6-
const globalConsoleTemp = console
7-
8-
beforeEach(() => {
9-
console = makeConsoleMock()
10-
})
11-
12-
afterAll(() => {
13-
console = globalConsoleTemp
14-
})
15-
164
it('should log errors into console', () => {
175
expect(() => dynamicImport('not-found.js')).rejects.toThrowError(`Cannot find module`)
186
})

0 commit comments

Comments
 (0)
Please sign in to comment.