-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathread-rc-file.ts
More file actions
72 lines (63 loc) · 1.89 KB
/
Copy pathread-rc-file.ts
File metadata and controls
72 lines (63 loc) · 1.89 KB
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
import ansis from 'ansis';
import path from 'node:path';
import {
CONFIG_FILE_NAME,
type CoreConfig,
SUPPORTED_CONFIG_FILE_FORMATS,
coreConfigSchema,
validate,
} from '@code-pushup/models';
import { fileExists, importModule, logger } from '@code-pushup/utils';
export async function readRcByPath(
filePath: string,
tsconfig?: string,
): Promise<CoreConfig> {
const formattedTarget = [
`${ansis.bold(path.relative(process.cwd(), filePath))}`,
tsconfig &&
`(paths from ${ansis.bold(path.relative(process.cwd(), tsconfig))})`,
]
.filter(Boolean)
.join(' ');
const value = await logger.task(
`Importing config from ${formattedTarget}`,
async () => {
const result = await importModule({
filepath: filePath,
tsconfig,
});
return { result, message: `Imported config from ${formattedTarget}` };
},
);
const config = validate(coreConfigSchema, value, { filePath });
logger.info('Configuration is valid ✓');
return config;
}
export async function autoloadRc(tsconfig?: string): Promise<CoreConfig> {
const configFilePatterns = [
CONFIG_FILE_NAME,
`{${SUPPORTED_CONFIG_FILE_FORMATS.join(',')}}`,
].join('.');
logger.debug(`Looking for default config file ${configFilePatterns}`);
// eslint-disable-next-line functional/no-let
let ext = '';
// eslint-disable-next-line functional/no-loop-statements
for (const extension of SUPPORTED_CONFIG_FILE_FORMATS) {
const filePath = `${CONFIG_FILE_NAME}.${extension}`;
const exists = await fileExists(filePath);
if (exists) {
logger.debug(`Found default config file ${ansis.bold(filePath)}`);
ext = extension;
break;
}
}
if (!ext) {
throw new Error(
`No ${configFilePatterns} file present in ${process.cwd()}`,
);
}
return readRcByPath(
path.join(process.cwd(), `${CONFIG_FILE_NAME}.${ext}`),
tsconfig,
);
}