1 | import { lilconfig } from 'lilconfig'
|
2 | import YAML from 'yaml'
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | const searchPlaces = [
|
9 | 'package.json',
|
10 | '.lintstagedrc',
|
11 | '.lintstagedrc.json',
|
12 | '.lintstagedrc.yaml',
|
13 | '.lintstagedrc.yml',
|
14 | '.lintstagedrc.mjs',
|
15 | '.lintstagedrc.js',
|
16 | '.lintstagedrc.cjs',
|
17 | 'lint-staged.config.mjs',
|
18 | 'lint-staged.config.js',
|
19 | 'lint-staged.config.cjs',
|
20 | ]
|
21 |
|
22 |
|
23 | export const dynamicImport = (path) =>
|
24 | import(path)
|
25 | .then((module) => module.default)
|
26 | .catch((error) => {
|
27 | console.error(error)
|
28 | throw error
|
29 | })
|
30 |
|
31 | const jsonParse = (path, content) => JSON.parse(content)
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 | const loaders = {
|
40 | '.js': dynamicImport,
|
41 | '.json': jsonParse,
|
42 | '.mjs': dynamicImport,
|
43 | '.cjs': dynamicImport,
|
44 | '.yaml': YAML.parse,
|
45 | '.yml': YAML.parse,
|
46 | noExt: YAML.parse,
|
47 | }
|
48 |
|
49 | const resolveConfig = (configPath) => {
|
50 | try {
|
51 | return require.resolve(configPath)
|
52 | } catch {
|
53 | return configPath
|
54 | }
|
55 | }
|
56 |
|
57 |
|
58 |
|
59 |
|
60 | export const loadConfig = (configPath) => {
|
61 | const explorer = lilconfig('lint-staged', { searchPlaces, loaders })
|
62 | return configPath ? explorer.load(resolveConfig(configPath)) : explorer.search()
|
63 | }
|