From 3e807f4f13f3ce70ff57025ebfb0568e8561b99b Mon Sep 17 00:00:00 2001 From: Denis Davidyuk Date: Fri, 20 Dec 2024 18:03:42 +0100 Subject: [PATCH] refactor: use fs.cp instead of own implementation --- src/init/init.js | 13 +++++------ src/utils/fs-utils.js | 50 ++++++++++++++++++++----------------------- 2 files changed, 28 insertions(+), 35 deletions(-) diff --git a/src/init/init.js b/src/init/init.js index ddf5b205..3728806f 100644 --- a/src/init/init.js +++ b/src/init/init.js @@ -2,10 +2,7 @@ import fs from "fs"; import path from "path"; import { exec } from "promisify-child-process"; import { print } from "../utils/utils.js"; -import { - copyFolderRecursiveSync, - deleteWithPrompt, -} from "../utils/fs-utils.js"; +import { copyFolderRecursive, deleteWithPrompt } from "../utils/fs-utils.js"; import { fileURLToPath } from "url"; import { readFile } from "fs/promises"; @@ -40,7 +37,7 @@ async function patchForNextRelease(folder, y) { const fileSource = `${__dirname}${constants.nextArtifactsDir}`; - await copyFolderRecursiveSync(fileSource, folder, y); + await copyFolderRecursive(fileSource, folder, y); } function checkNodeVersion() { @@ -117,11 +114,11 @@ async function installDependencies(folder, update = false) { async function setupArtifacts(folder) { print("===== creating project file and directory structure ====="); - await copyFolderRecursiveSync( + await copyFolderRecursive( `${__dirname}${constants.updateArtifactsDir}`, path.join(constants.artifactsDest, folder), ); - await copyFolderRecursiveSync( + await copyFolderRecursive( `${__dirname}${constants.artifactsDir}`, path.join(constants.artifactsDest, folder), ); @@ -137,7 +134,7 @@ async function updateArtifacts(folder, y) { await deleteWithPrompt(artifact, y); }, Promise.resolve()); - await copyFolderRecursiveSync(fileSource, folder, y); + await copyFolderRecursive(fileSource, folder, y); } export default { run }; diff --git a/src/utils/fs-utils.js b/src/utils/fs-utils.js index ef0d033a..1dabf854 100644 --- a/src/utils/fs-utils.js +++ b/src/utils/fs-utils.js @@ -1,5 +1,4 @@ -import fs from "fs"; -import path from "path"; +import fs from "fs/promises"; import prompts from "prompts"; async function prompt(action, target) { @@ -13,34 +12,31 @@ async function prompt(action, target) { return input === "YES" || input === "yes" || input === "Y" || input === "y"; } -export async function copyFolderRecursiveSync(srcDir, dstDir, y = false) { - let src; - let dst; - - return fs.readdirSync(srcDir).reduce(async (accPromise, file) => { - await accPromise; - src = path.join(srcDir, file); - dst = path.join(dstDir, file); - - const stat = fs.statSync(src); - if (stat && stat.isDirectory()) { - if (!fs.existsSync(dst)) { - fs.mkdirSync(dst); - } +async function getStatOrNullIfMissed(path) { + try { + return await fs.stat(path); + } catch (error) { + if (error.code === "ENOENT") return null; + throw error; + } +} - await copyFolderRecursiveSync(src, dst, y); - } else if (!fs.existsSync(dst)) { - fs.writeFileSync(dst, fs.readFileSync(src)); - } else if (y || (await prompt("overwrite", dst))) { - fs.writeFileSync(dst, fs.readFileSync(src)); - } - }, Promise.resolve()); +export async function copyFolderRecursive(srcDir, dstDir, y = false) { + return fs.cp(srcDir, dstDir, { + recursive: true, + async filter(_src, dest) { + if (y) return true; + const stat = await getStatOrNullIfMissed(dest); + if (stat == null) return true; + if (stat.isDirectory()) return true; + return await prompt("overwrite", dest); + }, + }); } export async function deleteWithPrompt(target, y = false) { - if (fs.existsSync(target)) { - if (y || (await prompt("delete", target))) { - fs.rmSync(target, { recursive: true }); - } + if ((await getStatOrNullIfMissed(target)) == null) return; + if (y || (await prompt("delete", target))) { + await fs.rm(target, { recursive: true }); } }