Skip to content

Commit

Permalink
Migrate from the Node.js builtin module fs to fs/promises.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaydenseric committed Aug 18, 2022
1 parent 193be23 commit 8878a8d
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 42 deletions.
14 changes: 7 additions & 7 deletions analyseCoverage.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// @ts-check

import v8Coverage from "@bcoe/v8-coverage";
import fs from "fs";
import { readdir, readFile } from "fs/promises";
import { join } from "path";
import { fileURLToPath } from "url";

Expand All @@ -18,15 +18,14 @@ export default async function analyseCoverage(coverageDirPath) {
if (typeof coverageDirPath !== "string")
throw new TypeError("Argument 1 `coverageDirPath` must be a string.");

const coverageDirFileNames = await fs.promises.readdir(coverageDirPath);
const coverageDirFileNames = await readdir(coverageDirPath);
const filteredProcessCoverages = [];

for (const fileName of coverageDirFileNames)
if (fileName.startsWith("coverage-"))
filteredProcessCoverages.push(
fs.promises
.readFile(join(coverageDirPath, fileName), "utf8")
.then((coverageFileJson) => {
readFile(join(coverageDirPath, fileName), "utf8").then(
(coverageFileJson) => {
/** @type {import("@bcoe/v8-coverage").ProcessCov} */
const { result } = JSON.parse(coverageFileJson);
return {
Expand All @@ -45,7 +44,8 @@ export default async function analyseCoverage(coverageDirPath) {
!/\/test\.\w+$/u.test(url)
),
};
})
}
)
);

const mergedCoverage = v8Coverage.mergeProcessCovs(
Expand All @@ -70,7 +70,7 @@ export default async function analyseCoverage(coverageDirPath) {
for (const range of ranges) if (!range.count) uncoveredRanges.push(range);

if (uncoveredRanges.length) {
const source = await fs.promises.readFile(path, "utf8");
const source = await readFile(path, "utf8");
const ignored = [];
const uncovered = [];

Expand Down
25 changes: 10 additions & 15 deletions analyseCoverage.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { deepStrictEqual, rejects } from "assert";
import { spawn } from "child_process";
import disposableDirectory from "disposable-directory";
import fs from "fs";
import { mkdir, writeFile } from "fs/promises";
import { join } from "path";

import analyseCoverage from "./analyseCoverage.mjs";
Expand Down Expand Up @@ -41,11 +41,9 @@ export default (tests) => {
nodeModulesModuleMainFileName
);

await fs.promises.mkdir(nodeModulesDirPath);
await fs.promises.mkdir(
join(nodeModulesDirPath, nodeModulesModuleName)
);
await fs.promises.writeFile(nodeModulesModuleMainFilePath, "1;");
await mkdir(nodeModulesDirPath);
await mkdir(join(nodeModulesDirPath, nodeModulesModuleName));
await writeFile(nodeModulesModuleMainFilePath, "1;");

await childProcessPromise(
spawn("node", [nodeModulesModuleMainFilePath], {
Expand All @@ -70,8 +68,8 @@ export default (tests) => {
const dirPath = join(tempDirPath, "test");
const filePath = join(dirPath, "index.mjs");

await fs.promises.mkdir(dirPath);
await fs.promises.writeFile(filePath, "1;");
await mkdir(dirPath);
await writeFile(filePath, "1;");

await childProcessPromise(
spawn("node", [filePath], {
Expand All @@ -96,7 +94,7 @@ export default (tests) => {
const coverageDirPath = join(tempDirPath, "coverage");
const filePath = join(tempDirPath, "index.test.mjs");

await fs.promises.writeFile(filePath, "1;");
await writeFile(filePath, "1;");

await childProcessPromise(
spawn("node", [filePath], {
Expand All @@ -120,7 +118,7 @@ export default (tests) => {
const coverageDirPath = join(tempDirPath, "coverage");
const filePath = join(tempDirPath, "test.mjs");

await fs.promises.writeFile(filePath, "1;");
await writeFile(filePath, "1;");

await childProcessPromise(
spawn("node", [filePath], {
Expand All @@ -143,7 +141,7 @@ export default (tests) => {
const coverageDirPath = join(tempDirPath, "coverage");
const filePath = join(tempDirPath, "index.mjs");

await fs.promises.writeFile(filePath, "1;");
await writeFile(filePath, "1;");

await childProcessPromise(
spawn("node", [filePath], {
Expand All @@ -166,10 +164,7 @@ export default (tests) => {
const coverageDirPath = join(tempDirPath, "coverage");
const filePath = join(tempDirPath, "index.mjs");

await fs.promises.writeFile(
filePath,
"function a() {}; function b() {};"
);
await writeFile(filePath, "function a() {}; function b() {};");

await childProcessPromise(
spawn("node", [filePath], {
Expand Down
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Next

### Major

- Migrated from the Node.js builtin module `fs` to `fs/promises`.

### Patch

- Updated dependencies.
Expand Down
30 changes: 15 additions & 15 deletions coverage-node.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { strictEqual } from "assert";
import { spawnSync } from "child_process";
import disposableDirectory from "disposable-directory";
import fs from "fs";
import { writeFile } from "fs/promises";
import { join, relative } from "path";
import replaceStackTraces from "replace-stack-traces";
import snapshot from "snapshot-assertion";
Expand All @@ -23,7 +23,7 @@ export default (tests) => {
await disposableDirectory(async (tempDirPath) => {
const filePath = join(tempDirPath, "index.mjs");

await fs.promises.writeFile(filePath, "1;");
await writeFile(filePath, "1;");

const { stdout, stderr, status, error } = spawnSync(
"node",
Expand Down Expand Up @@ -57,7 +57,7 @@ export default (tests) => {
await disposableDirectory(async (tempDirPath) => {
const filePath = join(tempDirPath, "index.mjs");

await fs.promises.writeFile(
await writeFile(
filePath,
`// coverage ignore next line
() => {};
Expand Down Expand Up @@ -98,7 +98,7 @@ export default (tests) => {
await disposableDirectory(async (tempDirPath) => {
const filePath = join(tempDirPath, "index.mjs");

await fs.promises.writeFile(filePath, "() => {};");
await writeFile(filePath, "() => {};");

const { stdout, stderr, status, error } = spawnSync(
"node",
Expand Down Expand Up @@ -142,7 +142,7 @@ export default (tests) => {
await disposableDirectory(async (tempDirPath) => {
const filePath = join(tempDirPath, "index.mjs");

await fs.promises.writeFile(filePath, "() => {};");
await writeFile(filePath, "() => {};");

const { stdout, stderr, status, error } = spawnSync(
"node",
Expand Down Expand Up @@ -192,7 +192,7 @@ export default (tests) => {
const fileEPath = join(tempDirPath, "e.mjs");
const fileFPath = join(tempDirPath, "f.mjs");

await fs.promises.writeFile(
await writeFile(
fileAPath,
`import "${fileBPath}";
import "${fileCPath}";
Expand All @@ -201,19 +201,19 @@ import "${fileEPath}";
import "${fileFPath}";
`
);
await fs.promises.writeFile(fileBPath, "function a() {}; a();");
await fs.promises.writeFile(
await writeFile(fileBPath, "function a() {}; a();");
await writeFile(
fileCPath,
`// coverage ignore next line
() => {};`
);
await fs.promises.writeFile(
await writeFile(
fileDPath,
`// coverage ignore next line
() => {};`
);
await fs.promises.writeFile(fileEPath, "() => {};");
await fs.promises.writeFile(fileFPath, "() => {};");
await writeFile(fileEPath, "() => {};");
await writeFile(fileFPath, "() => {};");

const { stdout, stderr, status, error } = spawnSync(
"node",
Expand Down Expand Up @@ -261,7 +261,7 @@ import "${fileFPath}";
await disposableDirectory(async (tempDirPath) => {
const filePath = join(tempDirPath, "index.mjs");

await fs.promises.writeFile(filePath, 'console.log("Message.");');
await writeFile(filePath, 'console.log("Message.");');

const { stdout, stderr, status, error } = spawnSync(
"node",
Expand Down Expand Up @@ -295,7 +295,7 @@ import "${fileFPath}";
await disposableDirectory(async (tempDirPath) => {
const filePath = join(tempDirPath, "index.mjs");

await fs.promises.writeFile(filePath, 'throw new Error("Error.");');
await writeFile(filePath, 'throw new Error("Error.");');

const { stdout, stderr, status, error } = spawnSync(
"node",
Expand Down Expand Up @@ -331,7 +331,7 @@ import "${fileFPath}";
await disposableDirectory(async (tempDirPath) => {
const filePath = join(tempDirPath, "index.mjs");

await fs.promises.writeFile(
await writeFile(
filePath,
`import { deprecate } from "util";
Expand Down Expand Up @@ -366,7 +366,7 @@ deprecated();
await disposableDirectory(async (tempDirPath) => {
const filePath = join(tempDirPath, "index.mjs");

await fs.promises.writeFile(filePath, "1;");
await writeFile(filePath, "1;");

const { stdout, stderr, status, error } = spawnSync(
"node",
Expand Down
10 changes: 5 additions & 5 deletions reportCliError.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { strictEqual, throws } from "assert";
import { spawnSync } from "child_process";
import disposableDirectory from "disposable-directory";
import fs from "fs";
import { writeFile } from "fs/promises";
import { join } from "path";
import replaceStackTraces from "replace-stack-traces";
import snapshot from "snapshot-assertion";
Expand Down Expand Up @@ -39,7 +39,7 @@ export default (tests) => {
await disposableDirectory(async (tempDirPath) => {
const filePath = join(tempDirPath, "test.mjs");

await fs.promises.writeFile(
await writeFile(
filePath,
`import reportCliError from "${REPORT_CLI_ERROR_PATH}";
Expand Down Expand Up @@ -81,7 +81,7 @@ reportCliError("CLI", new Error("Message."));
await disposableDirectory(async (tempDirPath) => {
const filePath = join(tempDirPath, "test.mjs");

await fs.promises.writeFile(
await writeFile(
filePath,
`import reportCliError from "${REPORT_CLI_ERROR_PATH}";
Expand Down Expand Up @@ -126,7 +126,7 @@ reportCliError("CLI", error);
new URL("./CliError.mjs", import.meta.url)
);

await fs.promises.writeFile(
await writeFile(
filePath,
`import CliError from "${cliErrorPath}";
import reportCliError from "${REPORT_CLI_ERROR_PATH}";
Expand Down Expand Up @@ -162,7 +162,7 @@ reportCliError("CLI", new CliError("Message."));
await disposableDirectory(async (tempDirPath) => {
const filePath = join(tempDirPath, "test.mjs");

await fs.promises.writeFile(
await writeFile(
filePath,
`import reportCliError from "${REPORT_CLI_ERROR_PATH}";
Expand Down

0 comments on commit 8878a8d

Please sign in to comment.