Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: config file parsing (#27863)
- Loading branch information
Showing
2 changed files
with
86 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import jsonValidator from 'json-dup-key-validator'; | ||
import JSON5 from 'json5'; | ||
import upath from 'upath'; | ||
import { logger } from '../logger'; | ||
import { parseJson } from '../util/common'; | ||
|
||
export function parseFileConfig( | ||
fileName: string, | ||
fileContents: string, | ||
): | ||
| { success: true; parsedContents: unknown } | ||
| { success: false; validationError: string; validationMessage: string } { | ||
const fileType = upath.extname(fileName); | ||
|
||
if (fileType === '.json5') { | ||
try { | ||
return { success: true, parsedContents: JSON5.parse(fileContents) }; | ||
} catch (err) /* istanbul ignore next */ { | ||
logger.debug({ fileName, fileContents }, 'Error parsing JSON5 file'); | ||
const validationError = 'Invalid JSON5 (parsing failed)'; | ||
const validationMessage = `JSON5.parse error: \`${err.message.replaceAll( | ||
'`', | ||
"'", | ||
)}\``; | ||
return { | ||
success: false, | ||
validationError, | ||
validationMessage, | ||
}; | ||
} | ||
} else { | ||
let allowDuplicateKeys = true; | ||
let jsonValidationError = jsonValidator.validate( | ||
fileContents, | ||
allowDuplicateKeys, | ||
); | ||
if (jsonValidationError) { | ||
const validationError = 'Invalid JSON (parsing failed)'; | ||
const validationMessage = jsonValidationError; | ||
return { | ||
success: false, | ||
validationError, | ||
validationMessage, | ||
}; | ||
} | ||
allowDuplicateKeys = false; | ||
jsonValidationError = jsonValidator.validate( | ||
fileContents, | ||
allowDuplicateKeys, | ||
); | ||
if (jsonValidationError) { | ||
const validationError = 'Duplicate keys in JSON'; | ||
const validationMessage = JSON.stringify(jsonValidationError); | ||
return { | ||
success: false, | ||
validationError, | ||
validationMessage, | ||
}; | ||
} | ||
try { | ||
return { | ||
success: true, | ||
parsedContents: parseJson(fileContents, fileName), | ||
}; | ||
} catch (err) /* istanbul ignore next */ { | ||
logger.debug({ fileContents }, 'Error parsing renovate config'); | ||
const validationError = 'Invalid JSON (parsing failed)'; | ||
const validationMessage = `JSON.parse error: \`${err.message.replaceAll( | ||
'`', | ||
"'", | ||
)}\``; | ||
return { success: false, validationError, validationMessage }; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters