@seriousgaming/sdk-tools 1.5.0 → 1.6.1
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/dist/agent/guidance.md +350 -0
- package/dist/bundling/ui-bundler.d.ts +12 -0
- package/dist/cli-25RWY5BQ.js +41 -0
- package/dist/cli-2BH7FX27.js +52 -0
- package/dist/cli-2GZYGRVL.js +36 -0
- package/dist/cli-2NU2LPEE.js +97 -0
- package/dist/cli-3BRC32CP.js +132 -0
- package/dist/cli-3D3ZXWOV.js +111 -0
- package/dist/cli-3ZKWV3PN.js +96 -0
- package/dist/cli-45OCBG53.js +60 -0
- package/dist/cli-4LKXZQ2B.js +48 -0
- package/dist/cli-4PXNOJQX.js +81 -0
- package/dist/cli-4SIWLHQO.js +717 -0
- package/dist/cli-4UWFQNGF.js +54 -0
- package/dist/cli-4VFZHLDG.js +202 -0
- package/dist/cli-4YB3ROVR.js +78 -0
- package/dist/cli-5ACDPCXI.js +21 -0
- package/dist/cli-5IAW3LIF.js +38 -0
- package/dist/cli-5SM367K7.js +30 -0
- package/dist/cli-64AB62PM.js +25 -0
- package/dist/cli-6K3VQQUS.js +307 -0
- package/dist/cli-6QB2VFPY.js +80 -0
- package/dist/cli-6SGCYEEC.js +649 -0
- package/dist/cli-6UTPHWRQ.js +207 -0
- package/dist/cli-6VNKQ3OX.js +391 -0
- package/dist/cli-72XDEFU3.js +61 -0
- package/dist/cli-7A6MKCUJ.js +463 -0
- package/dist/cli-7XLHO2FE.js +226 -0
- package/dist/cli-AV2FYC5G.js +225 -0
- package/dist/cli-B6YHEKIJ.js +248 -0
- package/dist/cli-BJMS3VX3.js +1837 -0
- package/dist/cli-BUQI5UU4.js +43 -0
- package/dist/cli-BWX5E7TQ.js +23 -0
- package/dist/cli-BYCHKSSH.js +1331 -0
- package/dist/cli-C3CMJSNL.js +89 -0
- package/dist/cli-C47GJATR.js +42 -0
- package/dist/cli-C5QOJJWX.js +46 -0
- package/dist/cli-C6ZLPK2U.js +124 -0
- package/dist/cli-CD5CIJZI.js +72 -0
- package/dist/cli-DDJYUKQG.js +36 -0
- package/dist/cli-DFBTZEBX.js +167 -0
- package/dist/cli-DOXRQ3S5.js +60 -0
- package/dist/cli-EVI36HEM.js +79 -0
- package/dist/cli-F2D4GGUA.js +549 -0
- package/dist/cli-FQRVFGN6.js +0 -0
- package/dist/cli-FWFRKQNE.js +11 -0
- package/dist/cli-G6CANSPL.js +88 -0
- package/dist/cli-GFNDW2DL.js +124 -0
- package/dist/cli-HCRTYQQS.js +16 -0
- package/dist/cli-HFX3YPUL.js +76 -0
- package/dist/cli-HKORECAG.js +53 -0
- package/dist/cli-HOE3WLFH.js +73 -0
- package/dist/cli-HSIHDN6K.js +187 -0
- package/dist/cli-HW53ICZC.js +89 -0
- package/dist/cli-IDVUHGKG.js +218 -0
- package/dist/cli-IEMKI7J7.js +1387 -0
- package/dist/cli-ILFDANBR.js +101 -0
- package/dist/cli-ILKCQNM6.js +8 -0
- package/dist/cli-IP4RXGHM.js +2847 -0
- package/dist/cli-IUS2YCD3.js +31 -0
- package/dist/cli-J4DKUUTK.js +138 -0
- package/dist/cli-J4UBELKF.js +37 -0
- package/dist/cli-JJSWBW4H.js +89 -0
- package/dist/cli-JVI24FI6.js +68 -0
- package/dist/cli-JZCLIT3O.js +135 -0
- package/dist/cli-JZOZOEZM.js +131 -0
- package/dist/cli-KGM6RRQW.js +185 -0
- package/dist/cli-KGT5L7VZ.js +13641 -0
- package/dist/cli-KOGRWFN7.js +89 -0
- package/dist/cli-KSGB2ZLK.js +101 -0
- package/dist/cli-L7VYSXQK.js +243 -0
- package/dist/cli-LK3V4RI5.js +36 -0
- package/dist/cli-LQOHATY2.js +126 -0
- package/dist/cli-MJUYJIEP.js +13 -0
- package/dist/cli-MJV47HKI.js +143 -0
- package/dist/cli-MMN4BPO4.js +145 -0
- package/dist/cli-MOPSZ3CS.js +88 -0
- package/dist/cli-MU5LB43S.js +136 -0
- package/dist/cli-MYJGMA6U.js +40 -0
- package/dist/cli-N24OHSAN.js +36 -0
- package/dist/cli-N45DREM4.js +99 -0
- package/dist/cli-N6H6T2E2.js +616 -0
- package/dist/cli-NROTDF5Y.js +182 -0
- package/dist/cli-O5ABH2WK.js +226 -0
- package/dist/cli-OMWC75CC.js +256 -0
- package/dist/cli-OXAUOWCN.js +50 -0
- package/dist/cli-PGCIHPU7.js +143 -0
- package/dist/cli-Q6ZBZYK2.js +35 -0
- package/dist/cli-QUNIRH7A.js +57 -0
- package/dist/cli-QUSCIUOM.js +142 -0
- package/dist/cli-QVASWX2A.js +187 -0
- package/dist/cli-QWQMPCL6.js +253 -0
- package/dist/cli-QY5NZAR7.js +97 -0
- package/dist/cli-R2MUZQE2.js +62 -0
- package/dist/cli-RCIWBCXD.js +36 -0
- package/dist/cli-RLEGRWFM.js +81 -0
- package/dist/cli-RSW6EBBD.js +89 -0
- package/dist/cli-RZITKPJX.js +367 -0
- package/dist/cli-S5IVGDDD.js +32 -0
- package/dist/cli-SRHBXZCN.js +159 -0
- package/dist/cli-TDSQSCX4.js +131 -0
- package/dist/cli-TIDQ4WUE.js +257 -0
- package/dist/cli-TT36JCU5.js +370 -0
- package/dist/cli-TTKICM5P.js +100 -0
- package/dist/cli-TTMBBEJC.js +131 -0
- package/dist/cli-UMEBPAZQ.js +16 -0
- package/dist/cli-UNAJ2OTE.js +15 -0
- package/dist/cli-UZZFNXRX.js +226 -0
- package/dist/cli-VDBW23SS.js +81 -0
- package/dist/cli-VL2P354N.js +46 -0
- package/dist/cli-VPRRICZA.js +304 -0
- package/dist/cli-VQ6HVQW4.js +4010 -0
- package/dist/cli-WR3NOY74.js +36 -0
- package/dist/cli-WU43JT4T.js +52 -0
- package/dist/cli-XAT25PRU.js +117 -0
- package/dist/cli-XOTNXEJA.js +124 -0
- package/dist/cli-XSLNH5PR.js +6417 -0
- package/dist/cli-XSVGKI3R.js +39 -0
- package/dist/cli-XZCJIKIV.js +124 -0
- package/dist/cli-Y3DDNMPX.js +97 -0
- package/dist/cli-Y3O5F4JN.js +90 -0
- package/dist/cli-YOTGR3NB.js +82 -0
- package/dist/cli-Z56ZIVA6.js +97 -0
- package/dist/cli-ZBN5KAIB.js +59 -0
- package/dist/cli-ZDOTXEKL.js +31 -0
- package/dist/cli-ZDUADFBY.js +88 -0
- package/dist/cli-ZJFQ7EEA.js +131 -0
- package/dist/cli-ZTWOE3ZT.js +65 -0
- package/dist/cli.js +148 -43093
- package/dist/client/build-surfaces.d.ts +33 -0
- package/dist/client/dev-server.d.ts +6 -0
- package/dist/client/index.d.ts +1 -0
- package/dist/client/playwright.d.ts +62 -0
- package/dist/client.js +228 -48
- package/dist/core/session.d.ts +15 -1
- package/dist/dev/console/assets/{index-BVF8exas.js → index-CuMOOH9m.js} +1 -1
- package/dist/dev/console/index.html +1 -1
- package/dist/dev/shell.d.ts +19 -0
- package/dist/dev/surfaces.d.ts +7 -0
- package/dist/docs/ai-agents.md +1 -1
- package/dist/docs/api/index.md +14 -12
- package/dist/docs/api/sdk-tools-client.md +29 -0
- package/dist/docs/api/sdk-tools-playwright.md +104 -0
- package/dist/docs/changelog.md +34 -0
- package/dist/docs/cli-and-dev-server.md +21 -0
- package/dist/docs/cli-reference.md +45 -31
- package/dist/docs/environment-variables.md +6 -0
- package/dist/docs/getting-started.md +1 -1
- package/dist/docs/guides/designing-a-game.md +29 -4
- package/dist/docs/guides/starter-game.md +3 -3
- package/dist/docs/guides/testing.md +33 -18
- package/dist/docs/guides/upgrading.md +19 -2
- package/dist/docs/page-index.md +10 -9
- package/dist/docs/recipes/e2e-testing.md +19 -20
- package/dist/playwright.js +32232 -0
- package/dist/templates/package/CLAUDE.md.template +7 -346
- package/dist/templates/package/claude/rules/workflow-create-engine.md +2 -2
- package/dist/templates/package/claude/rules/workflow-create-guide.md +2 -2
- package/dist/templates/package/claude/rules/workflow-create-user-interface.md +1 -1
- package/dist/templates/package/claude/rules/workflow-test-engine-or-ui.md +1 -1
- package/dist/templates/package/claude/rules/workflow-update-sdk-version.md +3 -3
- package/dist/templates/package/global-setup.ts.template +7 -18
- package/dist/templates/package/playwright.config.ts.liquid +8 -11
- package/dist/templates/starter-game/playwright.config.ts +8 -11
- package/dist/templates/starter-game/testing/e2e/game.spec.ts +1 -1
- package/dist/templates/starter-game/testing/e2e/global-setup.ts +7 -18
- package/dist/utils/prompt.d.ts +5 -5
- package/dist/utils/resource-scan.d.ts +39 -0
- package/package.json +7 -3
|
@@ -0,0 +1,1387 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import {
|
|
3
|
+
SCENARIO_FILES,
|
|
4
|
+
absolutiseTsTypes,
|
|
5
|
+
relativiseTsTypes
|
|
6
|
+
} from "./cli-OMWC75CC.js";
|
|
7
|
+
import {
|
|
8
|
+
resolveCapabilityScope
|
|
9
|
+
} from "./cli-BYCHKSSH.js";
|
|
10
|
+
import {
|
|
11
|
+
dereference,
|
|
12
|
+
isLocalRef,
|
|
13
|
+
resolveLocalRef
|
|
14
|
+
} from "./cli-VQ6HVQW4.js";
|
|
15
|
+
import {
|
|
16
|
+
logger
|
|
17
|
+
} from "./cli-4YB3ROVR.js";
|
|
18
|
+
import {
|
|
19
|
+
CliError
|
|
20
|
+
} from "./cli-N6H6T2E2.js";
|
|
21
|
+
|
|
22
|
+
// cli/generation/endpoint-types.ts
|
|
23
|
+
import { writeFile as writeFile3 } from "fs/promises";
|
|
24
|
+
import { join as join2 } from "path";
|
|
25
|
+
|
|
26
|
+
// cli/generation/capability-scope.ts
|
|
27
|
+
import { dirname, join } from "path";
|
|
28
|
+
import { isDeepStrictEqual } from "util";
|
|
29
|
+
|
|
30
|
+
// cli/generation/defaults.ts
|
|
31
|
+
import { rm, writeFile } from "fs/promises";
|
|
32
|
+
|
|
33
|
+
// cli/generation/generated-file.ts
|
|
34
|
+
import { existsSync } from "fs";
|
|
35
|
+
import { readFile } from "fs/promises";
|
|
36
|
+
var GENERATED_HEADER_MARKER = "// This file is auto-generated";
|
|
37
|
+
async function assertSafeToGenerate(outputPath) {
|
|
38
|
+
if (!existsSync(outputPath)) return;
|
|
39
|
+
const existing = await readFile(outputPath, "utf-8");
|
|
40
|
+
if (existing.startsWith(GENERATED_HEADER_MARKER)) return;
|
|
41
|
+
throw new CliError(
|
|
42
|
+
"PACKAGE_INVALID",
|
|
43
|
+
`Refusing to overwrite ${outputPath}: it was not generated by the SDK.
|
|
44
|
+
Every resource has a types.ts and a defaults.ts generated beside its bundle, so a hand-written file of either name collides with the generated one.
|
|
45
|
+
Move or rename the hand-written file, update the imports that reach it, then build again.`
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// cli/generation/defaults.ts
|
|
50
|
+
var DEFAULTS_HEADER = "// This file is auto-generated from the schemas the resource declares: the values\n// a session starts with, for use as test fixtures.\n// Do not edit this file directly. It will be regenerated on build.";
|
|
51
|
+
function schemaDefaults(args) {
|
|
52
|
+
const properties = args.schema?.properties;
|
|
53
|
+
if (!properties) return {};
|
|
54
|
+
const defaults = {};
|
|
55
|
+
for (const [name, property] of Object.entries(properties)) {
|
|
56
|
+
if (!isSchemaObject(property) || !("default" in property)) {
|
|
57
|
+
throw new CliError(
|
|
58
|
+
"PACKAGE_INVALID",
|
|
59
|
+
missingDefaultMessage(args.owner, args.kind, name, property)
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
defaults[name] = property.default;
|
|
63
|
+
}
|
|
64
|
+
return defaults;
|
|
65
|
+
}
|
|
66
|
+
function isSchemaObject(value) {
|
|
67
|
+
return typeof value === "object" && value !== null;
|
|
68
|
+
}
|
|
69
|
+
function missingDefaultMessage(owner, kind, name, property) {
|
|
70
|
+
if (kind === "configuration") {
|
|
71
|
+
return `${owner} declares settings property "${name}" with no \`default\`. Every settings property must declare one: every override layer above the defaults is partial, so nothing above reliably completes an incomplete first layer.`;
|
|
72
|
+
}
|
|
73
|
+
const declared = isSchemaObject(property) ? property.type : void 0;
|
|
74
|
+
const asType = typeof declared === "string" ? `\`${declared}\`` : "its declared type";
|
|
75
|
+
return `${owner} declares variable "${name}" with no \`default\`. The runtime seeds an undeclared default to null, while the generated \`Context\` types the variable as ${asType}, so a generated \`defaultContext\` could satisfy neither. Declare a \`default\` for "${name}".`;
|
|
76
|
+
}
|
|
77
|
+
async function writeDefaultsFile(args) {
|
|
78
|
+
const imports = [];
|
|
79
|
+
const declarations = [];
|
|
80
|
+
if (args.context) {
|
|
81
|
+
imports.push("Context");
|
|
82
|
+
declarations.push(
|
|
83
|
+
`export const defaultContext: Context = ${renderObject(args.context, 0)};`,
|
|
84
|
+
""
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
if (args.context && args.scoped) {
|
|
88
|
+
const context = args.context;
|
|
89
|
+
const pick = (ids) => Object.fromEntries(ids.filter((id) => id in context).map((id) => [id, context[id]]));
|
|
90
|
+
imports.push("TeamContext", "SharedContext");
|
|
91
|
+
declarations.push(
|
|
92
|
+
`export const defaultTeamContext: TeamContext = ${renderObject(pick(args.scoped.team), 0)};`,
|
|
93
|
+
"",
|
|
94
|
+
`export const defaultSharedContext: SharedContext = ${renderObject(pick(args.scoped.shared), 0)};`,
|
|
95
|
+
""
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
if (args.settings) {
|
|
99
|
+
imports.push("Settings");
|
|
100
|
+
declarations.push(
|
|
101
|
+
`export const defaultSettings: Settings = ${renderObject(args.settings, 0)};`,
|
|
102
|
+
""
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
if (imports.length === 0) {
|
|
106
|
+
await assertSafeToGenerate(args.outputPath);
|
|
107
|
+
await rm(args.outputPath, { force: true });
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
const contents = [
|
|
111
|
+
args.headerComment,
|
|
112
|
+
"",
|
|
113
|
+
`import type { ${imports.join(", ")} } from './types';`,
|
|
114
|
+
"",
|
|
115
|
+
...declarations
|
|
116
|
+
].join("\n");
|
|
117
|
+
await assertSafeToGenerate(args.outputPath);
|
|
118
|
+
await writeFile(args.outputPath, contents, "utf-8");
|
|
119
|
+
logger.debug(`Generated ${args.outputPath}`);
|
|
120
|
+
}
|
|
121
|
+
function render(value, indent, column) {
|
|
122
|
+
if (Array.isArray(value)) return renderArray(value, indent, column);
|
|
123
|
+
if (value !== null && typeof value === "object") {
|
|
124
|
+
return renderObject(value, indent);
|
|
125
|
+
}
|
|
126
|
+
return renderScalar(value);
|
|
127
|
+
}
|
|
128
|
+
var PRINT_WIDTH = 100;
|
|
129
|
+
var TAB = 4;
|
|
130
|
+
function renderObject(value, indent) {
|
|
131
|
+
const entries = Object.entries(value);
|
|
132
|
+
if (entries.length === 0) return "{}";
|
|
133
|
+
const inner = " ".repeat((indent + 1) * TAB);
|
|
134
|
+
const lines = entries.map(([key, entry]) => {
|
|
135
|
+
const prop = `${renderKey(key)}: `;
|
|
136
|
+
return `${inner}${prop}${render(entry, indent + 1, inner.length + prop.length)},`;
|
|
137
|
+
});
|
|
138
|
+
return `{
|
|
139
|
+
${lines.join("\n")}
|
|
140
|
+
${" ".repeat(indent * TAB)}}`;
|
|
141
|
+
}
|
|
142
|
+
function renderArray(value, indent, column) {
|
|
143
|
+
if (value.length === 0) return "[]";
|
|
144
|
+
const rendered = value.map((entry) => render(entry, indent + 1, 0));
|
|
145
|
+
const inline = `[${rendered.join(", ")}]`;
|
|
146
|
+
if (!inline.includes("\n") && column + inline.length + 1 <= PRINT_WIDTH) return inline;
|
|
147
|
+
const inner = " ".repeat((indent + 1) * TAB);
|
|
148
|
+
const lines = value.map((entry) => `${inner}${render(entry, indent + 1, inner.length)},`);
|
|
149
|
+
return `[
|
|
150
|
+
${lines.join("\n")}
|
|
151
|
+
${" ".repeat(indent * TAB)}]`;
|
|
152
|
+
}
|
|
153
|
+
function renderKey(key) {
|
|
154
|
+
return /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(key) ? key : renderString(key);
|
|
155
|
+
}
|
|
156
|
+
function renderScalar(value) {
|
|
157
|
+
if (typeof value === "string") return renderString(value);
|
|
158
|
+
if (value === void 0) return "undefined";
|
|
159
|
+
return JSON.stringify(value) ?? "null";
|
|
160
|
+
}
|
|
161
|
+
function renderString(value) {
|
|
162
|
+
const singles = (value.match(/'/g) ?? []).length;
|
|
163
|
+
const doubles = (value.match(/"/g) ?? []).length;
|
|
164
|
+
const quote = singles > doubles ? '"' : "'";
|
|
165
|
+
const body = JSON.stringify(value).slice(1, -1).replace(/\\"/g, '"').replaceAll(quote, `\\${quote}`);
|
|
166
|
+
return `${quote}${body}${quote}`;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// cli/generation/typescript-generator.ts
|
|
170
|
+
import { writeFile as writeFile2 } from "fs/promises";
|
|
171
|
+
|
|
172
|
+
// cli/generation/compile-schema.ts
|
|
173
|
+
import { compile } from "json-schema-to-typescript";
|
|
174
|
+
var COMPILE_STYLE = {
|
|
175
|
+
singleQuote: true,
|
|
176
|
+
tabWidth: 4,
|
|
177
|
+
trailingComma: "all",
|
|
178
|
+
printWidth: 80,
|
|
179
|
+
semi: true
|
|
180
|
+
};
|
|
181
|
+
var SCHEMA_MAPS = ["properties", "patternProperties", "definitions", "$defs"];
|
|
182
|
+
var SCHEMA_LISTS = ["anyOf", "oneOf", "allOf"];
|
|
183
|
+
var SCHEMA_SLOTS = [
|
|
184
|
+
"additionalProperties",
|
|
185
|
+
"additionalItems",
|
|
186
|
+
"items",
|
|
187
|
+
"contains",
|
|
188
|
+
"not",
|
|
189
|
+
"if",
|
|
190
|
+
"then",
|
|
191
|
+
"else",
|
|
192
|
+
"propertyNames"
|
|
193
|
+
];
|
|
194
|
+
function isNode(value) {
|
|
195
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
196
|
+
}
|
|
197
|
+
function namesAnInterface(schema) {
|
|
198
|
+
if (schema.tsType !== void 0) return false;
|
|
199
|
+
if (schema.type === "object") return true;
|
|
200
|
+
return schema.type === void 0 && isNode(schema.properties);
|
|
201
|
+
}
|
|
202
|
+
function withoutValueTitles(schema) {
|
|
203
|
+
if (Array.isArray(schema)) return schema.map(withoutValueTitles);
|
|
204
|
+
if (!isNode(schema)) return schema;
|
|
205
|
+
const copy = { ...schema };
|
|
206
|
+
if (typeof copy.title === "string" && !namesAnInterface(copy)) delete copy.title;
|
|
207
|
+
for (const key of SCHEMA_MAPS) {
|
|
208
|
+
const map = copy[key];
|
|
209
|
+
if (isNode(map)) {
|
|
210
|
+
copy[key] = Object.fromEntries(
|
|
211
|
+
Object.entries(map).map(([name, child]) => [name, withoutValueTitles(child)])
|
|
212
|
+
);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
for (const key of [...SCHEMA_LISTS, ...SCHEMA_SLOTS]) {
|
|
216
|
+
if (copy[key] !== void 0) copy[key] = withoutValueTitles(copy[key]);
|
|
217
|
+
}
|
|
218
|
+
return copy;
|
|
219
|
+
}
|
|
220
|
+
function compileSchema(schema, name) {
|
|
221
|
+
return compile(withoutValueTitles(schema), name, {
|
|
222
|
+
bannerComment: "",
|
|
223
|
+
declareExternallyReferenced: true,
|
|
224
|
+
style: COMPILE_STYLE
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
// cli/generation/ref-types.ts
|
|
229
|
+
var SCHEMA_MAPS2 = ["properties", "patternProperties"];
|
|
230
|
+
var SCHEMA_SLOTS2 = [
|
|
231
|
+
"additionalProperties",
|
|
232
|
+
"additionalItems",
|
|
233
|
+
"items",
|
|
234
|
+
"contains",
|
|
235
|
+
"propertyNames",
|
|
236
|
+
"if",
|
|
237
|
+
"then",
|
|
238
|
+
"else",
|
|
239
|
+
"not",
|
|
240
|
+
"allOf",
|
|
241
|
+
"anyOf",
|
|
242
|
+
"oneOf"
|
|
243
|
+
];
|
|
244
|
+
var RESERVED = [
|
|
245
|
+
"Context",
|
|
246
|
+
"CrossTeamContext",
|
|
247
|
+
"TeamContext",
|
|
248
|
+
"SharedContext",
|
|
249
|
+
"SharedWrites",
|
|
250
|
+
"SessionData",
|
|
251
|
+
"Settings",
|
|
252
|
+
"Callbacks",
|
|
253
|
+
"Endpoints",
|
|
254
|
+
"Request",
|
|
255
|
+
"Response",
|
|
256
|
+
"Writes",
|
|
257
|
+
"TeamWrites",
|
|
258
|
+
"Handler",
|
|
259
|
+
"Planner",
|
|
260
|
+
"Responder",
|
|
261
|
+
"Action"
|
|
262
|
+
];
|
|
263
|
+
function pascal(text) {
|
|
264
|
+
const words = text.split(/[^A-Za-z0-9]+/).filter((word) => word !== "");
|
|
265
|
+
const name = words.map((word) => word.charAt(0).toUpperCase() + word.slice(1)).join("");
|
|
266
|
+
return /^[A-Za-z]/.test(name) ? name : `T${name}`;
|
|
267
|
+
}
|
|
268
|
+
var RefTypes = class {
|
|
269
|
+
names = /* @__PURE__ */ new Map();
|
|
270
|
+
taken = new Set(RESERVED);
|
|
271
|
+
pending = [];
|
|
272
|
+
/** Each schema of a scope, keyed by resource identifier, with its references named. */
|
|
273
|
+
inlineScope(kind, properties) {
|
|
274
|
+
return Object.fromEntries(
|
|
275
|
+
Object.entries(properties).map(([identifier, schema]) => [
|
|
276
|
+
identifier,
|
|
277
|
+
this.inline({ key: `${kind}:${identifier}`, identifier, root: schema }, schema)
|
|
278
|
+
])
|
|
279
|
+
);
|
|
280
|
+
}
|
|
281
|
+
/** `schema`, part of `owner`'s, with every local reference replaced by its type's name. */
|
|
282
|
+
inline(owner, schema) {
|
|
283
|
+
const { $defs: _defs, definitions: _definitions, ...rest } = schema;
|
|
284
|
+
if (typeof rest.$ref === "string") {
|
|
285
|
+
const { $ref: ref, ...beside } = rest;
|
|
286
|
+
return { ...beside, tsType: this.nameFor(owner, ref) };
|
|
287
|
+
}
|
|
288
|
+
const copy = { ...rest };
|
|
289
|
+
for (const keyword of SCHEMA_MAPS2) {
|
|
290
|
+
const map = copy[keyword];
|
|
291
|
+
if (map && typeof map === "object") {
|
|
292
|
+
copy[keyword] = Object.fromEntries(
|
|
293
|
+
Object.entries(map).map(([key, child]) => [key, this.child(owner, child)])
|
|
294
|
+
);
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
for (const keyword of SCHEMA_SLOTS2) {
|
|
298
|
+
const value = copy[keyword];
|
|
299
|
+
if (Array.isArray(value))
|
|
300
|
+
copy[keyword] = value.map((child) => this.child(owner, child));
|
|
301
|
+
else if (value !== void 0) copy[keyword] = this.child(owner, value);
|
|
302
|
+
}
|
|
303
|
+
return copy;
|
|
304
|
+
}
|
|
305
|
+
child(owner, value) {
|
|
306
|
+
return value !== null && typeof value === "object" && !Array.isArray(value) ? this.inline(owner, value) : value;
|
|
307
|
+
}
|
|
308
|
+
nameFor(owner, ref) {
|
|
309
|
+
const key = `${owner.key}\0${ref}`;
|
|
310
|
+
const known = this.names.get(key);
|
|
311
|
+
if (known !== void 0) return known;
|
|
312
|
+
if (!isLocalRef(ref) || resolveLocalRef(owner.root, ref) === void 0) {
|
|
313
|
+
throw new CliError(
|
|
314
|
+
"PACKAGE_INVALID",
|
|
315
|
+
`The schema of "${owner.identifier}" has "$ref": ${JSON.stringify(ref)}, which names nothing in it. A reference may only name a part of the same schema, such as "#/$defs/rule"`
|
|
316
|
+
);
|
|
317
|
+
}
|
|
318
|
+
const last = ref === "#" ? owner.identifier : ref.split("/").pop() ?? owner.identifier;
|
|
319
|
+
let name = pascal(last);
|
|
320
|
+
if (this.taken.has(name)) name = `${pascal(owner.identifier)}${pascal(last)}`;
|
|
321
|
+
for (let suffix = 2; this.taken.has(name); suffix += 1) {
|
|
322
|
+
name = `${pascal(owner.identifier)}${pascal(last)}${suffix}`;
|
|
323
|
+
}
|
|
324
|
+
this.taken.add(name);
|
|
325
|
+
this.names.set(key, name);
|
|
326
|
+
this.pending.push({ name, owner, ref });
|
|
327
|
+
return name;
|
|
328
|
+
}
|
|
329
|
+
/** Every referenced type not yet declared, compiled once each. */
|
|
330
|
+
async declarations() {
|
|
331
|
+
let declared = "";
|
|
332
|
+
for (let next = this.pending.shift(); next; next = this.pending.shift()) {
|
|
333
|
+
const target = resolveLocalRef(next.owner.root, next.ref);
|
|
334
|
+
const schema = typeof target === "object" ? target : target === false ? { not: {} } : {};
|
|
335
|
+
const { title: _title, ...body } = this.inline(next.owner, schema);
|
|
336
|
+
const compiled = await compileSchema(
|
|
337
|
+
{ $schema: "https://json-schema.org/draft/2020-12/schema", ...body },
|
|
338
|
+
next.name
|
|
339
|
+
);
|
|
340
|
+
declared += `
|
|
341
|
+
${compiled.replace(/:\s*\{\};/g, ": Record<string, never>;")}`;
|
|
342
|
+
}
|
|
343
|
+
return declared;
|
|
344
|
+
}
|
|
345
|
+
};
|
|
346
|
+
|
|
347
|
+
// cli/generation/typescript-generator.ts
|
|
348
|
+
async function generateTypeScriptTypes(options) {
|
|
349
|
+
const {
|
|
350
|
+
properties,
|
|
351
|
+
required,
|
|
352
|
+
settingsScope,
|
|
353
|
+
callbacks,
|
|
354
|
+
endpoints,
|
|
355
|
+
contextAliases = [],
|
|
356
|
+
trailer = "",
|
|
357
|
+
crossTeamContext,
|
|
358
|
+
outputPath,
|
|
359
|
+
headerComment = `// This file is auto-generated from the resource's capability definitions.
|
|
360
|
+
// Do not edit this file directly. It will be regenerated on build.`
|
|
361
|
+
} = options;
|
|
362
|
+
const refs = new RefTypes();
|
|
363
|
+
const contextTypeName = "Context";
|
|
364
|
+
const settingsTypeName = "Settings";
|
|
365
|
+
let contextTypeScript;
|
|
366
|
+
if (properties && Object.keys(properties).length > 0) {
|
|
367
|
+
const contextSchema = {
|
|
368
|
+
$schema: "https://json-schema.org/draft/2020-12/schema",
|
|
369
|
+
type: "object",
|
|
370
|
+
properties: refs.inlineScope("capability", properties),
|
|
371
|
+
required,
|
|
372
|
+
additionalProperties: false
|
|
373
|
+
};
|
|
374
|
+
contextTypeScript = await compileSchema(contextSchema, contextTypeName);
|
|
375
|
+
} else {
|
|
376
|
+
contextTypeScript = `export type ${contextTypeName} = Record<string, never>;
|
|
377
|
+
`;
|
|
378
|
+
}
|
|
379
|
+
let configTypeScript;
|
|
380
|
+
if (settingsScope && Object.keys(settingsScope.properties).length > 0) {
|
|
381
|
+
const settingsSchema = {
|
|
382
|
+
$schema: "https://json-schema.org/draft/2020-12/schema",
|
|
383
|
+
type: "object",
|
|
384
|
+
properties: refs.inlineScope(
|
|
385
|
+
"settings",
|
|
386
|
+
Object.fromEntries(
|
|
387
|
+
Object.entries(settingsScope.properties).map(([identifier, schema]) => [
|
|
388
|
+
identifier,
|
|
389
|
+
resolvedSettingsSchema(schema)
|
|
390
|
+
])
|
|
391
|
+
)
|
|
392
|
+
),
|
|
393
|
+
required: settingsScope.required,
|
|
394
|
+
additionalProperties: false
|
|
395
|
+
};
|
|
396
|
+
configTypeScript = await compileSchema(settingsSchema, settingsTypeName);
|
|
397
|
+
configTypeScript = configTypeScript.replace(/:\s*\{\};/g, ": Record<string, never>;");
|
|
398
|
+
} else {
|
|
399
|
+
configTypeScript = `export type ${settingsTypeName} = Record<string, never>;
|
|
400
|
+
`;
|
|
401
|
+
}
|
|
402
|
+
const contextFixed = contextTypeScript.replace(/:\s*\{\};/g, ": Record<string, never>;");
|
|
403
|
+
let aliasesTypeScript = "";
|
|
404
|
+
for (const alias of contextAliases) {
|
|
405
|
+
aliasesTypeScript += buildContextAlias(alias);
|
|
406
|
+
}
|
|
407
|
+
const crossTeamContextTypeScript = await buildCrossTeamContext(crossTeamContext, refs);
|
|
408
|
+
const refsTypeScript = await refs.declarations();
|
|
409
|
+
const callbacksTypeScript = await buildCallbacksTypeAlias(callbacks, endpoints);
|
|
410
|
+
const fullTypeScript = `${headerComment}
|
|
411
|
+
|
|
412
|
+
${contextFixed}
|
|
413
|
+
${crossTeamContextTypeScript}${aliasesTypeScript}${trailer}${configTypeScript}${refsTypeScript}${callbacksTypeScript}`;
|
|
414
|
+
await assertSafeToGenerate(outputPath);
|
|
415
|
+
await writeFile2(outputPath, fullTypeScript, "utf-8");
|
|
416
|
+
logger.debug(`Generated ${outputPath}`);
|
|
417
|
+
}
|
|
418
|
+
function buildContextAlias(alias) {
|
|
419
|
+
if (alias.capabilities.length === 0) {
|
|
420
|
+
return `export type ${alias.name} = Record<string, never>;
|
|
421
|
+
|
|
422
|
+
`;
|
|
423
|
+
}
|
|
424
|
+
const keys = alias.capabilities.map((id) => `'${id.replace(/'/g, "\\'")}'`);
|
|
425
|
+
const oneLine = `export type ${alias.name} = Pick<Context, ${keys.join(" | ")}>;`;
|
|
426
|
+
if (oneLine.length <= 100) return `${oneLine}
|
|
427
|
+
|
|
428
|
+
`;
|
|
429
|
+
const union = ` ${keys.join(" | ")}`;
|
|
430
|
+
if (union.length <= 100)
|
|
431
|
+
return `export type ${alias.name} = Pick<
|
|
432
|
+
Context,
|
|
433
|
+
${union}
|
|
434
|
+
>;
|
|
435
|
+
|
|
436
|
+
`;
|
|
437
|
+
return `export type ${alias.name} = Pick<
|
|
438
|
+
Context,
|
|
439
|
+
${keys.map((key) => ` | ${key}`).join("\n")}
|
|
440
|
+
>;
|
|
441
|
+
|
|
442
|
+
`;
|
|
443
|
+
}
|
|
444
|
+
async function buildCrossTeamContext(crossTeamContext, refs) {
|
|
445
|
+
if (!crossTeamContext) return "";
|
|
446
|
+
if (Object.keys(crossTeamContext.properties).length === 0) {
|
|
447
|
+
return "export type CrossTeamContext = Record<string, never>;\n\n";
|
|
448
|
+
}
|
|
449
|
+
const schema = {
|
|
450
|
+
$schema: "https://json-schema.org/draft/2020-12/schema",
|
|
451
|
+
type: "object",
|
|
452
|
+
properties: refs.inlineScope("capability", crossTeamContext.properties),
|
|
453
|
+
required: crossTeamContext.required,
|
|
454
|
+
additionalProperties: false
|
|
455
|
+
};
|
|
456
|
+
const compiled = await compileSchema(schema, "CrossTeamContext");
|
|
457
|
+
return `${compiled.replace(/:\s*\{\};/g, ": Record<string, never>;")}
|
|
458
|
+
`;
|
|
459
|
+
}
|
|
460
|
+
function aliasFor(identifier) {
|
|
461
|
+
return identifier.replace(/[^a-zA-Z0-9]+/g, "_");
|
|
462
|
+
}
|
|
463
|
+
async function compileEndpointSchema(name, schema) {
|
|
464
|
+
if (!schema || !schema.properties || Object.keys(schema.properties).length === 0) {
|
|
465
|
+
return `export type ${name} = Record<string, never>;
|
|
466
|
+
`;
|
|
467
|
+
}
|
|
468
|
+
const withMeta = {
|
|
469
|
+
$schema: "https://json-schema.org/draft/2020-12/schema",
|
|
470
|
+
...schema
|
|
471
|
+
};
|
|
472
|
+
const compiled = await compileSchema(withMeta, name);
|
|
473
|
+
return compiled.replace(/:\s*\{\};/g, ": Record<string, never>;").replace(/^export\s+(interface|type)\s+\S+/m, `export $1 ${name}`);
|
|
474
|
+
}
|
|
475
|
+
async function buildEndpointBlock(endpoints) {
|
|
476
|
+
if (!endpoints || endpoints.length === 0) return { inlineTypes: "", interface: "" };
|
|
477
|
+
const inlineParts = [];
|
|
478
|
+
const interfaceLines = [];
|
|
479
|
+
for (const ep of endpoints) {
|
|
480
|
+
if (ep.requestSchema && ep.responseSchema) {
|
|
481
|
+
const alias = aliasFor(ep.identifier);
|
|
482
|
+
const requestName = `${alias}_Request`;
|
|
483
|
+
const responseName = `${alias}_Response`;
|
|
484
|
+
inlineParts.push(await compileEndpointSchema(requestName, ep.requestSchema));
|
|
485
|
+
inlineParts.push(await compileEndpointSchema(responseName, ep.responseSchema));
|
|
486
|
+
interfaceLines.push(
|
|
487
|
+
` '${ep.identifier}': (request: ${requestName}) => Promise<${responseName}>;`
|
|
488
|
+
);
|
|
489
|
+
} else {
|
|
490
|
+
interfaceLines.push(` '${ep.identifier}': (request: unknown) => Promise<unknown>;`);
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
return {
|
|
494
|
+
inlineTypes: inlineParts.join(""),
|
|
495
|
+
interface: `
|
|
496
|
+
// prettier-ignore
|
|
497
|
+
export interface Endpoints {
|
|
498
|
+
${interfaceLines.join("\n")}
|
|
499
|
+
}
|
|
500
|
+
`
|
|
501
|
+
};
|
|
502
|
+
}
|
|
503
|
+
async function buildCallbacksTypeAlias(options, endpoints) {
|
|
504
|
+
if (!options) return "";
|
|
505
|
+
const sortedUses = [...options.uses ?? []].sort();
|
|
506
|
+
const logic = options.sourceType === "SignatureCallbacks";
|
|
507
|
+
const quotedUses = sortedUses.map((api) => `'${api}'`).join(" | ");
|
|
508
|
+
const importLine = `import type { ${logic ? "LogicCallbacks" : options.sourceType} } from '@seriousgaming/sdk';
|
|
509
|
+
`;
|
|
510
|
+
const endpointBlock = await buildEndpointBlock(endpoints);
|
|
511
|
+
const baseAlias = logic ? sortedUses.length === 0 ? "LogicCallbacks" : `LogicCallbacks<${quotedUses}>` : sortedUses.length === 0 ? `Pick<${options.sourceType}, never>` : `Pick<${options.sourceType}, ${quotedUses}>`;
|
|
512
|
+
if (!endpointBlock.interface) {
|
|
513
|
+
return `
|
|
514
|
+
${importLine}
|
|
515
|
+
export type Callbacks = ${baseAlias};
|
|
516
|
+
`;
|
|
517
|
+
}
|
|
518
|
+
return `
|
|
519
|
+
${importLine}${endpointBlock.inlineTypes}${endpointBlock.interface}
|
|
520
|
+
export type Callbacks = ${baseAlias} & { endpoints: Endpoints };
|
|
521
|
+
`;
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
// cli/generation/capability-scope.ts
|
|
525
|
+
function collectCapabilityScope(args) {
|
|
526
|
+
const uniqueCapabilities = [.../* @__PURE__ */ new Set([...args.reads ?? [], ...args.writes ?? []])];
|
|
527
|
+
const properties = {};
|
|
528
|
+
const required = [];
|
|
529
|
+
for (const capabilityId of uniqueCapabilities) {
|
|
530
|
+
const capability = args.capabilityMap.get(capabilityId);
|
|
531
|
+
if (!capability) {
|
|
532
|
+
logger.warn(`Capability ${capabilityId} not found`);
|
|
533
|
+
continue;
|
|
534
|
+
}
|
|
535
|
+
properties[capabilityId] = capability.definition.variablesSchema;
|
|
536
|
+
required.push(capabilityId);
|
|
537
|
+
}
|
|
538
|
+
return { properties, required };
|
|
539
|
+
}
|
|
540
|
+
function resolvedSettingsSchema(schema) {
|
|
541
|
+
const root = dereference(schema, schema);
|
|
542
|
+
if (!root.properties) return schema;
|
|
543
|
+
return {
|
|
544
|
+
...root,
|
|
545
|
+
required: Object.keys(root.properties),
|
|
546
|
+
properties: Object.fromEntries(
|
|
547
|
+
Object.entries(root.properties).map(([key, property]) => [
|
|
548
|
+
key,
|
|
549
|
+
typeof property === "object" ? requiredWithin(schema, property, property.default) : property
|
|
550
|
+
])
|
|
551
|
+
)
|
|
552
|
+
};
|
|
553
|
+
}
|
|
554
|
+
function requiredWithin(root, schema, value) {
|
|
555
|
+
if (value === null || typeof value !== "object" || Array.isArray(value)) return schema;
|
|
556
|
+
if (typeof schema.$ref === "string") {
|
|
557
|
+
const followed = dereference(root, schema);
|
|
558
|
+
const resolved = requiredWithin(root, followed, value);
|
|
559
|
+
return isDeepStrictEqual(resolved, followed) ? schema : resolved;
|
|
560
|
+
}
|
|
561
|
+
if (!schema.properties) return schema;
|
|
562
|
+
const present = value;
|
|
563
|
+
return {
|
|
564
|
+
...schema,
|
|
565
|
+
required: Object.keys(schema.properties).filter((key) => key in present),
|
|
566
|
+
properties: Object.fromEntries(
|
|
567
|
+
Object.entries(schema.properties).map(([key, property]) => [
|
|
568
|
+
key,
|
|
569
|
+
typeof property === "object" ? requiredWithin(root, property, present[key]) : property
|
|
570
|
+
])
|
|
571
|
+
)
|
|
572
|
+
};
|
|
573
|
+
}
|
|
574
|
+
function collectSettingsScope(args) {
|
|
575
|
+
const properties = {};
|
|
576
|
+
const required = [];
|
|
577
|
+
for (const identifier of args.settings ?? []) {
|
|
578
|
+
const settings = args.settingsMap?.get(identifier);
|
|
579
|
+
if (!settings) {
|
|
580
|
+
logger.warn(`Settings ${identifier} not found`);
|
|
581
|
+
continue;
|
|
582
|
+
}
|
|
583
|
+
properties[identifier] = settings.definition.settingsSchema;
|
|
584
|
+
required.push(identifier);
|
|
585
|
+
}
|
|
586
|
+
return { properties, required };
|
|
587
|
+
}
|
|
588
|
+
function scopeAliases(args) {
|
|
589
|
+
const team = [];
|
|
590
|
+
const shared = [];
|
|
591
|
+
for (const capabilityId of /* @__PURE__ */ new Set([...args.reads ?? [], ...args.writes ?? []])) {
|
|
592
|
+
const capability = args.capabilityMap.get(capabilityId);
|
|
593
|
+
if (!capability) continue;
|
|
594
|
+
(resolveCapabilityScope(capability.definition) === "shared" ? shared : team).push(
|
|
595
|
+
capabilityId
|
|
596
|
+
);
|
|
597
|
+
}
|
|
598
|
+
return [
|
|
599
|
+
{ name: "TeamContext", capabilities: team },
|
|
600
|
+
{ name: "SharedContext", capabilities: shared }
|
|
601
|
+
];
|
|
602
|
+
}
|
|
603
|
+
function collectCrossTeamScope(args) {
|
|
604
|
+
const properties = {};
|
|
605
|
+
const required = [];
|
|
606
|
+
for (const capabilityId of args.crossTeamReads ?? []) {
|
|
607
|
+
const capability = args.capabilityMap.get(capabilityId);
|
|
608
|
+
if (!capability) {
|
|
609
|
+
logger.warn(`Capability ${capabilityId} not found`);
|
|
610
|
+
continue;
|
|
611
|
+
}
|
|
612
|
+
properties[capabilityId] = capability.definition.variablesSchema;
|
|
613
|
+
required.push(capabilityId);
|
|
614
|
+
}
|
|
615
|
+
return { properties, required };
|
|
616
|
+
}
|
|
617
|
+
async function generateCapabilityScopedTypes(args) {
|
|
618
|
+
const { properties, required } = collectCapabilityScope({
|
|
619
|
+
capabilityMap: args.capabilityMap,
|
|
620
|
+
reads: args.reads,
|
|
621
|
+
writes: args.writes
|
|
622
|
+
});
|
|
623
|
+
const settingsScope = collectSettingsScope({
|
|
624
|
+
settingsMap: args.settingsMap,
|
|
625
|
+
settings: args.settings
|
|
626
|
+
});
|
|
627
|
+
await generateTypeScriptTypes({
|
|
628
|
+
properties: relativiseTsTypes(properties, args.outputPath),
|
|
629
|
+
required,
|
|
630
|
+
settingsScope,
|
|
631
|
+
callbacks: args.callbacks,
|
|
632
|
+
endpoints: args.endpoints,
|
|
633
|
+
contextAliases: args.contextAliases,
|
|
634
|
+
trailer: args.trailer,
|
|
635
|
+
crossTeamContext: args.crossTeam ? relativiseTsTypes(
|
|
636
|
+
collectCrossTeamScope({
|
|
637
|
+
capabilityMap: args.capabilityMap,
|
|
638
|
+
crossTeamReads: args.crossTeamReads
|
|
639
|
+
}),
|
|
640
|
+
args.outputPath
|
|
641
|
+
) : void 0,
|
|
642
|
+
outputPath: args.outputPath,
|
|
643
|
+
headerComment: args.headerComment
|
|
644
|
+
});
|
|
645
|
+
await writeDefaultsFile({
|
|
646
|
+
outputPath: join(dirname(args.outputPath), "defaults.ts"),
|
|
647
|
+
headerComment: DEFAULTS_HEADER,
|
|
648
|
+
context: Object.fromEntries(
|
|
649
|
+
Object.entries(properties).map(([capabilityId, schema]) => [
|
|
650
|
+
capabilityId,
|
|
651
|
+
schemaDefaults({
|
|
652
|
+
owner: `Capability "${capabilityId}"`,
|
|
653
|
+
kind: "variable",
|
|
654
|
+
schema
|
|
655
|
+
})
|
|
656
|
+
])
|
|
657
|
+
),
|
|
658
|
+
settings: Object.fromEntries(
|
|
659
|
+
Object.entries(settingsScope.properties).map(([identifier, schema]) => [
|
|
660
|
+
identifier,
|
|
661
|
+
schemaDefaults({
|
|
662
|
+
owner: `Settings "${identifier}"`,
|
|
663
|
+
kind: "configuration",
|
|
664
|
+
schema
|
|
665
|
+
})
|
|
666
|
+
])
|
|
667
|
+
),
|
|
668
|
+
scoped: scopedDefaults(args.contextAliases)
|
|
669
|
+
});
|
|
670
|
+
}
|
|
671
|
+
function scopedDefaults(aliases) {
|
|
672
|
+
const team = aliases?.find((alias) => alias.name === "TeamContext");
|
|
673
|
+
const shared = aliases?.find((alias) => alias.name === "SharedContext");
|
|
674
|
+
return team && shared ? { team: team.capabilities, shared: shared.capabilities } : void 0;
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
// cli/generation/endpoint-types.ts
|
|
678
|
+
async function compileNamed(name, schema, scope) {
|
|
679
|
+
if (!schema || !schema.properties || Object.keys(schema.properties).length === 0) {
|
|
680
|
+
return `export type ${name} = Record<string, never>;
|
|
681
|
+
`;
|
|
682
|
+
}
|
|
683
|
+
const withMeta = {
|
|
684
|
+
$schema: "https://json-schema.org/draft/2020-12/schema",
|
|
685
|
+
...schema,
|
|
686
|
+
...scope ? {
|
|
687
|
+
properties: scope.refs.inlineScope(
|
|
688
|
+
scope.kind,
|
|
689
|
+
schema.properties
|
|
690
|
+
)
|
|
691
|
+
} : {}
|
|
692
|
+
};
|
|
693
|
+
const compiled = await compileSchema(withMeta, name);
|
|
694
|
+
return compiled.replace(/:\s*\{\};/g, ": Record<string, never>;");
|
|
695
|
+
}
|
|
696
|
+
function withBlobTypes(schema) {
|
|
697
|
+
if (!schema || typeof schema !== "object") return schema;
|
|
698
|
+
if (schema.blob) {
|
|
699
|
+
return { ...schema, tsType: "`blob:${string}`" };
|
|
700
|
+
}
|
|
701
|
+
return {
|
|
702
|
+
...schema,
|
|
703
|
+
...schema.properties ? {
|
|
704
|
+
properties: Object.fromEntries(
|
|
705
|
+
Object.entries(schema.properties).map(([key, child]) => [
|
|
706
|
+
key,
|
|
707
|
+
typeof child === "object" ? withBlobTypes(child) : child
|
|
708
|
+
])
|
|
709
|
+
)
|
|
710
|
+
} : {},
|
|
711
|
+
...schema.patternProperties ? {
|
|
712
|
+
patternProperties: Object.fromEntries(
|
|
713
|
+
Object.entries(schema.patternProperties).map(([pattern, child]) => [
|
|
714
|
+
pattern,
|
|
715
|
+
typeof child === "object" ? withBlobTypes(child) : child
|
|
716
|
+
])
|
|
717
|
+
)
|
|
718
|
+
} : {},
|
|
719
|
+
...typeof schema.additionalProperties === "object" ? { additionalProperties: withBlobTypes(schema.additionalProperties) } : {},
|
|
720
|
+
...schema.items && !Array.isArray(schema.items) && typeof schema.items === "object" ? { items: withBlobTypes(schema.items) } : {}
|
|
721
|
+
};
|
|
722
|
+
}
|
|
723
|
+
async function generateEndpointTypes(endpoint, capabilityMap, settingsMap, workingDir) {
|
|
724
|
+
logger.debug(`Generating types for ${endpoint.definition.identifier}...`);
|
|
725
|
+
const writes = endpoint.definition.writes ?? [];
|
|
726
|
+
const sessionScoped = endpoint.definition.scope === "session";
|
|
727
|
+
const scenarioScoped = endpoint.definition.scope === "scenario";
|
|
728
|
+
const isShared = (capabilityId) => {
|
|
729
|
+
const capability = capabilityMap.get(capabilityId);
|
|
730
|
+
return !!capability && resolveCapabilityScope(capability.definition) === "shared";
|
|
731
|
+
};
|
|
732
|
+
const declared = [.../* @__PURE__ */ new Set([...endpoint.definition.reads ?? [], ...writes])];
|
|
733
|
+
const contextReads = sessionScoped ? declared.filter(isShared) : declared;
|
|
734
|
+
const teamReads = sessionScoped ? declared.filter((id) => !isShared(id)) : [];
|
|
735
|
+
const { properties, required } = collectCapabilityScope({
|
|
736
|
+
capabilityMap,
|
|
737
|
+
reads: contextReads
|
|
738
|
+
});
|
|
739
|
+
const contextSchema = {
|
|
740
|
+
type: "object",
|
|
741
|
+
properties,
|
|
742
|
+
required,
|
|
743
|
+
additionalProperties: false
|
|
744
|
+
};
|
|
745
|
+
const teamScope = collectCapabilityScope({ capabilityMap, reads: teamReads });
|
|
746
|
+
const teamContextSchema = {
|
|
747
|
+
type: "object",
|
|
748
|
+
properties: teamScope.properties,
|
|
749
|
+
required: teamScope.required,
|
|
750
|
+
additionalProperties: false
|
|
751
|
+
};
|
|
752
|
+
const contextWrites = sessionScoped ? writes.filter(isShared) : writes;
|
|
753
|
+
const teamWrites = sessionScoped ? writes.filter((id) => !isShared(id)) : [];
|
|
754
|
+
const writesScope = collectCapabilityScope({ capabilityMap, writes: contextWrites });
|
|
755
|
+
const writesSchema = {
|
|
756
|
+
type: "object",
|
|
757
|
+
properties: writesScope.properties,
|
|
758
|
+
required: writesScope.required,
|
|
759
|
+
additionalProperties: false
|
|
760
|
+
};
|
|
761
|
+
const teamWritesScope = collectCapabilityScope({ capabilityMap, writes: teamWrites });
|
|
762
|
+
const teamWritesSchema = {
|
|
763
|
+
type: "object",
|
|
764
|
+
properties: teamWritesScope.properties,
|
|
765
|
+
required: teamWritesScope.required,
|
|
766
|
+
additionalProperties: false
|
|
767
|
+
};
|
|
768
|
+
const actions = [...endpoint.definition.actions ?? []].sort();
|
|
769
|
+
const effectful = actions.length > 0 || teamWrites.length > 0;
|
|
770
|
+
const requestSchema = withBlobTypes(
|
|
771
|
+
endpoint.definition.requestSchema
|
|
772
|
+
);
|
|
773
|
+
const responseSchema = endpoint.definition.responseSchema;
|
|
774
|
+
const settingsScope = collectSettingsScope({
|
|
775
|
+
settingsMap,
|
|
776
|
+
settings: endpoint.definition.settings
|
|
777
|
+
});
|
|
778
|
+
const settingsSchema = {
|
|
779
|
+
type: "object",
|
|
780
|
+
properties: Object.fromEntries(
|
|
781
|
+
Object.entries(settingsScope.properties).map(([identifier, schema]) => [
|
|
782
|
+
identifier,
|
|
783
|
+
resolvedSettingsSchema(schema)
|
|
784
|
+
])
|
|
785
|
+
),
|
|
786
|
+
required: settingsScope.required,
|
|
787
|
+
additionalProperties: false
|
|
788
|
+
};
|
|
789
|
+
const hasSettings = settingsScope.required.length > 0;
|
|
790
|
+
const refs = new RefTypes();
|
|
791
|
+
const capabilities = { refs, kind: "capability" };
|
|
792
|
+
const [requestTS, responseTS, contextTS, writesTS, teamWritesTS, teamContextTS, settingsTS] = await Promise.all([
|
|
793
|
+
compileNamed("Request", requestSchema),
|
|
794
|
+
compileNamed("Response", responseSchema),
|
|
795
|
+
compileNamed("Context", contextSchema, capabilities),
|
|
796
|
+
contextWrites.length > 0 ? compileNamed("Writes", writesSchema, capabilities) : Promise.resolve(""),
|
|
797
|
+
teamWrites.length > 0 ? compileNamed("TeamWrites", teamWritesSchema, capabilities) : Promise.resolve(""),
|
|
798
|
+
teamReads.length > 0 ? compileNamed("TeamContext", teamContextSchema, capabilities) : Promise.resolve(""),
|
|
799
|
+
hasSettings ? compileNamed("Settings", settingsSchema, { refs, kind: "settings" }) : Promise.resolve("")
|
|
800
|
+
]);
|
|
801
|
+
const refsTS = await refs.declarations();
|
|
802
|
+
const uses = [...endpoint.definition.uses ?? []].sort();
|
|
803
|
+
const callbacksAlias = uses.length === 0 ? `export type Callbacks = LogicCallbacks;
|
|
804
|
+
` : `export type Callbacks = LogicCallbacks<${uses.map((u) => `'${u}'`).join(" | ")}>;
|
|
805
|
+
`;
|
|
806
|
+
const header = "// This file is auto-generated from the endpoint's bundle definition.\n// Do not edit this file directly. It will be regenerated on build.";
|
|
807
|
+
const scopeArgs = sessionScoped ? [teamReads.length > 0 ? "TeamContext" : "never", "'session'"] : hasSettings ? ["never", "'team'"] : [];
|
|
808
|
+
const settingsArgs = hasSettings ? ["Settings"] : [];
|
|
809
|
+
const handlerArgs = (leading) => {
|
|
810
|
+
const all = [...leading, "Callbacks", ...scopeArgs, ...settingsArgs];
|
|
811
|
+
const inline = all.join(", ");
|
|
812
|
+
return inline.length > 60 ? `
|
|
813
|
+
${all.map((a) => ` ${a}`).join(",\n")}
|
|
814
|
+
` : inline;
|
|
815
|
+
};
|
|
816
|
+
const actionAlias = actions.length > 0 ? `export type Action = Extract<EndpointAction, { type: ${actions.map((a) => `'${a}'`).join(" | ")} }>;
|
|
817
|
+
` : "";
|
|
818
|
+
const effectfulArgs = [
|
|
819
|
+
"Request",
|
|
820
|
+
"Response",
|
|
821
|
+
"Context",
|
|
822
|
+
"Callbacks",
|
|
823
|
+
sessionScoped ? teamReads.length > 0 ? "TeamContext" : "never" : "never",
|
|
824
|
+
sessionScoped ? "'session'" : "'team'",
|
|
825
|
+
hasSettings ? "Settings" : "Record<string, never>",
|
|
826
|
+
contextWrites.length > 0 ? "Writes" : "never",
|
|
827
|
+
actions.length > 0 ? "Action" : "never",
|
|
828
|
+
teamWrites.length > 0 ? "TeamWrites" : "never"
|
|
829
|
+
];
|
|
830
|
+
const handlerAlias = scenarioScoped ? `
|
|
831
|
+
import type {
|
|
832
|
+
LogicCallbacks,
|
|
833
|
+
ScenarioEndpointHandler as SDKScenarioEndpointHandler,
|
|
834
|
+
} from '@seriousgaming/sdk';
|
|
835
|
+
|
|
836
|
+
${callbacksAlias}
|
|
837
|
+
export type Handler = SDKScenarioEndpointHandler<${["Request", "Response", "Callbacks", ...settingsArgs].join(", ")}>;
|
|
838
|
+
` : effectful ? `
|
|
839
|
+
import type {
|
|
840
|
+
EffectfulEndpointHandler as SDKEffectfulEndpointHandler,
|
|
841
|
+
${actions.length > 0 ? " EndpointAction,\n" : ""} LogicCallbacks,
|
|
842
|
+
} from '@seriousgaming/sdk';
|
|
843
|
+
|
|
844
|
+
${callbacksAlias}${actionAlias}
|
|
845
|
+
export type Handler = SDKEffectfulEndpointHandler<
|
|
846
|
+
${effectfulArgs.map((a) => ` ${a}`).join(",\n")}
|
|
847
|
+
>;
|
|
848
|
+
` : writes.length > 0 ? `
|
|
849
|
+
import type {
|
|
850
|
+
WritingEndpointHandler as SDKWritingEndpointHandler,
|
|
851
|
+
LogicCallbacks,
|
|
852
|
+
} from '@seriousgaming/sdk';
|
|
853
|
+
|
|
854
|
+
${callbacksAlias}
|
|
855
|
+
export type Handler = SDKWritingEndpointHandler<${handlerArgs(["Request", "Response", "Writes", "Context"])}>;
|
|
856
|
+
` : `
|
|
857
|
+
import type { EndpointHandler as SDKEndpointHandler, LogicCallbacks } from '@seriousgaming/sdk';
|
|
858
|
+
|
|
859
|
+
${callbacksAlias}
|
|
860
|
+
export type Handler = SDKEndpointHandler<${handlerArgs(["Request", "Response", "Context"])}>;
|
|
861
|
+
`;
|
|
862
|
+
const planArgs = [
|
|
863
|
+
"Request",
|
|
864
|
+
"Context",
|
|
865
|
+
"Callbacks",
|
|
866
|
+
effectfulArgs[4],
|
|
867
|
+
effectfulArgs[5],
|
|
868
|
+
effectfulArgs[6],
|
|
869
|
+
effectfulArgs[7],
|
|
870
|
+
effectfulArgs[8],
|
|
871
|
+
effectfulArgs[9]
|
|
872
|
+
];
|
|
873
|
+
const splitAlias = writes.length > 0 || actions.length > 0 ? `
|
|
874
|
+
import type {
|
|
875
|
+
EndpointPlanner as SDKEndpointPlanner,
|
|
876
|
+
EndpointResponder as SDKEndpointResponder,
|
|
877
|
+
} from '@seriousgaming/sdk';
|
|
878
|
+
|
|
879
|
+
export type Planner = SDKEndpointPlanner<
|
|
880
|
+
${planArgs.map((a) => ` ${a}`).join(",\n")}
|
|
881
|
+
>;
|
|
882
|
+
export type Responder = SDKEndpointResponder<
|
|
883
|
+
${["Request", "Response", ...planArgs.slice(1)].map((a) => ` ${a}`).join(",\n")}
|
|
884
|
+
>;
|
|
885
|
+
` : "";
|
|
886
|
+
const fullTypeScript = `${header}
|
|
887
|
+
|
|
888
|
+
${requestTS}
|
|
889
|
+
${responseTS}
|
|
890
|
+
${scenarioScoped ? "" : contextTS}${teamContextTS}${writesTS}${teamWritesTS}${settingsTS}${refsTS}${handlerAlias}${splitAlias}`;
|
|
891
|
+
const endpointDir = join2(workingDir, endpoint.rootDirectory);
|
|
892
|
+
const outputPath = join2(endpointDir, "types.ts");
|
|
893
|
+
await assertSafeToGenerate(outputPath);
|
|
894
|
+
await writeFile3(outputPath, fullTypeScript, "utf-8");
|
|
895
|
+
logger.debug(`Generated ${outputPath}`);
|
|
896
|
+
await writeDefaultsFile({
|
|
897
|
+
outputPath: join2(endpointDir, "defaults.ts"),
|
|
898
|
+
headerComment: DEFAULTS_HEADER,
|
|
899
|
+
// A scenario-scoped endpoint has no context to default.
|
|
900
|
+
context: scenarioScoped ? void 0 : Object.fromEntries(
|
|
901
|
+
Object.entries(properties).map(([capabilityId, schema]) => [
|
|
902
|
+
capabilityId,
|
|
903
|
+
schemaDefaults({
|
|
904
|
+
owner: `Capability "${capabilityId}"`,
|
|
905
|
+
kind: "variable",
|
|
906
|
+
schema
|
|
907
|
+
})
|
|
908
|
+
])
|
|
909
|
+
),
|
|
910
|
+
...hasSettings ? {
|
|
911
|
+
settings: Object.fromEntries(
|
|
912
|
+
Object.entries(settingsScope.properties).map(([identifier, schema]) => [
|
|
913
|
+
identifier,
|
|
914
|
+
schemaDefaults({
|
|
915
|
+
owner: `Settings "${identifier}"`,
|
|
916
|
+
kind: "configuration",
|
|
917
|
+
schema
|
|
918
|
+
})
|
|
919
|
+
])
|
|
920
|
+
)
|
|
921
|
+
} : {}
|
|
922
|
+
});
|
|
923
|
+
}
|
|
924
|
+
|
|
925
|
+
// cli/generation/engine-types.ts
|
|
926
|
+
import { join as join3 } from "path";
|
|
927
|
+
async function generateEngineTypes(engine, capabilityMap, settingsMap, workingDir) {
|
|
928
|
+
logger.debug(`Generating types for ${engine.definition.identifier}...`);
|
|
929
|
+
await generateCapabilityScopedTypes({
|
|
930
|
+
capabilityMap,
|
|
931
|
+
reads: engine.definition.reads,
|
|
932
|
+
writes: engine.definition.writes,
|
|
933
|
+
// `Context` is the composed view `Init` and `Run` receive; aggregate
|
|
934
|
+
// functions receive it split, the store once and each team's own.
|
|
935
|
+
contextAliases: scopeAliases({
|
|
936
|
+
capabilityMap,
|
|
937
|
+
reads: engine.definition.reads,
|
|
938
|
+
writes: engine.definition.writes
|
|
939
|
+
}),
|
|
940
|
+
settingsMap,
|
|
941
|
+
settings: engine.definition.settings,
|
|
942
|
+
callbacks: {
|
|
943
|
+
sourceType: "SignatureCallbacks",
|
|
944
|
+
uses: engine.definition.uses ?? []
|
|
945
|
+
},
|
|
946
|
+
owner: `Engine "${engine.definition.identifier}"`,
|
|
947
|
+
outputPath: join3(workingDir, engine.rootDirectory, "types.ts"),
|
|
948
|
+
headerComment: "// This file is auto-generated from the engine's capability definitions.\n// Do not edit this file directly. It will be regenerated on build."
|
|
949
|
+
});
|
|
950
|
+
}
|
|
951
|
+
|
|
952
|
+
// cli/generation/hook-types.ts
|
|
953
|
+
import { appendFile } from "fs/promises";
|
|
954
|
+
import { join as join4 } from "path";
|
|
955
|
+
async function generateHookTypes(hook, capabilityMap, settingsMap, workingDir) {
|
|
956
|
+
logger.debug(`Generating types for ${hook.definition.identifier}...`);
|
|
957
|
+
const outputPath = join4(workingDir, hook.rootDirectory, "types.ts");
|
|
958
|
+
await generateCapabilityScopedTypes({
|
|
959
|
+
capabilityMap,
|
|
960
|
+
reads: hook.definition.reads,
|
|
961
|
+
writes: hook.definition.writes,
|
|
962
|
+
settingsMap,
|
|
963
|
+
settings: hook.definition.settings,
|
|
964
|
+
callbacks: {
|
|
965
|
+
sourceType: "SignatureCallbacks",
|
|
966
|
+
uses: hook.definition.uses ?? []
|
|
967
|
+
},
|
|
968
|
+
owner: `Hook "${hook.definition.identifier}"`,
|
|
969
|
+
outputPath,
|
|
970
|
+
headerComment: "// This file is auto-generated from the hook's capability definitions.\n// Do not edit this file directly. It will be regenerated on build."
|
|
971
|
+
});
|
|
972
|
+
const signalLiteral = `'${hook.definition.signal}'`;
|
|
973
|
+
const hookTypeAlias = `
|
|
974
|
+
import type { HookFunction as SDKHookFunction } from '@seriousgaming/sdk';
|
|
975
|
+
|
|
976
|
+
export type HookFunction = SDKHookFunction<${signalLiteral}, Context, Settings, Callbacks>;
|
|
977
|
+
`;
|
|
978
|
+
await appendFile(outputPath, hookTypeAlias, "utf-8");
|
|
979
|
+
}
|
|
980
|
+
|
|
981
|
+
// cli/generation/presentation-types.ts
|
|
982
|
+
import { join as join5 } from "path";
|
|
983
|
+
async function generatePresentationTypes(presentation, capabilityMap, settingsMap, workingDir) {
|
|
984
|
+
logger.debug(`Generating types for ${presentation.definition.identifier}...`);
|
|
985
|
+
await generateCapabilityScopedTypes({
|
|
986
|
+
capabilityMap,
|
|
987
|
+
reads: presentation.definition.reads,
|
|
988
|
+
contextAliases: scopeAliases({ capabilityMap, reads: presentation.definition.reads }),
|
|
989
|
+
trailer: "import type { PresentationSession as SurfacePresentationSession } from '@seriousgaming/sdk/ui';\n\nexport type PresentationSession = SurfacePresentationSession<TeamContext, SharedContext>;\n\n",
|
|
990
|
+
settingsMap,
|
|
991
|
+
settings: presentation.definition.settings,
|
|
992
|
+
owner: `Presentation "${presentation.definition.identifier}"`,
|
|
993
|
+
outputPath: join5(workingDir, presentation.rootDirectory, "types.ts"),
|
|
994
|
+
headerComment: "// This file is auto-generated from the presentation's capability definitions.\n// Do not edit this file directly. It will be regenerated on build."
|
|
995
|
+
});
|
|
996
|
+
}
|
|
997
|
+
|
|
998
|
+
// cli/generation/session-types.ts
|
|
999
|
+
import { join as join6 } from "path";
|
|
1000
|
+
|
|
1001
|
+
// cli/generation/resolve-endpoint-bindings.ts
|
|
1002
|
+
function resolveEndpointBindings(args) {
|
|
1003
|
+
const declared = args.declaredEndpoints ?? [];
|
|
1004
|
+
if (declared.length === 0) return [];
|
|
1005
|
+
return declared.map((identifier) => {
|
|
1006
|
+
const endpoint = args.endpointMap.get(identifier);
|
|
1007
|
+
if (!endpoint) {
|
|
1008
|
+
return { identifier };
|
|
1009
|
+
}
|
|
1010
|
+
return {
|
|
1011
|
+
identifier,
|
|
1012
|
+
requestSchema: endpoint.definition.requestSchema,
|
|
1013
|
+
responseSchema: endpoint.definition.responseSchema
|
|
1014
|
+
};
|
|
1015
|
+
});
|
|
1016
|
+
}
|
|
1017
|
+
|
|
1018
|
+
// cli/generation/session-types.ts
|
|
1019
|
+
async function generateSessionUserInterfaceTypes(sessionUI, capabilityMap, settingsMap, endpointMap, workingDir) {
|
|
1020
|
+
logger.debug(`Generating types for ${sessionUI.definition.identifier}...`);
|
|
1021
|
+
const sharedWrites = [];
|
|
1022
|
+
const teamWrites = [];
|
|
1023
|
+
for (const capabilityId of sessionUI.definition.writes ?? []) {
|
|
1024
|
+
const capability = capabilityMap.get(capabilityId);
|
|
1025
|
+
if (capability === void 0) continue;
|
|
1026
|
+
(resolveCapabilityScope(capability.definition) === "shared" ? sharedWrites : teamWrites).push(capabilityId);
|
|
1027
|
+
}
|
|
1028
|
+
await generateCapabilityScopedTypes({
|
|
1029
|
+
capabilityMap,
|
|
1030
|
+
reads: sessionUI.definition.reads,
|
|
1031
|
+
writes: sessionUI.definition.writes,
|
|
1032
|
+
contextAliases: [
|
|
1033
|
+
...scopeAliases({
|
|
1034
|
+
capabilityMap,
|
|
1035
|
+
reads: sessionUI.definition.reads,
|
|
1036
|
+
writes: sessionUI.definition.writes
|
|
1037
|
+
}),
|
|
1038
|
+
{ name: "SharedWrites", capabilities: sharedWrites },
|
|
1039
|
+
{ name: "TeamWrites", capabilities: teamWrites }
|
|
1040
|
+
],
|
|
1041
|
+
trailer: "import type { SessionData as SurfaceSessionData } from '@seriousgaming/sdk/ui';\n\nexport type SessionData = SurfaceSessionData<TeamContext, SharedContext>;\n\n",
|
|
1042
|
+
settingsMap,
|
|
1043
|
+
settings: sessionUI.definition.settings,
|
|
1044
|
+
callbacks: {
|
|
1045
|
+
sourceType: "SessionUserInterfaceCallbacks"
|
|
1046
|
+
},
|
|
1047
|
+
endpoints: resolveEndpointBindings({
|
|
1048
|
+
declaredEndpoints: sessionUI.definition.endpoints ?? [],
|
|
1049
|
+
endpointMap
|
|
1050
|
+
}),
|
|
1051
|
+
owner: `Session user interface "${sessionUI.definition.identifier}"`,
|
|
1052
|
+
outputPath: join6(workingDir, sessionUI.rootDirectory, "types.ts"),
|
|
1053
|
+
headerComment: "// This file is auto-generated from the session user interface's capability definitions.\n// Do not edit this file directly. It will be regenerated on build."
|
|
1054
|
+
});
|
|
1055
|
+
}
|
|
1056
|
+
|
|
1057
|
+
// cli/generation/settings-editor-types.ts
|
|
1058
|
+
import { writeFile as writeFile4 } from "fs/promises";
|
|
1059
|
+
import { join as join7 } from "path";
|
|
1060
|
+
async function generateSettingsEditorTypes(editor, settingsMap, endpointMap, workingDir) {
|
|
1061
|
+
const slot = editor.definition.identifier;
|
|
1062
|
+
logger.debug(`Generating types for ${slot}...`);
|
|
1063
|
+
const settingsProperties = {};
|
|
1064
|
+
const settingsDefaults = {};
|
|
1065
|
+
const EMPTY = { type: "object", properties: {}, additionalProperties: false };
|
|
1066
|
+
for (const identifier of editor.definition.configures ?? []) {
|
|
1067
|
+
const settings = settingsMap.get(identifier);
|
|
1068
|
+
if (!settings) {
|
|
1069
|
+
logger.warn(`${slot} configures settings "${identifier}", which do not exist`);
|
|
1070
|
+
continue;
|
|
1071
|
+
}
|
|
1072
|
+
const schema = settings.definition.settingsSchema;
|
|
1073
|
+
settingsProperties[identifier] = schema?.properties && Object.keys(schema.properties).length > 0 ? resolvedSettingsSchema(schema) : EMPTY;
|
|
1074
|
+
settingsDefaults[identifier] = schemaDefaults({
|
|
1075
|
+
owner: `Settings "${identifier}"`,
|
|
1076
|
+
kind: "configuration",
|
|
1077
|
+
schema
|
|
1078
|
+
});
|
|
1079
|
+
}
|
|
1080
|
+
const refs = new RefTypes();
|
|
1081
|
+
let settingsTypeScript;
|
|
1082
|
+
if (Object.keys(settingsProperties).length > 0) {
|
|
1083
|
+
const settingsSchema = {
|
|
1084
|
+
$schema: "https://json-schema.org/draft/2020-12/schema",
|
|
1085
|
+
type: "object",
|
|
1086
|
+
properties: refs.inlineScope("settings", settingsProperties),
|
|
1087
|
+
required: Object.keys(settingsProperties),
|
|
1088
|
+
additionalProperties: false
|
|
1089
|
+
};
|
|
1090
|
+
settingsTypeScript = await compileSchema(settingsSchema, "Settings");
|
|
1091
|
+
} else {
|
|
1092
|
+
settingsTypeScript = `export type Settings = Record<string, never>;
|
|
1093
|
+
`;
|
|
1094
|
+
}
|
|
1095
|
+
const settingsFixed = settingsTypeScript.replace(/:\s*\{\};/g, ": Record<string, never>;") + await refs.declarations();
|
|
1096
|
+
const headerComment = "// This file is auto-generated from the settings this screen configures.\n// Do not edit this file directly. It will be regenerated on build.";
|
|
1097
|
+
const outputPath = join7(workingDir, editor.rootDirectory, "types.ts");
|
|
1098
|
+
const endpoints = resolveEndpointBindings({
|
|
1099
|
+
declaredEndpoints: editor.definition.endpoints ?? [],
|
|
1100
|
+
endpointMap
|
|
1101
|
+
});
|
|
1102
|
+
const callbacksTypeScript = await buildCallbacksTypeAlias2(endpoints);
|
|
1103
|
+
const fullTypeScript = `${headerComment}
|
|
1104
|
+
|
|
1105
|
+
${settingsFixed}${callbacksTypeScript}`;
|
|
1106
|
+
await assertSafeToGenerate(outputPath);
|
|
1107
|
+
await writeFile4(outputPath, fullTypeScript, "utf-8");
|
|
1108
|
+
logger.debug(`Generated ${outputPath}`);
|
|
1109
|
+
await writeDefaultsFile({
|
|
1110
|
+
outputPath: join7(workingDir, editor.rootDirectory, "defaults.ts"),
|
|
1111
|
+
headerComment: DEFAULTS_HEADER,
|
|
1112
|
+
settings: settingsDefaults
|
|
1113
|
+
});
|
|
1114
|
+
}
|
|
1115
|
+
async function compileEndpointSchema2(name, schema) {
|
|
1116
|
+
if (!schema || !schema.properties || Object.keys(schema.properties).length === 0) {
|
|
1117
|
+
return `export type ${name} = Record<string, never>;
|
|
1118
|
+
`;
|
|
1119
|
+
}
|
|
1120
|
+
const withMeta = {
|
|
1121
|
+
$schema: "https://json-schema.org/draft/2020-12/schema",
|
|
1122
|
+
...schema
|
|
1123
|
+
};
|
|
1124
|
+
const compiled = await compileSchema(withMeta, name);
|
|
1125
|
+
return compiled.replace(/:\s*\{\};/g, ": Record<string, never>;").replace(/^export\s+(interface|type)\s+\S+/m, `export $1 ${name}`);
|
|
1126
|
+
}
|
|
1127
|
+
async function buildCallbacksTypeAlias2(endpoints) {
|
|
1128
|
+
const importLine = "import type { SettingsEditorCallbacks } from '@seriousgaming/sdk';\n";
|
|
1129
|
+
const baseAlias = `Pick<SettingsEditorCallbacks, never>`;
|
|
1130
|
+
if (endpoints.length === 0) {
|
|
1131
|
+
return `
|
|
1132
|
+
${importLine}
|
|
1133
|
+
export type Callbacks = ${baseAlias};
|
|
1134
|
+
`;
|
|
1135
|
+
}
|
|
1136
|
+
const inlineParts = [];
|
|
1137
|
+
const interfaceLines = [];
|
|
1138
|
+
for (const ep of endpoints) {
|
|
1139
|
+
if (ep.requestSchema && ep.responseSchema) {
|
|
1140
|
+
const alias = ep.identifier.replace(/[^a-zA-Z0-9]+/g, "_");
|
|
1141
|
+
const requestName = `${alias}_Request`;
|
|
1142
|
+
const responseName = `${alias}_Response`;
|
|
1143
|
+
inlineParts.push(await compileEndpointSchema2(requestName, ep.requestSchema));
|
|
1144
|
+
inlineParts.push(await compileEndpointSchema2(responseName, ep.responseSchema));
|
|
1145
|
+
interfaceLines.push(
|
|
1146
|
+
` '${ep.identifier}': (request: ${requestName}) => Promise<${responseName}>;`
|
|
1147
|
+
);
|
|
1148
|
+
} else {
|
|
1149
|
+
interfaceLines.push(` '${ep.identifier}': (request: unknown) => Promise<unknown>;`);
|
|
1150
|
+
}
|
|
1151
|
+
}
|
|
1152
|
+
const endpointBlock = `${inlineParts.join("")}
|
|
1153
|
+
// prettier-ignore
|
|
1154
|
+
export interface Endpoints {
|
|
1155
|
+
${interfaceLines.join("\n")}
|
|
1156
|
+
}
|
|
1157
|
+
`;
|
|
1158
|
+
return `
|
|
1159
|
+
${importLine}${endpointBlock}
|
|
1160
|
+
export type Callbacks = ${baseAlias} & { endpoints: Endpoints };
|
|
1161
|
+
`;
|
|
1162
|
+
}
|
|
1163
|
+
|
|
1164
|
+
// cli/generation/tick-controller-types.ts
|
|
1165
|
+
import { join as join8 } from "path";
|
|
1166
|
+
async function generateTickControllerTypes(tickController, capabilityMap, settingsMap, workingDir) {
|
|
1167
|
+
logger.debug(`Generating types for ${tickController.definition.identifier}...`);
|
|
1168
|
+
await generateCapabilityScopedTypes({
|
|
1169
|
+
capabilityMap,
|
|
1170
|
+
reads: tickController.definition.reads,
|
|
1171
|
+
settingsMap,
|
|
1172
|
+
settings: tickController.definition.settings,
|
|
1173
|
+
callbacks: {
|
|
1174
|
+
sourceType: "SignatureCallbacks",
|
|
1175
|
+
uses: tickController.definition.uses ?? []
|
|
1176
|
+
},
|
|
1177
|
+
owner: `Tick controller "${tickController.definition.identifier}"`,
|
|
1178
|
+
outputPath: join8(workingDir, tickController.rootDirectory, "types.ts"),
|
|
1179
|
+
headerComment: "// This file is auto-generated from the tick controller's capability definitions.\n// Do not edit this file directly. It will be regenerated on build."
|
|
1180
|
+
});
|
|
1181
|
+
}
|
|
1182
|
+
|
|
1183
|
+
// cli/generation/ui-types.ts
|
|
1184
|
+
import { join as join9 } from "path";
|
|
1185
|
+
async function generateUserInterfaceTypes(userInterface, capabilityMap, settingsMap, endpointMap, workingDir) {
|
|
1186
|
+
logger.debug(`Generating types for ${userInterface.definition.identifier}...`);
|
|
1187
|
+
await generateCapabilityScopedTypes({
|
|
1188
|
+
capabilityMap,
|
|
1189
|
+
reads: userInterface.definition.reads,
|
|
1190
|
+
crossTeamReads: userInterface.definition.crossTeamReads,
|
|
1191
|
+
writes: userInterface.definition.writes,
|
|
1192
|
+
settingsMap,
|
|
1193
|
+
settings: userInterface.definition.settings,
|
|
1194
|
+
callbacks: {
|
|
1195
|
+
sourceType: "UserInterfaceCallbacks"
|
|
1196
|
+
},
|
|
1197
|
+
endpoints: resolveEndpointBindings({
|
|
1198
|
+
declaredEndpoints: userInterface.definition.endpoints ?? [],
|
|
1199
|
+
endpointMap
|
|
1200
|
+
}),
|
|
1201
|
+
crossTeam: true,
|
|
1202
|
+
owner: `User interface "${userInterface.definition.identifier}"`,
|
|
1203
|
+
outputPath: join9(workingDir, userInterface.rootDirectory, "types.ts"),
|
|
1204
|
+
headerComment: "// This file is auto-generated from the user interface's capability definitions.\n// Do not edit this file directly. It will be regenerated on build."
|
|
1205
|
+
});
|
|
1206
|
+
}
|
|
1207
|
+
|
|
1208
|
+
// cli/generation/validator-types.ts
|
|
1209
|
+
import { appendFile as appendFile2 } from "fs/promises";
|
|
1210
|
+
import { join as join10 } from "path";
|
|
1211
|
+
async function generateValidatorTypes(validator, capabilityMap, settingsMap, workingDir, options = {}) {
|
|
1212
|
+
logger.debug(`Generating types for ${validator.definition.identifier}...`);
|
|
1213
|
+
const outputPath = join10(workingDir, validator.rootDirectory, "types.ts");
|
|
1214
|
+
await generateCapabilityScopedTypes({
|
|
1215
|
+
capabilityMap,
|
|
1216
|
+
reads: validator.definition.reads,
|
|
1217
|
+
settingsMap,
|
|
1218
|
+
settings: validator.definition.settings,
|
|
1219
|
+
owner: `Validator "${validator.definition.identifier}"`,
|
|
1220
|
+
outputPath,
|
|
1221
|
+
headerComment: "// This file is auto-generated from the validator's capability definitions.\n// Do not edit this file directly. It will be regenerated on build."
|
|
1222
|
+
});
|
|
1223
|
+
const validatorTypeAlias = options.scenarioFiles ? `
|
|
1224
|
+
import type {
|
|
1225
|
+
RequestTickFunction as SDKRequestTickFunction,
|
|
1226
|
+
ScenarioCallbacks,
|
|
1227
|
+
ScenarioFunctionArgs as SDKScenarioFunctionArgs,
|
|
1228
|
+
ScenarioProblem,
|
|
1229
|
+
ValidatorFunction as SDKValidatorFunction,
|
|
1230
|
+
} from '@seriousgaming/sdk';
|
|
1231
|
+
|
|
1232
|
+
export type ValidatorFunction = SDKValidatorFunction<Context, Settings>;
|
|
1233
|
+
export type RequestTickFunction = SDKRequestTickFunction<Context, Settings>;
|
|
1234
|
+
export type ScenarioFunction = (
|
|
1235
|
+
args: SDKScenarioFunctionArgs<Settings> & { callbacks: ScenarioCallbacks }
|
|
1236
|
+
) => Promise<ScenarioProblem[]>;
|
|
1237
|
+
` : `
|
|
1238
|
+
import type {
|
|
1239
|
+
RequestTickFunction as SDKRequestTickFunction,
|
|
1240
|
+
ScenarioFunction as SDKScenarioFunction,
|
|
1241
|
+
ValidatorFunction as SDKValidatorFunction,
|
|
1242
|
+
} from '@seriousgaming/sdk';
|
|
1243
|
+
|
|
1244
|
+
export type ValidatorFunction = SDKValidatorFunction<Context, Settings>;
|
|
1245
|
+
export type RequestTickFunction = SDKRequestTickFunction<Context, Settings>;
|
|
1246
|
+
export type ScenarioFunction = SDKScenarioFunction<Settings>;
|
|
1247
|
+
`;
|
|
1248
|
+
await appendFile2(outputPath, validatorTypeAlias, "utf-8");
|
|
1249
|
+
}
|
|
1250
|
+
|
|
1251
|
+
// cli/generation/waiting-room-types.ts
|
|
1252
|
+
import { join as join11 } from "path";
|
|
1253
|
+
async function generateWaitingRoomUserInterfaceTypes(waitingRoomUI, capabilityMap, settingsMap, endpointMap, workingDir) {
|
|
1254
|
+
logger.debug(`Generating types for ${waitingRoomUI.definition.identifier}...`);
|
|
1255
|
+
await generateCapabilityScopedTypes({
|
|
1256
|
+
capabilityMap,
|
|
1257
|
+
reads: waitingRoomUI.definition.reads,
|
|
1258
|
+
crossTeamReads: waitingRoomUI.definition.crossTeamReads,
|
|
1259
|
+
writes: waitingRoomUI.definition.writes,
|
|
1260
|
+
settingsMap,
|
|
1261
|
+
settings: waitingRoomUI.definition.settings,
|
|
1262
|
+
callbacks: {
|
|
1263
|
+
sourceType: "WaitingRoomUserInterfaceCallbacks"
|
|
1264
|
+
},
|
|
1265
|
+
endpoints: resolveEndpointBindings({
|
|
1266
|
+
declaredEndpoints: waitingRoomUI.definition.endpoints ?? [],
|
|
1267
|
+
endpointMap
|
|
1268
|
+
}),
|
|
1269
|
+
crossTeam: true,
|
|
1270
|
+
owner: `Waiting room user interface "${waitingRoomUI.definition.identifier}"`,
|
|
1271
|
+
outputPath: join11(workingDir, waitingRoomUI.rootDirectory, "types.ts"),
|
|
1272
|
+
headerComment: "// This file is auto-generated from the waiting room user interface's capability definitions.\n// Do not edit this file directly. It will be regenerated on build."
|
|
1273
|
+
});
|
|
1274
|
+
}
|
|
1275
|
+
|
|
1276
|
+
// cli/generation/generate-all-types.ts
|
|
1277
|
+
function buildTypeGenerationMaps(config, workingDir) {
|
|
1278
|
+
const capabilityMap = /* @__PURE__ */ new Map();
|
|
1279
|
+
for (const cap of absolutiseTsTypes(config.capabilities ?? [], workingDir)) {
|
|
1280
|
+
capabilityMap.set(cap.definition.identifier, cap);
|
|
1281
|
+
}
|
|
1282
|
+
const engineMap = /* @__PURE__ */ new Map();
|
|
1283
|
+
if (config.engine) engineMap.set(config.engine.definition.identifier, config.engine);
|
|
1284
|
+
const userInterfaceMap = /* @__PURE__ */ new Map();
|
|
1285
|
+
if (config.ui) userInterfaceMap.set(config.ui.definition.identifier, config.ui);
|
|
1286
|
+
const sessionUserInterfaceMap = /* @__PURE__ */ new Map();
|
|
1287
|
+
if (config.session)
|
|
1288
|
+
sessionUserInterfaceMap.set(config.session.definition.identifier, config.session);
|
|
1289
|
+
const endpointMap = /* @__PURE__ */ new Map();
|
|
1290
|
+
for (const ep of config.endpoints ?? []) endpointMap.set(ep.definition.identifier, ep);
|
|
1291
|
+
const settingsMap = /* @__PURE__ */ new Map();
|
|
1292
|
+
for (const settings of config.settings ?? []) {
|
|
1293
|
+
settingsMap.set(settings.definition.identifier, settings);
|
|
1294
|
+
}
|
|
1295
|
+
return {
|
|
1296
|
+
capabilityMap,
|
|
1297
|
+
settingsMap,
|
|
1298
|
+
engineMap,
|
|
1299
|
+
userInterfaceMap,
|
|
1300
|
+
sessionUserInterfaceMap,
|
|
1301
|
+
endpointMap
|
|
1302
|
+
};
|
|
1303
|
+
}
|
|
1304
|
+
async function generateAllTypes(config, options) {
|
|
1305
|
+
const { workingDir } = options;
|
|
1306
|
+
const maps = buildTypeGenerationMaps(config, workingDir);
|
|
1307
|
+
const note = (message) => {
|
|
1308
|
+
if (options.verbose) logger.debug(message);
|
|
1309
|
+
};
|
|
1310
|
+
if (config.engine) {
|
|
1311
|
+
note(` Generating types for engine ${config.engine.definition.identifier}`);
|
|
1312
|
+
await generateEngineTypes(config.engine, maps.capabilityMap, maps.settingsMap, workingDir);
|
|
1313
|
+
}
|
|
1314
|
+
if (config.ui) {
|
|
1315
|
+
note(` Generating types for UI ${config.ui.definition.identifier}`);
|
|
1316
|
+
await generateUserInterfaceTypes(
|
|
1317
|
+
config.ui,
|
|
1318
|
+
maps.capabilityMap,
|
|
1319
|
+
maps.settingsMap,
|
|
1320
|
+
maps.endpointMap,
|
|
1321
|
+
workingDir
|
|
1322
|
+
);
|
|
1323
|
+
}
|
|
1324
|
+
for (const editor of [config.setup, config.scenario_editor]) {
|
|
1325
|
+
if (!editor) continue;
|
|
1326
|
+
note(` Generating types for ${editor.definition.identifier}`);
|
|
1327
|
+
await generateSettingsEditorTypes(editor, maps.settingsMap, maps.endpointMap, workingDir);
|
|
1328
|
+
}
|
|
1329
|
+
if (config.session) {
|
|
1330
|
+
note(` Generating types for session UI ${config.session.definition.identifier}`);
|
|
1331
|
+
await generateSessionUserInterfaceTypes(
|
|
1332
|
+
config.session,
|
|
1333
|
+
maps.capabilityMap,
|
|
1334
|
+
maps.settingsMap,
|
|
1335
|
+
maps.endpointMap,
|
|
1336
|
+
workingDir
|
|
1337
|
+
);
|
|
1338
|
+
}
|
|
1339
|
+
if (config.waiting_room) {
|
|
1340
|
+
note(` Generating types for waiting room UI ${config.waiting_room.definition.identifier}`);
|
|
1341
|
+
await generateWaitingRoomUserInterfaceTypes(
|
|
1342
|
+
config.waiting_room,
|
|
1343
|
+
maps.capabilityMap,
|
|
1344
|
+
maps.settingsMap,
|
|
1345
|
+
maps.endpointMap,
|
|
1346
|
+
workingDir
|
|
1347
|
+
);
|
|
1348
|
+
}
|
|
1349
|
+
if (config.presentation) {
|
|
1350
|
+
note(` Generating types for presentation ${config.presentation.definition.identifier}`);
|
|
1351
|
+
await generatePresentationTypes(
|
|
1352
|
+
config.presentation,
|
|
1353
|
+
maps.capabilityMap,
|
|
1354
|
+
maps.settingsMap,
|
|
1355
|
+
workingDir
|
|
1356
|
+
);
|
|
1357
|
+
}
|
|
1358
|
+
if (config.tick_controller) {
|
|
1359
|
+
note(
|
|
1360
|
+
` Generating types for tick controller ${config.tick_controller.definition.identifier}`
|
|
1361
|
+
);
|
|
1362
|
+
await generateTickControllerTypes(
|
|
1363
|
+
config.tick_controller,
|
|
1364
|
+
maps.capabilityMap,
|
|
1365
|
+
maps.settingsMap,
|
|
1366
|
+
workingDir
|
|
1367
|
+
);
|
|
1368
|
+
}
|
|
1369
|
+
for (const hook of config.hooks ?? []) {
|
|
1370
|
+
note(` Generating types for hook ${hook.definition.identifier}`);
|
|
1371
|
+
await generateHookTypes(hook, maps.capabilityMap, maps.settingsMap, workingDir);
|
|
1372
|
+
}
|
|
1373
|
+
for (const validator of config.validators ?? []) {
|
|
1374
|
+
note(` Generating types for validator ${validator.definition.identifier}`);
|
|
1375
|
+
await generateValidatorTypes(validator, maps.capabilityMap, maps.settingsMap, workingDir, {
|
|
1376
|
+
scenarioFiles: (config.requires ?? []).includes(SCENARIO_FILES)
|
|
1377
|
+
});
|
|
1378
|
+
}
|
|
1379
|
+
for (const endpoint of config.endpoints ?? []) {
|
|
1380
|
+
note(` Generating types for endpoint ${endpoint.definition.identifier}`);
|
|
1381
|
+
await generateEndpointTypes(endpoint, maps.capabilityMap, maps.settingsMap, workingDir);
|
|
1382
|
+
}
|
|
1383
|
+
}
|
|
1384
|
+
|
|
1385
|
+
export {
|
|
1386
|
+
generateAllTypes
|
|
1387
|
+
};
|