@typra/emitter 0.2.5 → 0.2.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -11,9 +11,14 @@ ship runtime service implementations or product-specific contracts.
11
11
  ## Install
12
12
 
13
13
  ```powershell
14
- npm install --save-dev @typra/emitter @typespec/compiler
14
+ npm install --save-dev @typra/emitter @typespec/compiler@1.10.0 @typespec/json-schema@1.10.0
15
15
  ```
16
16
 
17
+ Typra currently validates against TypeSpec compiler and JSON schema emitter
18
+ `1.10.0`. Unvalidated TypeSpec versions report a clear diagnostic during emit;
19
+ set `allow-unsupported-typespec-version: true` only when you intentionally accept
20
+ possible generated output churn.
21
+
17
22
  ## Configure TypeSpec
18
23
 
19
24
  Add the emitter to `tspconfig.yaml`:
@@ -0,0 +1,32 @@
1
+ import { EmitContext, NoTarget } from "@typespec/compiler";
2
+ import { TypraEmitterOptions } from "./lib.js";
3
+ export declare const SUPPORTED_TYPESPEC_COMPILER_VERSION = "1.10.0";
4
+ export declare const SUPPORTED_TYPESPEC_JSON_SCHEMA_VERSION = "1.10.0";
5
+ export interface ToolchainPackageMetadata {
6
+ name: string;
7
+ version: string;
8
+ supportedRange: string;
9
+ supported: boolean;
10
+ }
11
+ export interface ToolchainMetadata {
12
+ packages: ToolchainPackageMetadata[];
13
+ }
14
+ interface CompatibilityDiagnosticContext {
15
+ options: Pick<TypraEmitterOptions, "allow-unsupported-typespec-version">;
16
+ program: {
17
+ reportDiagnostic(diagnostic: {
18
+ code: string;
19
+ message: string;
20
+ severity: "error" | "warning";
21
+ target: typeof NoTarget;
22
+ }): void;
23
+ };
24
+ }
25
+ export declare function getToolchainMetadata(): ToolchainMetadata;
26
+ export declare function buildToolchainMetadata(packages: Array<Omit<ToolchainPackageMetadata, "supported"> & Partial<Pick<ToolchainPackageMetadata, "supported">>>): ToolchainMetadata;
27
+ export declare function reportTypeSpecCompatibility(context: EmitContext<TypraEmitterOptions>): ToolchainMetadata;
28
+ export declare function shouldBlockUnsupportedTypeSpecToolchain(options: Pick<TypraEmitterOptions, "allow-unsupported-typespec-version">, toolchain: ToolchainMetadata): boolean;
29
+ export declare function reportToolchainCompatibility(context: CompatibilityDiagnosticContext, toolchain: ToolchainMetadata): void;
30
+ export declare function getUnsupportedTypeSpecPackages(toolchain: ToolchainMetadata): ToolchainPackageMetadata[];
31
+ export declare function formatUnsupportedTypeSpecVersionMessage(unsupported: ToolchainPackageMetadata[], allowedUnsupportedVersion: boolean): string;
32
+ export {};
@@ -0,0 +1,96 @@
1
+ import { existsSync, readFileSync } from "node:fs";
2
+ import { dirname, join } from "node:path";
3
+ import { createRequire } from "node:module";
4
+ import { fileURLToPath } from "node:url";
5
+ import { NoTarget } from "@typespec/compiler";
6
+ export const SUPPORTED_TYPESPEC_COMPILER_VERSION = "1.10.0";
7
+ export const SUPPORTED_TYPESPEC_JSON_SCHEMA_VERSION = "1.10.0";
8
+ const require = createRequire(import.meta.url);
9
+ export function getToolchainMetadata() {
10
+ const emitterVersion = resolveNearestPackageVersion("@typra/emitter", dirname(fileURLToPath(import.meta.url)));
11
+ return buildToolchainMetadata([
12
+ {
13
+ name: "@typespec/compiler",
14
+ version: resolveInstalledPackageVersion("@typespec/compiler"),
15
+ supportedRange: SUPPORTED_TYPESPEC_COMPILER_VERSION,
16
+ },
17
+ {
18
+ name: "@typespec/json-schema",
19
+ version: resolveInstalledPackageVersion("@typespec/json-schema"),
20
+ supportedRange: SUPPORTED_TYPESPEC_JSON_SCHEMA_VERSION,
21
+ },
22
+ {
23
+ name: "@typra/emitter",
24
+ version: emitterVersion,
25
+ supportedRange: emitterVersion,
26
+ },
27
+ ]);
28
+ }
29
+ export function buildToolchainMetadata(packages) {
30
+ return {
31
+ packages: packages
32
+ .map((entry) => ({
33
+ ...entry,
34
+ supported: entry.supported ?? isSupportedVersion(entry.version, entry.supportedRange),
35
+ }))
36
+ .sort((left, right) => left.name.localeCompare(right.name)),
37
+ };
38
+ }
39
+ export function reportTypeSpecCompatibility(context) {
40
+ const toolchain = getToolchainMetadata();
41
+ reportToolchainCompatibility(context, toolchain);
42
+ return toolchain;
43
+ }
44
+ export function shouldBlockUnsupportedTypeSpecToolchain(options, toolchain) {
45
+ return options["allow-unsupported-typespec-version"] !== true && getUnsupportedTypeSpecPackages(toolchain).length > 0;
46
+ }
47
+ export function reportToolchainCompatibility(context, toolchain) {
48
+ const unsupported = getUnsupportedTypeSpecPackages(toolchain);
49
+ if (unsupported.length === 0) {
50
+ return;
51
+ }
52
+ context.program.reportDiagnostic({
53
+ code: "typra-emitter-unsupported-typespec-version",
54
+ message: formatUnsupportedTypeSpecVersionMessage(unsupported, context.options["allow-unsupported-typespec-version"] === true),
55
+ severity: context.options["allow-unsupported-typespec-version"] === true ? "warning" : "error",
56
+ target: NoTarget,
57
+ });
58
+ }
59
+ export function getUnsupportedTypeSpecPackages(toolchain) {
60
+ return toolchain.packages.filter((entry) => (entry.name === "@typespec/compiler" || entry.name === "@typespec/json-schema") && !entry.supported);
61
+ }
62
+ export function formatUnsupportedTypeSpecVersionMessage(unsupported, allowedUnsupportedVersion) {
63
+ const actual = unsupported.map((entry) => `${entry.name}@${entry.version}`).join(", ");
64
+ const expected = unsupported.map((entry) => `${entry.name}@${entry.supportedRange}`).join(", ");
65
+ const action = allowedUnsupportedVersion
66
+ ? "Generation will continue because allow-unsupported-typespec-version is enabled."
67
+ : "Pin the TypeSpec toolchain to the supported versions, or set allow-unsupported-typespec-version: true to continue with a warning.";
68
+ return `@typra/emitter has only been validated with ${expected}; found ${actual}. ${action}`;
69
+ }
70
+ function isSupportedVersion(version, supportedRange) {
71
+ return version === supportedRange;
72
+ }
73
+ function resolveInstalledPackageVersion(packageName) {
74
+ try {
75
+ const packageEntry = require.resolve(packageName);
76
+ return resolveNearestPackageVersion(packageName, dirname(packageEntry));
77
+ }
78
+ catch {
79
+ return "unresolved";
80
+ }
81
+ }
82
+ function resolveNearestPackageVersion(expectedPackageName, startDir) {
83
+ let current = startDir;
84
+ while (current !== dirname(current)) {
85
+ const candidate = join(current, "package.json");
86
+ if (existsSync(candidate)) {
87
+ const metadata = JSON.parse(readFileSync(candidate, "utf8"));
88
+ if (metadata.name === expectedPackageName && typeof metadata.version === "string") {
89
+ return metadata.version;
90
+ }
91
+ }
92
+ current = dirname(current);
93
+ }
94
+ return "unresolved";
95
+ }
96
+ //# sourceMappingURL=compatibility.js.map
@@ -1,6 +1,7 @@
1
1
  import { EmitContext } from "@typespec/compiler";
2
2
  import { EmitTarget, TypraEmitterOptions } from "./lib.js";
3
3
  import { TypeNode } from "./ir/ast.js";
4
+ import { ToolchainMetadata } from "./compatibility.js";
4
5
  export interface ExportSurfaceMethod {
5
6
  name: string;
6
7
  returns: string;
@@ -39,6 +40,7 @@ export interface TargetExportSurface {
39
40
  export interface ExportSurfaceSnapshot {
40
41
  emitter: "typra-emitter";
41
42
  version: 1;
43
+ toolchain: ToolchainMetadata;
42
44
  root: {
43
45
  object: string;
44
46
  namespace: string;
@@ -46,5 +48,5 @@ export interface ExportSurfaceSnapshot {
46
48
  };
47
49
  targets: TargetExportSurface[];
48
50
  }
49
- export declare function buildExportSurfaceSnapshot(rootObject: string, rootNamespace: string, rootAlias: string, targets: EmitTarget[], nodes: TypeNode[]): ExportSurfaceSnapshot;
51
+ export declare function buildExportSurfaceSnapshot(rootObject: string, rootNamespace: string, rootAlias: string, targets: EmitTarget[], nodes: TypeNode[], toolchain?: ToolchainMetadata): ExportSurfaceSnapshot;
50
52
  export declare function emitExportSurfaceSnapshot(context: EmitContext<TypraEmitterOptions>, snapshot: ExportSurfaceSnapshot): Promise<void>;
@@ -1,9 +1,11 @@
1
1
  import { emitFile, resolvePath } from "@typespec/compiler";
2
2
  import { toKebabCase, toSnakeCase } from "./ir/utilities.js";
3
- export function buildExportSurfaceSnapshot(rootObject, rootNamespace, rootAlias, targets, nodes) {
3
+ import { getToolchainMetadata } from "./compatibility.js";
4
+ export function buildExportSurfaceSnapshot(rootObject, rootNamespace, rootAlias, targets, nodes, toolchain = getToolchainMetadata()) {
4
5
  return {
5
6
  emitter: "typra-emitter",
6
7
  version: 1,
8
+ toolchain,
7
9
  root: {
8
10
  object: rootObject,
9
11
  namespace: rootNamespace,
@@ -8,6 +8,7 @@ import { generateGo } from "./languages/go/driver.js";
8
8
  import { generateRust } from "./languages/rust/driver.js";
9
9
  import { emitGeneratedFile, emitGeneratedManifest } from "./cleanup/generated-file.js";
10
10
  import { buildExportSurfaceSnapshot, emitExportSurfaceSnapshot } from "./contract-surface.js";
11
+ import { reportTypeSpecCompatibility, shouldBlockUnsupportedTypeSpecToolchain } from "./compatibility.js";
11
12
  /**
12
13
  * Filter nodes based on omit-models option.
13
14
  * Matches against model name (e.g., "AgentManifest") or fully qualified name (e.g., "Prompty.AgentManifest")
@@ -69,6 +70,10 @@ const generators = {
69
70
  rust: generateRust,
70
71
  };
71
72
  export async function $onEmit(context) {
73
+ const toolchain = reportTypeSpecCompatibility(context);
74
+ if (shouldBlockUnsupportedTypeSpecToolchain(context.options, toolchain)) {
75
+ return;
76
+ }
72
77
  const options = {
73
78
  emitterOutputDir: context.emitterOutputDir,
74
79
  ...context.options,
@@ -159,7 +164,7 @@ export async function $onEmit(context) {
159
164
  }
160
165
  }
161
166
  await emitGeneratedFile(context, resolvePath(context.emitterOutputDir, "json-ast", "model.json"), JSON.stringify(model.getSanitizedObject(), null, 2), { marker: false });
162
- await emitExportSurfaceSnapshot(context, buildExportSurfaceSnapshot(rootObject, rootNamespace, rootAlias, targets, exportSurfaceNodes));
167
+ await emitExportSurfaceSnapshot(context, buildExportSurfaceSnapshot(rootObject, rootNamespace, rootAlias, targets, exportSurfaceNodes, toolchain));
163
168
  await emitGeneratedManifest(context);
164
169
  }
165
170
  //# sourceMappingURL=emitter.js.map
package/dist/src/lib.d.ts CHANGED
@@ -18,6 +18,7 @@ export interface TypraEmitterOptions {
18
18
  "omit-models"?: string[];
19
19
  "schema-output-dir"?: string;
20
20
  "additional-roots"?: string[];
21
+ "allow-unsupported-typespec-version"?: boolean;
21
22
  }
22
23
  export declare const $lib: import("@typespec/compiler").TypeSpecLibrary<{
23
24
  [code: string]: import("@typespec/compiler").DiagnosticMessages;
package/dist/src/lib.js CHANGED
@@ -82,6 +82,12 @@ const TypraEmitterOptionsSchema = {
82
82
  items: { type: "string" },
83
83
  nullable: true,
84
84
  description: "Additional root types to resolve and generate alongside the main root object. These types need not be referenced from the main root. Specified as fully-qualified names (e.g., 'Typra.Message')."
85
+ },
86
+ "allow-unsupported-typespec-version": {
87
+ type: "boolean",
88
+ nullable: true,
89
+ default: false,
90
+ description: "Allow generation with an unvalidated TypeSpec compiler/json-schema version. Unsupported versions report a warning instead of an error."
85
91
  }
86
92
  },
87
93
  required: ["root-object"],
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@typra/emitter",
3
- "version": "0.2.5",
3
+ "version": "0.2.6",
4
4
  "description": "Generic TypeSpec emitter for generating multi-runtime model surfaces",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -40,15 +40,15 @@
40
40
  "fixtures/tspconfig.yaml"
41
41
  ],
42
42
  "peerDependencies": {
43
- "@typespec/compiler": "latest",
44
- "@typespec/json-schema": "latest"
43
+ "@typespec/compiler": "1.10.0",
44
+ "@typespec/json-schema": "1.10.0"
45
45
  },
46
46
  "devDependencies": {
47
47
  "@types/node": "^24.7.0",
48
48
  "@typescript-eslint/eslint-plugin": "^8.15.0",
49
49
  "@typescript-eslint/parser": "^8.15.0",
50
- "@typespec/compiler": "latest",
51
- "@typespec/json-schema": "^1.8.0",
50
+ "@typespec/compiler": "1.10.0",
51
+ "@typespec/json-schema": "1.10.0",
52
52
  "eslint": "^9.15.0",
53
53
  "markdownlint-cli": "^0.48.0",
54
54
  "prettier": "^3.3.3",