From e7b6412fb128f314346e28329c17a676cf691135 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iiro=20J=C3=A4ppinen?= Date: Mon, 22 Nov 2021 08:19:57 +0200 Subject: [PATCH] fix: improve error logging in loadConfig --- lib/index.js | 12 +++--------- lib/loadConfig.js | 41 +++++++++++++++++++++++++++-------------- test/index.spec.js | 5 +++-- test/loadConfig.spec.js | 12 ------------ 4 files changed, 33 insertions(+), 37 deletions(-) diff --git a/lib/index.js b/lib/index.js index 838489892..9f649d4fd 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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`. diff --git a/lib/loadConfig.js b/lib/loadConfig.js index af2286235..3e4b1b6b2 100644 --- a/lib/loadConfig.js +++ b/lib/loadConfig.js @@ -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. @@ -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) @@ -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 + } } diff --git a/test/index.spec.js b/test/index.spec.js index 5c1ed8ea2..004ef7444 100644 --- a/test/index.spec.js +++ b/test/index.spec.js @@ -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' @@ -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') }) }) diff --git a/test/loadConfig.spec.js b/test/loadConfig.spec.js index ea3a51ba1..33272e610 100644 --- a/test/loadConfig.spec.js +++ b/test/loadConfig.spec.js @@ -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`) })