add cloudflare workers support (#326)

This commit is contained in:
Aman Varshney
2025-06-16 22:55:26 +05:30
committed by GitHub
parent 5fc1ba164e
commit b34e94a09e
34 changed files with 556 additions and 538 deletions

View File

@@ -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) {}
}
}

View File

@@ -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`;

View File

@@ -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,
);
}
}
}

View File

@@ -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,
});
}