The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import * as Effect from "effect/Effect"; |
| 2 | import * as FileSystem from "effect/FileSystem"; |
| 3 | import * as Path from "effect/Path"; |
| 4 | import { runBuildChild } from "../core/BuildChild.ts"; |
| 5 | import { DeployTargetError, makeDeployTarget } from "../core/index.ts"; |
| 6 | import { |
| 7 | NODE_BUNDLE_CONDITIONS, |
| 8 | NODE_SERVE_ENTRY_FILE_NAME, |
| 9 | relativeClientDirExpression, |
| 10 | writeNodeServeEntry, |
| 11 | } from "../core/NodeServe.ts"; |
| 12 | import { make, type FoldkitTarget, type FoldkitTargetConfig } from "./Foldkit.ts"; |
| 13 | |
| 14 | const fail = (message: string, cause?: unknown) => |
| 15 | new DeployTargetError({ platform: "node", message, cause }); |
| 16 | |
| 17 | const makeNodeFinishTarget = (config: FoldkitTargetConfig = {}): FoldkitTarget => |
| 18 | makeDeployTarget({ |
| 19 | platform: "node", |
| 20 | config, |
| 21 | bundle: { conditions: [...NODE_BUNDLE_CONDITIONS] }, |
| 22 | finish: (output, context) => |
| 23 | Effect.gen(function* () { |
| 24 | const fs = yield* FileSystem.FileSystem; |
| 25 | const path = yield* Path.Path; |
| 26 | if (!output.distDirectory || !output.clientDirectory) |
| 27 | return yield* fail("Foldkit produced no deployment or client directory."); |
| 28 | const server = path.join(output.distDirectory, "server"); |
| 29 | yield* fs.makeDirectory(server, { recursive: true }); |
| 30 | yield* fs.writeFileString(path.join(server, "package.json"), '{"type":"module"}\n'); |
| 31 | const servePath = path.join(server, NODE_SERVE_ENTRY_FILE_NAME); |
| 32 | const handler = |
| 33 | context.entry === undefined |
| 34 | ? undefined |
| 35 | : { |
| 36 | kind: "fetch" as const, |
| 37 | imports: `import entry from ${JSON.stringify("./" + path.relative(server, context.entry).replaceAll("\\", "/"))};`, |
| 38 | expr: "(request) => entry.fetch(request)", |
| 39 | }; |
| 40 | return yield* writeNodeServeEntry({ |
| 41 | output, |
| 42 | servePath, |
| 43 | serveModuleName: `server/${NODE_SERVE_ENTRY_FILE_NAME}`, |
| 44 | clientDirExpression: relativeClientDirExpression(servePath, output.clientDirectory), |
| 45 | handler, |
| 46 | assetBasePath: context.assetBasePath, |
| 47 | serveRootIndex: true, |
| 48 | notFoundHandling: |
| 49 | config.notFoundHandling ?? (handler || config.output === "static" ? "none" : "spa"), |
| 50 | htmlHandling: config.htmlHandling, |
| 51 | platform: "node", |
| 52 | }); |
| 53 | }).pipe( |
| 54 | Effect.catchTag("PlatformError", (cause) => |
| 55 | Effect.fail(fail("Failed to emit the Foldkit Node entry", cause)), |
| 56 | ), |
| 57 | ), |
| 58 | }); |
| 59 | |
| 60 | /** Isolate Vite configuration execution from the deployment process. */ |
| 61 | export const buildInChild = (input: { |
| 62 | readonly rootDir: string; |
| 63 | readonly config: FoldkitTargetConfig; |
| 64 | }) => |
| 65 | Effect.gen(function* () { |
| 66 | const framework = yield* make({ |
| 67 | ...input.config, |
| 68 | root: input.rootDir, |
| 69 | target: makeNodeFinishTarget(input.config), |
| 70 | }); |
| 71 | return yield* framework.build({ root: input.rootDir }); |
| 72 | }); |
| 73 | |
| 74 | export const makeNodeTarget = (config: FoldkitTargetConfig = {}): FoldkitTarget => ({ |
| 75 | ...makeNodeFinishTarget(config), |
| 76 | build: (context) => |
| 77 | runBuildChild({ |
| 78 | module: import.meta.url, |
| 79 | rootDir: context.root, |
| 80 | env: context.env, |
| 81 | framework: "foldkit", |
| 82 | config: { rootDir: context.root, config }, |
| 83 | }).pipe(Effect.mapError((error) => fail(error.message, error.cause))), |
| 84 | }); |
| 85 | export const target = makeNodeTarget; |
| 86 | export default makeNodeTarget; |