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:
@@ -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,
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user