@kizunajs/cli 2.0.0-beta.1 → 2.0.0-beta.11
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/cli.cjs +33 -13
- package/dist/cli.mjs +33 -13
- package/dist/index.cjs +13 -12
- package/dist/index.d.cts +95 -37
- package/dist/index.d.mts +95 -37
- package/dist/index.mjs +13 -12
- package/dist/{route-map-D7cBgYmT.mjs → route-map-B2vDHecG.mjs} +285 -170
- package/dist/{route-map-DW2UUkD_.cjs → route-map-Sk1Wybox.cjs} +310 -171
- package/package.json +6 -4
|
@@ -23,9 +23,10 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
23
23
|
let jiti = require("jiti");
|
|
24
24
|
let kizunajs = require("kizunajs");
|
|
25
25
|
let kizunajs_generator = require("kizunajs/generator");
|
|
26
|
+
let node_path = require("node:path");
|
|
27
|
+
let yaml = require("yaml");
|
|
26
28
|
let kizunajs_adapter = require("kizunajs/adapter");
|
|
27
29
|
let node_fs = require("node:fs");
|
|
28
|
-
let node_path = require("node:path");
|
|
29
30
|
let typescript = require("typescript");
|
|
30
31
|
typescript = __toESM(typescript, 1);
|
|
31
32
|
let node_child_process = require("node:child_process");
|
|
@@ -39,53 +40,31 @@ let node_child_process = require("node:child_process");
|
|
|
39
40
|
* a config.
|
|
40
41
|
*/
|
|
41
42
|
const loadConfig = async (configPath) => {
|
|
42
|
-
return (0, kizunajs.apiEntries)(await (0, jiti.createJiti)(require("url").pathToFileURL(__filename).href, {
|
|
43
|
+
return (0, kizunajs.apiEntries)(await (0, jiti.createJiti)(require("url").pathToFileURL(__filename).href, {
|
|
44
|
+
interopDefault: true,
|
|
45
|
+
jsx: true,
|
|
46
|
+
tsconfigPaths: true
|
|
47
|
+
}).import(configPath)).map(([name, entry]) => ({
|
|
43
48
|
name,
|
|
44
49
|
api: entry.api,
|
|
45
50
|
clients: entry.clients ?? [],
|
|
46
|
-
|
|
51
|
+
generators: entry.generators ?? [],
|
|
52
|
+
typesOutput: entry.typescript?.outputFile,
|
|
53
|
+
diff: entry.diff ?? {}
|
|
47
54
|
}));
|
|
48
55
|
};
|
|
49
56
|
//#endregion
|
|
50
57
|
//#region src/diff-schemas.ts
|
|
51
|
-
const shapeOf = (schema) => {
|
|
52
|
-
const { inner } = (0, kizunajs_generator.unwrapOptionalWrappers)(schema);
|
|
53
|
-
const raw = (0, kizunajs_generator.readObjectShape)(inner);
|
|
54
|
-
if (!raw) return void 0;
|
|
55
|
-
const fields = /* @__PURE__ */ new Map();
|
|
56
|
-
for (const [key, field] of Object.entries(raw)) fields.set(key, {
|
|
57
|
-
schema: field,
|
|
58
|
-
optional: (0, kizunajs_generator.unwrapOptionalWrappers)(field).optional
|
|
59
|
-
});
|
|
60
|
-
return { fields };
|
|
61
|
-
};
|
|
62
|
-
/**
|
|
63
|
-
* What a schema accepts, flattened enough to compare: its kind, and its values
|
|
64
|
-
* when it is closed.
|
|
65
|
-
*/
|
|
66
|
-
const describe = (schema) => {
|
|
67
|
-
const { inner } = (0, kizunajs_generator.unwrapOptionalWrappers)(schema);
|
|
68
|
-
const def = (0, kizunajs_generator.readDef)(inner);
|
|
69
|
-
if (def.type === "enum") return {
|
|
70
|
-
kind: "enum",
|
|
71
|
-
values: Object.values(def.entries ?? {}).map(String).sort()
|
|
72
|
-
};
|
|
73
|
-
if (def.type === "literal") return {
|
|
74
|
-
kind: "enum",
|
|
75
|
-
values: (def.values ?? []).map(String).sort()
|
|
76
|
-
};
|
|
77
|
-
if (def.type === "array" && def.element) return { kind: `array<${describe(def.element).kind}>` };
|
|
78
|
-
return { kind: def.type ?? "unknown" };
|
|
79
|
-
};
|
|
80
58
|
const joinPath = (parent, key) => parent === "" ? key : `${parent}.${key}`;
|
|
59
|
+
const fieldsOf = (schema) => schema.kind === "object" ? schema.fields : void 0;
|
|
60
|
+
const elementOf = (schema) => schema.kind === "array" ? schema.element : void 0;
|
|
61
|
+
const valuesOf = (schema) => schema.kind === "enum" ? schema.values : void 0;
|
|
81
62
|
/**
|
|
82
|
-
*
|
|
83
|
-
* rather than as the whole array changing shape.
|
|
63
|
+
* What a reader sees when the kinds differ, so `array` reads as `array<string>`.
|
|
84
64
|
*/
|
|
85
|
-
const
|
|
86
|
-
const
|
|
87
|
-
|
|
88
|
-
return def.type === "array" ? def.element : void 0;
|
|
65
|
+
const describeKind = (schema) => {
|
|
66
|
+
const element = elementOf(schema);
|
|
67
|
+
return element ? `array<${describeKind(element)}>` : schema.kind;
|
|
89
68
|
};
|
|
90
69
|
/**
|
|
91
70
|
* What changed between two versions of one schema, and whether it breaks the
|
|
@@ -107,11 +86,11 @@ const diffSchemas = (before, after, direction, path = "") => {
|
|
|
107
86
|
const beforeElement = elementOf(before);
|
|
108
87
|
const afterElement = elementOf(after);
|
|
109
88
|
if (beforeElement && afterElement) return diffSchemas(beforeElement, afterElement, direction, `${path}[]`);
|
|
110
|
-
const
|
|
111
|
-
const
|
|
112
|
-
if (
|
|
113
|
-
for (const [key, field] of
|
|
114
|
-
const existing =
|
|
89
|
+
const beforeFields = fieldsOf(before);
|
|
90
|
+
const afterFields = fieldsOf(after);
|
|
91
|
+
if (beforeFields && afterFields) {
|
|
92
|
+
for (const [key, field] of Object.entries(afterFields)) {
|
|
93
|
+
const existing = beforeFields[key];
|
|
115
94
|
const here = joinPath(path, key);
|
|
116
95
|
if (!existing) {
|
|
117
96
|
if (!field.optional) changes.push({
|
|
@@ -128,8 +107,8 @@ const diffSchemas = (before, after, direction, path = "") => {
|
|
|
128
107
|
});
|
|
129
108
|
changes.push(...diffSchemas(existing.schema, field.schema, direction, here));
|
|
130
109
|
}
|
|
131
|
-
for (const
|
|
132
|
-
if (
|
|
110
|
+
for (const key of Object.keys(beforeFields)) {
|
|
111
|
+
if (afterFields[key]) continue;
|
|
133
112
|
const here = joinPath(path, key);
|
|
134
113
|
changes.push({
|
|
135
114
|
breaking: direction === "response",
|
|
@@ -139,19 +118,16 @@ const diffSchemas = (before, after, direction, path = "") => {
|
|
|
139
118
|
}
|
|
140
119
|
return changes;
|
|
141
120
|
}
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
if (was.values && now.values) {
|
|
153
|
-
const removed = was.values.filter((value) => !now.values?.includes(value));
|
|
154
|
-
const added = now.values.filter((value) => !was.values?.includes(value));
|
|
121
|
+
if (before.kind !== after.kind) return [{
|
|
122
|
+
breaking: true,
|
|
123
|
+
path,
|
|
124
|
+
summary: `${path || "the body"} is ${describeKind(after)} instead of ${describeKind(before)}`
|
|
125
|
+
}];
|
|
126
|
+
const was = valuesOf(before);
|
|
127
|
+
const now = valuesOf(after);
|
|
128
|
+
if (was && now) {
|
|
129
|
+
const removed = was.filter((value) => !now.includes(value));
|
|
130
|
+
const added = now.filter((value) => !was.includes(value));
|
|
155
131
|
if (removed.length > 0) changes.push({
|
|
156
132
|
breaking: true,
|
|
157
133
|
path,
|
|
@@ -163,53 +139,145 @@ const diffSchemas = (before, after, direction, path = "") => {
|
|
|
163
139
|
summary: direction === "response" ? `${path || "the body"} can now be ${added.join(", ")}` : `${path || "the body"} also accepts ${added.join(", ")}`
|
|
164
140
|
});
|
|
165
141
|
}
|
|
142
|
+
const wasRefined = before.refinements ?? 0;
|
|
143
|
+
const isRefined = after.refinements ?? 0;
|
|
144
|
+
if (wasRefined !== isRefined) changes.push({
|
|
145
|
+
breaking: direction === "request" && isRefined > wasRefined,
|
|
146
|
+
path,
|
|
147
|
+
summary: isRefined > wasRefined ? `${path || "the body"} is validated more tightly` : `${path || "the body"} is validated less tightly`
|
|
148
|
+
});
|
|
166
149
|
return changes;
|
|
167
150
|
};
|
|
168
151
|
//#endregion
|
|
169
|
-
//#region src/
|
|
170
|
-
|
|
171
|
-
|
|
152
|
+
//#region src/snapshot.ts
|
|
153
|
+
/**
|
|
154
|
+
* How many `.refine()` or `.superRefine()` calls a schema carries. The
|
|
155
|
+
* predicate itself cannot be compared across two files, but its arrival or
|
|
156
|
+
* departure can.
|
|
157
|
+
*/
|
|
158
|
+
const refinementCount = (schema) => {
|
|
159
|
+
const checks = (0, kizunajs_generator.readDef)(schema).checks;
|
|
160
|
+
if (!checks) return void 0;
|
|
161
|
+
const custom = checks.filter((check) => check._zod?.def?.check === "custom").length;
|
|
162
|
+
return custom === 0 ? void 0 : custom;
|
|
163
|
+
};
|
|
164
|
+
/**
|
|
165
|
+
* A Zod schema reduced to the shape a comparison reads.
|
|
166
|
+
*/
|
|
167
|
+
const toSchemaNode = (schema) => {
|
|
168
|
+
const { inner } = (0, kizunajs_generator.unwrapOptionalWrappers)(schema);
|
|
169
|
+
const def = (0, kizunajs_generator.readDef)(inner);
|
|
170
|
+
const refinements = refinementCount(inner);
|
|
171
|
+
const carry = refinements === void 0 ? {} : { refinements };
|
|
172
|
+
const shape = (0, kizunajs_generator.readObjectShape)(inner);
|
|
173
|
+
if (shape) {
|
|
174
|
+
const fields = {};
|
|
175
|
+
for (const [key, field] of Object.entries(shape)) fields[key] = {
|
|
176
|
+
optional: (0, kizunajs_generator.unwrapOptionalWrappers)(field).optional,
|
|
177
|
+
schema: toSchemaNode(field)
|
|
178
|
+
};
|
|
179
|
+
return {
|
|
180
|
+
kind: "object",
|
|
181
|
+
fields,
|
|
182
|
+
...carry
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
if (def.type === "array" && def.element) return {
|
|
186
|
+
kind: "array",
|
|
187
|
+
element: toSchemaNode(def.element),
|
|
188
|
+
...carry
|
|
189
|
+
};
|
|
190
|
+
if (def.type === "enum") return {
|
|
191
|
+
kind: "enum",
|
|
192
|
+
values: Object.values(def.entries ?? {}).map(String).sort(),
|
|
193
|
+
...carry
|
|
194
|
+
};
|
|
195
|
+
if (def.type === "literal") return {
|
|
196
|
+
kind: "enum",
|
|
197
|
+
values: (def.values ?? []).map(String).sort(),
|
|
198
|
+
...carry
|
|
199
|
+
};
|
|
172
200
|
return {
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
method: route.method,
|
|
176
|
-
path: route.path,
|
|
177
|
-
statuses: Object.keys(route.responses).map(Number).sort(),
|
|
178
|
-
deprecated,
|
|
179
|
-
...sunsetOf(route) === void 0 ? {} : { sunset: sunsetOf(route) },
|
|
180
|
-
...route.body ? { body: route.body } : {},
|
|
181
|
-
...route.query ? { query: route.query } : {},
|
|
182
|
-
...route.headers ? { headers: route.headers } : {},
|
|
183
|
-
responses: new Map(Object.entries(route.responses).map(([status, response]) => [Number(status), (0, kizunajs_generator.resolveResponseBody)(response)]))
|
|
184
|
-
});
|
|
185
|
-
},
|
|
186
|
-
finalize: () => facts
|
|
201
|
+
kind: def.type ?? "unknown",
|
|
202
|
+
...carry
|
|
187
203
|
};
|
|
188
|
-
}
|
|
204
|
+
};
|
|
189
205
|
const sunsetOf = (route) => {
|
|
190
206
|
const declared = route.sunset;
|
|
191
207
|
if (declared === void 0) return void 0;
|
|
192
208
|
return typeof declared === "string" ? declared : declared.date;
|
|
193
209
|
};
|
|
194
|
-
const
|
|
195
|
-
const
|
|
196
|
-
|
|
197
|
-
|
|
210
|
+
const routeSnapshots = (api) => {
|
|
211
|
+
const routes = {};
|
|
212
|
+
return (0, kizunajs_generator.walkApi)(api, {
|
|
213
|
+
processRoute({ routeKey, route, deprecated, hidden }) {
|
|
214
|
+
const sunset = sunsetOf(route);
|
|
215
|
+
const responses = {};
|
|
216
|
+
for (const [status, response] of Object.entries(route.responses)) {
|
|
217
|
+
const body = (0, kizunajs_generator.resolveResponseBody)(response);
|
|
218
|
+
responses[status] = body === void 0 ? null : toSchemaNode(body);
|
|
219
|
+
}
|
|
220
|
+
routes[routeKey] = {
|
|
221
|
+
method: route.method,
|
|
222
|
+
path: route.path,
|
|
223
|
+
statuses: Object.keys(route.responses).map(Number).sort((a, b) => a - b),
|
|
224
|
+
deprecated,
|
|
225
|
+
...hidden ? { hidden: true } : {},
|
|
226
|
+
...sunset === void 0 ? {} : { sunset },
|
|
227
|
+
...route.body ? { body: toSchemaNode(route.body) } : {},
|
|
228
|
+
...route.query ? { query: toSchemaNode(route.query) } : {},
|
|
229
|
+
...route.headers ? { headers: toSchemaNode(route.headers) } : {},
|
|
230
|
+
responses
|
|
231
|
+
};
|
|
232
|
+
},
|
|
233
|
+
finalize: () => routes
|
|
234
|
+
});
|
|
198
235
|
};
|
|
199
236
|
/**
|
|
200
|
-
*
|
|
237
|
+
* Everything `kizuna diff` compares, as plain data.
|
|
238
|
+
*
|
|
239
|
+
* Writing this at each commit is what lets a comparison read the other side
|
|
240
|
+
* out of git rather than checking it out and running it.
|
|
201
241
|
*/
|
|
202
|
-
const
|
|
242
|
+
const toSnapshot = (api) => {
|
|
243
|
+
const declared = api.jobs;
|
|
244
|
+
const jobs = declared ? (0, kizunajs_generator.flattenJobs)(declared).map((job) => job.jobKey).sort() : [];
|
|
245
|
+
const tools = Object.fromEntries((0, kizunajs_adapter.flattenRoutes)(api.routes).filter(({ route }) => route.tool !== void 0 && route.tool !== false).map(({ routeKey }) => [routeKey, (0, kizunajs_generator.toToolName)(routeKey)]));
|
|
246
|
+
return {
|
|
247
|
+
routes: routeSnapshots(api),
|
|
248
|
+
jobs,
|
|
249
|
+
tools
|
|
250
|
+
};
|
|
251
|
+
};
|
|
203
252
|
/**
|
|
204
|
-
*
|
|
205
|
-
*
|
|
206
|
-
|
|
207
|
-
|
|
253
|
+
* YAML rather than JSON, so the file a reviewer opens in a pull request reads
|
|
254
|
+
* as the API rather than as punctuation.
|
|
255
|
+
*/
|
|
256
|
+
const renderSnapshot = (api) => (0, yaml.stringify)(toSnapshot(api), { lineWidth: 0 });
|
|
257
|
+
/**
|
|
258
|
+
* Where a config's snapshot lives. `.kizuna/` sits beside the config, so CI
|
|
259
|
+
* always knows where to look without being told, and a repository holding
|
|
260
|
+
* several configs keeps one snapshot per config.
|
|
208
261
|
*/
|
|
209
|
-
const
|
|
262
|
+
const snapshotPathFor = (configPath) => {
|
|
263
|
+
const declared = (0, node_path.basename)(configPath).replace(/\.config\.[cm]?[jt]s$/, "").replace(/^kizuna\./, "");
|
|
264
|
+
const name = declared === "" || declared === "kizuna" ? "snapshot" : `${declared}.snapshot`;
|
|
265
|
+
return (0, node_path.join)((0, node_path.dirname)(configPath), ".kizuna", `${name}.yaml`);
|
|
266
|
+
};
|
|
267
|
+
/**
|
|
268
|
+
* The snapshot as a client target, so `kizuna generate` writes it and
|
|
269
|
+
* `kizuna generate --check` refuses to let it go stale.
|
|
270
|
+
*/
|
|
271
|
+
const snapshotTarget = (configPath) => ({
|
|
272
|
+
output: snapshotPathFor(configPath),
|
|
273
|
+
render: renderSnapshot
|
|
274
|
+
});
|
|
275
|
+
//#endregion
|
|
276
|
+
//#region src/diff-apis.ts
|
|
277
|
+
const diffSnapshots = (before, after, options = {}) => {
|
|
210
278
|
const changes = [];
|
|
211
|
-
const beforeRoutes =
|
|
212
|
-
const afterRoutes =
|
|
279
|
+
const beforeRoutes = new Map(Object.entries(before.routes));
|
|
280
|
+
const afterRoutes = new Map(Object.entries(after.routes));
|
|
213
281
|
const removed = [...beforeRoutes.keys()].filter((key) => !afterRoutes.has(key));
|
|
214
282
|
const added = [...afterRoutes.keys()].filter((key) => !beforeRoutes.has(key));
|
|
215
283
|
const renames = /* @__PURE__ */ new Map();
|
|
@@ -226,10 +294,10 @@ const diffApis = (before, after) => {
|
|
|
226
294
|
if (match) renames.set(match, goneKey);
|
|
227
295
|
}
|
|
228
296
|
for (const [toKey, fromKey] of renames) changes.push({
|
|
229
|
-
level: "breaking",
|
|
297
|
+
level: options.dottedKeys ? "breaking" : "changed",
|
|
230
298
|
key: fromKey,
|
|
231
299
|
summary: `${fromKey} renamed to ${toKey}`,
|
|
232
|
-
detail: "
|
|
300
|
+
detail: "the generated client method changes name, the HTTP surface does not"
|
|
233
301
|
});
|
|
234
302
|
for (const key of removed) {
|
|
235
303
|
if ([...renames.values()].includes(key)) continue;
|
|
@@ -302,15 +370,26 @@ const diffApis = (before, after) => {
|
|
|
302
370
|
key,
|
|
303
371
|
summary: `${httpLabel(arrived, key)} ${change.summary}`
|
|
304
372
|
});
|
|
305
|
-
for (const [status, was] of gone.responses) {
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
for (const change of diffSchemas(was, now, "response",
|
|
373
|
+
for (const [status, was] of Object.entries(gone.responses)) {
|
|
374
|
+
if (!(status in arrived.responses)) continue;
|
|
375
|
+
const now = arrived.responses[status];
|
|
376
|
+
for (const change of diffSchemas(was ?? void 0, now ?? void 0, "response", status)) changes.push({
|
|
309
377
|
level: change.breaking ? "breaking" : "changed",
|
|
310
378
|
key,
|
|
311
379
|
summary: `${httpLabel(arrived, key)} ${change.summary}`
|
|
312
380
|
});
|
|
313
381
|
}
|
|
382
|
+
if (gone.hidden !== true && arrived.hidden === true) changes.push({
|
|
383
|
+
level: "breaking",
|
|
384
|
+
key,
|
|
385
|
+
summary: `${httpLabel(arrived, key)} is now hidden`,
|
|
386
|
+
detail: "the server still serves it, but it is no longer in the generated clients or the OpenAPI document"
|
|
387
|
+
});
|
|
388
|
+
if (gone.hidden === true && arrived.hidden !== true) changes.push({
|
|
389
|
+
level: "added",
|
|
390
|
+
key,
|
|
391
|
+
summary: `${httpLabel(arrived, key)} is no longer hidden`
|
|
392
|
+
});
|
|
314
393
|
if (!gone.deprecated && arrived.deprecated) changes.push({
|
|
315
394
|
level: "changed",
|
|
316
395
|
key,
|
|
@@ -322,8 +401,8 @@ const diffApis = (before, after) => {
|
|
|
322
401
|
summary: `${httpLabel(arrived, key)} sunsets on ${arrived.sunset}`
|
|
323
402
|
});
|
|
324
403
|
}
|
|
325
|
-
for (const key of
|
|
326
|
-
if (
|
|
404
|
+
for (const key of options.jobs ? before.jobs : []) {
|
|
405
|
+
if (after.jobs.includes(key)) continue;
|
|
327
406
|
changes.push({
|
|
328
407
|
level: "breaking",
|
|
329
408
|
key,
|
|
@@ -331,10 +410,8 @@ const diffApis = (before, after) => {
|
|
|
331
410
|
detail: "whatever calls POST /jobs/run with that key stops working"
|
|
332
411
|
});
|
|
333
412
|
}
|
|
334
|
-
const
|
|
335
|
-
|
|
336
|
-
for (const [key, name] of beforeTools) {
|
|
337
|
-
const arrived = afterTools.get(key);
|
|
413
|
+
for (const [key, name] of options.tools ? Object.entries(before.tools) : []) {
|
|
414
|
+
const arrived = after.tools[key];
|
|
338
415
|
if (arrived === void 0) {
|
|
339
416
|
changes.push({
|
|
340
417
|
level: "breaking",
|
|
@@ -359,6 +436,13 @@ const diffApis = (before, after) => {
|
|
|
359
436
|
return changes.sort((left, right) => order[left.level] - order[right.level] || left.key.localeCompare(right.key));
|
|
360
437
|
};
|
|
361
438
|
/**
|
|
439
|
+
* What changed between two apis, for a caller holding both in memory.
|
|
440
|
+
*
|
|
441
|
+
* `kizuna diff` reads snapshots instead, so it never has to load the other
|
|
442
|
+
* side's code.
|
|
443
|
+
*/
|
|
444
|
+
const diffApis = (before, after, options = {}) => diffSnapshots(toSnapshot(before), toSnapshot(after), options);
|
|
445
|
+
/**
|
|
362
446
|
* One block per change, ready to print: the level, the summary, and the
|
|
363
447
|
* detail indented beneath it.
|
|
364
448
|
*/
|
|
@@ -381,38 +465,37 @@ const currentContents = (output) => {
|
|
|
381
465
|
}
|
|
382
466
|
};
|
|
383
467
|
/**
|
|
384
|
-
* Writes every
|
|
385
|
-
* matches so watchers and build tools see no
|
|
468
|
+
* Writes every generated file, the clients and what plugins generate, leaving
|
|
469
|
+
* a file alone when it already matches so watchers and build tools see no
|
|
470
|
+
* change.
|
|
386
471
|
*/
|
|
387
|
-
const
|
|
388
|
-
const rendered =
|
|
389
|
-
const changed = currentContents(
|
|
472
|
+
const writeFiles = (contract, files) => files.map((file) => {
|
|
473
|
+
const rendered = file.render(contract);
|
|
474
|
+
const changed = currentContents(file.output) !== rendered;
|
|
390
475
|
if (changed) {
|
|
391
|
-
(0, node_fs.mkdirSync)((0, node_path.dirname)(
|
|
392
|
-
(0, node_fs.writeFileSync)(
|
|
476
|
+
(0, node_fs.mkdirSync)((0, node_path.dirname)(file.output), { recursive: true });
|
|
477
|
+
(0, node_fs.writeFileSync)(file.output, rendered);
|
|
393
478
|
}
|
|
394
479
|
return {
|
|
395
|
-
|
|
396
|
-
output: client.output,
|
|
480
|
+
output: file.output,
|
|
397
481
|
changed
|
|
398
482
|
};
|
|
399
483
|
});
|
|
400
484
|
/**
|
|
401
|
-
* Every
|
|
485
|
+
* Every generated file that no longer matches its api, without writing
|
|
486
|
+
* anything.
|
|
402
487
|
*
|
|
403
488
|
* This is what a pipeline runs: a generated client that has fallen behind is a
|
|
404
489
|
* compile error waiting to happen in whatever imports it.
|
|
405
490
|
*/
|
|
406
|
-
const
|
|
407
|
-
const current = currentContents(
|
|
491
|
+
const checkFiles = (contract, files) => files.flatMap((file) => {
|
|
492
|
+
const current = currentContents(file.output);
|
|
408
493
|
if (current === void 0) return [{
|
|
409
|
-
|
|
410
|
-
output: client.output,
|
|
494
|
+
output: file.output,
|
|
411
495
|
reason: "missing"
|
|
412
496
|
}];
|
|
413
|
-
if (current !==
|
|
414
|
-
|
|
415
|
-
output: client.output,
|
|
497
|
+
if (current !== file.render(contract)) return [{
|
|
498
|
+
output: file.output,
|
|
416
499
|
reason: "outdated"
|
|
417
500
|
}];
|
|
418
501
|
return [];
|
|
@@ -422,12 +505,12 @@ const checkClients = (contract, clients) => clients.flatMap((client) => {
|
|
|
422
505
|
* rather than printing a diff nobody reads.
|
|
423
506
|
*/
|
|
424
507
|
const formatStale = (stale, command = "kizuna generate") => {
|
|
425
|
-
const lines = stale.map((
|
|
426
|
-
const where = (0, node_path.relative)(process.cwd(),
|
|
427
|
-
return
|
|
508
|
+
const lines = stale.map((file) => {
|
|
509
|
+
const where = (0, node_path.relative)(process.cwd(), file.output);
|
|
510
|
+
return file.reason === "missing" ? ` ${where} has not been generated` : ` ${where} is behind the config`;
|
|
428
511
|
});
|
|
429
512
|
return [
|
|
430
|
-
stale.length === 1 ? "A generated
|
|
513
|
+
stale.length === 1 ? "A generated file is out of date:" : `${stale.length} generated files are out of date:`,
|
|
431
514
|
...lines,
|
|
432
515
|
"",
|
|
433
516
|
`Run \`${command}\` and commit the result.`
|
|
@@ -588,22 +671,32 @@ const HEADER = `/**
|
|
|
588
671
|
* and re-run \`kizuna generate\` to regenerate this file.
|
|
589
672
|
*/`;
|
|
590
673
|
/**
|
|
674
|
+
* Points a relative specifier at the same file from somewhere else, so a
|
|
675
|
+
* `Config` written outside the config's own directory still resolves.
|
|
676
|
+
*/
|
|
677
|
+
const rebaseSpecifier = (specifier, configDir, outputDir) => {
|
|
678
|
+
if (!specifier.startsWith("./") && !specifier.startsWith("../")) return specifier;
|
|
679
|
+
const rebased = (0, node_path.relative)(outputDir, (0, node_path.resolve)(configDir, specifier)).split(node_path.sep).join("/");
|
|
680
|
+
return rebased.startsWith(".") ? rebased : `./${rebased}`;
|
|
681
|
+
};
|
|
682
|
+
/**
|
|
591
683
|
* The `import type` lines the interface needs, grouped by module and written in
|
|
592
684
|
* the order the config imported them.
|
|
593
685
|
*/
|
|
594
|
-
const renderImports = (imports, used) => {
|
|
686
|
+
const renderImports = (imports, used, rebase) => {
|
|
595
687
|
const byModule = /* @__PURE__ */ new Map();
|
|
596
688
|
const defaults = [];
|
|
597
689
|
for (const [local, imported] of imports) {
|
|
598
690
|
if (!used.has(local)) continue;
|
|
691
|
+
const module = rebase(imported.module);
|
|
599
692
|
if (imported.isDefault) {
|
|
600
|
-
defaults.push(`import type ${local} from '${
|
|
693
|
+
defaults.push(`import type ${local} from '${module}';`);
|
|
601
694
|
continue;
|
|
602
695
|
}
|
|
603
696
|
const specifier = imported.exported === local ? local : `${imported.exported} as ${local}`;
|
|
604
|
-
const existing = byModule.get(
|
|
697
|
+
const existing = byModule.get(module);
|
|
605
698
|
if (existing) existing.push(specifier);
|
|
606
|
-
else byModule.set(
|
|
699
|
+
else byModule.set(module, [specifier]);
|
|
607
700
|
}
|
|
608
701
|
const named = [...byModule].map(([module, names]) => `import type { ${names.join(", ")} } from '${module}';`);
|
|
609
702
|
return [...defaults, ...named].join("\n");
|
|
@@ -615,8 +708,11 @@ const renderImports = (imports, used) => {
|
|
|
615
708
|
* Read from the config's syntax rather than by running it: the generated file
|
|
616
709
|
* imports the same names the config did, and a config that loads is not a
|
|
617
710
|
* prerequisite for typing the routes it serves.
|
|
711
|
+
*
|
|
712
|
+
* `outputPath` is what the imports are written relative to. Left out, they stay
|
|
713
|
+
* relative to the config.
|
|
618
714
|
*/
|
|
619
|
-
const generateConfigTypes = (configSource, fileName = "kizuna.config.ts") => {
|
|
715
|
+
const generateConfigTypes = (configSource, fileName = "kizuna.config.ts", outputPath) => {
|
|
620
716
|
const source = typescript.default.createSourceFile(fileName, configSource, typescript.default.ScriptTarget.Latest, true);
|
|
621
717
|
const imports = collectImports(source);
|
|
622
718
|
const configObject = findConfigObject(source);
|
|
@@ -643,7 +739,9 @@ const generateConfigTypes = (configSource, fileName = "kizuna.config.ts") => {
|
|
|
643
739
|
} else if (key === "plugins") lines.push(` ${key}: ${typeOfPluginList(value, used)};`);
|
|
644
740
|
else lines.push(` ${key}: ${typeOfValue(value, used, key)};`);
|
|
645
741
|
}
|
|
646
|
-
const
|
|
742
|
+
const configDir = (0, node_path.dirname)((0, node_path.resolve)(fileName));
|
|
743
|
+
const outputDir = outputPath === void 0 ? configDir : (0, node_path.dirname)((0, node_path.resolve)(outputPath));
|
|
744
|
+
const importBlock = renderImports(imports, used, (specifier) => outputDir === configDir ? specifier : rebaseSpecifier(specifier, configDir, outputDir));
|
|
647
745
|
const body = `export interface Config {\n${lines.join("\n")}\n}`;
|
|
648
746
|
return `${importBlock === "" ? HEADER : `${HEADER}\n${importBlock}`}\n\n${body}\n`;
|
|
649
747
|
};
|
|
@@ -651,44 +749,59 @@ const generateConfigTypes = (configSource, fileName = "kizuna.config.ts") => {
|
|
|
651
749
|
//#region src/diff-against.ts
|
|
652
750
|
const git = (cwd, ...args) => (0, node_child_process.execFileSync)("git", args, {
|
|
653
751
|
cwd,
|
|
654
|
-
encoding: "utf8"
|
|
752
|
+
encoding: "utf8",
|
|
753
|
+
stdio: [
|
|
754
|
+
"ignore",
|
|
755
|
+
"pipe",
|
|
756
|
+
"ignore"
|
|
757
|
+
]
|
|
655
758
|
}).trim();
|
|
656
759
|
/**
|
|
657
|
-
*
|
|
658
|
-
*
|
|
659
|
-
* The ref is checked out into a worktree inside the repository, which costs a
|
|
660
|
-
* few files rather than a clone. Nothing is installed there: module resolution
|
|
661
|
-
* walks up to the repository's own `node_modules`, so the base tree needs only
|
|
662
|
-
* its source.
|
|
663
|
-
*
|
|
664
|
-
* @example
|
|
665
|
-
* const changes = await diffAgainst('main', './src/contract.ts');
|
|
760
|
+
* The repository holding the snapshot, falling back to the one the caller ran in.
|
|
666
761
|
*/
|
|
667
|
-
const
|
|
668
|
-
const { cwd = process.cwd() } = options;
|
|
669
|
-
const root = git(cwd, "rev-parse", "--show-toplevel");
|
|
670
|
-
const absolute = (0, node_path.isAbsolute)(configPath) ? configPath : (0, node_path.resolve)(cwd, configPath);
|
|
671
|
-
const fromRoot = (0, node_path.relative)(root, absolute);
|
|
672
|
-
if (fromRoot.startsWith("..")) throw new Error(`${configPath} is outside the repository at ${root}`);
|
|
673
|
-
const worktree = (0, node_fs.mkdtempSync)((0, node_path.join)(root, ".kizuna-diff-"));
|
|
762
|
+
const repositoryFor = (directory, cwd) => {
|
|
674
763
|
try {
|
|
675
|
-
git(
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
}
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
764
|
+
return git(directory, "rev-parse", "--show-toplevel");
|
|
765
|
+
} catch {
|
|
766
|
+
return git(cwd, "rev-parse", "--show-toplevel");
|
|
767
|
+
}
|
|
768
|
+
};
|
|
769
|
+
const parseSnapshot = (contents, source) => {
|
|
770
|
+
try {
|
|
771
|
+
return (0, yaml.parse)(contents);
|
|
772
|
+
} catch {
|
|
773
|
+
throw new Error(`${source} is not a Kizuna snapshot. Run \`kizuna generate\` to write one.`);
|
|
774
|
+
}
|
|
775
|
+
};
|
|
776
|
+
/**
|
|
777
|
+
* Reads a path out of a ref without checking anything out.
|
|
778
|
+
*/
|
|
779
|
+
const showAtRef = (root, ref, path) => {
|
|
780
|
+
try {
|
|
781
|
+
return git(root, "show", `${ref}:${path}`);
|
|
782
|
+
} catch {
|
|
783
|
+
throw new Error(`${path} does not exist at ${ref}. It was added or renamed since, so there is nothing to compare against.`);
|
|
690
784
|
}
|
|
691
785
|
};
|
|
786
|
+
const readSnapshot = (path) => {
|
|
787
|
+
if (!(0, node_fs.existsSync)(path)) throw new Error(`No snapshot at ${path}. Run \`kizuna generate\` to write one.`);
|
|
788
|
+
return parseSnapshot((0, node_fs.readFileSync)(path, "utf8"), path);
|
|
789
|
+
};
|
|
790
|
+
/**
|
|
791
|
+
* Compares a snapshot against the same file at a git ref.
|
|
792
|
+
*
|
|
793
|
+
* The ref is read with `git show`, so nothing is checked out, installed or
|
|
794
|
+
* executed for the other side. A snapshot written a year ago compares fine
|
|
795
|
+
* against one written today.
|
|
796
|
+
*/
|
|
797
|
+
const diffAgainst = (ref, snapshotPath, options = {}) => {
|
|
798
|
+
const { cwd = process.cwd(), ...diff } = options;
|
|
799
|
+
const absolute = (0, node_path.isAbsolute)(snapshotPath) ? snapshotPath : (0, node_path.resolve)(cwd, snapshotPath);
|
|
800
|
+
const root = repositoryFor((0, node_path.dirname)(absolute), cwd);
|
|
801
|
+
const fromRoot = (0, node_path.relative)(root, absolute);
|
|
802
|
+
if (fromRoot.startsWith("..")) throw new Error(`${snapshotPath} is outside the repository at ${root}`);
|
|
803
|
+
return diffSnapshots(parseSnapshot(showAtRef(root, ref, fromRoot.split("\\").join("/")), `${fromRoot} at ${ref}`), readSnapshot(absolute), diff);
|
|
804
|
+
};
|
|
692
805
|
//#endregion
|
|
693
806
|
//#region src/route-map.ts
|
|
694
807
|
const permissionList = (requires) => {
|
|
@@ -719,11 +832,11 @@ const describeSunset = (sunset) => {
|
|
|
719
832
|
/**
|
|
720
833
|
* Every route an api serves, in the order it declares them.
|
|
721
834
|
*/
|
|
722
|
-
const routeMap = (
|
|
835
|
+
const routeMap = (api, _options = {}) => {
|
|
723
836
|
const entries = [];
|
|
724
837
|
const toolKeys = [];
|
|
725
|
-
return {
|
|
726
|
-
processRoute({ routeKey, route, routeTags, deprecated, deprecationMessage }) {
|
|
838
|
+
return (0, kizunajs_generator.walkApi)(api, {
|
|
839
|
+
processRoute({ routeKey, route, routeTags, deprecated, deprecationMessage, hidden }) {
|
|
727
840
|
if (route.tool) toolKeys.push({
|
|
728
841
|
key: routeKey,
|
|
729
842
|
origin: "route"
|
|
@@ -735,7 +848,8 @@ const routeMap = (0, kizunajs_generator.createGenerator)((_options, _api) => {
|
|
|
735
848
|
auth: describeAuth(route.auth),
|
|
736
849
|
tags: routeTags,
|
|
737
850
|
deprecated: deprecated ? deprecationMessage ?? "" : void 0,
|
|
738
|
-
sunset: describeSunset(route.sunset)
|
|
851
|
+
sunset: describeSunset(route.sunset),
|
|
852
|
+
...hidden ? { hidden: true } : {}
|
|
739
853
|
});
|
|
740
854
|
},
|
|
741
855
|
finalize() {
|
|
@@ -745,8 +859,8 @@ const routeMap = (0, kizunajs_generator.createGenerator)((_options, _api) => {
|
|
|
745
859
|
tool: names.get(entry.key) ?? (toolKeys.some((tool) => tool.key === entry.key) ? (0, kizunajs_generator.toToolName)(entry.key) : void 0)
|
|
746
860
|
}));
|
|
747
861
|
}
|
|
748
|
-
};
|
|
749
|
-
}
|
|
862
|
+
});
|
|
863
|
+
};
|
|
750
864
|
const pad = (text, width) => text + " ".repeat(Math.max(0, width - text.length));
|
|
751
865
|
/**
|
|
752
866
|
* The route map as aligned columns, one route per line.
|
|
@@ -761,7 +875,8 @@ const formatRoutes = (entries) => {
|
|
|
761
875
|
`auth: ${entry.auth}`,
|
|
762
876
|
entry.tool && `tool: ${entry.tool}`,
|
|
763
877
|
entry.deprecated !== void 0 && `deprecated${entry.deprecated ? `, ${entry.deprecated}` : ""}`,
|
|
764
|
-
entry.sunset && `sunset ${entry.sunset}
|
|
878
|
+
entry.sunset && `sunset ${entry.sunset}`,
|
|
879
|
+
entry.hidden && "hidden"
|
|
765
880
|
].filter(Boolean);
|
|
766
881
|
return `${pad(entry.method, methodWidth)} ${pad(entry.path, pathWidth)} ${pad(entry.key, keyWidth)} ${notes.join(" ")}`.trimEnd();
|
|
767
882
|
}).join("\n");
|
|
@@ -779,10 +894,10 @@ Object.defineProperty(exports, "__toESM", {
|
|
|
779
894
|
return __toESM;
|
|
780
895
|
}
|
|
781
896
|
});
|
|
782
|
-
Object.defineProperty(exports, "
|
|
897
|
+
Object.defineProperty(exports, "checkFiles", {
|
|
783
898
|
enumerable: true,
|
|
784
899
|
get: function() {
|
|
785
|
-
return
|
|
900
|
+
return checkFiles;
|
|
786
901
|
}
|
|
787
902
|
});
|
|
788
903
|
Object.defineProperty(exports, "diffAgainst", {
|
|
@@ -803,6 +918,12 @@ Object.defineProperty(exports, "diffSchemas", {
|
|
|
803
918
|
return diffSchemas;
|
|
804
919
|
}
|
|
805
920
|
});
|
|
921
|
+
Object.defineProperty(exports, "diffSnapshots", {
|
|
922
|
+
enumerable: true,
|
|
923
|
+
get: function() {
|
|
924
|
+
return diffSnapshots;
|
|
925
|
+
}
|
|
926
|
+
});
|
|
806
927
|
Object.defineProperty(exports, "formatChange", {
|
|
807
928
|
enumerable: true,
|
|
808
929
|
get: function() {
|
|
@@ -839,15 +960,33 @@ Object.defineProperty(exports, "loadConfig", {
|
|
|
839
960
|
return loadConfig;
|
|
840
961
|
}
|
|
841
962
|
});
|
|
963
|
+
Object.defineProperty(exports, "readSnapshot", {
|
|
964
|
+
enumerable: true,
|
|
965
|
+
get: function() {
|
|
966
|
+
return readSnapshot;
|
|
967
|
+
}
|
|
968
|
+
});
|
|
842
969
|
Object.defineProperty(exports, "routeMap", {
|
|
843
970
|
enumerable: true,
|
|
844
971
|
get: function() {
|
|
845
972
|
return routeMap;
|
|
846
973
|
}
|
|
847
974
|
});
|
|
848
|
-
Object.defineProperty(exports, "
|
|
975
|
+
Object.defineProperty(exports, "snapshotPathFor", {
|
|
976
|
+
enumerable: true,
|
|
977
|
+
get: function() {
|
|
978
|
+
return snapshotPathFor;
|
|
979
|
+
}
|
|
980
|
+
});
|
|
981
|
+
Object.defineProperty(exports, "snapshotTarget", {
|
|
982
|
+
enumerable: true,
|
|
983
|
+
get: function() {
|
|
984
|
+
return snapshotTarget;
|
|
985
|
+
}
|
|
986
|
+
});
|
|
987
|
+
Object.defineProperty(exports, "writeFiles", {
|
|
849
988
|
enumerable: true,
|
|
850
989
|
get: function() {
|
|
851
|
-
return
|
|
990
|
+
return writeFiles;
|
|
852
991
|
}
|
|
853
992
|
});
|