mirror of
https://github.com/FranP-code/create-better-t-stack.git
synced 2025-10-12 23:52:15 +00:00
add cloudflare workers support (#326)
This commit is contained in:
@@ -105,6 +105,8 @@ export const dependencyVersionMap = {
|
||||
|
||||
"@tanstack/solid-query": "^5.75.0",
|
||||
"@tanstack/solid-query-devtools": "^5.75.0",
|
||||
|
||||
wrangler: "^4.20.0",
|
||||
} as const;
|
||||
|
||||
export type AvailableDependencies = keyof typeof dependencyVersionMap;
|
||||
|
||||
@@ -200,7 +200,11 @@ export async function setupEnvironmentVariables(
|
||||
databaseUrl = "mongodb://localhost:27017/mydatabase";
|
||||
break;
|
||||
case "sqlite":
|
||||
databaseUrl = "file:./local.db";
|
||||
if (config.runtime === "workers") {
|
||||
databaseUrl = "http://127.0.0.1:8080";
|
||||
} else {
|
||||
databaseUrl = "file:./local.db";
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -234,4 +238,11 @@ export async function setupEnvironmentVariables(
|
||||
];
|
||||
|
||||
await addEnvVariablesToFile(envPath, serverVars);
|
||||
|
||||
if (config.runtime === "workers") {
|
||||
const devVarsPath = path.join(serverDir, ".dev.vars");
|
||||
try {
|
||||
await fs.copy(envPath, devVarsPath);
|
||||
} catch (_err) {}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -93,7 +93,18 @@ export function displayPostInstallInstructions(
|
||||
)}\n`;
|
||||
output += `${pc.cyan(`${stepCounter++}.`)} ${runCmd} dev\n\n`;
|
||||
} else {
|
||||
output += `${pc.cyan(`${stepCounter++}.`)} ${runCmd} dev\n\n`;
|
||||
if (runtime !== "workers") {
|
||||
output += `${pc.cyan(`${stepCounter++}.`)} ${runCmd} dev\n`;
|
||||
}
|
||||
|
||||
if (runtime === "workers") {
|
||||
output += `${pc.cyan(`${stepCounter++}.`)} bun dev\n`;
|
||||
output += `${pc.cyan(
|
||||
`${stepCounter++}.`,
|
||||
)} cd apps/server && bun run cf-typegen\n\n`;
|
||||
} else {
|
||||
output += "\n";
|
||||
}
|
||||
}
|
||||
|
||||
output += `${pc.bold("Your project will be available at:")}\n`;
|
||||
|
||||
@@ -818,4 +818,17 @@ export async function handleExtras(
|
||||
await processTemplate(npmrcTemplateSrc, npmrcDest, context);
|
||||
}
|
||||
}
|
||||
|
||||
if (context.runtime === "workers") {
|
||||
const runtimeWorkersDir = path.join(PKG_ROOT, "templates/runtime/workers");
|
||||
if (await fs.pathExists(runtimeWorkersDir)) {
|
||||
await processAndCopyFiles(
|
||||
"**/*",
|
||||
runtimeWorkersDir,
|
||||
projectDir,
|
||||
context,
|
||||
false,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,6 +20,8 @@ export async function setupRuntime(config: ProjectConfig): Promise<void> {
|
||||
await setupBunRuntime(serverDir, backend);
|
||||
} else if (runtime === "node") {
|
||||
await setupNodeRuntime(serverDir, backend);
|
||||
} else if (runtime === "workers") {
|
||||
await setupWorkersRuntime(serverDir);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -80,3 +82,26 @@ async function setupNodeRuntime(
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async function setupWorkersRuntime(serverDir: string): Promise<void> {
|
||||
const packageJsonPath = path.join(serverDir, "package.json");
|
||||
if (!(await fs.pathExists(packageJsonPath))) return;
|
||||
|
||||
const packageJson = await fs.readJson(packageJsonPath);
|
||||
|
||||
packageJson.scripts = {
|
||||
...packageJson.scripts,
|
||||
dev: "wrangler dev --port=3000",
|
||||
start: "wrangler dev",
|
||||
deploy: "wrangler deploy",
|
||||
build: "wrangler deploy --dry-run",
|
||||
"cf-typegen": "wrangler types --env-interface CloudflareBindings",
|
||||
};
|
||||
|
||||
await fs.writeJson(packageJsonPath, packageJson, { spaces: 2 });
|
||||
|
||||
await addPackageDependency({
|
||||
devDependencies: ["wrangler"],
|
||||
projectDir: serverDir,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -57,13 +57,14 @@ export async function gatherConfig(
|
||||
runtime: ({ results }) =>
|
||||
getRuntimeChoice(flags.runtime, results.backend),
|
||||
database: ({ results }) =>
|
||||
getDatabaseChoice(flags.database, results.backend),
|
||||
getDatabaseChoice(flags.database, results.backend, results.runtime),
|
||||
orm: ({ results }) =>
|
||||
getORMChoice(
|
||||
flags.orm,
|
||||
results.database !== "none",
|
||||
results.database,
|
||||
results.backend,
|
||||
results.runtime,
|
||||
),
|
||||
api: ({ results }) =>
|
||||
getApiChoice(flags.api, results.frontend, results.backend),
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
import { cancel, isCancel, select } from "@clack/prompts";
|
||||
import pc from "picocolors";
|
||||
import { DEFAULT_CONFIG } from "../constants";
|
||||
import type { Backend, Database } from "../types";
|
||||
import type { Backend, Database, Runtime } from "../types";
|
||||
|
||||
export async function getDatabaseChoice(
|
||||
database?: Database,
|
||||
backend?: Backend,
|
||||
runtime?: Runtime,
|
||||
): Promise<Database> {
|
||||
if (backend === "convex" || backend === "none") {
|
||||
return "none";
|
||||
@@ -13,35 +14,44 @@ export async function getDatabaseChoice(
|
||||
|
||||
if (database !== undefined) return database;
|
||||
|
||||
const databaseOptions: Array<{
|
||||
value: Database;
|
||||
label: string;
|
||||
hint: string;
|
||||
}> = [
|
||||
{
|
||||
value: "none",
|
||||
label: "None",
|
||||
hint: "No database setup",
|
||||
},
|
||||
{
|
||||
value: "sqlite",
|
||||
label: "SQLite",
|
||||
hint: "lightweight, server-less, embedded relational database",
|
||||
},
|
||||
{
|
||||
value: "postgres",
|
||||
label: "PostgreSQL",
|
||||
hint: "powerful, open source object-relational database system",
|
||||
},
|
||||
{
|
||||
value: "mysql",
|
||||
label: "MySQL",
|
||||
hint: "popular open-source relational database system",
|
||||
},
|
||||
];
|
||||
|
||||
if (runtime !== "workers") {
|
||||
databaseOptions.push({
|
||||
value: "mongodb",
|
||||
label: "MongoDB",
|
||||
hint: "open-source NoSQL database that stores data in JSON-like documents called BSON",
|
||||
});
|
||||
}
|
||||
|
||||
const response = await select<Database>({
|
||||
message: "Select database",
|
||||
options: [
|
||||
{
|
||||
value: "none",
|
||||
label: "None",
|
||||
hint: "No database setup",
|
||||
},
|
||||
{
|
||||
value: "sqlite",
|
||||
label: "SQLite",
|
||||
hint: "lightweight, server-less, embedded relational database",
|
||||
},
|
||||
{
|
||||
value: "postgres",
|
||||
label: "PostgreSQL",
|
||||
hint: "powerful, open source object-relational database system",
|
||||
},
|
||||
{
|
||||
value: "mysql",
|
||||
label: "MySQL",
|
||||
hint: "popular open-source relational database system",
|
||||
},
|
||||
{
|
||||
value: "mongodb",
|
||||
label: "MongoDB",
|
||||
hint: "open-source NoSQL database that stores data in JSON-like documents called BSON",
|
||||
},
|
||||
],
|
||||
options: databaseOptions,
|
||||
initialValue: DEFAULT_CONFIG.database,
|
||||
});
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { cancel, isCancel, select } from "@clack/prompts";
|
||||
import pc from "picocolors";
|
||||
import { DEFAULT_CONFIG } from "../constants";
|
||||
import type { Backend, Database, ORM } from "../types";
|
||||
import type { Backend, Database, ORM, Runtime } from "../types";
|
||||
|
||||
const ormOptions = {
|
||||
prisma: {
|
||||
@@ -26,6 +26,7 @@ export async function getORMChoice(
|
||||
hasDatabase: boolean,
|
||||
database?: Database,
|
||||
backend?: Backend,
|
||||
runtime?: Runtime,
|
||||
): Promise<ORM> {
|
||||
if (backend === "convex") {
|
||||
return "none";
|
||||
@@ -34,6 +35,10 @@ export async function getORMChoice(
|
||||
if (!hasDatabase) return "none";
|
||||
if (orm !== undefined) return orm;
|
||||
|
||||
if (runtime === "workers") {
|
||||
return "drizzle";
|
||||
}
|
||||
|
||||
const options = [
|
||||
...(database === "mongodb"
|
||||
? [ormOptions.prisma, ormOptions.mongoose]
|
||||
|
||||
@@ -17,20 +17,34 @@ export async function getRuntimeChoice(
|
||||
return "node";
|
||||
}
|
||||
|
||||
const runtimeOptions: Array<{
|
||||
value: Runtime;
|
||||
label: string;
|
||||
hint: string;
|
||||
}> = [
|
||||
{
|
||||
value: "bun",
|
||||
label: "Bun",
|
||||
hint: "Fast all-in-one JavaScript runtime",
|
||||
},
|
||||
{
|
||||
value: "node",
|
||||
label: "Node.js",
|
||||
hint: "Traditional Node.js runtime",
|
||||
},
|
||||
];
|
||||
|
||||
if (backend === "hono") {
|
||||
runtimeOptions.push({
|
||||
value: "workers",
|
||||
label: "Cloudflare Workers (beta)",
|
||||
hint: "Edge runtime on Cloudflare's global network",
|
||||
});
|
||||
}
|
||||
|
||||
const response = await select<Runtime>({
|
||||
message: "Select runtime",
|
||||
options: [
|
||||
{
|
||||
value: "bun",
|
||||
label: "Bun",
|
||||
hint: "Fast all-in-one JavaScript runtime",
|
||||
},
|
||||
{
|
||||
value: "node",
|
||||
label: "Node.js",
|
||||
hint: "Traditional Node.js runtime",
|
||||
},
|
||||
],
|
||||
options: runtimeOptions,
|
||||
initialValue: DEFAULT_CONFIG.runtime,
|
||||
});
|
||||
|
||||
|
||||
@@ -16,8 +16,10 @@ export const BackendSchema = z
|
||||
export type Backend = z.infer<typeof BackendSchema>;
|
||||
|
||||
export const RuntimeSchema = z
|
||||
.enum(["bun", "node", "none"])
|
||||
.describe("Runtime environment");
|
||||
.enum(["bun", "node", "workers", "none"])
|
||||
.describe(
|
||||
"Runtime environment (workers only available with hono backend and drizzle orm)",
|
||||
);
|
||||
export type Runtime = z.infer<typeof RuntimeSchema>;
|
||||
|
||||
export const FrontendSchema = z
|
||||
|
||||
@@ -28,9 +28,9 @@ export async function processTemplate(
|
||||
}
|
||||
}
|
||||
|
||||
handlebars.registerHelper("or", (a, b) => a || b);
|
||||
|
||||
handlebars.registerHelper("eq", (a, b) => a === b);
|
||||
handlebars.registerHelper("and", (a, b) => a && b);
|
||||
handlebars.registerHelper("or", (a, b) => a || b);
|
||||
|
||||
handlebars.registerHelper(
|
||||
"includes",
|
||||
|
||||
@@ -358,6 +358,78 @@ export function processAndValidateFlags(
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
if (
|
||||
providedFlags.has("runtime") &&
|
||||
options.runtime === "workers" &&
|
||||
config.backend &&
|
||||
config.backend !== "hono"
|
||||
) {
|
||||
consola.fatal(
|
||||
`Cloudflare Workers runtime (--runtime workers) is only supported with Hono backend (--backend hono). Current backend: ${config.backend}. Please use '--backend hono' or choose a different runtime.`,
|
||||
);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
if (
|
||||
providedFlags.has("backend") &&
|
||||
config.backend &&
|
||||
config.backend !== "hono" &&
|
||||
config.runtime === "workers"
|
||||
) {
|
||||
consola.fatal(
|
||||
`Backend '${config.backend}' is not compatible with Cloudflare Workers runtime. Cloudflare Workers runtime is only supported with Hono backend. Please use '--backend hono' or choose a different runtime.`,
|
||||
);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
if (
|
||||
providedFlags.has("runtime") &&
|
||||
options.runtime === "workers" &&
|
||||
config.orm &&
|
||||
config.orm !== "drizzle" &&
|
||||
config.orm !== "none"
|
||||
) {
|
||||
consola.fatal(
|
||||
`Cloudflare Workers runtime (--runtime workers) is only supported with Drizzle ORM (--orm drizzle) or no ORM (--orm none). Current ORM: ${config.orm}. Please use '--orm drizzle', '--orm none', or choose a different runtime.`,
|
||||
);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
if (
|
||||
providedFlags.has("orm") &&
|
||||
config.orm &&
|
||||
config.orm !== "drizzle" &&
|
||||
config.orm !== "none" &&
|
||||
config.runtime === "workers"
|
||||
) {
|
||||
consola.fatal(
|
||||
`ORM '${config.orm}' is not compatible with Cloudflare Workers runtime. Cloudflare Workers runtime is only supported with Drizzle ORM or no ORM. Please use '--orm drizzle', '--orm none', or choose a different runtime.`,
|
||||
);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
if (
|
||||
providedFlags.has("runtime") &&
|
||||
options.runtime === "workers" &&
|
||||
config.database === "mongodb"
|
||||
) {
|
||||
consola.fatal(
|
||||
"Cloudflare Workers runtime (--runtime workers) is not compatible with MongoDB database. MongoDB requires Prisma or Mongoose ORM, but Workers runtime only supports Drizzle ORM. Please use a different database or runtime.",
|
||||
);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
if (
|
||||
providedFlags.has("database") &&
|
||||
config.database === "mongodb" &&
|
||||
config.runtime === "workers"
|
||||
) {
|
||||
consola.fatal(
|
||||
"MongoDB database is not compatible with Cloudflare Workers runtime. MongoDB requires Prisma or Mongoose ORM, but Workers runtime only supports Drizzle ORM. Please use a different database or runtime.",
|
||||
);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
return config;
|
||||
}
|
||||
|
||||
@@ -368,6 +440,33 @@ export function validateConfigCompatibility(
|
||||
const effectiveBackend = config.backend;
|
||||
const effectiveFrontend = config.frontend;
|
||||
const effectiveApi = config.api;
|
||||
const effectiveRuntime = config.runtime;
|
||||
|
||||
if (effectiveRuntime === "workers" && effectiveBackend !== "hono") {
|
||||
consola.fatal(
|
||||
`Cloudflare Workers runtime is only supported with Hono backend. Current backend: ${effectiveBackend}. Please use a different runtime or change to Hono backend.`,
|
||||
);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const effectiveOrm = config.orm;
|
||||
if (
|
||||
effectiveRuntime === "workers" &&
|
||||
effectiveOrm !== "drizzle" &&
|
||||
effectiveOrm !== "none"
|
||||
) {
|
||||
consola.fatal(
|
||||
`Cloudflare Workers runtime is only supported with Drizzle ORM or no ORM. Current ORM: ${effectiveOrm}. Please use a different runtime or change to Drizzle ORM or no ORM.`,
|
||||
);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
if (effectiveRuntime === "workers" && effectiveDatabase === "mongodb") {
|
||||
consola.fatal(
|
||||
"Cloudflare Workers runtime is not compatible with MongoDB database. MongoDB requires Prisma or Mongoose ORM, but Workers runtime only supports Drizzle ORM. Please use a different database or runtime.",
|
||||
);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const includesNuxt = effectiveFrontend?.includes("nuxt");
|
||||
const includesSvelte = effectiveFrontend?.includes("svelte");
|
||||
|
||||
Reference in New Issue
Block a user