1 |
|
2 |
|
3 | import { basename, join } from 'path'
|
4 |
|
5 | import normalize from 'normalize-path'
|
6 |
|
7 | import { execGit } from './execGit.js'
|
8 | import { loadConfig, searchPlaces } from './loadConfig.js'
|
9 | import { parseGitZOutput } from './parseGitZOutput.js'
|
10 | import { validateConfig } from './validateConfig.js'
|
11 |
|
12 | const EXEC_GIT = ['ls-files', '-z', '--full-name']
|
13 |
|
14 | const filterPossibleConfigFiles = (file) => searchPlaces.includes(basename(file))
|
15 |
|
16 | const numberOfLevels = (file) => file.split('/').length
|
17 |
|
18 | const sortDeepestParth = (a, b) => (numberOfLevels(a) > numberOfLevels(b) ? -1 : 1)
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | export const searchConfigs = async (gitDir = process.cwd(), logger) => {
|
28 |
|
29 | const cachedFiles = parseGitZOutput(await execGit(EXEC_GIT, { cwd: gitDir })).filter(
|
30 | filterPossibleConfigFiles
|
31 | )
|
32 |
|
33 |
|
34 | const otherFiles = parseGitZOutput(
|
35 | await execGit([...EXEC_GIT, '--others', '--exclude-standard'], { cwd: gitDir })
|
36 | ).filter(filterPossibleConfigFiles)
|
37 |
|
38 |
|
39 | const possibleConfigFiles = [...cachedFiles, ...otherFiles]
|
40 | .map((file) => join(gitDir, file))
|
41 | .map((file) => normalize(file))
|
42 | .sort(sortDeepestParth)
|
43 |
|
44 |
|
45 | const configs = possibleConfigFiles.reduce(
|
46 | (acc, configPath) => Object.assign(acc, { [configPath]: null }),
|
47 | {}
|
48 | )
|
49 |
|
50 |
|
51 | await Promise.all(
|
52 | possibleConfigFiles
|
53 | .map((configPath) => loadConfig({ configPath }, logger))
|
54 | .map((promise) =>
|
55 | promise.then(({ config, filepath }) => {
|
56 | if (config) {
|
57 | configs[filepath] = validateConfig(config, filepath, logger)
|
58 | }
|
59 | })
|
60 | )
|
61 | )
|
62 |
|
63 |
|
64 | const foundConfigs = Object.entries(configs)
|
65 | .filter(([, value]) => !!value)
|
66 | .reduce((acc, [key, value]) => ({ ...acc, [key]: value }), {})
|
67 |
|
68 | return foundConfigs
|
69 | }
|