mirror of
https://github.com/FranP-code/create-better-t-stack.git
synced 2025-10-12 23:52:15 +00:00
add orpc
This commit is contained in:
@@ -1,44 +1,27 @@
|
||||
import path from "node:path";
|
||||
import fs from "fs-extra";
|
||||
import type { ProjectBackend, ProjectRuntime } from "../types";
|
||||
import type { ProjectBackend, ProjectConfig, ProjectRuntime } from "../types";
|
||||
import { addPackageDependency } from "../utils/add-package-deps";
|
||||
|
||||
export async function setupRuntime(
|
||||
projectDir: string,
|
||||
runtime: ProjectRuntime,
|
||||
backendFramework: ProjectBackend,
|
||||
): Promise<void> {
|
||||
if (backendFramework === "next") {
|
||||
export async function setupRuntime(config: ProjectConfig): Promise<void> {
|
||||
const { projectName, runtime, backend } = config;
|
||||
const projectDir = path.resolve(process.cwd(), projectName);
|
||||
if (backend === "next") {
|
||||
return;
|
||||
}
|
||||
|
||||
const serverDir = path.join(projectDir, "apps/server");
|
||||
const serverIndexPath = path.join(serverDir, "src/index.ts");
|
||||
|
||||
const indexContent = await fs.readFile(serverIndexPath, "utf-8");
|
||||
|
||||
if (runtime === "bun") {
|
||||
await setupBunRuntime(
|
||||
serverDir,
|
||||
serverIndexPath,
|
||||
indexContent,
|
||||
backendFramework,
|
||||
);
|
||||
await setupBunRuntime(serverDir, backend);
|
||||
} else if (runtime === "node") {
|
||||
await setupNodeRuntime(
|
||||
serverDir,
|
||||
serverIndexPath,
|
||||
indexContent,
|
||||
backendFramework,
|
||||
);
|
||||
await setupNodeRuntime(serverDir, backend);
|
||||
}
|
||||
}
|
||||
|
||||
async function setupBunRuntime(
|
||||
serverDir: string,
|
||||
serverIndexPath: string,
|
||||
indexContent: string,
|
||||
backendFramework: ProjectBackend,
|
||||
backend: ProjectBackend,
|
||||
): Promise<void> {
|
||||
const packageJsonPath = path.join(serverDir, "package.json");
|
||||
const packageJson = await fs.readJson(packageJsonPath);
|
||||
@@ -51,22 +34,15 @@ async function setupBunRuntime(
|
||||
|
||||
await fs.writeJson(packageJsonPath, packageJson, { spaces: 2 });
|
||||
|
||||
addPackageDependency({
|
||||
await addPackageDependency({
|
||||
devDependencies: ["@types/bun"],
|
||||
projectDir: serverDir,
|
||||
});
|
||||
|
||||
if (backendFramework === "hono") {
|
||||
const updatedContent = `${indexContent}\n\nexport default app;\n`;
|
||||
await fs.writeFile(serverIndexPath, updatedContent);
|
||||
}
|
||||
}
|
||||
|
||||
async function setupNodeRuntime(
|
||||
serverDir: string,
|
||||
serverIndexPath: string,
|
||||
indexContent: string,
|
||||
backendFramework: ProjectBackend,
|
||||
backend: ProjectBackend,
|
||||
): Promise<void> {
|
||||
const packageJsonPath = path.join(serverDir, "package.json");
|
||||
const packageJson = await fs.readJson(packageJsonPath);
|
||||
@@ -79,62 +55,20 @@ async function setupNodeRuntime(
|
||||
|
||||
await fs.writeJson(packageJsonPath, packageJson, { spaces: 2 });
|
||||
|
||||
addPackageDependency({
|
||||
await addPackageDependency({
|
||||
devDependencies: ["tsx", "@types/node"],
|
||||
projectDir: serverDir,
|
||||
});
|
||||
|
||||
if (backendFramework === "hono") {
|
||||
addPackageDependency({
|
||||
if (backend === "hono") {
|
||||
await addPackageDependency({
|
||||
dependencies: ["@hono/node-server"],
|
||||
projectDir: serverDir,
|
||||
});
|
||||
|
||||
const importLine = 'import { serve } from "@hono/node-server";\n';
|
||||
const serverCode = `
|
||||
serve(
|
||||
{
|
||||
fetch: app.fetch,
|
||||
port: 3000,
|
||||
},
|
||||
(info) => {
|
||||
console.log(\`Server is running on http://localhost:\${info.port}\`);
|
||||
},
|
||||
);\n`;
|
||||
|
||||
if (!indexContent.includes("@hono/node-server")) {
|
||||
const importEndIndex = indexContent.lastIndexOf("import");
|
||||
const importSection = indexContent.substring(0, importEndIndex);
|
||||
const restOfFile = indexContent.substring(importEndIndex);
|
||||
|
||||
const updatedContent =
|
||||
importSection + importLine + restOfFile + serverCode;
|
||||
await fs.writeFile(serverIndexPath, updatedContent);
|
||||
}
|
||||
} else if (backendFramework === "elysia") {
|
||||
addPackageDependency({
|
||||
} else if (backend === "elysia") {
|
||||
await addPackageDependency({
|
||||
dependencies: ["@elysiajs/node"],
|
||||
projectDir: serverDir,
|
||||
});
|
||||
|
||||
if (!indexContent.includes("@elysiajs/node")) {
|
||||
const nodeImport = 'import { node } from "@elysiajs/node";\n';
|
||||
|
||||
const firstImportEnd = indexContent.indexOf(
|
||||
"\n",
|
||||
indexContent.indexOf("import"),
|
||||
);
|
||||
const before = indexContent.substring(0, firstImportEnd + 1);
|
||||
const after = indexContent.substring(firstImportEnd + 1);
|
||||
|
||||
let updatedContent = before + nodeImport + after;
|
||||
|
||||
updatedContent = updatedContent.replace(
|
||||
/const app = new Elysia\([^)]*\)/,
|
||||
"const app = new Elysia({ adapter: node() })",
|
||||
);
|
||||
|
||||
await fs.writeFile(serverIndexPath, updatedContent);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user