-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathgetUserConfig.js
53 lines (47 loc) · 1.42 KB
/
getUserConfig.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
import {cosmiconfig} from 'cosmiconfig';
import {getPkg} from './getPkg.js';
import {getUserPkg} from './getUserPkg.js';
const moduleName = getPkg().name;
const explorer = cosmiconfig(moduleName, {
packageProp: 'better-scripts',
searchPlaces: [
'package.json',
'scripts.json',
`${moduleName}.json`,
`.${moduleName}rc`,
`.${moduleName}rc.json`,
`.${moduleName}rc.yaml`,
`.${moduleName}rc.yml`,
`.${moduleName}rc.js`,
`.${moduleName}rc.cjs`,
`${moduleName}.config.js`,
`${moduleName}.config.cjs`
]
});
/** Get user config object (haven't parsed yet)
* @param {string} configPath Custom specified config path
* @returns {Promise<import('./types').UserConfig>}
*/
export async function getUserConfig(configPath) {
const result = await (configPath
? explorer.load(configPath).catch(() => {
throw new Error(`Config file "${configPath}" doesn't exist`);
})
: explorer.search());
const userPkg = getUserPkg();
// falsy
if (!result?.config && !userPkg?.scripts) {
throw new Error(
'Config validation failed, you probably forgot to write ”better-scripts“ in package.json or "scripts.json" config file'
);
}
// empty
if (
result &&
(result?.isEmpty || !Object.keys(result.config).length) &&
!userPkg?.scripts
) {
throw new Error('Config validation failed, your config is empty');
}
return result?.config ?? userPkg.scripts;
}