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:
Nejc Habjan 2021-11-17 07:49:53 +01:00 committed by GitHub
parent e042b917dc
commit 0f1256317c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 20 additions and 19 deletions

View file

@ -1,7 +1,7 @@
#!/usr/bin/env node
// istanbul ignore file
import { dequal } from 'dequal';
import { readFileSync } from 'fs-extra';
import { readFile } from 'fs-extra';
import JSON5 from 'json5';
import { configFileNames } from './config/app-strings';
import { massageConfig } from './config/massage';
@ -52,7 +52,7 @@ type PackageJson = {
(name) => name !== 'package.json'
)) {
try {
const rawContent = readFileSync(file, 'utf8');
const rawContent = await readFile(file, 'utf8');
logger.info(`Validating ${file}`);
try {
let jsonContent: RenovateConfig;
@ -72,7 +72,7 @@ type PackageJson = {
}
try {
const pkgJson = JSON.parse(
readFileSync('package.json', 'utf8')
await readFile('package.json', 'utf8')
) as PackageJson;
if (pkgJson.renovate) {
logger.info(`Validating package.json > renovate`);
@ -88,7 +88,7 @@ type PackageJson = {
// ignore
}
try {
const fileConfig = getFileConfig(process.env);
const fileConfig = await getFileConfig(process.env);
if (!dequal(fileConfig, {})) {
const file = process.env.RENOVATE_CONFIG_FILE ?? 'config.js';
logger.info(`Validating ${file}`);

View file

@ -16,24 +16,24 @@ describe('workers/global/config/parse/file', () => {
});
describe('.getConfig()', () => {
it('parses custom config file', () => {
it('parses custom config file', async () => {
const configFile = upath.resolve(__dirname, './__fixtures__/file.js');
expect(file.getConfig({ RENOVATE_CONFIG_FILE: configFile })).toEqual(
customConfig
);
expect(
await file.getConfig({ RENOVATE_CONFIG_FILE: configFile })
).toEqual(customConfig);
});
it('migrates', () => {
it('migrates', async () => {
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.rangeStrategy).toBe('bump');
});
it('parse and returns empty config if there is no RENOVATE_CONFIG_FILE in env', () => {
expect(file.getConfig({})).toBeDefined();
it('parse and returns empty config if there is no RENOVATE_CONFIG_FILE in env', async () => {
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
.spyOn(process, 'exit')
.mockImplementation(() => undefined as never);
@ -50,19 +50,19 @@ describe('workers/global/config/parse/file', () => {
"repositories": [ "test/test" ],
};`;
fs.writeFileSync(configFile, fileContent, { encoding: 'utf8' });
file.getConfig({ RENOVATE_CONFIG_FILE: configFile });
await file.getConfig({ RENOVATE_CONFIG_FILE: configFile });
expect(mockProcessExit).toHaveBeenCalledWith(1);
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
.spyOn(process, 'exit')
.mockImplementation(() => undefined as never);
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);
});

View file

@ -3,7 +3,7 @@ import { migrateConfig } from '../../../../config/migration';
import type { AllConfig } from '../../../../config/types';
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';
if (!upath.isAbsolute(configFile)) {
configFile = `${process.cwd()}/${configFile}`;
@ -11,7 +11,8 @@ export function getConfig(env: NodeJS.ProcessEnv): AllConfig {
}
let config: AllConfig = {};
try {
config = require(configFile);
const tmpConfig = await import(configFile);
config = tmpConfig.default ? tmpConfig.default : tmpConfig;
} catch (err) {
// istanbul ignore if
if (err instanceof SyntaxError || err instanceof TypeError) {

View file

@ -18,7 +18,7 @@ export async function parseConfigs(
// Get configs
const defaultConfig = defaultsParser.getConfig();
const fileConfig = fileParser.getConfig(env);
const fileConfig = await fileParser.getConfig(env);
const cliConfig = cliParser.getConfig(argv);
const envConfig = envParser.getConfig(env);