Open History to see the last commit
1#!/usr/bin/env node
2
3import * as NodeRuntime from "@effect/platform-node/NodeRuntime";
4import * as NodeServices from "@effect/platform-node/NodeServices";
5import { Data, Effect, FileSystem, Logger, Option, Path } from "effect";
6import { Command, Flag } from "effect/unstable/cli";
7import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process";
8
9import { resolveCatalogDependencies } from "../../../scripts/lib/resolve-catalog.ts";
10import rootPackageJson from "../../../package.json" with { type: "json" };
11import serverPackageJson from "../package.json" with { type: "json" };
12
13class CliError extends Data.TaggedError("CliError")<{
14 readonly message: string;
15 readonly cause?: unknown;
16}> {}
17
18const RepoRoot = Effect.service(Path.Path).pipe(
19 Effect.flatMap((path) => path.fromFileUrl(new URL("../../..", import.meta.url))),
20);
21
22const runCommand = Effect.fn("runCommand")(function* (command: ChildProcess.Command) {
23 const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
24 const child = yield* spawner.spawn(command);
25 const exitCode = yield* child.exitCode;
26
27 if (exitCode !== 0) {
28 return yield* new CliError({
29 message: `Command exited with non-zero exit code (${exitCode})`,
30 });
31 }
32});
33
34// ---------------------------------------------------------------------------
35// build subcommand
36// ---------------------------------------------------------------------------
37
38const buildCmd = Command.make(
39 "build",
40 {
41 verbose: Flag.boolean("verbose").pipe(Flag.withDefault(false)),
42 },
43 (config) =>
44 Effect.gen(function* () {
45 const path = yield* Path.Path;
46 const fs = yield* FileSystem.FileSystem;
47 const repoRoot = yield* RepoRoot;
48 const serverDir = path.join(repoRoot, "apps/server");
49
50 yield* Effect.log("[cli] Running tsdown...");
51 yield* runCommand(
52 ChildProcess.make({
53 cwd: serverDir,
54 stdout: config.verbose ? "inherit" : "ignore",
55 stderr: "inherit",
56 })`bun tsdown`,
57 );
58
59 const webDist = path.join(repoRoot, "apps/web/dist");
60 const clientTarget = path.join(serverDir, "dist/client");
61
62 if (yield* fs.exists(webDist)) {
63 yield* fs.copy(webDist, clientTarget);
64 yield* Effect.log("[cli] Bundled web app into dist/client");
65 } else {
66 yield* Effect.logWarning("[cli] Web dist not found — skipping client bundle.");
67 }
68 }),
69).pipe(Command.withDescription("Build the server package (tsdown + bundle web client)."));
70
71// ---------------------------------------------------------------------------
72// publish subcommand
73// ---------------------------------------------------------------------------
74
75const publishCmd = Command.make(
76 "publish",
77 {
78 tag: Flag.string("tag").pipe(Flag.withDefault("latest")),
79 access: Flag.string("access").pipe(Flag.withDefault("public")),
80 appVersion: Flag.string("app-version").pipe(Flag.optional),
81 provenance: Flag.boolean("provenance").pipe(Flag.withDefault(false)),
82 dryRun: Flag.boolean("dry-run").pipe(Flag.withDefault(false)),
83 verbose: Flag.boolean("verbose").pipe(Flag.withDefault(false)),
84 },
85 (config) =>
86 Effect.gen(function* () {
87 const path = yield* Path.Path;
88 const fs = yield* FileSystem.FileSystem;
89 const repoRoot = yield* RepoRoot;
90 const serverDir = path.join(repoRoot, "apps/server");
91 const packageJsonPath = path.join(serverDir, "package.json");
92 const backupPath = `${packageJsonPath}.bak`;
93
94 // Assert build assets exist
95 for (const relPath of ["dist/index.mjs", "dist/client/index.html"]) {
96 const abs = path.join(serverDir, relPath);
97 if (!(yield* fs.exists(abs))) {
98 return yield* new CliError({
99 message: `Missing build asset: ${abs}. Run the build subcommand first.`,
100 });
101 }
102 }
103
104 yield* Effect.acquireUseRelease(
105 // Acquire: backup package.json, resolve catalog: deps, strip devDependencies/scripts
106 Effect.gen(function* () {
107 const original = yield* fs.readFileString(packageJsonPath);
108 yield* fs.writeFileString(backupPath, original);
109
110 // Build package.json for publish
111 const version = Option.getOrElse(config.appVersion, () => serverPackageJson.version);
112 const pkg = {
113 name: serverPackageJson.name,
114 repository: serverPackageJson.repository,
115 bin: serverPackageJson.bin,
116 type: serverPackageJson.type,
117 version,
118 engines: serverPackageJson.engines,
119 files: serverPackageJson.files,
120 dependencies: serverPackageJson.dependencies as Record<string, unknown>,
121 };
122
123 // Resolve catalog: entries in production dependencies
124 pkg.dependencies = resolveCatalogDependencies(
125 pkg.dependencies,
126 rootPackageJson.workspaces.catalog,
127 "apps/server dependencies",
128 );
129
130 yield* fs.writeFileString(packageJsonPath, `${JSON.stringify(pkg, null, 2)}\n`);
131 yield* Effect.log("[cli] Resolved package.json for publish");
132 }),
133 // Use: npm publish
134 () =>
135 Effect.gen(function* () {
136 const args = ["publish", "--access", config.access, "--tag", config.tag];
137 if (config.provenance) args.push("--provenance");
138 if (config.dryRun) args.push("--dry-run");
139
140 yield* Effect.log(`[cli] Running: npm ${args.join(" ")}`);
141 yield* runCommand(
142 ChildProcess.make("npm", [...args], {
143 cwd: serverDir,
144 stdout: config.verbose ? "inherit" : "ignore",
145 stderr: "inherit",
146 }),
147 );
148 }),
149 // Release: restore
150 () =>
151 Effect.gen(function* () {
152 yield* fs.rename(backupPath, packageJsonPath);
153 if (config.verbose) yield* Effect.log("[cli] Restored original package.json");
154 }),
155 );
156 }),
157).pipe(Command.withDescription("Publish the server package to npm."));
158
159// ---------------------------------------------------------------------------
160// root command
161// ---------------------------------------------------------------------------
162
163const cli = Command.make("cli").pipe(
164 Command.withDescription("T3 server build & publish CLI."),
165 Command.withSubcommands([buildCmd, publishCmd]),
166);
167
168Command.run(cli, { version: "0.0.0" }).pipe(
169 Effect.scoped,
170 Effect.provide([Logger.layer([Logger.consolePretty()]), NodeServices.layer]),
171 NodeRuntime.runMain,
172);