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

chore: migrate to node:fs/promises #8310

Closed
wants to merge 11 commits into from

Conversation

queengooborg
Copy link
Collaborator

@queengooborg queengooborg commented Feb 25, 2023

This PR converts various files and functions to use async (promises), specifically ones involving file access.

@github-actions github-actions bot added the macros tracking issues related to kumascript macros label Feb 25, 2023
@queengooborg queengooborg marked this pull request as draft February 25, 2023 07:24
@queengooborg queengooborg marked this pull request as ready for review February 25, 2023 08:51
@queengooborg queengooborg changed the title perf: Convert various functions to async refactor: Convert various functions to async Feb 25, 2023
@github-actions
Copy link
Contributor

This pull request has merge conflicts that must be resolved before it can be merged.

@github-actions github-actions bot added the merge conflicts 🚧 Please rebase onto or merge the latest main. label Feb 28, 2023
@github-actions github-actions bot added merge conflicts 🚧 Please rebase onto or merge the latest main. and removed merge conflicts 🚧 Please rebase onto or merge the latest main. labels Feb 28, 2023
@github-actions
Copy link
Contributor

github-actions bot commented Mar 6, 2023

This pull request has merge conflicts that must be resolved before it can be merged.

@github-actions github-actions bot removed the merge conflicts 🚧 Please rebase onto or merge the latest main. label Mar 16, 2023
@github-actions github-actions bot added the merge conflicts 🚧 Please rebase onto or merge the latest main. label May 3, 2023
@github-actions
Copy link
Contributor

github-actions bot commented May 3, 2023

This pull request has merge conflicts that must be resolved before it can be merged.

1 similar comment
@github-actions
Copy link
Contributor

github-actions bot commented May 3, 2023

This pull request has merge conflicts that must be resolved before it can be merged.

@github-actions github-actions bot added the merge conflicts 🚧 Please rebase onto or merge the latest main. label May 3, 2023
Copy link
Member

@LeoMcA LeoMcA left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the delay reviewing this, and the merge conflicts you've had to resolve as a result.

Generally, I'm not a fan of using an external lib where an inbuilt node one is sufficient. fs-extra might be a little more elegant to check the existence of a file, but it enforces bad coding habits, i.e. checking if a file exists, then doing an operation on it, rather than doing an operation on it and catching the error if it doesn't exist.

Generally we want to do that, in some cases we check if files exist to display console messages or similar, in those cases we should use try/catch with fs.access as I suggested in one of my comments.

import path from "node:path";
import zlib from "node:zlib";

import chalk from "chalk";
import cliProgress from "cli-progress";
import caporal from "@caporal/core";
import fse from "fs-extra";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a fan of using an extra library here (I see we already use it in main, we should remove it from there too) - the inbuilt node lib should be sufficient.

path.join(outPath, "metadata.json"),
JSON.stringify(builtMetadata)
);
await fse.writeJson(path.join(outPath, "metadata.json"), builtMetadata);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keep the prior JSON.stringify, but use await fs.writeFile - this applies to all introductions of fse.writeJson.

Comment on lines +391 to 394
if (await fse.pathExists(sitemapFilePath)) {
sitemapsBuilt.push(sitemapFilePath);
locales.push(locale);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

access should work here instead, and all similar checks:

Suggested change
if (await fse.pathExists(sitemapFilePath)) {
sitemapsBuilt.push(sitemapFilePath);
locales.push(locale);
}
try {
await fs.access(sitemapFilePath);
sitemapsBuilt.push(sitemapFilePath);
locales.push(locale);
} catch {
if (e.code !== "ENOENT") throw e;
}

const map = new Map();
if (previousFile) {
const previous = JSON.parse(fs.readFileSync(previousFile, "utf-8"));
const previous = await fse.readJson(previousFile, "utf-8");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keep JSON.parse for this and all other additions of fse.ReadJson:

Suggested change
const previous = await fse.readJson(previousFile, "utf-8");
const previous = JSON.parse(await fs.readFile(previousFile, "utf-8"));

Comment on lines +59 to 61
if (await fse.pathExists(path.join(outPath, "index.html"))) {
await fs.unlink(path.join(outPath, "index.html"));
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The node docs recommend against checking to see if a file exists, and then performing an op on it, otherwise a race condition can happen where the file is modified between the check and operation. Best to just do the operation, and catch it if it fails (do the same in the instance below):

Suggested change
if (await fse.pathExists(path.join(outPath, "index.html"))) {
await fs.unlink(path.join(outPath, "index.html"));
}
try {
await fs.unlink(path.join(outPath, "index.html"));
} catch (e) {
if (e.code !== "ENOENT") throw e;
}

@@ -514,11 +515,11 @@ program
let url: string;
// Perhaps they typed in a path relative to the content root
if (
(slug.startsWith("files") || fs.existsSync(slug)) &&
(slug.startsWith("files") || (await fse.pathExists(slug))) &&
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this whole command can be re-factored, here just attempt to load the split slug and if it fails move on.

Comment on lines 523 to 524
slug.includes("translated-content") &&
!CONTENT_TRANSLATED_ROOT
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This logic can exist in the catch of the above file load, explaining possibly why it failed

Comment on lines +550 to +557
(await fse.pathExists(slug)) &&
(await fse.pathExists(path.join(slug, "index.json")))
) {
// Someone probably yarn `yarn build` and copy-n-pasted one of the lines
// it spits out from its CLI.
const { doc } = JSON.parse(
fs.readFileSync(path.join(slug, "index.json"), "utf-8")
const { doc } = await fse.readJson(
path.join(slug, "index.json"),
"utf-8"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Attempt to load the index.json and catch if it fails

roots,
loadHistory && fs.existsSync(loadHistory) ? loadHistory : null
loadHistory && (await fse.pathExists(loadHistory)) ? loadHistory : null
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just delete loadHistory, or set to null, above if fs.access(loadHistory) fails

Comment on lines +840 to +841
if (!refresh && (await fse.pathExists(outfile))) {
const stat = await fs.stat(outfile);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just catch the stat error

@LeoMcA LeoMcA changed the title refactor: Convert various functions to async chore: migrate to node:fs/promises May 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
macros tracking issues related to kumascript macros merge conflicts 🚧 Please rebase onto or merge the latest main.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants