From 8878a8d8eeff586dd3b55e3d64a02d0fc46c7753 Mon Sep 17 00:00:00 2001 From: Jayden Seric Date: Thu, 18 Aug 2022 14:18:20 +1000 Subject: [PATCH] Migrate from the Node.js builtin module `fs` to `fs/promises`. --- analyseCoverage.mjs | 14 +++++++------- analyseCoverage.test.mjs | 25 ++++++++++--------------- changelog.md | 4 ++++ coverage-node.test.mjs | 30 +++++++++++++++--------------- reportCliError.test.mjs | 10 +++++----- 5 files changed, 41 insertions(+), 42 deletions(-) diff --git a/analyseCoverage.mjs b/analyseCoverage.mjs index 52f25e9..e31fbf4 100644 --- a/analyseCoverage.mjs +++ b/analyseCoverage.mjs @@ -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"; @@ -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 { @@ -45,7 +44,8 @@ export default async function analyseCoverage(coverageDirPath) { !/\/test\.\w+$/u.test(url) ), }; - }) + } + ) ); const mergedCoverage = v8Coverage.mergeProcessCovs( @@ -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 = []; diff --git a/analyseCoverage.test.mjs b/analyseCoverage.test.mjs index 35d6a3f..6341236 100644 --- a/analyseCoverage.test.mjs +++ b/analyseCoverage.test.mjs @@ -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"; @@ -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], { @@ -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], { @@ -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], { @@ -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], { @@ -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], { @@ -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], { diff --git a/changelog.md b/changelog.md index a2e5021..e46df5e 100644 --- a/changelog.md +++ b/changelog.md @@ -2,6 +2,10 @@ ## Next +### Major + +- Migrated from the Node.js builtin module `fs` to `fs/promises`. + ### Patch - Updated dependencies. diff --git a/coverage-node.test.mjs b/coverage-node.test.mjs index 6d4b9a5..999154f 100644 --- a/coverage-node.test.mjs +++ b/coverage-node.test.mjs @@ -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"; @@ -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", @@ -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 () => {}; @@ -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", @@ -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", @@ -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}"; @@ -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", @@ -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", @@ -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", @@ -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"; @@ -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", diff --git a/reportCliError.test.mjs b/reportCliError.test.mjs index ebd9f16..f7e0de1 100644 --- a/reportCliError.test.mjs +++ b/reportCliError.test.mjs @@ -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"; @@ -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}"; @@ -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}"; @@ -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}"; @@ -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}";