Skip to content

Commit bb0030a

Browse files
committed
refactor: extract loadConfig into separate file
1 parent 8d3b176 commit bb0030a

File tree

2 files changed

+38
-32
lines changed

2 files changed

+38
-32
lines changed

lib/index.js

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { cosmiconfig } from 'cosmiconfig'
21
import debug from 'debug'
32
import inspect from 'object-inspect'
43

4+
import { loadConfig } from './loadConfig.js'
55
import { PREVENTED_EMPTY_COMMIT, GIT_ERROR, RESTORE_STASH_EXAMPLE } from './messages.js'
66
import { printTaskOutput } from './printTaskOutput.js'
77
import { runAll } from './runAll.js'
@@ -16,37 +16,6 @@ import { validateOptions } from './validateOptions.js'
1616

1717
const debugLog = debug('lint-staged')
1818

19-
const resolveConfig = (configPath) => {
20-
try {
21-
return require.resolve(configPath)
22-
} catch {
23-
return configPath
24-
}
25-
}
26-
27-
const loadConfig = (configPath) => {
28-
const explorer = cosmiconfig('lint-staged', {
29-
searchPlaces: [
30-
'package.json',
31-
'.lintstagedrc',
32-
'.lintstagedrc.json',
33-
'.lintstagedrc.yaml',
34-
'.lintstagedrc.yml',
35-
'.lintstagedrc.mjs',
36-
'.lintstagedrc.js',
37-
'.lintstagedrc.cjs',
38-
'lint-staged.config.mjs',
39-
'lint-staged.config.js',
40-
'lint-staged.config.cjs',
41-
],
42-
loaders: {
43-
'.mjs': (path) => import(path).then((module) => module.default),
44-
},
45-
})
46-
47-
return configPath ? explorer.load(resolveConfig(configPath)) : explorer.search()
48-
}
49-
5019
/**
5120
* @typedef {(...any) => void} LogFunction
5221
* @typedef {{ error: LogFunction, log: LogFunction, warn: LogFunction }} Logger

lib/loadConfig.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { cosmiconfig } from 'cosmiconfig'
2+
3+
const dynamicImport = (path) => import(path).then((module) => module.default)
4+
5+
const resolveConfig = (configPath) => {
6+
try {
7+
return require.resolve(configPath)
8+
} catch {
9+
return configPath
10+
}
11+
}
12+
13+
/**
14+
* @param {string} [configPath]
15+
*/
16+
export const loadConfig = (configPath) => {
17+
const explorer = cosmiconfig('lint-staged', {
18+
searchPlaces: [
19+
'package.json',
20+
'.lintstagedrc',
21+
'.lintstagedrc.json',
22+
'.lintstagedrc.yaml',
23+
'.lintstagedrc.yml',
24+
'.lintstagedrc.mjs',
25+
'.lintstagedrc.js',
26+
'.lintstagedrc.cjs',
27+
'lint-staged.config.mjs',
28+
'lint-staged.config.js',
29+
'lint-staged.config.cjs',
30+
],
31+
loaders: {
32+
'.mjs': dynamicImport,
33+
},
34+
})
35+
36+
return configPath ? explorer.load(resolveConfig(configPath)) : explorer.search()
37+
}

0 commit comments

Comments
 (0)