From c7ea3594c81f7c2724a7babc8e8d57926b4679c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iiro=20J=C3=A4ppinen?= Date: Sun, 2 Jan 2022 18:39:30 +0200 Subject: [PATCH] fix: search configuration starting from explicit cwd option --- lib/index.js | 2 +- lib/loadConfig.js | 17 +++++++++++------ 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/lib/index.js b/lib/index.js index 9f649d4fd..4048b444d 100644 --- a/lib/index.js +++ b/lib/index.js @@ -58,7 +58,7 @@ const lintStaged = async ( ) => { await validateOptions({ shell }, logger) - const inputConfig = configObject || (await loadConfig(configPath, logger)) + const inputConfig = configObject || (await loadConfig({ configPath, cwd }, logger)) if (!inputConfig) { logger.error(`${ConfigNotFoundError.message}.`) diff --git a/lib/loadConfig.js b/lib/loadConfig.js index 3e4b1b6b2..76493447d 100644 --- a/lib/loadConfig.js +++ b/lib/loadConfig.js @@ -1,3 +1,5 @@ +/** @typedef {import('./index').Logger} Logger */ + import { pathToFileURL } from 'url' import debug from 'debug' @@ -56,20 +58,23 @@ const resolveConfig = (configPath) => { } /** - * @param {string} [configPath] - * @param {Logger} [logger] + * @param {object} options + * @param {string} [options.configPath] - Explicit path to a config file + * @param {string} [options.cwd] - Current working directory */ -export const loadConfig = async (configPath, logger) => { +export const loadConfig = async ({ configPath, cwd }, logger) => { try { if (configPath) { debugLog('Loading configuration from `%s`...', configPath) } else { - debugLog('Searching for configuration...') + debugLog('Searching for configuration from `%s`...', cwd) } const explorer = lilconfig('lint-staged', { searchPlaces, loaders }) - const result = await (configPath ? explorer.load(resolveConfig(configPath)) : explorer.search()) + const result = await (configPath + ? explorer.load(resolveConfig(configPath)) + : explorer.search(cwd)) if (!result) return null // config is a promise when using the `dynamicImport` loader @@ -79,7 +84,7 @@ export const loadConfig = async (configPath, logger) => { return config } catch (error) { - debugLog('Failed to load configuration from `%s`', configPath) + debugLog('Failed to load configuration!') logger.error(error) return null }