mirror of
https://github.com/FranP-code/create-better-t-stack.git
synced 2025-10-12 23:52:15 +00:00
allow specifying project path in prompt
This commit is contained in:
5
.changeset/eleven-pugs-obey.md
Normal file
5
.changeset/eleven-pugs-obey.md
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"create-better-t-stack": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
allow specifying project path in prompt
|
||||||
@@ -5,10 +5,12 @@ import pc from "picocolors";
|
|||||||
import { DEFAULT_CONFIG } from "../constants";
|
import { DEFAULT_CONFIG } from "../constants";
|
||||||
|
|
||||||
const INVALID_CHARS = ["<", ">", ":", '"', "|", "?", "*"];
|
const INVALID_CHARS = ["<", ">", ":", '"', "|", "?", "*"];
|
||||||
|
|
||||||
const MAX_LENGTH = 255;
|
const MAX_LENGTH = 255;
|
||||||
|
|
||||||
function validateDirectoryName(name: string): string | undefined {
|
function validateDirectoryName(name: string): string | undefined {
|
||||||
|
// Allow "." as it represents current directory
|
||||||
|
if (name === ".") return undefined;
|
||||||
|
|
||||||
if (!name) return "Project name cannot be empty";
|
if (!name) return "Project name cannot be empty";
|
||||||
if (name.length > MAX_LENGTH) {
|
if (name.length > MAX_LENGTH) {
|
||||||
return `Project name must be less than ${MAX_LENGTH} characters`;
|
return `Project name must be less than ${MAX_LENGTH} characters`;
|
||||||
@@ -30,6 +32,12 @@ function validateDirectoryName(name: string): string | undefined {
|
|||||||
|
|
||||||
export async function getProjectName(initialName?: string): Promise<string> {
|
export async function getProjectName(initialName?: string): Promise<string> {
|
||||||
if (initialName) {
|
if (initialName) {
|
||||||
|
if (initialName === ".") {
|
||||||
|
const projectDir = process.cwd();
|
||||||
|
if (fs.readdirSync(projectDir).length === 0) {
|
||||||
|
return initialName;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
const finalDirName = path.basename(initialName);
|
const finalDirName = path.basename(initialName);
|
||||||
const validationError = validateDirectoryName(finalDirName);
|
const validationError = validateDirectoryName(finalDirName);
|
||||||
if (!validationError) {
|
if (!validationError) {
|
||||||
@@ -42,9 +50,10 @@ export async function getProjectName(initialName?: string): Promise<string> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let isValid = false;
|
let isValid = false;
|
||||||
let projectName = "";
|
let projectPath = "";
|
||||||
let defaultName = DEFAULT_CONFIG.projectName;
|
let defaultName = DEFAULT_CONFIG.projectName;
|
||||||
let counter = 1;
|
let counter = 1;
|
||||||
|
|
||||||
@@ -55,18 +64,29 @@ export async function getProjectName(initialName?: string): Promise<string> {
|
|||||||
|
|
||||||
while (!isValid) {
|
while (!isValid) {
|
||||||
const response = await text({
|
const response = await text({
|
||||||
message: "What is your project named? (directory name or path)",
|
message:
|
||||||
|
"Enter your project name or path (relative to current directory)",
|
||||||
placeholder: defaultName,
|
placeholder: defaultName,
|
||||||
initialValue: initialName,
|
initialValue: initialName,
|
||||||
defaultValue: defaultName,
|
defaultValue: defaultName,
|
||||||
validate: (value) => {
|
validate: (value) => {
|
||||||
const nameToUse = value.trim() || defaultName;
|
const nameToUse = value.trim() || defaultName;
|
||||||
|
|
||||||
const finalDirName = path.basename(nameToUse);
|
if (nameToUse === ".") {
|
||||||
|
const dirContents = fs.readdirSync(process.cwd());
|
||||||
|
if (dirContents.length > 0) {
|
||||||
|
return "Current directory is not empty. Please choose a different directory.";
|
||||||
|
}
|
||||||
|
isValid = true;
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
const projectDir = path.resolve(process.cwd(), nameToUse);
|
||||||
|
const finalDirName = path.basename(projectDir);
|
||||||
|
|
||||||
const validationError = validateDirectoryName(finalDirName);
|
const validationError = validateDirectoryName(finalDirName);
|
||||||
if (validationError) return validationError;
|
if (validationError) return validationError;
|
||||||
|
|
||||||
const projectDir = path.resolve(process.cwd(), nameToUse);
|
|
||||||
if (!projectDir.startsWith(process.cwd())) {
|
if (!projectDir.startsWith(process.cwd())) {
|
||||||
return "Project path must be within current directory";
|
return "Project path must be within current directory";
|
||||||
}
|
}
|
||||||
@@ -74,7 +94,7 @@ export async function getProjectName(initialName?: string): Promise<string> {
|
|||||||
if (fs.pathExistsSync(projectDir)) {
|
if (fs.pathExistsSync(projectDir)) {
|
||||||
const dirContents = fs.readdirSync(projectDir);
|
const dirContents = fs.readdirSync(projectDir);
|
||||||
if (dirContents.length > 0) {
|
if (dirContents.length > 0) {
|
||||||
return `Directory "${nameToUse}" already exists and is not empty. Please choose a different name.`;
|
return `Directory "${nameToUse}" already exists and is not empty. Please choose a different name or path.`;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -88,8 +108,8 @@ export async function getProjectName(initialName?: string): Promise<string> {
|
|||||||
process.exit(0);
|
process.exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
projectName = response || defaultName;
|
projectPath = response || defaultName;
|
||||||
}
|
}
|
||||||
|
|
||||||
return projectName;
|
return projectPath;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user