Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions apps/desktop/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { defaultInclude, defineConfig } from "vite-plus";

import { isDesktopRuntimeExternalDependency } from "../../scripts/lib/desktop-external-packages.ts";
import { loadRepoEnv } from "../../scripts/lib/public-config.ts";
import { releaseRepositoryDefine } from "../../scripts/lib/release-repository.ts";

const repoEnv = loadRepoEnv();

Expand All @@ -11,6 +12,7 @@ const isMainProcessExternal = (id: string) =>
id === "electron" || id.startsWith("electron/") || isDesktopRuntimeExternalDependency(id);
const shouldLaunchElectronAfterPack = process.env.T3CODE_DESKTOP_DEV === "1";
const publicConfigDefine = {
...releaseRepositoryDefine(),
__T3CODE_BUILD_CLERK_PUBLISHABLE_KEY__: JSON.stringify(
repoEnv.T3CODE_CLERK_PUBLISHABLE_KEY?.trim() ?? "",
),
Expand Down
2 changes: 2 additions & 0 deletions apps/server/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { defineConfig, mergeConfig } from "vite-plus";

import baseConfig from "../../vite.config.ts";
import { loadRepoEnv } from "../../scripts/lib/public-config.ts";
import { releaseRepositoryDefine } from "../../scripts/lib/release-repository.ts";
import rootPackageJson from "../../package.json" with { type: "json" };
import packageJson from "./package.json" with { type: "json" };
import { WeightedShardSequencer } from "./src/testUtils/weightedShardSequencer.ts";
Expand Down Expand Up @@ -126,6 +127,7 @@ export default mergeConfig(
js: "#!/usr/bin/env node\n",
},
define: {
...releaseRepositoryDefine(),
__T3CODE_BUILD_CHANNEL__: JSON.stringify(cliBuildChannel),
__T3CODE_TELEMETRY_CHANNEL__: JSON.stringify(telemetryChannel),
__T3CODE_BUILD_RELAY_URL__: JSON.stringify(repoEnv.T3CODE_RELAY_URL?.trim() ?? ""),
Expand Down
1 change: 1 addition & 0 deletions apps/web/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"scripts/verify-built-boot.ts",
"scripts/warm-dep-cache.ts",
"../../scripts/lib/public-config.ts",
"../../scripts/lib/release-repository.ts",
"../../scripts/lib/third-party-licenses.ts"
]
}
2 changes: 2 additions & 0 deletions apps/web/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
} from "@t3tools/shared/devProxy";

import { loadRepoEnv } from "../../scripts/lib/public-config";
import { releaseRepositoryDefine } from "../../scripts/lib/release-repository";
import { thirdPartyLicensesPlugin } from "../../scripts/lib/third-party-licenses";
import { tailwindPlugins } from "./vite/tailwind";
import { warmBundledDevPlugin } from "./vite/warmBundledDev";
Expand Down Expand Up @@ -298,6 +299,7 @@ export default defineConfig(() => {
],
},
define: {
...releaseRepositoryDefine(),
"import.meta.env.VITE_WS_URL": JSON.stringify(configuredWsUrl ?? ""),
// Override .env URLs with empty values in single-origin dev so clients use window.location.origin.
"import.meta.env.VITE_HTTP_URL": JSON.stringify(configuredHttpUrl ?? ""),
Expand Down
1 change: 1 addition & 0 deletions docs/operations/release.md
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,7 @@ available.
- Repository slug source:
- `T3CODE_DESKTOP_UPDATE_REPOSITORY` (format `owner/repo`), if set.
- otherwise `GITHUB_REPOSITORY` from GitHub Actions.
- The server, `t3 update` and desktop SSH installs bake in the same slug (`scripts/lib/release-repository.ts`).
- Required release assets for updater:
- platform installers (`.exe`, `.dmg`, `.AppImage`, `.deb`, plus macOS `.zip` for Squirrel.Mac update payloads)
- channel metadata: `latest*.yml` for stable releases, `nightly*.yml` for nightly releases
Expand Down
49 changes: 47 additions & 2 deletions packages/shared/src/cliRelease.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { describe, expect, it } from "vite-plus/test";
import { afterEach, describe, expect, it, vi } from "vite-plus/test";

import { cliArchiveTarCommand, newestCliReleaseVersion, parseChecksums } from "./cliRelease.ts";
import {
cliArchiveTarCommand,
newestCliReleaseVersion,
parseChecksums,
resolveCliReleaseRepository,
} from "./cliRelease.ts";

describe("cliRelease", () => {
it("parses sha256sum output including binary-mode markers", () => {
Expand Down Expand Up @@ -40,3 +45,43 @@ describe("cliRelease", () => {
expect(newestCliReleaseVersion([{ tag_name: "v1.2.3" }], "preview")).toBeUndefined();
});
});

describe("cliRelease repository", () => {
afterEach(() => {
vi.unstubAllGlobals();
vi.resetModules();
});

it("defaults to upstream when the build names no repository", () => {
expect(resolveCliReleaseRepository(undefined)).toBe("pingdotgg/t3code");
expect(resolveCliReleaseRepository("")).toBe("pingdotgg/t3code");
expect(resolveCliReleaseRepository(" ")).toBe("pingdotgg/t3code");
});

it("lists and downloads releases only from the repository the build names", async () => {
vi.stubGlobal("__T3CODE_RELEASE_REPOSITORY__", "fork-owner/t3code");
vi.resetModules();
const release = await import("./cliRelease.ts");
expect(release.cliReleaseIndexPageUrl(2)).toBe(
"https://api.github.com/repos/fork-owner/t3code/releases?per_page=100&page=2",
);
expect(release.cliReleaseDownloadBaseUrl("0.1.9-fork.1")).toBe(
"https://github.com/fork-owner/t3code/releases/download/v0.1.9-fork.1",
);
expect(release.cliReleaseAssetUrl("0.1.9-fork.1", "SHA256SUMS")).toBe(
"https://github.com/fork-owner/t3code/releases/download/v0.1.9-fork.1/SHA256SUMS",
);
expect(release.CLI_RELEASES_PAGE_URL).toBe("https://github.com/fork-owner/t3code/releases");
});

it("keeps upstream URLs in a build without the setting", async () => {
vi.resetModules();
const release = await import("./cliRelease.ts");
expect(release.cliReleaseIndexPageUrl(1)).toBe(
"https://api.github.com/repos/pingdotgg/t3code/releases?per_page=100&page=1",
);
expect(release.cliReleaseDownloadBaseUrl("0.1.9")).toBe(
"https://github.com/pingdotgg/t3code/releases/download/v0.1.9",
);
});
});
14 changes: 13 additions & 1 deletion packages/shared/src/cliRelease.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,19 @@ import * as Schema from "effect/Schema";

import { compareSemverVersions } from "./semver.ts";

const CLI_RELEASE_REPOSITORY = "pingdotgg/t3code";
// Set at build time from T3CODE_DESKTOP_UPDATE_REPOSITORY (scripts/lib/release-repository.ts), the
// desktop updater's setting, so a fork's builds list and download only the fork's releases.
declare const __T3CODE_RELEASE_REPOSITORY__: string | undefined;
const UPSTREAM_RELEASE_REPOSITORY = "pingdotgg/t3code";

/** The repository releases come from: the build's setting, or upstream when it has none. */
export function resolveCliReleaseRepository(configured: string | undefined): string {
return configured?.trim() || UPSTREAM_RELEASE_REPOSITORY;
}

const CLI_RELEASE_REPOSITORY = resolveCliReleaseRepository(
typeof __T3CODE_RELEASE_REPOSITORY__ === "undefined" ? undefined : __T3CODE_RELEASE_REPOSITORY__,
);
export const CLI_RELEASES_PAGE_URL = `https://github.com/${CLI_RELEASE_REPOSITORY}/releases`;
export const CLI_RELEASE_CHECKSUMS_FILE = "SHA256SUMS";
export const CLI_RELEASE_BASE_URL_ENV = "T3CODE_RELEASE_BASE_URL";
Expand Down
72 changes: 72 additions & 0 deletions scripts/lib/release-repository.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { it as effectIt } from "@effect/vitest";
import * as ConfigProvider from "effect/ConfigProvider";
import * as Effect from "effect/Effect";
import { describe, expect, it } from "vite-plus/test";

import { resolveGitHubPublishConfig } from "../build-desktop-artifact.ts";
import { releaseRepositoryDefine, resolveReleaseRepository } from "./release-repository.ts";

const desktopFeedRepository = (env: Record<string, string>) =>
resolveGitHubPublishConfig("latest").pipe(
Effect.map((config) => (config ? `${config.owner}/${config.repo}` : "")),
Effect.provide(ConfigProvider.layer(ConfigProvider.fromEnv({ env }))),
);

describe("release repository build setting", () => {
it("is empty (the upstream default) when unset or blank", () => {
expect(resolveReleaseRepository({})).toBe("");
expect(resolveReleaseRepository({ T3CODE_DESKTOP_UPDATE_REPOSITORY: " " })).toBe("");
});

it("is upstream's desktop update setting, so the server follows the desktop feed", () => {
const env = { T3CODE_DESKTOP_UPDATE_REPOSITORY: " some-owner/t3code " };
expect(resolveReleaseRepository(env)).toBe("some-owner/t3code");
expect(releaseRepositoryDefine(env)).toEqual({
__T3CODE_RELEASE_REPOSITORY__: JSON.stringify("some-owner/t3code"),
});
});

it("names the repository a GitHub Actions build runs in, unless the setting overrides it", () => {
const actions = { GITHUB_ACTIONS: "true", GITHUB_REPOSITORY: "fork-owner/t3code" };
expect(resolveReleaseRepository(actions)).toBe("fork-owner/t3code");
expect(
resolveReleaseRepository({ ...actions, T3CODE_DESKTOP_UPDATE_REPOSITORY: "other/t3code" }),
).toBe("other/t3code");
});

effectIt.effect("names the same repository as the desktop updater feed for every setting", () =>
Effect.gen(function* () {
const cases: Array<Record<string, string>> = [
{},
{ T3CODE_DESKTOP_UPDATE_REPOSITORY: "fork-owner/t3code" },
{ GITHUB_REPOSITORY: "fork-owner/t3code" },
{ T3CODE_DESKTOP_UPDATE_REPOSITORY: "a/t3code", GITHUB_REPOSITORY: "b/t3code" },
{ T3CODE_RELEASE_REPOSITORY: "b/t3code", GITHUB_REPOSITORY: "b/t3code" },
];
for (const env of cases) {
const feed = yield* desktopFeedRepository(env);
expect(resolveReleaseRepository(env), JSON.stringify(env)).toBe(feed);
}
}),
);

it("fails the build when the former setting disagrees with the desktop updater's", () => {
for (const env of [
{ T3CODE_RELEASE_REPOSITORY: "fork-owner/t3code" },
{ T3CODE_RELEASE_REPOSITORY: "b/t3code", T3CODE_DESKTOP_UPDATE_REPOSITORY: "a/t3code" },
{ T3CODE_RELEASE_REPOSITORY: "b/t3code", GITHUB_REPOSITORY: "a/t3code" },
]) {
expect(() => resolveReleaseRepository(env), JSON.stringify(env)).toThrow(
/T3CODE_RELEASE_REPOSITORY=".+" disagrees.*Set T3CODE_DESKTOP_UPDATE_REPOSITORY/,
);
}
});

it("fails the build on a malformed value instead of falling back to upstream", () => {
for (const value of ["owner", "owner/repo/extra", "owner/..", "https://github.com/o/r"]) {
expect(() => resolveReleaseRepository({ T3CODE_DESKTOP_UPDATE_REPOSITORY: value })).toThrow(
/T3CODE_DESKTOP_UPDATE_REPOSITORY/,
);
}
});
});
45 changes: 45 additions & 0 deletions scripts/lib/release-repository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* The repository a build updates from, for the server, CLI and desktop SSH installs. It is the one
* upstream already uses for the desktop updater feed and release links (docs/operations/release.md):
* T3CODE_DESKTOP_UPDATE_REPOSITORY, else GITHUB_REPOSITORY, else "" for upstream.
*/
export const RELEASE_REPOSITORY_ENV = "T3CODE_DESKTOP_UPDATE_REPOSITORY";
// The setting's first name in this fork; still accepted only when it names the same repository.
const FORMER_RELEASE_REPOSITORY_ENV = "T3CODE_RELEASE_REPOSITORY";

const SEGMENT = /^[A-Za-z0-9._-]+$/u;

function isValidSegment(value: string | undefined): value is string {
return value !== undefined && value !== "." && value !== ".." && SEGMENT.test(value);
}

/** The configured repository, or "" for the upstream default. Throws on a malformed value or a
* disagreeing former setting, so no build ships the desktop and the server on different feeds. */
export function resolveReleaseRepository(
env: Readonly<Record<string, string | undefined>> = process.env,
): string {
// The same order as apps/web/vite.config.ts and resolveGitHubPublishConfig read them.
const configured = env[RELEASE_REPOSITORY_ENV]?.trim() || env.GITHUB_REPOSITORY?.trim() || "";
const former = env[FORMER_RELEASE_REPOSITORY_ENV]?.trim() || "";
if (former !== "" && former !== configured) {
throw new Error(
`${FORMER_RELEASE_REPOSITORY_ENV}="${former}" disagrees with the repository the desktop ` +
`updater uses ("${configured || "pingdotgg/t3code"}"). Set ${RELEASE_REPOSITORY_ENV} instead.`,
);
}
if (configured === "") return "";
const [owner, name, ...rest] = configured.split("/");
if (!isValidSegment(owner) || !isValidSegment(name) || rest.length > 0) {
throw new Error(
`${RELEASE_REPOSITORY_ENV} (or GITHUB_REPOSITORY) must be "<owner>/<repo>", got "${configured}".`,
);
}
return configured;
}

/** The bundler `define` entry every build that bundles `@t3tools/shared/cliRelease` carries. */
export function releaseRepositoryDefine(
env: Readonly<Record<string, string | undefined>> = process.env,
): Record<string, string> {
return { __T3CODE_RELEASE_REPOSITORY__: JSON.stringify(resolveReleaseRepository(env)) };
}