mirror of
https://github.com/facebook/docusaurus.git
synced 2025-04-29 10:17:55 +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,
|
'global-require': WARNING,
|
||||||
'prefer-destructuring': WARNING,
|
'prefer-destructuring': WARNING,
|
||||||
yoda: WARNING,
|
yoda: WARNING,
|
||||||
|
'no-await-in-loop': OFF,
|
||||||
'no-control-regex': WARNING,
|
'no-control-regex': WARNING,
|
||||||
'no-empty': [WARNING, {allowEmptyCatch: true}],
|
'no-empty': [WARNING, {allowEmptyCatch: true}],
|
||||||
'no-prototype-builtins': WARNING,
|
'no-prototype-builtins': WARNING,
|
||||||
|
|
|
@ -43,12 +43,8 @@ export async function readAuthorsMapFile(
|
||||||
): Promise<AuthorsMap | undefined> {
|
): Promise<AuthorsMap | undefined> {
|
||||||
if (await fs.pathExists(filePath)) {
|
if (await fs.pathExists(filePath)) {
|
||||||
const contentString = await fs.readFile(filePath, {encoding: 'utf8'});
|
const contentString = await fs.readFile(filePath, {encoding: 'utf8'});
|
||||||
const parse =
|
|
||||||
filePath.endsWith('.yml') || filePath.endsWith('.yaml')
|
|
||||||
? Yaml.load
|
|
||||||
: JSON.parse;
|
|
||||||
try {
|
try {
|
||||||
const unsafeContent = parse(contentString);
|
const unsafeContent = Yaml.load(contentString);
|
||||||
return validateAuthorsMapFile(unsafeContent);
|
return validateAuthorsMapFile(unsafeContent);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// TODO replace later by error cause: see https://v8.dev/features/error-cause
|
// 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(
|
async function readCategoryMetadatasFile(
|
||||||
categoryDirPath: string,
|
categoryDirPath: string,
|
||||||
): Promise<CategoryMetadatasFile | null> {
|
): Promise<CategoryMetadatasFile | null> {
|
||||||
async function tryReadFile(
|
async function tryReadFile(filePath: string): Promise<CategoryMetadatasFile> {
|
||||||
fileNameWithExtension: string,
|
const contentString = await fs.readFile(filePath, {encoding: 'utf8'});
|
||||||
parse: (content: string) => unknown,
|
const unsafeContent = Yaml.load(contentString);
|
||||||
): Promise<CategoryMetadatasFile | null> {
|
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
|
// Simpler to use only posix paths for mocking file metadatas in tests
|
||||||
const filePath = posixPath(
|
const filePath = posixPath(
|
||||||
path.join(categoryDirPath, fileNameWithExtension),
|
path.join(categoryDirPath, `${CategoryMetadataFilenameBase}${ext}`),
|
||||||
);
|
);
|
||||||
if (await fs.pathExists(filePath)) {
|
if (await fs.pathExists(filePath)) {
|
||||||
const contentString = await fs.readFile(filePath, {encoding: 'utf8'});
|
return tryReadFile(filePath);
|
||||||
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 null;
|
|
||||||
}
|
}
|
||||||
|
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))
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Comment for this feature: https://github.com/facebook/docusaurus/issues/3464#issuecomment-818670449
|
// 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
|
// eslint-disable-next-line no-restricted-syntax
|
||||||
for (const localeFile of localesFiles) {
|
for (const localeFile of localesFiles) {
|
||||||
logSection(`Will update ${path.basename(localeFile)}`);
|
logSection(`Will update ${path.basename(localeFile)}`);
|
||||||
// eslint-disable-next-line no-await-in-loop
|
|
||||||
await updateLocaleCodeTranslations(localeFile, baseFileMessages);
|
await updateLocaleCodeTranslations(localeFile, baseFileMessages);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -44,9 +44,7 @@ export async function readDefaultCodeTranslationMessages({
|
||||||
for (const fileName of localesToTry) {
|
for (const fileName of localesToTry) {
|
||||||
const filePath = path.resolve(dirPath, `${fileName}.json`);
|
const filePath = path.resolve(dirPath, `${fileName}.json`);
|
||||||
|
|
||||||
// eslint-disable-next-line no-await-in-loop
|
|
||||||
if (await fs.pathExists(filePath)) {
|
if (await fs.pathExists(filePath)) {
|
||||||
// eslint-disable-next-line no-await-in-loop
|
|
||||||
const fileContent = await fs.readFile(filePath, 'utf8');
|
const fileContent = await fs.readFile(filePath, 'utf8');
|
||||||
return JSON.parse(fileContent);
|
return JSON.parse(fileContent);
|
||||||
}
|
}
|
||||||
|
|
|
@ -313,7 +313,6 @@ export async function mapAsyncSequencial<T extends unknown, R extends unknown>(
|
||||||
const results: R[] = [];
|
const results: R[] = [];
|
||||||
// eslint-disable-next-line no-restricted-syntax
|
// eslint-disable-next-line no-restricted-syntax
|
||||||
for (const t of array) {
|
for (const t of array) {
|
||||||
// eslint-disable-next-line no-await-in-loop
|
|
||||||
const result = await action(t);
|
const result = await action(t);
|
||||||
results.push(result);
|
results.push(result);
|
||||||
}
|
}
|
||||||
|
@ -326,7 +325,6 @@ export async function findAsyncSequential<T>(
|
||||||
): Promise<T | undefined> {
|
): Promise<T | undefined> {
|
||||||
// eslint-disable-next-line no-restricted-syntax
|
// eslint-disable-next-line no-restricted-syntax
|
||||||
for (const t of array) {
|
for (const t of array) {
|
||||||
// eslint-disable-next-line no-await-in-loop
|
|
||||||
if (await predicate(t)) {
|
if (await predicate(t)) {
|
||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue