Generate and preserve keybindings schemas
- Add marketing JSON Schema artifacts for keybindings - Reuse shared JSON schema writer for server settings and keybindings - Preserve top-level `$schema` when rewriting settings
This commit is contained in:
1 parent
805c68dd0e
commit
f1793ad2eb
17 files changed
+476
-71
No files matched your search
@@ -4,9 +4,8 @@
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"build:schemas": "bun ../../scripts/build-server-settings-schema.ts",
|
||||
"dev": "bun run build:schemas && astro dev",
|
||||
"build": "bun run build:schemas && astro build",
|
||||
"dev": "astro dev",
|
||||
"build": "astro build",
|
||||
"preview": "astro preview",
|
||||
"typecheck": "astro check"
|
||||
},
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
{
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"title": "T3 Code Keybindings",
|
||||
"description": "JSON Schema for the keybindings.json file consumed by T3 Code.",
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"key": {
|
||||
"type": "string"
|
||||
},
|
||||
"command": {
|
||||
"anyOf": [
|
||||
{
|
||||
"type": "string",
|
||||
"enum": [
|
||||
"terminal.toggle",
|
||||
"terminal.split",
|
||||
"terminal.new",
|
||||
"terminal.close",
|
||||
"diff.toggle",
|
||||
"chat.new",
|
||||
"chat.newLocal",
|
||||
"editor.openFavorite",
|
||||
"thread.previous",
|
||||
"thread.next",
|
||||
"thread.jump.1",
|
||||
"thread.jump.2",
|
||||
"thread.jump.3",
|
||||
"thread.jump.4",
|
||||
"thread.jump.5",
|
||||
"thread.jump.6",
|
||||
"thread.jump.7",
|
||||
"thread.jump.8",
|
||||
"thread.jump.9"
|
||||
]
|
||||
},
|
||||
{
|
||||
"type": "string",
|
||||
"pattern": "^script\\.[\\s\\S]*?\\.run$"
|
||||
}
|
||||
]
|
||||
},
|
||||
"when": {
|
||||
"anyOf": [
|
||||
{
|
||||
"type": "string"
|
||||
},
|
||||
{
|
||||
"type": "null"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"required": ["key", "command"],
|
||||
"additionalProperties": false
|
||||
},
|
||||
"allOf": [
|
||||
{
|
||||
"maxItems": 256
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
{
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"title": "T3 Code Keybindings",
|
||||
"description": "JSON Schema for the keybindings.json file consumed by T3 Code.",
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"key": {
|
||||
"type": "string"
|
||||
},
|
||||
"command": {
|
||||
"anyOf": [
|
||||
{
|
||||
"type": "string",
|
||||
"enum": [
|
||||
"terminal.toggle",
|
||||
"terminal.split",
|
||||
"terminal.new",
|
||||
"terminal.close",
|
||||
"diff.toggle",
|
||||
"chat.new",
|
||||
"chat.newLocal",
|
||||
"editor.openFavorite",
|
||||
"thread.previous",
|
||||
"thread.next",
|
||||
"thread.jump.1",
|
||||
"thread.jump.2",
|
||||
"thread.jump.3",
|
||||
"thread.jump.4",
|
||||
"thread.jump.5",
|
||||
"thread.jump.6",
|
||||
"thread.jump.7",
|
||||
"thread.jump.8",
|
||||
"thread.jump.9"
|
||||
]
|
||||
},
|
||||
{
|
||||
"type": "string",
|
||||
"pattern": "^script\\.[\\s\\S]*?\\.run$"
|
||||
}
|
||||
]
|
||||
},
|
||||
"when": {
|
||||
"anyOf": [
|
||||
{
|
||||
"type": "string"
|
||||
},
|
||||
{
|
||||
"type": "null"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"required": ["key", "command"],
|
||||
"additionalProperties": false
|
||||
},
|
||||
"allOf": [
|
||||
{
|
||||
"maxItems": 256
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -34,7 +34,6 @@ import {
|
||||
Predicate,
|
||||
PubSub,
|
||||
Schema,
|
||||
SchemaGetter,
|
||||
SchemaIssue,
|
||||
SchemaTransformation,
|
||||
Ref,
|
||||
@@ -44,7 +43,7 @@ import {
|
||||
} from "effect";
|
||||
import * as Semaphore from "effect/Semaphore";
|
||||
import { ServerConfig } from "./config";
|
||||
import { fromLenientJson } from "@t3tools/shared/schemaJson";
|
||||
import { encodePrettyJsonEffect, fromLenientJson } from "@t3tools/shared/schemaJson";
|
||||
|
||||
export class KeybindingsConfigError extends Schema.TaggedErrorClass<KeybindingsConfigError>()(
|
||||
"KeybindingsConfigParseError",
|
||||
@@ -418,16 +417,7 @@ function encodeWhenAst(node: KeybindingWhenNode): string {
|
||||
const DEFAULT_RESOLVED_KEYBINDINGS = compileResolvedKeybindingsConfig(DEFAULT_KEYBINDINGS);
|
||||
|
||||
const RawKeybindingsEntries = fromLenientJson(Schema.Array(Schema.Unknown));
|
||||
const KeybindingsConfigJson = Schema.fromJsonString(KeybindingsConfig);
|
||||
const PrettyJsonString = SchemaGetter.parseJson<string>().compose(
|
||||
SchemaGetter.stringifyJson({ space: 2 }),
|
||||
);
|
||||
const KeybindingsConfigPrettyJson = KeybindingsConfigJson.pipe(
|
||||
Schema.encode({
|
||||
decode: PrettyJsonString,
|
||||
encode: PrettyJsonString,
|
||||
}),
|
||||
);
|
||||
const encodeKeybindingsConfigPrettyJson = encodePrettyJsonEffect(KeybindingsConfig);
|
||||
|
||||
export interface KeybindingsConfigState {
|
||||
readonly keybindings: ResolvedKeybindingsConfig;
|
||||
@@ -676,7 +666,7 @@ const makeKeybindings = Effect.gen(function* () {
|
||||
const writeConfigAtomically = (rules: readonly KeybindingRule[]) => {
|
||||
const tempPath = `${keybindingsConfigPath}.${process.pid}.${Date.now()}.tmp`;
|
||||
|
||||
return Schema.encodeEffect(KeybindingsConfigPrettyJson)(rules).pipe(
|
||||
return encodeKeybindingsConfigPrettyJson(rules).pipe(
|
||||
Effect.map((encoded) => `${encoded}\n`),
|
||||
Effect.tap(() => fs.makeDirectory(path.dirname(keybindingsConfigPath), { recursive: true })),
|
||||
Effect.tap((encoded) => fs.writeFileString(tempPath, encoded)),
|
||||
|
||||
@@ -216,4 +216,50 @@ it.layer(NodeServices.layer)("server settings", (it) => {
|
||||
});
|
||||
}).pipe(Effect.provide(makeServerSettingsLayer())),
|
||||
);
|
||||
|
||||
it.effect("preserves a top-level $schema declaration when rewriting settings", () =>
|
||||
Effect.gen(function* () {
|
||||
const serverConfig = yield* ServerConfig;
|
||||
const fileSystem = yield* FileSystem.FileSystem;
|
||||
|
||||
yield* fileSystem.writeFileString(
|
||||
serverConfig.settingsPath,
|
||||
`${JSON.stringify(
|
||||
{
|
||||
$schema: "https://t3.chat/schemas/server-settings/0.0.15.schema.json",
|
||||
providers: {
|
||||
codex: {
|
||||
binaryPath: "/usr/local/bin/codex",
|
||||
},
|
||||
},
|
||||
},
|
||||
null,
|
||||
2,
|
||||
)}\n`,
|
||||
);
|
||||
|
||||
const serverSettings = yield* ServerSettingsService;
|
||||
yield* serverSettings.start;
|
||||
yield* serverSettings.updateSettings({
|
||||
providers: {
|
||||
claudeAgent: {
|
||||
binaryPath: "/usr/local/bin/claude",
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const raw = yield* fileSystem.readFileString(serverConfig.settingsPath);
|
||||
assert.deepEqual(JSON.parse(raw), {
|
||||
$schema: "https://t3.chat/schemas/server-settings/0.0.15.schema.json",
|
||||
providers: {
|
||||
codex: {
|
||||
binaryPath: "/usr/local/bin/codex",
|
||||
},
|
||||
claudeAgent: {
|
||||
binaryPath: "/usr/local/bin/claude",
|
||||
},
|
||||
},
|
||||
});
|
||||
}).pipe(Effect.provide(makeServerSettingsLayer())),
|
||||
);
|
||||
});
|
||||
@@ -39,7 +39,7 @@ import {
|
||||
import * as Semaphore from "effect/Semaphore";
|
||||
import { ServerConfig } from "./config";
|
||||
import { type DeepPartial, deepMerge } from "@t3tools/shared/Struct";
|
||||
import { fromLenientJson } from "@t3tools/shared/schemaJson";
|
||||
import { encodePrettyJsonEffect, fromLenientJson } from "@t3tools/shared/schemaJson";
|
||||
|
||||
export class ServerSettingsError extends Schema.TaggedErrorClass<ServerSettingsError>()(
|
||||
"ServerSettingsError",
|
||||
@@ -101,6 +101,9 @@ export class ServerSettingsService extends ServiceMap.Service<
|
||||
}
|
||||
|
||||
const ServerSettingsJson = fromLenientJson(ServerSettings);
|
||||
const UnknownJson = fromLenientJson(Schema.Unknown);
|
||||
const encodeServerSettingsDocument = encodePrettyJsonEffect(Schema.Unknown);
|
||||
const SETTINGS_SCHEMA_DECLARATION_KEY = "$schema";
|
||||
|
||||
const PROVIDER_ORDER: readonly ProviderKind[] = ["codex", "claudeAgent"];
|
||||
|
||||
@@ -177,6 +180,7 @@ const makeServerSettings = Effect.gen(function* () {
|
||||
const changesPubSub = yield* PubSub.unbounded<ServerSettings>();
|
||||
const startedRef = yield* Ref.make(false);
|
||||
const startedDeferred = yield* Deferred.make<void, ServerSettingsError>();
|
||||
const schemaDeclarationRef = yield* Ref.make<string | undefined>(undefined);
|
||||
const watcherScope = yield* Scope.make("sequential");
|
||||
yield* Effect.addFinalizer(() => Scope.close(watcherScope, Exit.void));
|
||||
|
||||
@@ -207,10 +211,25 @@ const makeServerSettings = Effect.gen(function* () {
|
||||
|
||||
const loadSettingsFromDisk = Effect.gen(function* () {
|
||||
if (!(yield* readConfigExists)) {
|
||||
yield* Ref.set(schemaDeclarationRef, undefined);
|
||||
return DEFAULT_SERVER_SETTINGS;
|
||||
}
|
||||
|
||||
const raw = yield* readRawConfig;
|
||||
const rawJson = Schema.decodeUnknownExit(UnknownJson)(raw);
|
||||
if (rawJson._tag === "Success") {
|
||||
const value = rawJson.value;
|
||||
const valueRecord =
|
||||
value && typeof value === "object" && !Array.isArray(value)
|
||||
? (value as Record<string, unknown>)
|
||||
: undefined;
|
||||
const schemaDeclaration =
|
||||
valueRecord && typeof valueRecord[SETTINGS_SCHEMA_DECLARATION_KEY] === "string"
|
||||
? valueRecord[SETTINGS_SCHEMA_DECLARATION_KEY]
|
||||
: undefined;
|
||||
yield* Ref.set(schemaDeclarationRef, schemaDeclaration);
|
||||
}
|
||||
|
||||
const decoded = Schema.decodeUnknownExit(ServerSettingsJson)(raw);
|
||||
if (decoded._tag === "Failure") {
|
||||
yield* Effect.logWarning("failed to parse settings.json, using defaults", {
|
||||
@@ -232,7 +251,17 @@ const makeServerSettings = Effect.gen(function* () {
|
||||
const tempPath = `${settingsPath}.${process.pid}.${Date.now()}.tmp`;
|
||||
const sparseSettings = stripDefaultServerSettings(settings, DEFAULT_SERVER_SETTINGS) ?? {};
|
||||
|
||||
return Effect.succeed(`${JSON.stringify(sparseSettings, null, 2)}\n`).pipe(
|
||||
return Ref.get(schemaDeclarationRef).pipe(
|
||||
Effect.map((schemaDeclaration) =>
|
||||
schemaDeclaration
|
||||
? {
|
||||
[SETTINGS_SCHEMA_DECLARATION_KEY]: schemaDeclaration,
|
||||
...sparseSettings,
|
||||
}
|
||||
: sparseSettings,
|
||||
),
|
||||
Effect.flatMap(encodeServerSettingsDocument),
|
||||
Effect.map((encoded) => `${encoded}\n`),
|
||||
Effect.tap(() => fs.makeDirectory(pathService.dirname(settingsPath), { recursive: true })),
|
||||
Effect.tap((encoded) => fs.writeFileString(tempPath, encoded)),
|
||||
Effect.flatMap(() => fs.rename(tempPath, settingsPath)),
|
||||
|
||||
+2
-2
@@ -17,8 +17,8 @@ This document covers how to run desktop releases from one tag, first without sig
|
||||
- Includes Electron auto-update metadata (for example `latest*.yml` and `*.blockmap`) in release assets.
|
||||
- Publishes the CLI package (`apps/server`, npm package `t3`) with OIDC trusted publishing.
|
||||
- Signing is optional and auto-detected per platform from secrets.
|
||||
- Refreshes the marketing-site JSON Schema for `settings.json` at `/schemas/server-settings.schema.json`.
|
||||
- Commits an immutable versioned JSON Schema copy at `/schemas/server-settings/<version>.schema.json`.
|
||||
- Refreshes the marketing-site JSON Schemas for `settings.json` and `keybindings.json` at `/schemas/server-settings.schema.json` and `/schemas/keybindings.schema.json`.
|
||||
- Commits immutable versioned JSON Schema copies at `/schemas/server-settings/<version>.schema.json` and `/schemas/keybindings/<version>.schema.json`.
|
||||
|
||||
## Desktop auto-update notes
|
||||
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
import { assert, it } from "@effect/vitest";
|
||||
import { Effect, Schema } from "effect";
|
||||
import { encodePrettyJsonEffect } from "./schemaJson";
|
||||
|
||||
it.effect("encodePrettyJsonEffect writes indented JSON", () =>
|
||||
Effect.gen(function* () {
|
||||
const encodePrettyJson = encodePrettyJsonEffect(
|
||||
Schema.Struct({
|
||||
provider: Schema.String,
|
||||
options: Schema.Struct({
|
||||
enabled: Schema.Boolean,
|
||||
}),
|
||||
}),
|
||||
);
|
||||
|
||||
const encoded = yield* encodePrettyJson({
|
||||
provider: "codex",
|
||||
options: {
|
||||
enabled: true,
|
||||
},
|
||||
});
|
||||
|
||||
assert.strictEqual(
|
||||
encoded,
|
||||
`{
|
||||
"provider": "codex",
|
||||
"options": {
|
||||
"enabled": true
|
||||
}
|
||||
}`,
|
||||
);
|
||||
}),
|
||||
);
|
||||
@@ -36,6 +36,20 @@ export const decodeUnknownJsonResult = <S extends Schema.Codec<unknown, unknown,
|
||||
};
|
||||
};
|
||||
|
||||
const PrettyJsonString = SchemaGetter.parseJson<string>().compose(
|
||||
SchemaGetter.stringifyJson({ space: 2 }),
|
||||
);
|
||||
|
||||
export const encodePrettyJsonEffect = <S extends Schema.Top>(schema: S) =>
|
||||
Schema.encodeEffect(
|
||||
Schema.fromJsonString(schema).pipe(
|
||||
Schema.encode({
|
||||
decode: PrettyJsonString,
|
||||
encode: PrettyJsonString,
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
export const formatSchemaError = (cause: Cause.Cause<Schema.SchemaError>) => {
|
||||
const squashed = Cause.squash(cause);
|
||||
return Schema.isSchemaError(squashed)
|
||||
|
||||
@@ -1,12 +1,19 @@
|
||||
import { dirname, resolve } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
|
||||
import {
|
||||
KEYBINDINGS_SCHEMA_RELATIVE_PATH,
|
||||
writeKeybindingsJsonSchemas,
|
||||
} from "./lib/keybindings-schema.ts";
|
||||
import {
|
||||
SERVER_SETTINGS_SCHEMA_RELATIVE_PATH,
|
||||
writeServerSettingsJsonSchemas,
|
||||
} from "./lib/server-settings-schema.ts";
|
||||
|
||||
const rootDir = resolve(dirname(fileURLToPath(import.meta.url)), "..");
|
||||
|
||||
writeServerSettingsJsonSchemas({ rootDir });
|
||||
writeKeybindingsJsonSchemas({ rootDir });
|
||||
|
||||
console.log(`Wrote ${SERVER_SETTINGS_SCHEMA_RELATIVE_PATH}`);
|
||||
console.log(`Wrote ${KEYBINDINGS_SCHEMA_RELATIVE_PATH}`);
|
||||
@@ -0,0 +1,75 @@
|
||||
import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
||||
import { dirname, resolve } from "node:path";
|
||||
|
||||
import { type Schema } from "effect";
|
||||
|
||||
import { toJsonSchemaObject } from "@t3tools/shared/schemaJson";
|
||||
|
||||
const JSON_SCHEMA_DRAFT_2020_12 = "https://json-schema.org/draft/2020-12/schema";
|
||||
|
||||
export function buildJsonSchemaDocument(
|
||||
schema: Schema.Top,
|
||||
options: {
|
||||
readonly title: string;
|
||||
readonly description: string;
|
||||
},
|
||||
): Record<string, unknown> {
|
||||
const jsonSchema = toJsonSchemaObject(schema);
|
||||
if (!jsonSchema || typeof jsonSchema !== "object" || Array.isArray(jsonSchema)) {
|
||||
throw new Error(
|
||||
`${options.title} JSON schema must be an object or array JSON schema document.`,
|
||||
);
|
||||
}
|
||||
|
||||
return {
|
||||
$schema: JSON_SCHEMA_DRAFT_2020_12,
|
||||
title: options.title,
|
||||
description: options.description,
|
||||
...jsonSchema,
|
||||
};
|
||||
}
|
||||
|
||||
function writeJsonFileIfChanged(filePath: string, document: Record<string, unknown>): boolean {
|
||||
const nextContent = `${JSON.stringify(document, null, 2)}\n`;
|
||||
const previousContent = (() => {
|
||||
try {
|
||||
return readFileSync(filePath, "utf8");
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
})();
|
||||
|
||||
if (previousContent === nextContent) {
|
||||
return false;
|
||||
}
|
||||
|
||||
mkdirSync(dirname(filePath), { recursive: true });
|
||||
writeFileSync(filePath, nextContent);
|
||||
return true;
|
||||
}
|
||||
|
||||
export function writeJsonSchemaArtifacts(options: {
|
||||
readonly rootDir?: string;
|
||||
readonly version?: string;
|
||||
readonly latestRelativePath: string;
|
||||
readonly getVersionedRelativePath: (version: string) => string;
|
||||
readonly document: Record<string, unknown>;
|
||||
}): {
|
||||
readonly changed: boolean;
|
||||
} {
|
||||
const rootDir = resolve(options.rootDir ?? process.cwd());
|
||||
let changed = writeJsonFileIfChanged(
|
||||
resolve(rootDir, options.latestRelativePath),
|
||||
options.document,
|
||||
);
|
||||
|
||||
if (options.version) {
|
||||
changed =
|
||||
writeJsonFileIfChanged(
|
||||
resolve(rootDir, options.getVersionedRelativePath(options.version)),
|
||||
options.document,
|
||||
) || changed;
|
||||
}
|
||||
|
||||
return { changed };
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
import { mkdtempSync, readFileSync, rmSync } from "node:fs";
|
||||
import { join, resolve } from "node:path";
|
||||
import { tmpdir } from "node:os";
|
||||
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import {
|
||||
buildKeybindingsJsonSchema,
|
||||
getVersionedKeybindingsSchemaRelativePath,
|
||||
KEYBINDINGS_SCHEMA_RELATIVE_PATH,
|
||||
writeKeybindingsJsonSchemas,
|
||||
} from "./keybindings-schema";
|
||||
|
||||
describe("buildKeybindingsJsonSchema", () => {
|
||||
it("builds a JSON schema document for keybindings.json", () => {
|
||||
const schema = buildKeybindingsJsonSchema();
|
||||
|
||||
expect(schema.$schema).toBe("https://json-schema.org/draft/2020-12/schema");
|
||||
expect(schema.title).toBe("T3 Code Keybindings");
|
||||
expect(schema.type).toBe("array");
|
||||
expect(schema.items).toMatchObject({
|
||||
type: "object",
|
||||
properties: {
|
||||
key: expect.any(Object),
|
||||
command: expect.any(Object),
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it("writes latest and versioned schema files", () => {
|
||||
const rootDir = mkdtempSync(join(tmpdir(), "t3-keybindings-schema-"));
|
||||
|
||||
try {
|
||||
const result = writeKeybindingsJsonSchemas({ rootDir, version: "1.2.3" });
|
||||
expect(result.changed).toBe(true);
|
||||
|
||||
const latestSchema = JSON.parse(
|
||||
readFileSync(resolve(rootDir, KEYBINDINGS_SCHEMA_RELATIVE_PATH), "utf8"),
|
||||
) as Record<string, unknown>;
|
||||
const versionedSchema = JSON.parse(
|
||||
readFileSync(resolve(rootDir, getVersionedKeybindingsSchemaRelativePath("1.2.3")), "utf8"),
|
||||
) as Record<string, unknown>;
|
||||
|
||||
expect(latestSchema).toEqual(versionedSchema);
|
||||
expect(latestSchema.title).toBe("T3 Code Keybindings");
|
||||
} finally {
|
||||
rmSync(rootDir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,32 @@
|
||||
import { KeybindingsConfig } from "@t3tools/contracts";
|
||||
import { buildJsonSchemaDocument, writeJsonSchemaArtifacts } from "./json-schema";
|
||||
|
||||
export const KEYBINDINGS_SCHEMA_RELATIVE_PATH =
|
||||
"apps/marketing/public/schemas/keybindings.schema.json";
|
||||
export const KEYBINDINGS_VERSIONED_SCHEMA_DIRECTORY_RELATIVE_PATH =
|
||||
"apps/marketing/public/schemas/keybindings";
|
||||
|
||||
export const getVersionedKeybindingsSchemaRelativePath = (version: string) =>
|
||||
`${KEYBINDINGS_VERSIONED_SCHEMA_DIRECTORY_RELATIVE_PATH}/${version}.schema.json`;
|
||||
|
||||
export function buildKeybindingsJsonSchema(): Record<string, unknown> {
|
||||
return buildJsonSchemaDocument(KeybindingsConfig, {
|
||||
title: "T3 Code Keybindings",
|
||||
description: "JSON Schema for the keybindings.json file consumed by T3 Code.",
|
||||
});
|
||||
}
|
||||
|
||||
export function writeKeybindingsJsonSchemas(options?: {
|
||||
readonly rootDir?: string;
|
||||
readonly version?: string;
|
||||
}): {
|
||||
readonly changed: boolean;
|
||||
} {
|
||||
return writeJsonSchemaArtifacts({
|
||||
latestRelativePath: KEYBINDINGS_SCHEMA_RELATIVE_PATH,
|
||||
getVersionedRelativePath: getVersionedKeybindingsSchemaRelativePath,
|
||||
document: buildKeybindingsJsonSchema(),
|
||||
...(options?.rootDir === undefined ? {} : { rootDir: options.rootDir }),
|
||||
...(options?.version === undefined ? {} : { version: options.version }),
|
||||
});
|
||||
}
|
||||
@@ -1,10 +1,5 @@
|
||||
import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
||||
import { resolve, dirname } from "node:path";
|
||||
|
||||
import { ServerSettings } from "@t3tools/contracts/settings";
|
||||
import { toJsonSchemaObject } from "@t3tools/shared/schemaJson";
|
||||
|
||||
const JSON_SCHEMA_DRAFT_2020_12 = "https://json-schema.org/draft/2020-12/schema";
|
||||
import { buildJsonSchemaDocument, writeJsonSchemaArtifacts } from "./json-schema";
|
||||
|
||||
export const SERVER_SETTINGS_SCHEMA_RELATIVE_PATH =
|
||||
"apps/marketing/public/schemas/server-settings.schema.json";
|
||||
@@ -15,36 +10,10 @@ export const getVersionedServerSettingsSchemaRelativePath = (version: string) =>
|
||||
`${SERVER_SETTINGS_VERSIONED_SCHEMA_DIRECTORY_RELATIVE_PATH}/${version}.schema.json`;
|
||||
|
||||
export function buildServerSettingsJsonSchema(): Record<string, unknown> {
|
||||
const schema = toJsonSchemaObject(ServerSettings);
|
||||
if (!schema || typeof schema !== "object" || Array.isArray(schema)) {
|
||||
throw new Error("ServerSettings JSON schema must be an object schema.");
|
||||
}
|
||||
|
||||
return {
|
||||
$schema: JSON_SCHEMA_DRAFT_2020_12,
|
||||
return buildJsonSchemaDocument(ServerSettings, {
|
||||
title: "T3 Code Server Settings",
|
||||
description: "JSON Schema for the server-authoritative settings.json file consumed by T3 Code.",
|
||||
...schema,
|
||||
};
|
||||
}
|
||||
|
||||
function writeJsonFileIfChanged(filePath: string, document: Record<string, unknown>): boolean {
|
||||
const nextContent = `${JSON.stringify(document, null, 2)}\n`;
|
||||
const previousContent = (() => {
|
||||
try {
|
||||
return readFileSync(filePath, "utf8");
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
})();
|
||||
|
||||
if (previousContent === nextContent) {
|
||||
return false;
|
||||
}
|
||||
|
||||
mkdirSync(dirname(filePath), { recursive: true });
|
||||
writeFileSync(filePath, nextContent);
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
export function writeServerSettingsJsonSchemas(options?: {
|
||||
@@ -53,20 +22,11 @@ export function writeServerSettingsJsonSchemas(options?: {
|
||||
}): {
|
||||
readonly changed: boolean;
|
||||
} {
|
||||
const rootDir = resolve(options?.rootDir ?? process.cwd());
|
||||
const latestSchema = buildServerSettingsJsonSchema();
|
||||
let changed = writeJsonFileIfChanged(
|
||||
resolve(rootDir, SERVER_SETTINGS_SCHEMA_RELATIVE_PATH),
|
||||
latestSchema,
|
||||
);
|
||||
|
||||
if (options?.version) {
|
||||
changed =
|
||||
writeJsonFileIfChanged(
|
||||
resolve(rootDir, getVersionedServerSettingsSchemaRelativePath(options.version)),
|
||||
latestSchema,
|
||||
) || changed;
|
||||
}
|
||||
|
||||
return { changed };
|
||||
return writeJsonSchemaArtifacts({
|
||||
latestRelativePath: SERVER_SETTINGS_SCHEMA_RELATIVE_PATH,
|
||||
getVersionedRelativePath: getVersionedServerSettingsSchemaRelativePath,
|
||||
document: buildServerSettingsJsonSchema(),
|
||||
...(options?.rootDir === undefined ? {} : { rootDir: options.rootDir }),
|
||||
...(options?.version === undefined ? {} : { version: options.version }),
|
||||
});
|
||||
}
|
||||
@@ -122,6 +122,14 @@ try {
|
||||
tempRoot,
|
||||
"apps/marketing/public/schemas/server-settings/9.9.9-smoke.0.schema.json",
|
||||
);
|
||||
const latestKeybindingsSchemaPath = resolve(
|
||||
tempRoot,
|
||||
"apps/marketing/public/schemas/keybindings.schema.json",
|
||||
);
|
||||
const versionedKeybindingsSchemaPath = resolve(
|
||||
tempRoot,
|
||||
"apps/marketing/public/schemas/keybindings/9.9.9-smoke.0.schema.json",
|
||||
);
|
||||
if (!existsSync(latestSchemaPath)) {
|
||||
throw new Error(
|
||||
"Expected release version alignment to generate the latest server settings schema.",
|
||||
@@ -132,12 +140,28 @@ try {
|
||||
"Expected release version alignment to generate the versioned server settings schema.",
|
||||
);
|
||||
}
|
||||
if (!existsSync(latestKeybindingsSchemaPath)) {
|
||||
throw new Error(
|
||||
"Expected release version alignment to generate the latest keybindings schema.",
|
||||
);
|
||||
}
|
||||
if (!existsSync(versionedKeybindingsSchemaPath)) {
|
||||
throw new Error(
|
||||
"Expected release version alignment to generate the versioned keybindings schema.",
|
||||
);
|
||||
}
|
||||
const versionedSchema = readFileSync(versionedSchemaPath, "utf8");
|
||||
assertContains(
|
||||
versionedSchema,
|
||||
'"title": "T3 Code Server Settings"',
|
||||
"Expected versioned server settings schema to contain schema metadata.",
|
||||
);
|
||||
const versionedKeybindingsSchema = readFileSync(versionedKeybindingsSchemaPath, "utf8");
|
||||
assertContains(
|
||||
versionedKeybindingsSchema,
|
||||
'"title": "T3 Code Keybindings"',
|
||||
"Expected versioned keybindings schema to contain schema metadata.",
|
||||
);
|
||||
|
||||
const { arm64Path, x64Path } = writeMacManifestFixtures(tempRoot);
|
||||
execFileSync(
|
||||
|
||||
@@ -10,7 +10,7 @@ import {
|
||||
} from "./update-release-package-versions";
|
||||
|
||||
describe("updateReleasePackageVersions", () => {
|
||||
it("updates package versions and writes latest plus versioned server settings schemas", () => {
|
||||
it("updates package versions and writes latest plus versioned config schemas", () => {
|
||||
const rootDir = mkdtempSync(join(tmpdir(), "t3-release-version-bump-"));
|
||||
|
||||
try {
|
||||
@@ -50,6 +50,24 @@ describe("updateReleasePackageVersions", () => {
|
||||
),
|
||||
),
|
||||
).toMatchObject({ title: "T3 Code Server Settings" });
|
||||
|
||||
expect(
|
||||
JSON.parse(
|
||||
readFileSync(
|
||||
resolve(rootDir, "apps/marketing/public/schemas/keybindings.schema.json"),
|
||||
"utf8",
|
||||
),
|
||||
),
|
||||
).toMatchObject({ title: "T3 Code Keybindings" });
|
||||
|
||||
expect(
|
||||
JSON.parse(
|
||||
readFileSync(
|
||||
resolve(rootDir, "apps/marketing/public/schemas/keybindings/1.2.3.schema.json"),
|
||||
"utf8",
|
||||
),
|
||||
),
|
||||
).toMatchObject({ title: "T3 Code Keybindings" });
|
||||
} finally {
|
||||
rmSync(rootDir, { recursive: true, force: true });
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ import { appendFileSync, readFileSync, writeFileSync } from "node:fs";
|
||||
import { resolve } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
|
||||
import { writeKeybindingsJsonSchemas } from "./lib/keybindings-schema.ts";
|
||||
import { writeServerSettingsJsonSchemas } from "./lib/server-settings-schema.ts";
|
||||
|
||||
export const releasePackageFiles = [
|
||||
@@ -40,6 +41,7 @@ export function updateReleasePackageVersions(
|
||||
}
|
||||
|
||||
changed = writeServerSettingsJsonSchemas({ rootDir, version }).changed || changed;
|
||||
changed = writeKeybindingsJsonSchemas({ rootDir, version }).changed || changed;
|
||||
|
||||
return { changed };
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user