Skip to content

Commit

Permalink
fix: search configuration starting from explicit cwd option
Browse files Browse the repository at this point in the history
  • Loading branch information
iiroj committed Jan 2, 2022
1 parent 5cceeb6 commit c7ea359
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lib/index.js
Expand Up @@ -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}.`)
Expand Down
17 changes: 11 additions & 6 deletions lib/loadConfig.js
@@ -1,3 +1,5 @@
/** @typedef {import('./index').Logger} Logger */

import { pathToFileURL } from 'url'

import debug from 'debug'
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand Down

0 comments on commit c7ea359

Please sign in to comment.