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

build-npm-launcher.ts

114 lines4.6 KB

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