feat(release): publish npx t3 as a launcher over per-platform executable packages (#11607)
Co-authored-by: Claude Fable 5 <[email protected]>
This commit is contained in:
1 parent
2f7616ef17
commit
91cd91c087
9 files changed
+819
-261
No files matched your search
+48
-177
@@ -6,69 +6,24 @@ import * as FileSystem from "effect/FileSystem";
|
||||
import * as Logger from "effect/Logger";
|
||||
import * as Option from "effect/Option";
|
||||
import * as Path from "effect/Path";
|
||||
import * as Schema from "effect/Schema";
|
||||
import { Command, Flag } from "effect/unstable/cli";
|
||||
import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process";
|
||||
|
||||
import {
|
||||
DEVELOPMENT_ICON_OVERRIDES,
|
||||
resolveWebAssetBrandForPackageVersion,
|
||||
resolveWebIconOverrides,
|
||||
} from "../../../scripts/lib/brand-assets.ts";
|
||||
import { DEVELOPMENT_ICON_OVERRIDES } from "../../../scripts/lib/brand-assets.ts";
|
||||
import { findEsmImportsOfExternalPackages } from "../../../scripts/lib/cli-external-packages.ts";
|
||||
import { resolveCatalogDependencies } from "../../../scripts/lib/resolve-catalog.ts";
|
||||
import { fromJsonStringPretty } from "@t3tools/shared/schemaJson";
|
||||
import { fromYaml } from "@t3tools/shared/schemaYaml";
|
||||
import { resolveSpawnCommand } from "@t3tools/shared/shell";
|
||||
import serverPackageJson from "../package.json" with { type: "json" };
|
||||
import {
|
||||
ServerCliBuildAssetMissingError,
|
||||
ServerCliCommandExitError,
|
||||
ServerCliDevelopmentIconSourceMissingError,
|
||||
ServerCliDevelopmentIconTargetMissingError,
|
||||
ServerCliExecutableImportError,
|
||||
ServerCliPublishIconSourceMissingError,
|
||||
ServerCliPublishIconTargetMissingError,
|
||||
} from "./cliErrors.ts";
|
||||
|
||||
interface PackageJson {
|
||||
name: string;
|
||||
repository: {
|
||||
type: string;
|
||||
url: string;
|
||||
directory: string;
|
||||
};
|
||||
bin: Record<string, string>;
|
||||
type: string;
|
||||
version: string;
|
||||
engines: Record<string, string>;
|
||||
files: string[];
|
||||
dependencies: Record<string, string>;
|
||||
overrides: Record<string, string>;
|
||||
}
|
||||
|
||||
const PackageJsonPrettyJson = fromJsonStringPretty(Schema.Unknown);
|
||||
const encodePackageJson = Schema.encodeEffect(PackageJsonPrettyJson);
|
||||
|
||||
const WorkspaceConfig = Schema.Struct({
|
||||
catalog: Schema.optional(Schema.Record(Schema.String, Schema.String)),
|
||||
overrides: Schema.optional(Schema.Record(Schema.String, Schema.String)),
|
||||
});
|
||||
type WorkspaceConfig = typeof WorkspaceConfig.Type;
|
||||
const decodeWorkspaceConfig = Schema.decodeEffect(fromYaml(WorkspaceConfig));
|
||||
|
||||
const RepoRoot = Effect.service(Path.Path).pipe(
|
||||
Effect.flatMap((path) => path.fromFileUrl(new URL("../../..", import.meta.url))),
|
||||
);
|
||||
|
||||
const readWorkspaceConfig = Effect.fn("readWorkspaceConfig")(function* () {
|
||||
const path = yield* Path.Path;
|
||||
const fs = yield* FileSystem.FileSystem;
|
||||
const repoRoot = yield* RepoRoot;
|
||||
const workspaceYaml = yield* fs.readFileString(path.join(repoRoot, "pnpm-workspace.yaml"));
|
||||
return yield* decodeWorkspaceConfig(workspaceYaml);
|
||||
});
|
||||
|
||||
const runCommand = Effect.fn("runCommand")(function* (command: ChildProcess.StandardCommand) {
|
||||
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
|
||||
const child = yield* spawner.spawn(command);
|
||||
@@ -84,36 +39,6 @@ const runCommand = Effect.fn("runCommand")(function* (command: ChildProcess.Stan
|
||||
}
|
||||
});
|
||||
|
||||
const preparePublishIcons = Effect.fn("preparePublishIcons")(function* (
|
||||
repoRoot: string,
|
||||
serverDir: string,
|
||||
version: string,
|
||||
) {
|
||||
const path = yield* Path.Path;
|
||||
const fs = yield* FileSystem.FileSystem;
|
||||
const brand = resolveWebAssetBrandForPackageVersion(version);
|
||||
const icons = resolveWebIconOverrides(brand, "dist/client").map((override) => ({
|
||||
sourcePath: path.join(repoRoot, override.sourceRelativePath),
|
||||
targetPath: path.join(serverDir, override.targetRelativePath),
|
||||
}));
|
||||
|
||||
for (const icon of icons) {
|
||||
if (!(yield* fs.exists(icon.sourcePath))) {
|
||||
return yield* new ServerCliPublishIconSourceMissingError({ sourcePath: icon.sourcePath });
|
||||
}
|
||||
if (!(yield* fs.exists(icon.targetPath))) {
|
||||
return yield* new ServerCliPublishIconTargetMissingError({ targetPath: icon.targetPath });
|
||||
}
|
||||
}
|
||||
|
||||
return yield* Effect.forEach(icons, (icon) =>
|
||||
Effect.all({
|
||||
original: fs.readFile(icon.targetPath),
|
||||
publish: fs.readFile(icon.sourcePath),
|
||||
}).pipe(Effect.map((contents) => ({ ...icon, ...contents }))),
|
||||
);
|
||||
});
|
||||
|
||||
const applyDevelopmentIconOverrides = Effect.fn("applyDevelopmentIconOverrides")(function* (
|
||||
repoRoot: string,
|
||||
serverDir: string,
|
||||
@@ -240,37 +165,21 @@ const buildExeCmd = Command.make(
|
||||
// publish subcommand
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
interface PublishCommandConfig {
|
||||
readonly access: string;
|
||||
readonly tag: string;
|
||||
readonly provenance: boolean;
|
||||
readonly dryRun: boolean;
|
||||
}
|
||||
|
||||
const createVpPmPublishArgs = (config: PublishCommandConfig): ReadonlyArray<string> => {
|
||||
const args = [
|
||||
"publish",
|
||||
"--filter",
|
||||
"t3",
|
||||
"--access",
|
||||
config.access,
|
||||
"--tag",
|
||||
config.tag,
|
||||
"--no-git-checks",
|
||||
];
|
||||
|
||||
if (config.provenance) args.push("--provenance");
|
||||
if (config.dryRun) args.push("--dry-run");
|
||||
|
||||
return args;
|
||||
};
|
||||
|
||||
/**
|
||||
* Publishes the tarballs scripts/build-npm-platform-packages.ts produced:
|
||||
* every `@t3code/t3-<platform>.tgz` first, `t3.tgz` (the launcher) last, so
|
||||
* the launcher is never installable before the executables it depends on.
|
||||
* Tarballs rather than directories because `npm publish <dir>` strips the
|
||||
* `node_modules/` the executable loads its native addons from.
|
||||
*/
|
||||
const publishCmd = Command.make(
|
||||
"publish",
|
||||
{
|
||||
packagesDir: Flag.string("packages-dir").pipe(
|
||||
Flag.withDescription("Output dir of scripts/build-npm-platform-packages.ts."),
|
||||
),
|
||||
tag: Flag.string("tag").pipe(Flag.withDefault("latest")),
|
||||
access: Flag.string("access").pipe(Flag.withDefault("public")),
|
||||
appVersion: Flag.string("app-version").pipe(Flag.optional),
|
||||
provenance: Flag.boolean("provenance").pipe(Flag.withDefault(false)),
|
||||
dryRun: Flag.boolean("dry-run").pipe(Flag.withDefault(false)),
|
||||
verbose: Flag.boolean("verbose").pipe(Flag.withDefault(false)),
|
||||
@@ -279,86 +188,48 @@ const publishCmd = Command.make(
|
||||
Effect.gen(function* () {
|
||||
const path = yield* Path.Path;
|
||||
const fs = yield* FileSystem.FileSystem;
|
||||
const repoRoot = yield* RepoRoot;
|
||||
const serverDir = path.join(repoRoot, "apps/server");
|
||||
const packageJsonPath = path.join(serverDir, "package.json");
|
||||
|
||||
// Assert build assets exist
|
||||
for (const relPath of ["dist/bin.mjs", "dist/client/index.html"]) {
|
||||
const abs = path.join(serverDir, relPath);
|
||||
if (!(yield* fs.exists(abs))) {
|
||||
return yield* new ServerCliBuildAssetMissingError({ assetPath: abs });
|
||||
}
|
||||
// npm runs with cwd set to the packages dir below, so tarball paths are
|
||||
// resolved once here rather than joined twice.
|
||||
const packagesDir = path.resolve(config.packagesDir);
|
||||
const scopeDir = path.join(packagesDir, "@t3code");
|
||||
const launcherTarball = path.join(packagesDir, "t3.tgz");
|
||||
const platformTarballs = (yield* fs
|
||||
.readDirectory(scopeDir)
|
||||
.pipe(Effect.orElseSucceed((): ReadonlyArray<string> => [])))
|
||||
.filter((entry) => entry.startsWith("t3-") && entry.endsWith(".tgz"))
|
||||
.sort()
|
||||
.map((entry) => path.join(scopeDir, entry));
|
||||
if (platformTarballs.length === 0) {
|
||||
return yield* new ServerCliBuildAssetMissingError({
|
||||
assetPath: path.join(scopeDir, "t3-<platform>.tgz"),
|
||||
});
|
||||
}
|
||||
if (!(yield* fs.exists(launcherTarball))) {
|
||||
return yield* new ServerCliBuildAssetMissingError({ assetPath: launcherTarball });
|
||||
}
|
||||
|
||||
yield* Effect.acquireUseRelease(
|
||||
// Acquire: resolve publish metadata and read every original before mutation.
|
||||
Effect.gen(function* () {
|
||||
const version = Option.getOrElse(config.appVersion, () => serverPackageJson.version);
|
||||
const workspaceConfig = yield* readWorkspaceConfig();
|
||||
const workspaceCatalog = workspaceConfig.catalog ?? {};
|
||||
const workspaceOverrides = workspaceConfig.overrides ?? {};
|
||||
const pkg: PackageJson = {
|
||||
name: serverPackageJson.name,
|
||||
repository: serverPackageJson.repository,
|
||||
bin: serverPackageJson.bin,
|
||||
type: serverPackageJson.type,
|
||||
version,
|
||||
engines: serverPackageJson.engines,
|
||||
files: serverPackageJson.files,
|
||||
dependencies: resolveCatalogDependencies(
|
||||
serverPackageJson.dependencies,
|
||||
workspaceCatalog,
|
||||
"apps/server",
|
||||
),
|
||||
overrides: resolveCatalogDependencies(
|
||||
workspaceOverrides,
|
||||
workspaceCatalog,
|
||||
"apps/server",
|
||||
),
|
||||
};
|
||||
const args = ["publish", "--access", config.access, "--tag", config.tag];
|
||||
if (config.provenance) args.push("--provenance");
|
||||
if (config.dryRun) args.push("--dry-run");
|
||||
|
||||
return {
|
||||
packageJsonString: yield* encodePackageJson(pkg),
|
||||
originalPackageJson: yield* fs.readFile(packageJsonPath),
|
||||
icons: yield* preparePublishIcons(repoRoot, serverDir, version),
|
||||
};
|
||||
}),
|
||||
// Use: pnpm publish from the workspace root so pnpm-only workspace
|
||||
// config, including override selectors, is interpreted correctly.
|
||||
(resource) =>
|
||||
Effect.gen(function* () {
|
||||
yield* fs.writeFileString(packageJsonPath, `${resource.packageJsonString}\n`);
|
||||
for (const icon of resource.icons) {
|
||||
yield* fs.writeFile(icon.targetPath, icon.publish);
|
||||
}
|
||||
yield* Effect.log("[cli] Applied package metadata and publish icon overrides");
|
||||
|
||||
const args = createVpPmPublishArgs(config);
|
||||
const spawnCommand = yield* resolveSpawnCommand("vp", ["pm", ...args]);
|
||||
|
||||
yield* Effect.log(`[cli] Running: vp pm ${args.join(" ")}`);
|
||||
yield* runCommand(
|
||||
ChildProcess.make(spawnCommand.command, spawnCommand.args, {
|
||||
cwd: repoRoot,
|
||||
stdout: config.verbose ? "inherit" : "ignore",
|
||||
stderr: "inherit",
|
||||
shell: spawnCommand.shell,
|
||||
}),
|
||||
);
|
||||
for (const tarball of [...platformTarballs, launcherTarball]) {
|
||||
const spawnCommand = yield* resolveSpawnCommand("npm", [...args, tarball]);
|
||||
yield* Effect.log(`[cli] npm ${args.join(" ")} ${path.basename(tarball)}`);
|
||||
yield* runCommand(
|
||||
ChildProcess.make(spawnCommand.command, spawnCommand.args, {
|
||||
cwd: packagesDir,
|
||||
stdout: config.verbose ? "inherit" : "ignore",
|
||||
stderr: "inherit",
|
||||
shell: spawnCommand.shell,
|
||||
}),
|
||||
// Release: restore every file even if applying overrides or publishing fails.
|
||||
(resource) =>
|
||||
Effect.gen(function* () {
|
||||
yield* fs.writeFile(packageJsonPath, resource.originalPackageJson);
|
||||
for (const icon of resource.icons) {
|
||||
yield* fs.writeFile(icon.targetPath, icon.original);
|
||||
}
|
||||
if (config.verbose) yield* Effect.log("[cli] Restored original publish assets");
|
||||
}),
|
||||
);
|
||||
);
|
||||
}
|
||||
}),
|
||||
).pipe(Command.withDescription("Publish the server package to npm."));
|
||||
).pipe(
|
||||
Command.withDescription(
|
||||
"Publish the @t3code/t3-<platform> tarballs and then the t3 launcher to npm.",
|
||||
),
|
||||
);
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// root command
|
||||
|
||||
@@ -14,28 +14,6 @@ export class ServerCliCommandExitError extends Schema.TaggedError<ServerCliComma
|
||||
}
|
||||
}
|
||||
|
||||
export class ServerCliPublishIconSourceMissingError extends Schema.TaggedError<ServerCliPublishIconSourceMissingError>()(
|
||||
"ServerCliPublishIconSourceMissingError",
|
||||
{
|
||||
sourcePath: Schema.String,
|
||||
},
|
||||
) {
|
||||
override get message(): string {
|
||||
return `Missing publish icon source: ${this.sourcePath}`;
|
||||
}
|
||||
}
|
||||
|
||||
export class ServerCliPublishIconTargetMissingError extends Schema.TaggedError<ServerCliPublishIconTargetMissingError>()(
|
||||
"ServerCliPublishIconTargetMissingError",
|
||||
{
|
||||
targetPath: Schema.String,
|
||||
},
|
||||
) {
|
||||
override get message(): string {
|
||||
return `Missing publish icon target: ${this.targetPath}. Run the build subcommand first.`;
|
||||
}
|
||||
}
|
||||
|
||||
export class ServerCliDevelopmentIconSourceMissingError extends Schema.TaggedError<ServerCliDevelopmentIconSourceMissingError>()(
|
||||
"ServerCliDevelopmentIconSourceMissingError",
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user