mirror of
https://github.com/FranP-code/create-better-t-stack.git
synced 2025-10-12 23:52:15 +00:00
Add merge and rename options for existing directories
This commit is contained in:
5
.changeset/moody-taxis-post.md
Normal file
5
.changeset/moody-taxis-post.md
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"create-better-t-stack": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Add merge and rename options for existing directories
|
||||||
@@ -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);
|
||||||
|
|||||||
Reference in New Issue
Block a user