Add merge and rename options for existing directories

This commit is contained in:
Aman Varshney
2025-05-05 23:47:51 +05:30
parent 357dfbbbf9
commit a2469b63ff
2 changed files with 53 additions and 12 deletions

View File

@@ -0,0 +1,5 @@
---
"create-better-t-stack": patch
---
Add merge and rename options for existing directories

View File

@@ -1,11 +1,11 @@
import path from "node:path"; import path from "node:path";
import { import {
cancel, cancel,
confirm,
intro, intro,
isCancel, isCancel,
log, log,
outro, outro,
select,
spinner, spinner,
} from "@clack/prompts"; } from "@clack/prompts";
import { consola } from "consola"; import { consola } from "consola";
@@ -195,26 +195,62 @@ async function main() {
break; break;
} }
const shouldOverwrite = await confirm({ log.warn(
message: `Directory "${pc.yellow( `Directory "${pc.yellow(
currentPathInput, currentPathInput,
)}" already exists and is not empty. Overwrite and replace all existing files?`, )}" already exists and is not empty.`,
initialValue: false, );
const action = await select<"overwrite" | "merge" | "rename" | "cancel">({
message: "What would you like to do?",
options: [
{
value: "overwrite",
label: "Overwrite",
hint: "Empty the directory and create the project",
},
{
value: "merge",
label: "Merge",
hint: "Create project files inside, potentially overwriting conflicts",
},
{
value: "rename",
label: "Choose a different name/path",
hint: "Keep the existing directory and create a new one",
},
{ value: "cancel", label: "Cancel", hint: "Abort the process" },
],
initialValue: "rename",
}); });
if (isCancel(shouldOverwrite)) { if (isCancel(action)) {
cancel(pc.red("Operation cancelled.")); cancel(pc.red("Operation cancelled."));
process.exit(0); process.exit(0);
} }
if (shouldOverwrite) { if (action === "overwrite") {
finalPathInput = currentPathInput; finalPathInput = currentPathInput;
shouldClearDirectory = true; shouldClearDirectory = true;
break; break;
} }
if (action === "merge") {
log.info("Please choose a different project name or path."); finalPathInput = currentPathInput;
currentPathInput = await getProjectName(undefined); shouldClearDirectory = false;
log.info(
`Proceeding into existing directory "${pc.yellow(
currentPathInput,
)}". Files may be overwritten.`,
);
break;
}
if (action === "rename") {
log.info("Please choose a different project name or path.");
currentPathInput = await getProjectName(undefined);
} else if (action === "cancel") {
cancel(pc.red("Operation cancelled."));
process.exit(0);
}
} }
if (finalPathInput === ".") { if (finalPathInput === ".") {
@@ -236,10 +272,11 @@ async function main() {
consola.error(error); consola.error(error);
process.exit(1); process.exit(1);
} }
} else {
await fs.ensureDir(finalResolvedPath);
} }
const flagConfig = processAndValidateFlags(options, finalBaseName); const flagConfig = processAndValidateFlags(options, finalBaseName);
const { projectName: _projectNameFromFlags, ...otherFlags } = flagConfig; const { projectName: _projectNameFromFlags, ...otherFlags } = flagConfig;
if (!options.yes && Object.keys(otherFlags).length > 0) { if (!options.yes && Object.keys(otherFlags).length > 0) {
@@ -786,7 +823,6 @@ main().catch((err) => {
) { ) {
consola.error(err.message); consola.error(err.message);
consola.error(err.stack); consola.error(err.stack);
} else {
} }
} else { } else {
console.error(err); console.error(err);