1 |
|
2 |
|
3 | import { basename, join } from 'path'
|
4 |
|
5 | import debug from 'debug'
|
6 | import normalize from 'normalize-path'
|
7 |
|
8 | import { execGit } from './execGit.js'
|
9 | import { loadConfig, searchPlaces } from './loadConfig.js'
|
10 | import { parseGitZOutput } from './parseGitZOutput.js'
|
11 | import { validateConfig } from './validateConfig.js'
|
12 |
|
13 | const debugLog = debug('lint-staged:searchConfigs')
|
14 |
|
15 | const EXEC_GIT = ['ls-files', '-z', '--full-name']
|
16 |
|
17 | const filterPossibleConfigFiles = (files) =>
|
18 | files.filter((file) => searchPlaces.includes(basename(file)))
|
19 |
|
20 | const numberOfLevels = (file) => file.split('/').length
|
21 |
|
22 | const sortDeepestParth = (a, b) => (numberOfLevels(a) > numberOfLevels(b) ? -1 : 1)
|
23 |
|
24 | const isInsideDirectory = (dir) => (file) => file.startsWith(normalize(dir))
|
25 |
|
26 | export const ConfigObjectSymbol = Symbol()
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 | export const searchConfigs = async (
|
40 | { configObject, configPath, cwd = process.cwd(), gitDir = cwd },
|
41 | logger
|
42 | ) => {
|
43 | debugLog('Searching for configuration files...')
|
44 |
|
45 |
|
46 | if (configObject) {
|
47 | debugLog('Using single direct configuration object...')
|
48 |
|
49 | return { [ConfigObjectSymbol]: validateConfig(configObject, 'config object', logger) }
|
50 | }
|
51 |
|
52 |
|
53 | if (configPath) {
|
54 | debugLog('Using single configuration path...')
|
55 |
|
56 | const { config, filepath } = await loadConfig({ configPath }, logger)
|
57 |
|
58 | if (!config) return {}
|
59 | return { [configPath]: validateConfig(config, filepath, logger) }
|
60 | }
|
61 |
|
62 | const [cachedFiles, otherFiles] = await Promise.all([
|
63 |
|
64 | execGit(EXEC_GIT, { cwd: gitDir }).then(parseGitZOutput).then(filterPossibleConfigFiles),
|
65 |
|
66 | execGit([...EXEC_GIT, '--others', '--exclude-standard'], { cwd: gitDir })
|
67 | .then(parseGitZOutput)
|
68 | .then(filterPossibleConfigFiles),
|
69 | ])
|
70 |
|
71 |
|
72 | const possibleConfigFiles = [...cachedFiles, ...otherFiles]
|
73 | .map((file) => normalize(join(gitDir, file)))
|
74 | .filter(isInsideDirectory(cwd))
|
75 | .sort(sortDeepestParth)
|
76 |
|
77 | debugLog('Found possible config files:', possibleConfigFiles)
|
78 |
|
79 |
|
80 | const configs = possibleConfigFiles.reduce(
|
81 | (acc, configPath) => Object.assign(acc, { [configPath]: null }),
|
82 | {}
|
83 | )
|
84 |
|
85 |
|
86 | await Promise.all(
|
87 | Object.keys(configs).map((configPath) =>
|
88 | loadConfig({ configPath }, logger).then(({ config, filepath }) => {
|
89 | if (config) {
|
90 | if (configPath !== filepath) {
|
91 | debugLog('Config file "%s" resolved to "%s"', configPath, filepath)
|
92 | }
|
93 |
|
94 | configs[configPath] = validateConfig(config, filepath, logger)
|
95 | }
|
96 | })
|
97 | )
|
98 | )
|
99 |
|
100 |
|
101 | const foundConfigs = Object.entries(configs)
|
102 | .filter(([, value]) => !!value)
|
103 | .reduce((acc, [key, value]) => ({ ...acc, [key]: value }), {})
|
104 |
|
105 | |
106 |
|
107 |
|
108 |
|
109 | if (!Object.keys(foundConfigs).length) {
|
110 | debugLog('Could not find config files inside "%s"', cwd)
|
111 |
|
112 | const { config, filepath } = await loadConfig({ cwd }, logger)
|
113 | if (config) {
|
114 | debugLog('Found parent configuration file from "%s"', filepath)
|
115 |
|
116 | foundConfigs[filepath] = validateConfig(config, filepath, logger)
|
117 | } else {
|
118 | debugLog('Could not find parent configuration files from "%s"', cwd)
|
119 | }
|
120 | }
|
121 |
|
122 | debugLog('Found %d config files', Object.keys(foundConfigs).length)
|
123 |
|
124 | return foundConfigs
|
125 | }
|