-
-
Notifications
You must be signed in to change notification settings - Fork 412
/
loadConfig.js
57 lines (50 loc) Β· 1.62 KB
/
loadConfig.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { isAbsolute } from 'path'
import { lilconfig } from 'lilconfig'
import YAML from 'yaml'
/**
* The list of files `lint-staged` will read configuration
* from, in the declared order.
*/
const searchPlaces = [
'package.json',
'.lintstagedrc',
'.lintstagedrc.json',
'.lintstagedrc.yaml',
'.lintstagedrc.yml',
'.lintstagedrc.js',
'.lintstagedrc.cjs',
'lint-staged.config.js',
'lint-staged.config.cjs',
]
/**
* `lilconfig` doesn't support yaml files by default,
* so we add custom loaders for those. Files without
* an extensions are assumed to be yaml β this
* assumption is in `cosmiconfig` as well.
*/
const loaders = { '.yml': YAML.parse, '.yaml': YAML.parse, noExt: YAML.parse }
/**
* Load and parse config from config file, with optional path.
* @param {string} [configPath]
* @returns {Promise<{ config: *; filepath: string } | null>} config
*/
const loadConfig = async (configPath) => {
const isRelativeConfigPath = configPath && !isAbsolute(configPath)
/* Try import to relative filepath as an ESM Module */
if (isRelativeConfigPath) {
try {
const { default: config } = await import(configPath)
return { config, filepath: configPath }
} catch {
/**
* We can safely ignore this error, because it probably means
* the module configPath didn't point to a module and import()
* threw a `MODULE_NOT_FOUND` error. Continue to load it as a
* relative path instead.
*/
}
}
const explorer = lilconfig('lint-staged', { searchPlaces, loaders })
return configPath ? explorer.load(configPath) : explorer.search()
}
export default loadConfig