mirror of
https://github.com/renovatebot/renovate.git
synced 2025-01-11 14:36:25 +00:00
refactor(config): use async readFile and dynamic import to load config file (#12649)
Co-authored-by: Michael Kriese <michael.kriese@visualon.de>
This commit is contained in:
parent
e042b917dc
commit
0f1256317c
4 changed files with 20 additions and 19 deletions
|
@ -1,7 +1,7 @@
|
||||||
#!/usr/bin/env node
|
#!/usr/bin/env node
|
||||||
// istanbul ignore file
|
// istanbul ignore file
|
||||||
import { dequal } from 'dequal';
|
import { dequal } from 'dequal';
|
||||||
import { readFileSync } from 'fs-extra';
|
import { readFile } from 'fs-extra';
|
||||||
import JSON5 from 'json5';
|
import JSON5 from 'json5';
|
||||||
import { configFileNames } from './config/app-strings';
|
import { configFileNames } from './config/app-strings';
|
||||||
import { massageConfig } from './config/massage';
|
import { massageConfig } from './config/massage';
|
||||||
|
@ -52,7 +52,7 @@ type PackageJson = {
|
||||||
(name) => name !== 'package.json'
|
(name) => name !== 'package.json'
|
||||||
)) {
|
)) {
|
||||||
try {
|
try {
|
||||||
const rawContent = readFileSync(file, 'utf8');
|
const rawContent = await readFile(file, 'utf8');
|
||||||
logger.info(`Validating ${file}`);
|
logger.info(`Validating ${file}`);
|
||||||
try {
|
try {
|
||||||
let jsonContent: RenovateConfig;
|
let jsonContent: RenovateConfig;
|
||||||
|
@ -72,7 +72,7 @@ type PackageJson = {
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
const pkgJson = JSON.parse(
|
const pkgJson = JSON.parse(
|
||||||
readFileSync('package.json', 'utf8')
|
await readFile('package.json', 'utf8')
|
||||||
) as PackageJson;
|
) as PackageJson;
|
||||||
if (pkgJson.renovate) {
|
if (pkgJson.renovate) {
|
||||||
logger.info(`Validating package.json > renovate`);
|
logger.info(`Validating package.json > renovate`);
|
||||||
|
@ -88,7 +88,7 @@ type PackageJson = {
|
||||||
// ignore
|
// ignore
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
const fileConfig = getFileConfig(process.env);
|
const fileConfig = await getFileConfig(process.env);
|
||||||
if (!dequal(fileConfig, {})) {
|
if (!dequal(fileConfig, {})) {
|
||||||
const file = process.env.RENOVATE_CONFIG_FILE ?? 'config.js';
|
const file = process.env.RENOVATE_CONFIG_FILE ?? 'config.js';
|
||||||
logger.info(`Validating ${file}`);
|
logger.info(`Validating ${file}`);
|
||||||
|
|
|
@ -16,24 +16,24 @@ describe('workers/global/config/parse/file', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('.getConfig()', () => {
|
describe('.getConfig()', () => {
|
||||||
it('parses custom config file', () => {
|
it('parses custom config file', async () => {
|
||||||
const configFile = upath.resolve(__dirname, './__fixtures__/file.js');
|
const configFile = upath.resolve(__dirname, './__fixtures__/file.js');
|
||||||
expect(file.getConfig({ RENOVATE_CONFIG_FILE: configFile })).toEqual(
|
expect(
|
||||||
customConfig
|
await file.getConfig({ RENOVATE_CONFIG_FILE: configFile })
|
||||||
);
|
).toEqual(customConfig);
|
||||||
});
|
});
|
||||||
it('migrates', () => {
|
it('migrates', async () => {
|
||||||
const configFile = upath.resolve(__dirname, './__fixtures__/file2.js');
|
const configFile = upath.resolve(__dirname, './__fixtures__/file2.js');
|
||||||
const res = file.getConfig({ RENOVATE_CONFIG_FILE: configFile });
|
const res = await file.getConfig({ RENOVATE_CONFIG_FILE: configFile });
|
||||||
expect(res).toMatchSnapshot();
|
expect(res).toMatchSnapshot();
|
||||||
expect(res.rangeStrategy).toBe('bump');
|
expect(res.rangeStrategy).toBe('bump');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('parse and returns empty config if there is no RENOVATE_CONFIG_FILE in env', () => {
|
it('parse and returns empty config if there is no RENOVATE_CONFIG_FILE in env', async () => {
|
||||||
expect(file.getConfig({})).toBeDefined();
|
expect(await file.getConfig({})).toBeDefined();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('fatal error and exit if error in parsing config.js', () => {
|
it('fatal error and exit if error in parsing config.js', async () => {
|
||||||
const mockProcessExit = jest
|
const mockProcessExit = jest
|
||||||
.spyOn(process, 'exit')
|
.spyOn(process, 'exit')
|
||||||
.mockImplementation(() => undefined as never);
|
.mockImplementation(() => undefined as never);
|
||||||
|
@ -50,19 +50,19 @@ describe('workers/global/config/parse/file', () => {
|
||||||
"repositories": [ "test/test" ],
|
"repositories": [ "test/test" ],
|
||||||
};`;
|
};`;
|
||||||
fs.writeFileSync(configFile, fileContent, { encoding: 'utf8' });
|
fs.writeFileSync(configFile, fileContent, { encoding: 'utf8' });
|
||||||
file.getConfig({ RENOVATE_CONFIG_FILE: configFile });
|
await file.getConfig({ RENOVATE_CONFIG_FILE: configFile });
|
||||||
expect(mockProcessExit).toHaveBeenCalledWith(1);
|
expect(mockProcessExit).toHaveBeenCalledWith(1);
|
||||||
|
|
||||||
fs.unlinkSync(configFile);
|
fs.unlinkSync(configFile);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('fatal error and exit if custom config file does not exist', () => {
|
it('fatal error and exit if custom config file does not exist', async () => {
|
||||||
const mockProcessExit = jest
|
const mockProcessExit = jest
|
||||||
.spyOn(process, 'exit')
|
.spyOn(process, 'exit')
|
||||||
.mockImplementation(() => undefined as never);
|
.mockImplementation(() => undefined as never);
|
||||||
|
|
||||||
const configFile = upath.resolve(tmp.path, './file4.js');
|
const configFile = upath.resolve(tmp.path, './file4.js');
|
||||||
file.getConfig({ RENOVATE_CONFIG_FILE: configFile });
|
await file.getConfig({ RENOVATE_CONFIG_FILE: configFile });
|
||||||
|
|
||||||
expect(mockProcessExit).toHaveBeenCalledWith(1);
|
expect(mockProcessExit).toHaveBeenCalledWith(1);
|
||||||
});
|
});
|
||||||
|
|
|
@ -3,7 +3,7 @@ import { migrateConfig } from '../../../../config/migration';
|
||||||
import type { AllConfig } from '../../../../config/types';
|
import type { AllConfig } from '../../../../config/types';
|
||||||
import { logger } from '../../../../logger';
|
import { logger } from '../../../../logger';
|
||||||
|
|
||||||
export function getConfig(env: NodeJS.ProcessEnv): AllConfig {
|
export async function getConfig(env: NodeJS.ProcessEnv): Promise<AllConfig> {
|
||||||
let configFile = env.RENOVATE_CONFIG_FILE || 'config';
|
let configFile = env.RENOVATE_CONFIG_FILE || 'config';
|
||||||
if (!upath.isAbsolute(configFile)) {
|
if (!upath.isAbsolute(configFile)) {
|
||||||
configFile = `${process.cwd()}/${configFile}`;
|
configFile = `${process.cwd()}/${configFile}`;
|
||||||
|
@ -11,7 +11,8 @@ export function getConfig(env: NodeJS.ProcessEnv): AllConfig {
|
||||||
}
|
}
|
||||||
let config: AllConfig = {};
|
let config: AllConfig = {};
|
||||||
try {
|
try {
|
||||||
config = require(configFile);
|
const tmpConfig = await import(configFile);
|
||||||
|
config = tmpConfig.default ? tmpConfig.default : tmpConfig;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
// istanbul ignore if
|
// istanbul ignore if
|
||||||
if (err instanceof SyntaxError || err instanceof TypeError) {
|
if (err instanceof SyntaxError || err instanceof TypeError) {
|
||||||
|
|
|
@ -18,7 +18,7 @@ export async function parseConfigs(
|
||||||
|
|
||||||
// Get configs
|
// Get configs
|
||||||
const defaultConfig = defaultsParser.getConfig();
|
const defaultConfig = defaultsParser.getConfig();
|
||||||
const fileConfig = fileParser.getConfig(env);
|
const fileConfig = await fileParser.getConfig(env);
|
||||||
const cliConfig = cliParser.getConfig(argv);
|
const cliConfig = cliParser.getConfig(argv);
|
||||||
const envConfig = envParser.getConfig(env);
|
const envConfig = envParser.getConfig(env);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue