Files
create-better-t-stack/apps/cli/src/prompts/web-deploy.ts
2025-08-09 12:06:23 +05:30

109 lines
2.3 KiB
TypeScript

import { isCancel, select } from "@clack/prompts";
import { DEFAULT_CONFIG } from "../constants";
import type { Backend, Frontend, Runtime, WebDeploy } from "../types";
import { WEB_FRAMEWORKS } from "../utils/compatibility";
import { exitCancelled } from "../utils/errors";
function hasWebFrontend(frontends: Frontend[]): boolean {
return frontends.some((f) => WEB_FRAMEWORKS.includes(f));
}
type DeploymentOption = {
value: WebDeploy;
label: string;
hint: string;
};
function getDeploymentDisplay(deployment: WebDeploy): {
label: string;
hint: string;
} {
if (deployment === "workers") {
return {
label: "Cloudflare Workers",
hint: "Deploy to Cloudflare Workers using Wrangler",
};
}
return {
label: deployment,
hint: `Add ${deployment} deployment`,
};
}
export async function getDeploymentChoice(
deployment?: WebDeploy,
_runtime?: Runtime,
_backend?: Backend,
frontend: Frontend[] = [],
): Promise<WebDeploy> {
if (deployment !== undefined) return deployment;
if (!hasWebFrontend(frontend)) {
return "none";
}
const options: DeploymentOption[] = [
{
value: "workers",
label: "Cloudflare Workers",
hint: "Deploy to Cloudflare Workers using Wrangler",
},
{ value: "none", label: "None", hint: "Manual setup" },
];
const response = await select<WebDeploy>({
message: "Select web deployment",
options,
initialValue: DEFAULT_CONFIG.webDeploy,
});
if (isCancel(response)) return exitCancelled("Operation cancelled");
return response;
}
export async function getDeploymentToAdd(
frontend: Frontend[],
existingDeployment?: WebDeploy,
): Promise<WebDeploy> {
if (!hasWebFrontend(frontend)) {
return "none";
}
const options: DeploymentOption[] = [];
if (existingDeployment !== "workers") {
const { label, hint } = getDeploymentDisplay("workers");
options.push({
value: "workers",
label,
hint,
});
}
if (existingDeployment && existingDeployment !== "none") {
return "none";
}
if (options.length > 0) {
options.push({
value: "none",
label: "None",
hint: "Skip deployment setup",
});
}
if (options.length === 0) {
return "none";
}
const response = await select<WebDeploy>({
message: "Select web deployment",
options,
initialValue: DEFAULT_CONFIG.webDeploy,
});
if (isCancel(response)) return exitCancelled("Operation cancelled");
return response;
}