Skip to content

Commit

Permalink
refactor: extract loadConfig into separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
iiroj committed Nov 21, 2021
1 parent 8d3b176 commit bb0030a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 32 deletions.
33 changes: 1 addition & 32 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { cosmiconfig } from 'cosmiconfig'
import debug from 'debug'
import inspect from 'object-inspect'

import { loadConfig } from './loadConfig.js'
import { PREVENTED_EMPTY_COMMIT, GIT_ERROR, RESTORE_STASH_EXAMPLE } from './messages.js'
import { printTaskOutput } from './printTaskOutput.js'
import { runAll } from './runAll.js'
Expand All @@ -16,37 +16,6 @@ import { validateOptions } from './validateOptions.js'

const debugLog = debug('lint-staged')

const resolveConfig = (configPath) => {
try {
return require.resolve(configPath)
} catch {
return configPath
}
}

const loadConfig = (configPath) => {
const explorer = cosmiconfig('lint-staged', {
searchPlaces: [
'package.json',
'.lintstagedrc',
'.lintstagedrc.json',
'.lintstagedrc.yaml',
'.lintstagedrc.yml',
'.lintstagedrc.mjs',
'.lintstagedrc.js',
'.lintstagedrc.cjs',
'lint-staged.config.mjs',
'lint-staged.config.js',
'lint-staged.config.cjs',
],
loaders: {
'.mjs': (path) => import(path).then((module) => module.default),
},
})

return configPath ? explorer.load(resolveConfig(configPath)) : explorer.search()
}

/**
* @typedef {(...any) => void} LogFunction
* @typedef {{ error: LogFunction, log: LogFunction, warn: LogFunction }} Logger
Expand Down
37 changes: 37 additions & 0 deletions lib/loadConfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { cosmiconfig } from 'cosmiconfig'

const dynamicImport = (path) => import(path).then((module) => module.default)

const resolveConfig = (configPath) => {
try {
return require.resolve(configPath)
} catch {
return configPath
}
}

/**
* @param {string} [configPath]
*/
export const loadConfig = (configPath) => {
const explorer = cosmiconfig('lint-staged', {
searchPlaces: [
'package.json',
'.lintstagedrc',
'.lintstagedrc.json',
'.lintstagedrc.yaml',
'.lintstagedrc.yml',
'.lintstagedrc.mjs',
'.lintstagedrc.js',
'.lintstagedrc.cjs',
'lint-staged.config.mjs',
'lint-staged.config.js',
'lint-staged.config.cjs',
],
loaders: {
'.mjs': dynamicImport,
},
})

return configPath ? explorer.load(resolveConfig(configPath)) : explorer.search()
}

0 comments on commit bb0030a

Please sign in to comment.