-
-
Notifications
You must be signed in to change notification settings - Fork 412
/
loadConfig.js
143 lines (119 loc) Β· 3.74 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/** @typedef {import('./index').Logger} Logger */
import fs from 'node:fs/promises'
import path from 'node:path'
import debug from 'debug'
import YAML from 'yaml'
import { dynamicImport } from './dynamicImport.js'
import { resolveConfig } from './resolveConfig.js'
const debugLog = debug('lint-staged:loadConfig')
const CONFIG_NAME = 'lint-staged'
const PACKAGE_JSON_FILE = 'package.json'
const PACKAGE_YAML_FILES = ['package.yaml', 'package.yml']
/**
* The list of files `lint-staged` will read configuration
* from, in the declared order.
*/
export const searchPlaces = [
PACKAGE_JSON_FILE,
...PACKAGE_YAML_FILES,
'.lintstagedrc',
'.lintstagedrc.json',
'.lintstagedrc.yaml',
'.lintstagedrc.yml',
'.lintstagedrc.mjs',
'.lintstagedrc.js',
'.lintstagedrc.cjs',
'lint-staged.config.mjs',
'lint-staged.config.js',
'lint-staged.config.cjs',
]
const jsonParse = (filePath, content) => {
const isPackageFile = PACKAGE_JSON_FILE.includes(path.basename(filePath))
try {
const json = JSON.parse(content)
return isPackageFile ? json[CONFIG_NAME] : json
} catch (error) {
if (path.basename(filePath) === PACKAGE_JSON_FILE) {
debugLog('Ignoring invalid package file `%s` with content:\n%s', filePath, content)
return undefined
}
throw error
}
}
const yamlParse = (filePath, content) => {
const isPackageFile = PACKAGE_YAML_FILES.includes(path.basename(filePath))
try {
const yaml = YAML.parse(content)
return isPackageFile ? yaml[CONFIG_NAME] : yaml
} catch (error) {
if (isPackageFile) {
debugLog('Ignoring invalid package file `%s` with content:\n%s', filePath, content)
return undefined
}
throw error
}
}
const NO_EXT = 'noExt'
/**
* `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 = {
'.js': dynamicImport,
'.json': jsonParse,
'.mjs': dynamicImport,
'.cjs': dynamicImport,
'.yaml': yamlParse,
'.yml': yamlParse,
[NO_EXT]: yamlParse,
}
const readFile = async (filepath) => {
const absolutePath = path.resolve(filepath)
const file = await fs.readFile(absolutePath)
return await file.toString()
}
const loadConfigByExt = async (filepath) => {
filepath = path.resolve(filepath)
const ext = path.extname(filepath) || NO_EXT
const loader = loaders[ext]
/**
* No need to read file contents when loader only takes in the filepath argument
* and reads itself; this is for `lilconfig` compatibility
*/
const content = loader.length > 1 ? await readFile(filepath) : undefined
return {
config: await loader(filepath, content),
filepath,
}
}
/**
* @param {object} options
* @param {string} [options.configPath] - Explicit path to a config file
* @param {string} [options.cwd] - Current working directory
*/
export const loadConfig = async ({ configPath, cwd }, logger) => {
try {
let result
if (configPath) {
debugLog('Loading configuration from `%s`...', configPath)
result = await loadConfigByExt(resolveConfig(configPath))
} else {
debugLog('Searching for configuration from `%s`...', cwd)
const { lilconfig } = await import('lilconfig')
const explorer = lilconfig(CONFIG_NAME, { searchPlaces, loaders })
result = await explorer.search(cwd)
}
if (!result) return {}
// config is a promise when using the `dynamicImport` loader
const config = (await result.config) ?? null
const filepath = result.filepath
debugLog('Successfully loaded config from `%s`:\n%O', filepath, config)
return { config, filepath }
} catch (error) {
debugLog('Failed to load configuration!')
logger.error(error)
return {}
}
}