diff --git a/extra/download-dist.js b/extra/download-dist.js index dc64166c4..c184c2846 100644 --- a/extra/download-dist.js +++ b/extra/download-dist.js @@ -4,6 +4,7 @@ const tar = require("tar"); const packageJSON = require("../package.json"); const fs = require("fs"); +const rmSync = require("./fs-rmSync.js"); const version = packageJSON.version; const filename = "dist.tar.gz"; @@ -21,7 +22,7 @@ function download(url) { if (fs.existsSync("./dist")) { if (fs.existsSync("./dist-backup")) { - fs.rmdirSync("./dist-backup", { + rmSync("./dist-backup", { recursive: true }); } @@ -35,7 +36,7 @@ function download(url) { tarStream.on("close", () => { if (fs.existsSync("./dist-backup")) { - fs.rmdirSync("./dist-backup", { + rmSync("./dist-backup", { recursive: true }); } diff --git a/extra/fs-rmSync.js b/extra/fs-rmSync.js new file mode 100644 index 000000000..8def68d0f --- /dev/null +++ b/extra/fs-rmSync.js @@ -0,0 +1,20 @@ +const fs = require("fs"); +/** + * Detect if `fs.rmSync` is available + * to avoid the runtime warning triggered for using `fs.rmdirSync` with `{ recursive: true }` in Node.js v16, + * or the `recursive` property removing completely in the future Node.js version. + * See the link below. + * @link https://nodejs.org/docs/latest-v16.x/api/deprecations.html#dep0147-fsrmdirpath--recursive-true- + * @param {fs.PathLike} path Valid types for path values in "fs". + * @param {fs.RmDirOptions} [options] options for `fs.rmdirSync`, if `fs.rmSync` is available and property `recursive` is true, it will automatically have property `force` with value `true`. + */ +const rmSync = (path, options) => { + if (typeof fs.rmSync === "function") { + if (options.recursive) { + options.force = true; + } + return fs.rmSync(path, options); + } + return fs.rmdirSync(path, options); +}; +module.exports = rmSync; diff --git a/extra/update-language-files/index.js b/extra/update-language-files/index.js index 7ba30cc05..1f58d01f3 100644 --- a/extra/update-language-files/index.js +++ b/extra/update-language-files/index.js @@ -3,6 +3,7 @@ import fs from "fs"; import path from "path"; import util from "util"; +import rmSync from "../fs-rmSync.js"; // https://stackoverflow.com/questions/13786160/copy-folder-recursively-in-node-js /** @@ -30,7 +31,7 @@ console.log("Arguments:", process.argv); const baseLangCode = process.argv[2] || "en"; console.log("Base Lang: " + baseLangCode); if (fs.existsSync("./languages")) { - fs.rmdirSync("./languages", { recursive: true }); + rmSync("./languages", { recursive: true }); } copyRecursiveSync("../../src/languages", "./languages"); @@ -61,7 +62,7 @@ for (const file of files) { // En first for (const key in en) { - if (! obj[key]) { + if (!obj[key]) { obj[key] = en[key]; } } @@ -69,7 +70,7 @@ for (const file of files) { if (baseLang !== en) { // Base second for (const key in baseLang) { - if (! obj[key]) { + if (!obj[key]) { obj[key] = key; } } @@ -82,5 +83,5 @@ for (const file of files) { fs.writeFileSync(`../../src/languages/${file}`, code); } -fs.rmdirSync("./languages", { recursive: true }); +rmSync("./languages", { recursive: true }); console.log("Done. Fixing formatting by ESLint..."); diff --git a/extra/update-version.js b/extra/update-version.js index 2e3b42da8..499426b8a 100644 --- a/extra/update-version.js +++ b/extra/update-version.js @@ -1,5 +1,6 @@ const pkg = require("../package.json"); const fs = require("fs"); +const rmSync = require("./fs-rmSync.js"); const child_process = require("child_process"); const util = require("../src/util"); @@ -11,14 +12,14 @@ const newVersion = process.argv[2]; console.log("Old Version: " + oldVersion); console.log("New Version: " + newVersion); -if (! newVersion) { +if (!newVersion) { console.error("invalid version"); process.exit(1); } const exists = tagExists(newVersion); -if (! exists) { +if (!exists) { // Process package.json pkg.version = newVersion; @@ -55,7 +56,7 @@ function tag(version) { } function tagExists(version) { - if (! version) { + if (!version) { throw new Error("invalid version"); } @@ -93,7 +94,7 @@ function updateWiki(oldVersion, newVersion) { function safeDelete(dir) { if (fs.existsSync(dir)) { - fs.rmdirSync(dir, { + rmSync(dir, { recursive: true, }); } diff --git a/test/prepare-jest.js b/test/prepare-jest.js index 9dfaba7d9..3fd89d10b 100644 --- a/test/prepare-jest.js +++ b/test/prepare-jest.js @@ -1,9 +1,10 @@ const fs = require("fs"); +const rmSync = require("../extra/fs-rmSync.js"); const path = "./data/test-chrome-profile"; if (fs.existsSync(path)) { - fs.rmdirSync(path, { + rmSync(path, { recursive: true, }); } diff --git a/test/prepare-test-server.js b/test/prepare-test-server.js index 0e49c7fb9..1a9b04df1 100644 --- a/test/prepare-test-server.js +++ b/test/prepare-test-server.js @@ -1,9 +1,10 @@ const fs = require("fs"); +const rmSync = require("../extra/fs-rmSync.js"); const path = "./data/test"; if (fs.existsSync(path)) { - fs.rmdirSync(path, { + rmSync(path, { recursive: true, }); }