Skip to content

Commit

Permalink
refactor(repo/init): use memCache and return raw config from detectRe…
Browse files Browse the repository at this point in the history
…poFileConfig
  • Loading branch information
Gabriel-Ladzaretti committed Aug 16, 2022
1 parent df0061b commit c5eb47d
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions lib/workers/repository/init/merge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export async function detectRepoFileConfig(): Promise<RepoFileConfig> {
logger.debug(`Found ${configFileName} config file`);
// TODO #7154
let configFileParsed: any;
let rawFileContents;
let configFileRaw: string | undefined;
if (configFileName === 'package.json') {
// We already know it parses
configFileParsed = JSON.parse(
Expand All @@ -83,25 +83,25 @@ export async function detectRepoFileConfig(): Promise<RepoFileConfig> {
}
logger.debug({ config: configFileParsed }, 'package.json>renovate config');
} else {
rawFileContents = await readLocalFile(configFileName, 'utf8');
configFileRaw = (await readLocalFile(configFileName, 'utf8')) ?? undefined;
// istanbul ignore if
if (!is.string(rawFileContents)) {
if (!is.string(configFileRaw)) {
logger.warn({ configFileName }, 'Null contents when reading config file');
throw new Error(REPOSITORY_CHANGED);
}
// istanbul ignore if
if (!rawFileContents.length) {
rawFileContents = '{}';
if (!configFileRaw.length) {
configFileRaw = '{}';
}

const fileType = upath.extname(configFileName);

if (fileType === '.json5') {
try {
configFileParsed = JSON5.parse(rawFileContents);
configFileParsed = JSON5.parse(configFileRaw);
} catch (err) /* istanbul ignore next */ {
logger.debug(
{ renovateConfig: rawFileContents },
{ renovateConfig: configFileRaw },
'Error parsing renovate config renovate.json5'
);
const validationError = 'Invalid JSON5 (parsing failed)';
Expand All @@ -114,7 +114,7 @@ export async function detectRepoFileConfig(): Promise<RepoFileConfig> {
} else {
let allowDuplicateKeys = true;
let jsonValidationError = jsonValidator.validate(
rawFileContents,
configFileRaw,
allowDuplicateKeys
);
if (jsonValidationError) {
Expand All @@ -127,7 +127,7 @@ export async function detectRepoFileConfig(): Promise<RepoFileConfig> {
}
allowDuplicateKeys = false;
jsonValidationError = jsonValidator.validate(
rawFileContents,
configFileRaw,
allowDuplicateKeys
);
if (jsonValidationError) {
Expand All @@ -139,10 +139,10 @@ export async function detectRepoFileConfig(): Promise<RepoFileConfig> {
};
}
try {
configFileParsed = JSON5.parse(rawFileContents);
configFileParsed = JSON5.parse(configFileRaw);
} catch (err) /* istanbul ignore next */ {
logger.debug(
{ renovateConfig: rawFileContents },
{ renovateConfig: configFileRaw },
'Error parsing renovate config'
);
const validationError = 'Invalid JSON (parsing failed)';
Expand All @@ -158,7 +158,7 @@ export async function detectRepoFileConfig(): Promise<RepoFileConfig> {
'Repository config'
);
}
return { configFileName, configFileRaw: rawFileContents, configFileParsed };
return { configFileName, configFileRaw, configFileParsed };
}

export function checkForRepoConfigError(repoConfig: RepoFileConfig): void {
Expand Down

0 comments on commit c5eb47d

Please sign in to comment.