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