mirror of
https://github.com/facebook/docusaurus.git
synced 2025-04-29 02:08:36 +02:00
refactor: use js-yaml to parse both JSON and YAML (#5806)
This commit is contained in:
parent
9f13f8f7af
commit
1363a25819
6 changed files with 21 additions and 35 deletions
|
@ -119,6 +119,7 @@ module.exports = {
|
|||
'global-require': WARNING,
|
||||
'prefer-destructuring': WARNING,
|
||||
yoda: WARNING,
|
||||
'no-await-in-loop': OFF,
|
||||
'no-control-regex': WARNING,
|
||||
'no-empty': [WARNING, {allowEmptyCatch: true}],
|
||||
'no-prototype-builtins': WARNING,
|
||||
|
|
|
@ -43,12 +43,8 @@ export async function readAuthorsMapFile(
|
|||
): Promise<AuthorsMap | undefined> {
|
||||
if (await fs.pathExists(filePath)) {
|
||||
const contentString = await fs.readFile(filePath, {encoding: 'utf8'});
|
||||
const parse =
|
||||
filePath.endsWith('.yml') || filePath.endsWith('.yaml')
|
||||
? Yaml.load
|
||||
: JSON.parse;
|
||||
try {
|
||||
const unsafeContent = parse(contentString);
|
||||
const unsafeContent = Yaml.load(contentString);
|
||||
return validateAuthorsMapFile(unsafeContent);
|
||||
} catch (e) {
|
||||
// TODO replace later by error cause: see https://v8.dev/features/error-cause
|
||||
|
|
|
@ -65,37 +65,31 @@ const CategoryMetadatasFileSchema = Joi.object<CategoryMetadatasFile>({
|
|||
async function readCategoryMetadatasFile(
|
||||
categoryDirPath: string,
|
||||
): Promise<CategoryMetadatasFile | null> {
|
||||
async function tryReadFile(
|
||||
fileNameWithExtension: string,
|
||||
parse: (content: string) => unknown,
|
||||
): Promise<CategoryMetadatasFile | null> {
|
||||
async function tryReadFile(filePath: string): Promise<CategoryMetadatasFile> {
|
||||
const contentString = await fs.readFile(filePath, {encoding: 'utf8'});
|
||||
const unsafeContent = Yaml.load(contentString);
|
||||
try {
|
||||
return Joi.attempt(unsafeContent, CategoryMetadatasFileSchema);
|
||||
} catch (e) {
|
||||
console.error(
|
||||
chalk.red(
|
||||
`The docs sidebar category metadata file looks invalid!\nPath: ${filePath}`,
|
||||
),
|
||||
);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
// eslint-disable-next-line no-restricted-syntax
|
||||
for (const ext of ['.json', '.yml', '.yaml']) {
|
||||
// Simpler to use only posix paths for mocking file metadatas in tests
|
||||
const filePath = posixPath(
|
||||
path.join(categoryDirPath, fileNameWithExtension),
|
||||
path.join(categoryDirPath, `${CategoryMetadataFilenameBase}${ext}`),
|
||||
);
|
||||
if (await fs.pathExists(filePath)) {
|
||||
const contentString = await fs.readFile(filePath, {encoding: 'utf8'});
|
||||
const unsafeContent = parse(contentString);
|
||||
try {
|
||||
return Joi.attempt(unsafeContent, CategoryMetadatasFileSchema);
|
||||
} catch (e) {
|
||||
console.error(
|
||||
chalk.red(
|
||||
`The docs sidebar category metadata file looks invalid!\nPath: ${filePath}`,
|
||||
),
|
||||
);
|
||||
throw e;
|
||||
}
|
||||
return tryReadFile(filePath);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
return (
|
||||
(await tryReadFile(`${CategoryMetadataFilenameBase}.json`, JSON.parse)) ??
|
||||
(await tryReadFile(`${CategoryMetadataFilenameBase}.yml`, Yaml.load)) ??
|
||||
// eslint-disable-next-line no-return-await
|
||||
(await tryReadFile(`${CategoryMetadataFilenameBase}.yaml`, Yaml.load))
|
||||
);
|
||||
return null;
|
||||
}
|
||||
|
||||
// Comment for this feature: https://github.com/facebook/docusaurus/issues/3464#issuecomment-818670449
|
||||
|
|
|
@ -216,7 +216,6 @@ async function updateCodeTranslations() {
|
|||
// eslint-disable-next-line no-restricted-syntax
|
||||
for (const localeFile of localesFiles) {
|
||||
logSection(`Will update ${path.basename(localeFile)}`);
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
await updateLocaleCodeTranslations(localeFile, baseFileMessages);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -44,9 +44,7 @@ export async function readDefaultCodeTranslationMessages({
|
|||
for (const fileName of localesToTry) {
|
||||
const filePath = path.resolve(dirPath, `${fileName}.json`);
|
||||
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
if (await fs.pathExists(filePath)) {
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
const fileContent = await fs.readFile(filePath, 'utf8');
|
||||
return JSON.parse(fileContent);
|
||||
}
|
||||
|
|
|
@ -313,7 +313,6 @@ export async function mapAsyncSequencial<T extends unknown, R extends unknown>(
|
|||
const results: R[] = [];
|
||||
// eslint-disable-next-line no-restricted-syntax
|
||||
for (const t of array) {
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
const result = await action(t);
|
||||
results.push(result);
|
||||
}
|
||||
|
@ -326,7 +325,6 @@ export async function findAsyncSequential<T>(
|
|||
): Promise<T | undefined> {
|
||||
// eslint-disable-next-line no-restricted-syntax
|
||||
for (const t of array) {
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
if (await predicate(t)) {
|
||||
return t;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue