mirror of
https://github.com/FranP-code/create-better-t-stack.git
synced 2025-10-12 23:52:15 +00:00
feat(cli): add alchemy and improve cli tooling and structure (#520)
This commit is contained in:
47
apps/cli/src/utils/project-name-validation.ts
Normal file
47
apps/cli/src/utils/project-name-validation.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
import path from "node:path";
|
||||
import { ProjectNameSchema } from "../types";
|
||||
import { exitWithError } from "./errors";
|
||||
|
||||
export function validateProjectName(name: string): void {
|
||||
const result = ProjectNameSchema.safeParse(name);
|
||||
if (!result.success) {
|
||||
exitWithError(
|
||||
`Invalid project name: ${
|
||||
result.error.issues[0]?.message || "Invalid project name"
|
||||
}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export function validateProjectNameThrow(name: string): void {
|
||||
const result = ProjectNameSchema.safeParse(name);
|
||||
if (!result.success) {
|
||||
throw new Error(`Invalid project name: ${result.error.issues[0]?.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
export function extractAndValidateProjectName(
|
||||
projectName?: string,
|
||||
projectDirectory?: string,
|
||||
throwOnError = false,
|
||||
): string {
|
||||
const derivedName =
|
||||
projectName ||
|
||||
(projectDirectory
|
||||
? path.basename(path.resolve(process.cwd(), projectDirectory))
|
||||
: "");
|
||||
|
||||
if (!derivedName) {
|
||||
return "";
|
||||
}
|
||||
|
||||
const nameToValidate = projectName ? path.basename(projectName) : derivedName;
|
||||
|
||||
if (throwOnError) {
|
||||
validateProjectNameThrow(nameToValidate);
|
||||
} else {
|
||||
validateProjectName(nameToValidate);
|
||||
}
|
||||
|
||||
return projectName || derivedName;
|
||||
}
|
||||
Reference in New Issue
Block a user