Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

build-npm-launcher.ts

125 lines5.1 KB

1#!/usr/bin/env node
2/**
3 * Builds the `t3` npm package behind `npx t3` / `npm i -g t3`: a launcher
4 * with no dependencies that downloads this exact version's CLI archive from
5 * the GitHub Release on first run (scripts/npm-launcher/t3.mjs).
6 * The executables are not published to npm. npm's publish-time scanning held
7 * 250 MB platform packages back for up to an hour after the launcher went
8 * live, and every install in that window ran a launcher with nothing to
9 * launch.
10 *
11 * Output: `<output-dir>/t3/` (the package tree) and `<output-dir>/t3.tgz`,
12 * which is what gets published.
13 */
14import * as NodeRuntime from "@effect/platform-node/NodeRuntime";
15import * as NodeServices from "@effect/platform-node/NodeServices";
16import * as Effect from "effect/Effect";
17import * as FileSystem from "effect/FileSystem";
18import * as Layer from "effect/Layer";
19import * as Logger from "effect/Logger";
20import * as Path from "effect/Path";
21import * as Schema from "effect/Schema";
22import { Command, Flag } from "effect/cli";
23import { ChildProcess, ChildProcessSpawner } from "effect/process";
24​
25import * as HostProcess from "@t3tools/shared/HostProcess";
26import { fromJsonStringPretty } from "@t3tools/shared/schemaJson";
27import serverPackageJson from "../apps/server/package.json" with { type: "json" };
28​
29import { windowsSystemTar } from "./build-cli-archive.ts";
30​
31export const NPM_LAUNCHER_PACKAGE_NAME = "t3";
32​
33const encodePackageJson = Schema.encodeEffect(fromJsonStringPretty(Schema.Unknown));
34​
35export class NpmLauncherCommandFailedError extends Schema.TaggedError<NpmLauncherCommandFailedError>()(
36 "NpmLauncherCommandFailedError",
37 { command: Schema.String, exitCode: Schema.Int },
38) {
39 override get message(): string {
40 return `${this.command} exited with code ${this.exitCode}.`;
41 }
43​
44/** package.json for the launcher. `engines` asks for `import.meta.dirname` and `fetch`. */
45export function npmLauncherPackageManifest(version: string) {
46 return {
47 name: NPM_LAUNCHER_PACKAGE_NAME,
48 version,
49 description:
50 "T3 Code CLI. Downloads the self-contained executable for this platform from the GitHub Release.",
51 license: serverPackageJson.license,
52 repository: serverPackageJson.repository,
53 bin: { t3: "./bin/t3.mjs" },
54 files: ["bin", "dist"],
55 engines: { node: ">=20.11" },
56 };
58​
59export const buildNpmLauncher = Effect.fn("buildNpmLauncher")(function* (input: {
60 readonly version: string;
61 readonly outputDir: string;
62}) {
63 const fs = yield* FileSystem.FileSystem;
64 const path = yield* Path.Path;
65 const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
66 const sourceDir = yield* path.fromFileUrl(new URL("./npm-launcher/", import.meta.url));
67​
68 yield* fs.makeDirectory(input.outputDir, { recursive: true });
69 const scratch = yield* fs.makeTempDirectoryScoped({
70 directory: input.outputDir,
71 prefix: ".launcher-",
72 });
73 const stageDir = path.join(scratch, "package");
74 yield* fs.makeDirectory(path.join(stageDir, "bin"), { recursive: true });
75 yield* fs.makeDirectory(path.join(stageDir, "dist"));
76 yield* fs.writeFileString(
77 path.join(stageDir, "package.json"),
78 `${yield* encodePackageJson(npmLauncherPackageManifest(input.version))}\n`,
79 );
80 yield* fs.copyFile(path.join(sourceDir, "t3.mjs"), path.join(stageDir, "bin/t3.mjs"));
81 yield* fs.chmod(path.join(stageDir, "bin/t3.mjs"), 0o755);
82 yield* fs.copyFile(path.join(sourceDir, "bin.mjs"), path.join(stageDir, "dist/bin.mjs"));
83 const readme = yield* path.fromFileUrl(new URL("../apps/server/README.md", import.meta.url));
84 if (yield* fs.exists(readme)) {
85 yield* fs.copyFile(readme, path.join(stageDir, "README.md"));
86 }
87​
88 const packageDir = path.join(input.outputDir, NPM_LAUNCHER_PACKAGE_NAME);
89 const tarball = path.join(input.outputDir, `${NPM_LAUNCHER_PACKAGE_NAME}.tgz`);
90 yield* fs.remove(tarball, { force: true });
91 const tar = (yield* HostProcess.Platform) === "win32" ? windowsSystemTar() : "tar";
92 // npm tarballs root everything under `package/`.
93 const child = yield* spawner.spawn(
94 ChildProcess.make(tar, ["-czf", tarball, "-C", scratch, "package"], {
95 stdout: "inherit",
96 stderr: "inherit",
97 }),
98 );
99 const exitCode = Number(yield* child.exitCode);
100 if (exitCode !== 0) {
101 return yield* new NpmLauncherCommandFailedError({ command: `tar (${tarball})`, exitCode });
102 }
103 yield* fs.remove(packageDir, { recursive: true, force: true });
104 yield* fs.rename(stageDir, packageDir);
105 yield* Effect.log(`[npm-launcher] Wrote ${packageDir} and ${tarball}`);
106 return { packageDir, tarball };
107}, Effect.scoped);
108​
109const command = Command.make(
110 "build-npm-launcher",
111 {
112 version: Flag.String("version").pipe(
113 Flag.withDescription("Exact release version; the launcher downloads this version's archive."),
114 ),
115 outputDir: Flag.String("output-dir").pipe(Flag.withDefault("npm-packages")),
116 },
117 buildNpmLauncher,
118).pipe(Command.withDescription("Build the t3 npm launcher package."));
119​
120if (import.meta.main) {
121 Command.run(command, { version: "0.0.0" }).pipe(
122 Effect.provide(Layer.mergeAll(Logger.layer([Logger.consolePretty()]), NodeServices.layer)),
123 NodeRuntime.runMain,
124 );