Skip to content

Commit

Permalink
refactor: use fs.cp instead of own implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
davidyuk committed Dec 20, 2024
1 parent 9a66b74 commit 3e807f4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 35 deletions.
13 changes: 5 additions & 8 deletions src/init/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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),
);
Expand All @@ -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 };
50 changes: 23 additions & 27 deletions src/utils/fs-utils.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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 });
}
}

0 comments on commit 3e807f4

Please sign in to comment.