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