forked from renovatebot/renovate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.ts
114 lines (103 loc) · 3.28 KB
/
file.ts
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
import is from '@sindresorhus/is';
import fs from 'fs-extra';
import { load } from 'js-yaml';
import JSON5 from 'json5';
import upath from 'upath';
import { migrateConfig } from '../../../../config/migration';
import type { AllConfig, RenovateConfig } from '../../../../config/types';
import { logger } from '../../../../logger';
import { readSystemFile } from '../../../../util/fs';
export async function getParsedContent(file: string): Promise<RenovateConfig> {
switch (upath.extname(file)) {
case '.yaml':
case '.yml':
return load(await readSystemFile(file, 'utf8'), {
json: true,
}) as RenovateConfig;
case '.json5':
case '.json':
return JSON5.parse(await readSystemFile(file, 'utf8'));
case '.js': {
const tmpConfig = await import(file);
let config = tmpConfig.default ? tmpConfig.default : tmpConfig;
// Allow the config to be a function
if (is.function_(config)) {
config = config();
}
return config;
}
default:
throw new Error('Unsupported file type');
}
}
export async function getConfig(env: NodeJS.ProcessEnv): Promise<AllConfig> {
let configFile = env.RENOVATE_CONFIG_FILE ?? 'config.js';
if (!upath.isAbsolute(configFile)) {
configFile = `${process.cwd()}/${configFile}`;
}
if (env.RENOVATE_CONFIG_FILE && !(await fs.pathExists(configFile))) {
logger.fatal(
{ configFile },
`Custom config file specified in RENOVATE_CONFIG_FILE must exist`
);
process.exit(1);
}
logger.debug('Checking for config file in ' + configFile);
let config: AllConfig = {};
try {
config = await getParsedContent(configFile);
} catch (err) {
// istanbul ignore if
if (err instanceof SyntaxError || err instanceof TypeError) {
logger.fatal(`Could not parse config file \n ${err.stack!}`);
process.exit(1);
} else if (err instanceof ReferenceError) {
logger.fatal(
`Error parsing config file due to unresolved variable(s): ${err.message}`
);
process.exit(1);
} else if (err.message === 'Unsupported file type') {
logger.fatal(err.message);
process.exit(1);
} else if (env.RENOVATE_CONFIG_FILE) {
logger.fatal('No custom config file found on disk');
process.exit(1);
} else {
// istanbul ignore next: we can ignore this
logger.debug('No config file found on disk - skipping');
}
}
if (
env.RENOVATE_CONFIG_FILE &&
env.RENOVATE_X_DELETE_CONFIG_FILE === 'true'
) {
await deleteConfigFile(env);
}
const { isMigrated, migratedConfig } = migrateConfig(config);
if (isMigrated) {
logger.warn(
{ originalConfig: config, migratedConfig },
'Config needs migrating'
);
config = migratedConfig;
}
return config;
}
export async function deleteConfigFile(env: NodeJS.ProcessEnv): Promise<void> {
const configFile = env.RENOVATE_CONFIG_FILE;
if (is.undefined(configFile) || is.emptyStringOrWhitespace(configFile)) {
return;
}
if (!(await fs.pathExists(configFile))) {
return;
}
if (env.RENOVATE_X_DELETE_CONFIG_FILE !== 'true') {
return;
}
try {
await fs.remove(configFile);
logger.trace({ path: configFile }, 'config file successfully deleted');
} catch (err) {
logger.warn({ err }, 'error deleting config file');
}
}