t3-code-android-nightly/packages/shared/src/t3ProjectFile.ts
Julius Marminge d547e3b12a
chore(deps): upgrade Effect to rc.115 and Alchemy to beta.78 (#12326)
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-09-17 23:21:24 -07:00

45 lines
1.7 KiB
TypeScript

import * as Exit from "effect/Exit";
import * as Schema from "effect/Schema";
import { T3ProjectFile, T3_PROJECT_FILE_SCHEMA_URL } from "@t3tools/contracts";
import { fromLenientJson } from "./schemaJson.ts";
/**
* Codec between the raw `t3.json` file contents (lenient JSONC string) and the
* decoded {@link T3ProjectFile}.
*/
export const T3ProjectFileFromJson = fromLenientJson(T3ProjectFile);
const decodeT3ProjectFile = Schema.decodeExit(T3ProjectFileFromJson);
/**
* Decode raw `t3.json` contents, treating invalid or malformed files as
* absent. Clients use this to read optional defaults (scripts, thread env
* mode) without surfacing decode errors to the user.
*/
export function parseT3ProjectFile(contents: string): T3ProjectFile | null {
const decoded = decodeT3ProjectFile(contents);
return Exit.isSuccess(decoded) ? decoded.value : null;
}
/**
* Build the publishable JSON Schema document for `t3.json` (draft 2020-12).
*
* Served from the marketing site at {@link T3_PROJECT_FILE_SCHEMA_URL} so
* editors get LSP support via a `$schema` reference.
*/
export function buildT3ProjectFileJsonSchema(): Record<string, unknown> {
// Closed objects, as before effect rc.113 changed the generator default;
// editors then flag unknown keys in t3.json.
const document = Schema.toJsonSchemaDocument(T3ProjectFile, { onExcessProperty: "error" });
const jsonSchema: Record<string, unknown> = {
$schema: "https://json-schema.org/draft/2020-12/schema",
$id: T3_PROJECT_FILE_SCHEMA_URL,
...document.schema,
};
if (document.definitions && Object.keys(document.definitions).length > 0) {
jsonSchema.$defs = document.definitions;
}
return jsonSchema;
}