Skip to content

Commit

Permalink
fix: improve error logging in loadConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
iiroj committed Nov 22, 2021
1 parent 0082ec2 commit e7b6412
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 37 deletions.
12 changes: 3 additions & 9 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,14 @@ const lintStaged = async (
) => {
await validateOptions({ shell }, logger)

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

const resolved = configObject
? { config: configObject, filepath: '(input)' }
: await loadConfig(configPath)

if (!resolved) {
if (!inputConfig) {
logger.error(`${ConfigNotFoundError.message}.`)
throw ConfigNotFoundError
}

debugLog('Successfully loaded config from `%s`:\n%O', resolved.filepath, resolved.config)

const config = validateConfig(resolved.config, logger)
const config = validateConfig(inputConfig, logger)

if (debug) {
// Log using logger to be able to test through `consolemock`.
Expand Down
41 changes: 27 additions & 14 deletions lib/loadConfig.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { pathToFileURL } from 'url'

import debug from 'debug'
import { lilconfig } from 'lilconfig'
import YAML from 'yaml'

const debugLog = debug('lint-staged:loadConfig')

/**
* The list of files `lint-staged` will read configuration
* from, in the declared order.
Expand All @@ -22,13 +25,7 @@ const searchPlaces = [
]

/** exported for tests */
export const dynamicImport = (path) =>
import(pathToFileURL(path))
.then((module) => module.default)
.catch((error) => {
console.error(error)
throw error
})
export const dynamicImport = (path) => import(pathToFileURL(path)).then((module) => module.default)

const jsonParse = (path, content) => JSON.parse(content)

Expand Down Expand Up @@ -60,14 +57,30 @@ const resolveConfig = (configPath) => {

/**
* @param {string} [configPath]
* @param {Logger} [logger]
*/
export const loadConfig = async (configPath) => {
const explorer = lilconfig('lint-staged', { searchPlaces, loaders })
const result = await (configPath ? explorer.load(resolveConfig(configPath)) : explorer.search())
if (!result) return null
export const loadConfig = async (configPath, logger) => {
try {
if (configPath) {
debugLog('Loading configuration from `%s`...', configPath)
} else {
debugLog('Searching for configuration...')
}

const explorer = lilconfig('lint-staged', { searchPlaces, loaders })

const { config, filepath } = result
const result = await (configPath ? explorer.load(resolveConfig(configPath)) : explorer.search())
if (!result) return null

// config is a promise when using the `dynamicImport` loader
return { config: await config, filepath }
// config is a promise when using the `dynamicImport` loader
const config = await result.config

debugLog('Successfully loaded config from `%s`:\n%O', result.filepath, config)

return config
} catch (error) {
debugLog('Failed to load configuration from `%s`', configPath)
logger.error(error)
return null
}
}
5 changes: 3 additions & 2 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ describe('lintStaged', () => {
})

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

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

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

expect(logger.printHistory()).toMatchInlineSnapshot(`""`)
expect(logger.printHistory()).toMatch('ENOENT')
expect(logger.printHistory()).toMatch('Configuration could not be found')
})
})
12 changes: 0 additions & 12 deletions test/loadConfig.spec.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,6 @@
import makeConsoleMock from 'consolemock'

import { dynamicImport } from '../lib/loadConfig.js'

describe('dynamicImport', () => {
const globalConsoleTemp = console

beforeEach(() => {
console = makeConsoleMock()
})

afterAll(() => {
console = globalConsoleTemp
})

it('should log errors into console', () => {
expect(() => dynamicImport('not-found.js')).rejects.toThrowError(`Cannot find module`)
})
Expand Down

0 comments on commit e7b6412

Please sign in to comment.