Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: prefer fs.readJSON over readFile.then(JSON.parse) #7186

Merged
merged 2 commits into from
Apr 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions __tests__/validate-package-json.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,8 @@ type PackageJsonFile = {

async function getPackagesJsonFiles(): Promise<PackageJsonFile[]> {
const files = await Globby('packages/*/package.json');

return Promise.all(
files.map(async (file) => ({
file,
content: JSON.parse(await fs.readFile(file, 'utf8')),
})),
files.map((file) => fs.readJSON(file).then((content) => ({file, content}))),
);
}

Expand Down
4 changes: 2 additions & 2 deletions admin/scripts/generateExamples.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ async function generateTemplateExample(template) {
);

// read the content of the package.json
const templatePackageJson = JSON.parse(
await fs.readFile(`examples/${template}/package.json`, 'utf8'),
const templatePackageJson = await fs.readJSON(
`examples/${template}/package.json`,
);

// attach the dev script which would be used in code sandbox by default
Expand Down
3 changes: 1 addition & 2 deletions packages/create-docusaurus/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,7 @@ function isValidGitRepoUrl(gitRepoUrl: string) {
}

async function updatePkg(pkgPath: string, obj: {[key: string]: unknown}) {
const content = await fs.readFile(pkgPath, 'utf-8');
const pkg = JSON.parse(content);
const pkg = await fs.readJSON(pkgPath);
const newPkg = Object.assign(pkg, obj);

await fs.outputFile(pkgPath, `${JSON.stringify(newPkg, null, 2)}\n`);
Expand Down
6 changes: 3 additions & 3 deletions packages/docusaurus-migrate/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -443,8 +443,8 @@ async function migrateBlogFiles(context: MigrationContext) {
async function handleVersioning(context: MigrationContext) {
const {siteDir, newDir} = context;
if (await fs.pathExists(path.join(siteDir, 'versions.json'))) {
const loadedVersions: string[] = JSON.parse(
await fs.readFile(path.join(siteDir, 'versions.json'), 'utf-8'),
const loadedVersions: string[] = await fs.readJSON(
path.join(siteDir, 'versions.json'),
);
await fs.copyFile(
path.join(siteDir, 'versions.json'),
Expand Down Expand Up @@ -542,7 +542,7 @@ async function migrateVersionedSidebar(
`version-${version}-sidebars.json`,
);
try {
sidebarEntries = JSON.parse(await fs.readFile(sidebarPath, 'utf-8'));
sidebarEntries = await fs.readJSON(sidebarPath);
} catch {
sidebars.push({version, entries: sidebars[i - 1]!.entries});
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,24 @@ jest.setTimeout(15000);
describe('theme translations', () => {
it('has base messages files contain EXACTLY all the translations extracted from the theme. Please run "yarn workspace @docusaurus/theme-translations update" to keep base messages files up-to-date', async () => {
const baseMessagesDirPath = path.join(__dirname, '../base');
const baseMessages = Object.fromEntries(
await Promise.all(
(
await fs.readdir(baseMessagesDirPath)
).map(async (baseMessagesFile) =>
Object.entries(
(await fs.readJSON(
path.join(baseMessagesDirPath, baseMessagesFile),
'utf-8',
)) as {[key: string]: string},
const baseMessages = await fs
.readdir(baseMessagesDirPath)
.then((files) =>
Promise.all(
files.map(
(baseMessagesFile): Promise<{[key: string]: string}> =>
fs.readJSON(path.join(baseMessagesDirPath, baseMessagesFile)),
),
),
).then((translations) =>
translations.flat().filter(([key]) => !key.endsWith('___DESCRIPTION')),
),
);
)
.then((translations) =>
Object.fromEntries(
translations
.map(Object.entries)
.flat()
.filter(([key]) => !key.endsWith('___DESCRIPTION')),
),
);
const codeMessages = _.mapValues(
await extractThemeCodeMessages(),
(translation) => translation.message,
Expand Down
3 changes: 1 addition & 2 deletions packages/docusaurus-theme-translations/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ export async function readDefaultCodeTranslationMessages({
const filePath = path.resolve(dirPath, localeToTry, `${name}.json`);

if (await fs.pathExists(filePath)) {
const fileContent = await fs.readFile(filePath, 'utf8');
return JSON.parse(fileContent);
return fs.readJSON(filePath);
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/docusaurus-theme-translations/update.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ async function readMessagesFile(filePath) {
logger.info`File path=${filePath} not found. Creating new translation base file.`;
await fs.outputFile(filePath, '{}\n');
}
return JSON.parse((await fs.readFile(filePath)).toString());
return fs.readJSON(filePath);
}

/**
Expand Down
4 changes: 1 addition & 3 deletions packages/docusaurus/src/client/serverEntry.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,7 @@ async function doRender(locals: Locals & {path: string}) {

const {generatedFilesDir} = locals;
const manifestPath = path.join(generatedFilesDir, 'client-manifest.json');
const manifest: Manifest = JSON.parse(
await fs.readFile(manifestPath, 'utf8'),
);
const manifest: Manifest = await fs.readJSON(manifestPath);

// Get all required assets for this particular page based on client
// manifest information.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ async function readTranslationFileContent(
): Promise<TranslationFileContent | undefined> {
if (await fs.pathExists(filePath)) {
try {
const content = JSON.parse(await fs.readFile(filePath, 'utf8'));
const content = await fs.readJSON(filePath);
ensureTranslationFileContent(content);
return content;
} catch (err) {
Expand Down