@kizunajs/cli 2.0.0-beta.3 → 2.0.0-beta.30
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 +75 -10
- package/dist/cli.mjs +75 -10
- package/dist/index.cjs +13 -12
- package/dist/index.d.cts +99 -37
- package/dist/index.d.mts +99 -37
- package/dist/index.mjs +13 -12
- package/dist/{route-map-BUq6Iw3_.cjs → route-map-BHe__KDP.cjs} +329 -171
- package/dist/{route-map-BZVDVH7m.mjs → route-map-C0K0orUw.mjs} +304 -170
- package/package.json +10 -9
|
@@ -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");
|
|
@@ -47,49 +48,23 @@ const loadConfig = async (configPath) => {
|
|
|
47
48
|
name,
|
|
48
49
|
api: entry.api,
|
|
49
50
|
clients: entry.clients ?? [],
|
|
50
|
-
|
|
51
|
+
generators: entry.generators ?? [],
|
|
52
|
+
typesOutput: entry.typescript?.outputFile,
|
|
53
|
+
diff: entry.diff ?? {}
|
|
51
54
|
}));
|
|
52
55
|
};
|
|
53
56
|
//#endregion
|
|
54
57
|
//#region src/diff-schemas.ts
|
|
55
|
-
const shapeOf = (schema) => {
|
|
56
|
-
const { inner } = (0, kizunajs_generator.unwrapOptionalWrappers)(schema);
|
|
57
|
-
const raw = (0, kizunajs_generator.readObjectShape)(inner);
|
|
58
|
-
if (!raw) return void 0;
|
|
59
|
-
const fields = /* @__PURE__ */ new Map();
|
|
60
|
-
for (const [key, field] of Object.entries(raw)) fields.set(key, {
|
|
61
|
-
schema: field,
|
|
62
|
-
optional: (0, kizunajs_generator.unwrapOptionalWrappers)(field).optional
|
|
63
|
-
});
|
|
64
|
-
return { fields };
|
|
65
|
-
};
|
|
66
|
-
/**
|
|
67
|
-
* What a schema accepts, flattened enough to compare: its kind, and its values
|
|
68
|
-
* when it is closed.
|
|
69
|
-
*/
|
|
70
|
-
const describe = (schema) => {
|
|
71
|
-
const { inner } = (0, kizunajs_generator.unwrapOptionalWrappers)(schema);
|
|
72
|
-
const def = (0, kizunajs_generator.readDef)(inner);
|
|
73
|
-
if (def.type === "enum") return {
|
|
74
|
-
kind: "enum",
|
|
75
|
-
values: Object.values(def.entries ?? {}).map(String).sort()
|
|
76
|
-
};
|
|
77
|
-
if (def.type === "literal") return {
|
|
78
|
-
kind: "enum",
|
|
79
|
-
values: (def.values ?? []).map(String).sort()
|
|
80
|
-
};
|
|
81
|
-
if (def.type === "array" && def.element) return { kind: `array<${describe(def.element).kind}>` };
|
|
82
|
-
return { kind: def.type ?? "unknown" };
|
|
83
|
-
};
|
|
84
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;
|
|
85
62
|
/**
|
|
86
|
-
*
|
|
87
|
-
* rather than as the whole array changing shape.
|
|
63
|
+
* What a reader sees when the kinds differ, so `array` reads as `array<string>`.
|
|
88
64
|
*/
|
|
89
|
-
const
|
|
90
|
-
const
|
|
91
|
-
|
|
92
|
-
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;
|
|
93
68
|
};
|
|
94
69
|
/**
|
|
95
70
|
* What changed between two versions of one schema, and whether it breaks the
|
|
@@ -107,15 +82,30 @@ const diffSchemas = (before, after, direction, path = "") => {
|
|
|
107
82
|
path,
|
|
108
83
|
summary: `${path || "the body"} is gone`
|
|
109
84
|
}];
|
|
85
|
+
if (before.kind !== after.kind) return [{
|
|
86
|
+
breaking: true,
|
|
87
|
+
path,
|
|
88
|
+
summary: `${path || "the body"} is ${describeKind(after)} instead of ${describeKind(before)}`
|
|
89
|
+
}];
|
|
110
90
|
const changes = [];
|
|
91
|
+
const wasNullable = before.nullable === true;
|
|
92
|
+
const isNullable = after.nullable === true;
|
|
93
|
+
if (wasNullable !== isNullable) changes.push({
|
|
94
|
+
breaking: direction === (isNullable ? "response" : "request"),
|
|
95
|
+
path,
|
|
96
|
+
summary: isNullable ? `${path || "the body"} can now be null` : `${path || "the body"} can no longer be null`
|
|
97
|
+
});
|
|
111
98
|
const beforeElement = elementOf(before);
|
|
112
99
|
const afterElement = elementOf(after);
|
|
113
|
-
if (beforeElement && afterElement)
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
100
|
+
if (beforeElement && afterElement) {
|
|
101
|
+
changes.push(...diffSchemas(beforeElement, afterElement, direction, `${path}[]`));
|
|
102
|
+
return changes;
|
|
103
|
+
}
|
|
104
|
+
const beforeFields = fieldsOf(before);
|
|
105
|
+
const afterFields = fieldsOf(after);
|
|
106
|
+
if (beforeFields && afterFields) {
|
|
107
|
+
for (const [key, field] of Object.entries(afterFields)) {
|
|
108
|
+
const existing = beforeFields[key];
|
|
119
109
|
const here = joinPath(path, key);
|
|
120
110
|
if (!existing) {
|
|
121
111
|
if (!field.optional) changes.push({
|
|
@@ -130,10 +120,15 @@ const diffSchemas = (before, after, direction, path = "") => {
|
|
|
130
120
|
path: here,
|
|
131
121
|
summary: `${here} is no longer optional`
|
|
132
122
|
});
|
|
123
|
+
if (!existing.optional && field.optional) changes.push({
|
|
124
|
+
breaking: direction === "response",
|
|
125
|
+
path: here,
|
|
126
|
+
summary: direction === "response" ? `${here} may now be left out` : `${here} is now optional`
|
|
127
|
+
});
|
|
133
128
|
changes.push(...diffSchemas(existing.schema, field.schema, direction, here));
|
|
134
129
|
}
|
|
135
|
-
for (const
|
|
136
|
-
if (
|
|
130
|
+
for (const key of Object.keys(beforeFields)) {
|
|
131
|
+
if (afterFields[key]) continue;
|
|
137
132
|
const here = joinPath(path, key);
|
|
138
133
|
changes.push({
|
|
139
134
|
breaking: direction === "response",
|
|
@@ -143,19 +138,11 @@ const diffSchemas = (before, after, direction, path = "") => {
|
|
|
143
138
|
}
|
|
144
139
|
return changes;
|
|
145
140
|
}
|
|
146
|
-
const was =
|
|
147
|
-
const now =
|
|
148
|
-
if (was
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
path,
|
|
152
|
-
summary: `${path || "the body"} is ${now.kind} instead of ${was.kind}`
|
|
153
|
-
});
|
|
154
|
-
return changes;
|
|
155
|
-
}
|
|
156
|
-
if (was.values && now.values) {
|
|
157
|
-
const removed = was.values.filter((value) => !now.values?.includes(value));
|
|
158
|
-
const added = now.values.filter((value) => !was.values?.includes(value));
|
|
141
|
+
const was = valuesOf(before);
|
|
142
|
+
const now = valuesOf(after);
|
|
143
|
+
if (was && now) {
|
|
144
|
+
const removed = was.filter((value) => !now.includes(value));
|
|
145
|
+
const added = now.filter((value) => !was.includes(value));
|
|
159
146
|
if (removed.length > 0) changes.push({
|
|
160
147
|
breaking: true,
|
|
161
148
|
path,
|
|
@@ -167,53 +154,157 @@ const diffSchemas = (before, after, direction, path = "") => {
|
|
|
167
154
|
summary: direction === "response" ? `${path || "the body"} can now be ${added.join(", ")}` : `${path || "the body"} also accepts ${added.join(", ")}`
|
|
168
155
|
});
|
|
169
156
|
}
|
|
157
|
+
const wasRefined = before.refinements ?? 0;
|
|
158
|
+
const isRefined = after.refinements ?? 0;
|
|
159
|
+
if (wasRefined !== isRefined) changes.push({
|
|
160
|
+
breaking: direction === "request" && isRefined > wasRefined,
|
|
161
|
+
path,
|
|
162
|
+
summary: isRefined > wasRefined ? `${path || "the body"} is validated more tightly` : `${path || "the body"} is validated less tightly`
|
|
163
|
+
});
|
|
170
164
|
return changes;
|
|
171
165
|
};
|
|
172
166
|
//#endregion
|
|
173
|
-
//#region src/
|
|
174
|
-
|
|
175
|
-
|
|
167
|
+
//#region src/snapshot.ts
|
|
168
|
+
/**
|
|
169
|
+
* How many `.refine()` or `.superRefine()` calls a schema carries. The
|
|
170
|
+
* predicate itself cannot be compared across two files, but its arrival or
|
|
171
|
+
* departure can.
|
|
172
|
+
*/
|
|
173
|
+
const refinementCount = (schema) => {
|
|
174
|
+
const checks = (0, kizunajs_generator.readDef)(schema).checks;
|
|
175
|
+
if (!checks) return void 0;
|
|
176
|
+
const custom = checks.filter((check) => check._zod?.def?.check === "custom").length;
|
|
177
|
+
return custom === 0 ? void 0 : custom;
|
|
178
|
+
};
|
|
179
|
+
/**
|
|
180
|
+
* A Zod schema reduced to the shape a comparison reads.
|
|
181
|
+
*/
|
|
182
|
+
const toSchemaNode = (schema) => {
|
|
183
|
+
const { inner, nullable } = (0, kizunajs_generator.unwrapOptionalWrappers)(schema);
|
|
184
|
+
const def = (0, kizunajs_generator.readDef)(inner);
|
|
185
|
+
const options = def.type === "union" ? def.options ?? [] : [];
|
|
186
|
+
const nonNull = options.filter((option) => (0, kizunajs_generator.readDef)(option).type !== "null");
|
|
187
|
+
if (nonNull.length === 1 && options.length > 1) return {
|
|
188
|
+
...toSchemaNode(nonNull[0]),
|
|
189
|
+
nullable: true
|
|
190
|
+
};
|
|
191
|
+
const nullability = nullable ? { nullable: true } : {};
|
|
192
|
+
const refinements = refinementCount(inner);
|
|
193
|
+
const carry = refinements === void 0 ? {} : { refinements };
|
|
194
|
+
const shape = (0, kizunajs_generator.readObjectShape)(inner);
|
|
195
|
+
if (shape) {
|
|
196
|
+
const fields = {};
|
|
197
|
+
for (const [key, field] of Object.entries(shape)) fields[key] = {
|
|
198
|
+
optional: (0, kizunajs_generator.unwrapOptionalWrappers)(field).optional,
|
|
199
|
+
schema: toSchemaNode(field)
|
|
200
|
+
};
|
|
201
|
+
return {
|
|
202
|
+
kind: "object",
|
|
203
|
+
...nullability,
|
|
204
|
+
fields,
|
|
205
|
+
...carry
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
if (def.type === "array" && def.element) return {
|
|
209
|
+
kind: "array",
|
|
210
|
+
...nullability,
|
|
211
|
+
element: toSchemaNode(def.element),
|
|
212
|
+
...carry
|
|
213
|
+
};
|
|
214
|
+
if (def.type === "enum") return {
|
|
215
|
+
kind: "enum",
|
|
216
|
+
...nullability,
|
|
217
|
+
values: Object.values(def.entries ?? {}).map(String).sort(),
|
|
218
|
+
...carry
|
|
219
|
+
};
|
|
220
|
+
if (def.type === "literal") return {
|
|
221
|
+
kind: "enum",
|
|
222
|
+
...nullability,
|
|
223
|
+
values: (def.values ?? []).map(String).sort(),
|
|
224
|
+
...carry
|
|
225
|
+
};
|
|
176
226
|
return {
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
path: route.path,
|
|
181
|
-
statuses: Object.keys(route.responses).map(Number).sort(),
|
|
182
|
-
deprecated,
|
|
183
|
-
...sunsetOf(route) === void 0 ? {} : { sunset: sunsetOf(route) },
|
|
184
|
-
...route.body ? { body: route.body } : {},
|
|
185
|
-
...route.query ? { query: route.query } : {},
|
|
186
|
-
...route.headers ? { headers: route.headers } : {},
|
|
187
|
-
responses: new Map(Object.entries(route.responses).map(([status, response]) => [Number(status), (0, kizunajs_generator.resolveResponseBody)(response)]))
|
|
188
|
-
});
|
|
189
|
-
},
|
|
190
|
-
finalize: () => facts
|
|
227
|
+
kind: def.type ?? "unknown",
|
|
228
|
+
...nullability,
|
|
229
|
+
...carry
|
|
191
230
|
};
|
|
192
|
-
}
|
|
231
|
+
};
|
|
193
232
|
const sunsetOf = (route) => {
|
|
194
233
|
const declared = route.sunset;
|
|
195
234
|
if (declared === void 0) return void 0;
|
|
196
235
|
return typeof declared === "string" ? declared : declared.date;
|
|
197
236
|
};
|
|
198
|
-
const
|
|
199
|
-
const
|
|
200
|
-
|
|
201
|
-
|
|
237
|
+
const routeSnapshots = (api) => {
|
|
238
|
+
const routes = {};
|
|
239
|
+
return (0, kizunajs_generator.walkApi)(api, {
|
|
240
|
+
processRoute({ routeKey, route, deprecated, hidden }) {
|
|
241
|
+
const sunset = sunsetOf(route);
|
|
242
|
+
const responses = {};
|
|
243
|
+
for (const [status, response] of Object.entries(route.responses)) {
|
|
244
|
+
const body = (0, kizunajs_generator.resolveResponseBody)(response);
|
|
245
|
+
responses[status] = body === void 0 ? null : toSchemaNode(body);
|
|
246
|
+
}
|
|
247
|
+
routes[routeKey] = {
|
|
248
|
+
method: route.method,
|
|
249
|
+
path: route.path,
|
|
250
|
+
statuses: Object.keys(route.responses).map(Number).sort((a, b) => a - b),
|
|
251
|
+
deprecated,
|
|
252
|
+
...hidden ? { hidden: true } : {},
|
|
253
|
+
...sunset === void 0 ? {} : { sunset },
|
|
254
|
+
...route.body ? { body: toSchemaNode(route.body) } : {},
|
|
255
|
+
...route.query ? { query: toSchemaNode(route.query) } : {},
|
|
256
|
+
...route.headers ? { headers: toSchemaNode(route.headers) } : {},
|
|
257
|
+
responses
|
|
258
|
+
};
|
|
259
|
+
},
|
|
260
|
+
finalize: () => routes
|
|
261
|
+
});
|
|
262
|
+
};
|
|
263
|
+
/**
|
|
264
|
+
* Everything `kizuna diff` compares, as plain data.
|
|
265
|
+
*
|
|
266
|
+
* Writing this at each commit is what lets a comparison read the other side
|
|
267
|
+
* out of git rather than checking it out and running it.
|
|
268
|
+
*/
|
|
269
|
+
const toSnapshot = (api) => {
|
|
270
|
+
const declared = api.jobs;
|
|
271
|
+
const jobs = declared ? (0, kizunajs_generator.flattenJobs)(declared).map((job) => job.jobKey).sort() : [];
|
|
272
|
+
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)]));
|
|
273
|
+
return {
|
|
274
|
+
routes: routeSnapshots(api),
|
|
275
|
+
jobs,
|
|
276
|
+
tools
|
|
277
|
+
};
|
|
202
278
|
};
|
|
203
279
|
/**
|
|
204
|
-
*
|
|
280
|
+
* YAML rather than JSON, so the file a reviewer opens in a pull request reads
|
|
281
|
+
* as the API rather than as punctuation.
|
|
205
282
|
*/
|
|
206
|
-
const
|
|
283
|
+
const renderSnapshot = (api) => (0, yaml.stringify)(toSnapshot(api), { lineWidth: 0 });
|
|
207
284
|
/**
|
|
208
|
-
*
|
|
209
|
-
*
|
|
210
|
-
*
|
|
211
|
-
* keys, MCP tool names and job keys, none of which a document carries.
|
|
285
|
+
* Where a config's snapshot lives. `.kizuna/` sits beside the config, so CI
|
|
286
|
+
* always knows where to look without being told, and a repository holding
|
|
287
|
+
* several configs keeps one snapshot per config.
|
|
212
288
|
*/
|
|
213
|
-
const
|
|
289
|
+
const snapshotPathFor = (configPath) => {
|
|
290
|
+
const declared = (0, node_path.basename)(configPath).replace(/\.config\.[cm]?[jt]s$/, "").replace(/^kizuna\./, "");
|
|
291
|
+
const name = declared === "" || declared === "kizuna" ? "snapshot" : `${declared}.snapshot`;
|
|
292
|
+
return (0, node_path.join)((0, node_path.dirname)(configPath), ".kizuna", `${name}.yaml`);
|
|
293
|
+
};
|
|
294
|
+
/**
|
|
295
|
+
* The snapshot as a client target, so `kizuna generate` writes it and
|
|
296
|
+
* `kizuna generate --check` refuses to let it go stale.
|
|
297
|
+
*/
|
|
298
|
+
const snapshotTarget = (configPath) => ({
|
|
299
|
+
output: snapshotPathFor(configPath),
|
|
300
|
+
render: renderSnapshot
|
|
301
|
+
});
|
|
302
|
+
//#endregion
|
|
303
|
+
//#region src/diff-apis.ts
|
|
304
|
+
const diffSnapshots = (before, after, options = {}) => {
|
|
214
305
|
const changes = [];
|
|
215
|
-
const beforeRoutes =
|
|
216
|
-
const afterRoutes =
|
|
306
|
+
const beforeRoutes = new Map(Object.entries(before.routes));
|
|
307
|
+
const afterRoutes = new Map(Object.entries(after.routes));
|
|
217
308
|
const removed = [...beforeRoutes.keys()].filter((key) => !afterRoutes.has(key));
|
|
218
309
|
const added = [...afterRoutes.keys()].filter((key) => !beforeRoutes.has(key));
|
|
219
310
|
const renames = /* @__PURE__ */ new Map();
|
|
@@ -230,10 +321,10 @@ const diffApis = (before, after) => {
|
|
|
230
321
|
if (match) renames.set(match, goneKey);
|
|
231
322
|
}
|
|
232
323
|
for (const [toKey, fromKey] of renames) changes.push({
|
|
233
|
-
level: "breaking",
|
|
324
|
+
level: options.dottedKeys ? "breaking" : "changed",
|
|
234
325
|
key: fromKey,
|
|
235
326
|
summary: `${fromKey} renamed to ${toKey}`,
|
|
236
|
-
detail: "
|
|
327
|
+
detail: "the generated client method changes name, the HTTP surface does not"
|
|
237
328
|
});
|
|
238
329
|
for (const key of removed) {
|
|
239
330
|
if ([...renames.values()].includes(key)) continue;
|
|
@@ -306,15 +397,26 @@ const diffApis = (before, after) => {
|
|
|
306
397
|
key,
|
|
307
398
|
summary: `${httpLabel(arrived, key)} ${change.summary}`
|
|
308
399
|
});
|
|
309
|
-
for (const [status, was] of gone.responses) {
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
for (const change of diffSchemas(was, now, "response",
|
|
400
|
+
for (const [status, was] of Object.entries(gone.responses)) {
|
|
401
|
+
if (!(status in arrived.responses)) continue;
|
|
402
|
+
const now = arrived.responses[status];
|
|
403
|
+
for (const change of diffSchemas(was ?? void 0, now ?? void 0, "response", status)) changes.push({
|
|
313
404
|
level: change.breaking ? "breaking" : "changed",
|
|
314
405
|
key,
|
|
315
406
|
summary: `${httpLabel(arrived, key)} ${change.summary}`
|
|
316
407
|
});
|
|
317
408
|
}
|
|
409
|
+
if (gone.hidden !== true && arrived.hidden === true) changes.push({
|
|
410
|
+
level: "breaking",
|
|
411
|
+
key,
|
|
412
|
+
summary: `${httpLabel(arrived, key)} is now hidden`,
|
|
413
|
+
detail: "the server still serves it, but it is no longer in the generated clients or the OpenAPI document"
|
|
414
|
+
});
|
|
415
|
+
if (gone.hidden === true && arrived.hidden !== true) changes.push({
|
|
416
|
+
level: "added",
|
|
417
|
+
key,
|
|
418
|
+
summary: `${httpLabel(arrived, key)} is no longer hidden`
|
|
419
|
+
});
|
|
318
420
|
if (!gone.deprecated && arrived.deprecated) changes.push({
|
|
319
421
|
level: "changed",
|
|
320
422
|
key,
|
|
@@ -326,8 +428,8 @@ const diffApis = (before, after) => {
|
|
|
326
428
|
summary: `${httpLabel(arrived, key)} sunsets on ${arrived.sunset}`
|
|
327
429
|
});
|
|
328
430
|
}
|
|
329
|
-
for (const key of
|
|
330
|
-
if (
|
|
431
|
+
for (const key of options.jobs ? before.jobs : []) {
|
|
432
|
+
if (after.jobs.includes(key)) continue;
|
|
331
433
|
changes.push({
|
|
332
434
|
level: "breaking",
|
|
333
435
|
key,
|
|
@@ -335,10 +437,8 @@ const diffApis = (before, after) => {
|
|
|
335
437
|
detail: "whatever calls POST /jobs/run with that key stops working"
|
|
336
438
|
});
|
|
337
439
|
}
|
|
338
|
-
const
|
|
339
|
-
|
|
340
|
-
for (const [key, name] of beforeTools) {
|
|
341
|
-
const arrived = afterTools.get(key);
|
|
440
|
+
for (const [key, name] of options.tools ? Object.entries(before.tools) : []) {
|
|
441
|
+
const arrived = after.tools[key];
|
|
342
442
|
if (arrived === void 0) {
|
|
343
443
|
changes.push({
|
|
344
444
|
level: "breaking",
|
|
@@ -363,6 +463,13 @@ const diffApis = (before, after) => {
|
|
|
363
463
|
return changes.sort((left, right) => order[left.level] - order[right.level] || left.key.localeCompare(right.key));
|
|
364
464
|
};
|
|
365
465
|
/**
|
|
466
|
+
* What changed between two apis, for a caller holding both in memory.
|
|
467
|
+
*
|
|
468
|
+
* `kizuna diff` reads snapshots instead, so it never has to load the other
|
|
469
|
+
* side's code.
|
|
470
|
+
*/
|
|
471
|
+
const diffApis = (before, after, options = {}) => diffSnapshots(toSnapshot(before), toSnapshot(after), options);
|
|
472
|
+
/**
|
|
366
473
|
* One block per change, ready to print: the level, the summary, and the
|
|
367
474
|
* detail indented beneath it.
|
|
368
475
|
*/
|
|
@@ -385,38 +492,37 @@ const currentContents = (output) => {
|
|
|
385
492
|
}
|
|
386
493
|
};
|
|
387
494
|
/**
|
|
388
|
-
* Writes every
|
|
389
|
-
* matches so watchers and build tools see no
|
|
495
|
+
* Writes every generated file, the clients and what plugins generate, leaving
|
|
496
|
+
* a file alone when it already matches so watchers and build tools see no
|
|
497
|
+
* change.
|
|
390
498
|
*/
|
|
391
|
-
const
|
|
392
|
-
const rendered =
|
|
393
|
-
const changed = currentContents(
|
|
499
|
+
const writeFiles = (contract, files) => files.map((file) => {
|
|
500
|
+
const rendered = file.render(contract);
|
|
501
|
+
const changed = currentContents(file.output) !== rendered;
|
|
394
502
|
if (changed) {
|
|
395
|
-
(0, node_fs.mkdirSync)((0, node_path.dirname)(
|
|
396
|
-
(0, node_fs.writeFileSync)(
|
|
503
|
+
(0, node_fs.mkdirSync)((0, node_path.dirname)(file.output), { recursive: true });
|
|
504
|
+
(0, node_fs.writeFileSync)(file.output, rendered);
|
|
397
505
|
}
|
|
398
506
|
return {
|
|
399
|
-
|
|
400
|
-
output: client.output,
|
|
507
|
+
output: file.output,
|
|
401
508
|
changed
|
|
402
509
|
};
|
|
403
510
|
});
|
|
404
511
|
/**
|
|
405
|
-
* Every
|
|
512
|
+
* Every generated file that no longer matches its api, without writing
|
|
513
|
+
* anything.
|
|
406
514
|
*
|
|
407
515
|
* This is what a pipeline runs: a generated client that has fallen behind is a
|
|
408
516
|
* compile error waiting to happen in whatever imports it.
|
|
409
517
|
*/
|
|
410
|
-
const
|
|
411
|
-
const current = currentContents(
|
|
518
|
+
const checkFiles = (contract, files) => files.flatMap((file) => {
|
|
519
|
+
const current = currentContents(file.output);
|
|
412
520
|
if (current === void 0) return [{
|
|
413
|
-
|
|
414
|
-
output: client.output,
|
|
521
|
+
output: file.output,
|
|
415
522
|
reason: "missing"
|
|
416
523
|
}];
|
|
417
|
-
if (current !==
|
|
418
|
-
|
|
419
|
-
output: client.output,
|
|
524
|
+
if (current !== file.render(contract)) return [{
|
|
525
|
+
output: file.output,
|
|
420
526
|
reason: "outdated"
|
|
421
527
|
}];
|
|
422
528
|
return [];
|
|
@@ -426,12 +532,12 @@ const checkClients = (contract, clients) => clients.flatMap((client) => {
|
|
|
426
532
|
* rather than printing a diff nobody reads.
|
|
427
533
|
*/
|
|
428
534
|
const formatStale = (stale, command = "kizuna generate") => {
|
|
429
|
-
const lines = stale.map((
|
|
430
|
-
const where = (0, node_path.relative)(process.cwd(),
|
|
431
|
-
return
|
|
535
|
+
const lines = stale.map((file) => {
|
|
536
|
+
const where = (0, node_path.relative)(process.cwd(), file.output);
|
|
537
|
+
return file.reason === "missing" ? ` ${where} has not been generated` : ` ${where} is behind the config`;
|
|
432
538
|
});
|
|
433
539
|
return [
|
|
434
|
-
stale.length === 1 ? "A generated
|
|
540
|
+
stale.length === 1 ? "A generated file is out of date:" : `${stale.length} generated files are out of date:`,
|
|
435
541
|
...lines,
|
|
436
542
|
"",
|
|
437
543
|
`Run \`${command}\` and commit the result.`
|
|
@@ -446,7 +552,7 @@ const formatStale = (stale, command = "kizuna generate") => {
|
|
|
446
552
|
*/
|
|
447
553
|
const CONFIG_KEYS = [
|
|
448
554
|
"adapter",
|
|
449
|
-
"
|
|
555
|
+
"groups",
|
|
450
556
|
"auth",
|
|
451
557
|
"requestContext",
|
|
452
558
|
"validation",
|
|
@@ -555,12 +661,22 @@ const typeOfStringUnion = (expression, key) => {
|
|
|
555
661
|
return codes.length > 0 ? codes.join(" | ") : "never";
|
|
556
662
|
};
|
|
557
663
|
/**
|
|
664
|
+
* The repo's Prettier `printWidth`. A line the generator writes longer than this
|
|
665
|
+
* is one `pnpm format` would rewrite, and `--check` would then report the file
|
|
666
|
+
* as behind.
|
|
667
|
+
*/
|
|
668
|
+
const PRINT_WIDTH = 140;
|
|
669
|
+
/**
|
|
558
670
|
* Each plugin in order, as a tuple, so a handler reaches each one under its own
|
|
559
|
-
* slug.
|
|
671
|
+
* slug. It wraps one plugin per line when the line would run past
|
|
672
|
+
* {@link PRINT_WIDTH}, the way Prettier does.
|
|
560
673
|
*/
|
|
561
|
-
const typeOfPluginList = (expression, used) => {
|
|
674
|
+
const typeOfPluginList = (expression, used, prefix) => {
|
|
562
675
|
if (!typescript.default.isArrayLiteralExpression(expression)) throw new ConfigSyntaxError("`plugins` has to be an array, so each plugin keeps its place.");
|
|
563
|
-
|
|
676
|
+
const entries = expression.elements.map((element) => typeOfValue(element, used, "plugins"));
|
|
677
|
+
const inline = `[${entries.join(", ")}]`;
|
|
678
|
+
if (`${prefix}${inline};`.length <= PRINT_WIDTH) return inline;
|
|
679
|
+
return `[\n${entries.map((entry) => ` ${entry},`).join("\n")}\n ]`;
|
|
564
680
|
};
|
|
565
681
|
/**
|
|
566
682
|
* The `issueCodes` inside `validation`, or nothing when it declares none.
|
|
@@ -657,7 +773,7 @@ const generateConfigTypes = (configSource, fileName = "kizuna.config.ts", output
|
|
|
657
773
|
else if (key === "validation") {
|
|
658
774
|
const codes = issueCodesOf(value);
|
|
659
775
|
if (codes !== void 0) lines.push(` validation: {\n issueCodes: ${codes};\n };`);
|
|
660
|
-
} else if (key === "plugins") lines.push(` ${key}: ${typeOfPluginList(value, used)};`);
|
|
776
|
+
} else if (key === "plugins") lines.push(` ${key}: ${typeOfPluginList(value, used, ` ${key}: `)};`);
|
|
661
777
|
else lines.push(` ${key}: ${typeOfValue(value, used, key)};`);
|
|
662
778
|
}
|
|
663
779
|
const configDir = (0, node_path.dirname)((0, node_path.resolve)(fileName));
|
|
@@ -670,44 +786,59 @@ const generateConfigTypes = (configSource, fileName = "kizuna.config.ts", output
|
|
|
670
786
|
//#region src/diff-against.ts
|
|
671
787
|
const git = (cwd, ...args) => (0, node_child_process.execFileSync)("git", args, {
|
|
672
788
|
cwd,
|
|
673
|
-
encoding: "utf8"
|
|
789
|
+
encoding: "utf8",
|
|
790
|
+
stdio: [
|
|
791
|
+
"ignore",
|
|
792
|
+
"pipe",
|
|
793
|
+
"ignore"
|
|
794
|
+
]
|
|
674
795
|
}).trim();
|
|
675
796
|
/**
|
|
676
|
-
*
|
|
677
|
-
*
|
|
678
|
-
* The ref is checked out into a worktree inside the repository, which costs a
|
|
679
|
-
* few files rather than a clone. Nothing is installed there: module resolution
|
|
680
|
-
* walks up to the repository's own `node_modules`, so the base tree needs only
|
|
681
|
-
* its source.
|
|
682
|
-
*
|
|
683
|
-
* @example
|
|
684
|
-
* const changes = await diffAgainst('main', './src/contract.ts');
|
|
797
|
+
* The repository holding the snapshot, falling back to the one the caller ran in.
|
|
685
798
|
*/
|
|
686
|
-
const
|
|
687
|
-
const { cwd = process.cwd() } = options;
|
|
688
|
-
const root = git(cwd, "rev-parse", "--show-toplevel");
|
|
689
|
-
const absolute = (0, node_path.isAbsolute)(configPath) ? configPath : (0, node_path.resolve)(cwd, configPath);
|
|
690
|
-
const fromRoot = (0, node_path.relative)(root, absolute);
|
|
691
|
-
if (fromRoot.startsWith("..")) throw new Error(`${configPath} is outside the repository at ${root}`);
|
|
692
|
-
const worktree = (0, node_fs.mkdtempSync)((0, node_path.join)(root, ".kizuna-diff-"));
|
|
799
|
+
const repositoryFor = (directory, cwd) => {
|
|
693
800
|
try {
|
|
694
|
-
git(
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
}
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
801
|
+
return git(directory, "rev-parse", "--show-toplevel");
|
|
802
|
+
} catch {
|
|
803
|
+
return git(cwd, "rev-parse", "--show-toplevel");
|
|
804
|
+
}
|
|
805
|
+
};
|
|
806
|
+
const parseSnapshot = (contents, source) => {
|
|
807
|
+
try {
|
|
808
|
+
return (0, yaml.parse)(contents);
|
|
809
|
+
} catch {
|
|
810
|
+
throw new Error(`${source} is not a Kizuna snapshot. Run \`kizuna generate\` to write one.`);
|
|
811
|
+
}
|
|
812
|
+
};
|
|
813
|
+
/**
|
|
814
|
+
* Reads a path out of a ref without checking anything out.
|
|
815
|
+
*/
|
|
816
|
+
const showAtRef = (root, ref, path) => {
|
|
817
|
+
try {
|
|
818
|
+
return git(root, "show", `${ref}:${path}`);
|
|
819
|
+
} catch {
|
|
820
|
+
throw new Error(`${path} does not exist at ${ref}. It was added or renamed since, so there is nothing to compare against.`);
|
|
709
821
|
}
|
|
710
822
|
};
|
|
823
|
+
const readSnapshot = (path) => {
|
|
824
|
+
if (!(0, node_fs.existsSync)(path)) throw new Error(`No snapshot at ${path}. Run \`kizuna generate\` to write one.`);
|
|
825
|
+
return parseSnapshot((0, node_fs.readFileSync)(path, "utf8"), path);
|
|
826
|
+
};
|
|
827
|
+
/**
|
|
828
|
+
* Compares a snapshot against the same file at a git ref.
|
|
829
|
+
*
|
|
830
|
+
* The ref is read with `git show`, so nothing is checked out, installed or
|
|
831
|
+
* executed for the other side. A snapshot written a year ago compares fine
|
|
832
|
+
* against one written today.
|
|
833
|
+
*/
|
|
834
|
+
const diffAgainst = (ref, snapshotPath, options = {}) => {
|
|
835
|
+
const { cwd = process.cwd(), ...diff } = options;
|
|
836
|
+
const absolute = (0, node_path.isAbsolute)(snapshotPath) ? snapshotPath : (0, node_path.resolve)(cwd, snapshotPath);
|
|
837
|
+
const root = repositoryFor((0, node_path.dirname)(absolute), cwd);
|
|
838
|
+
const fromRoot = (0, node_path.relative)(root, absolute);
|
|
839
|
+
if (fromRoot.startsWith("..")) throw new Error(`${snapshotPath} is outside the repository at ${root}`);
|
|
840
|
+
return diffSnapshots(parseSnapshot(showAtRef(root, ref, fromRoot.split("\\").join("/")), `${fromRoot} at ${ref}`), readSnapshot(absolute), diff);
|
|
841
|
+
};
|
|
711
842
|
//#endregion
|
|
712
843
|
//#region src/route-map.ts
|
|
713
844
|
const permissionList = (requires) => {
|
|
@@ -738,11 +869,11 @@ const describeSunset = (sunset) => {
|
|
|
738
869
|
/**
|
|
739
870
|
* Every route an api serves, in the order it declares them.
|
|
740
871
|
*/
|
|
741
|
-
const routeMap = (
|
|
872
|
+
const routeMap = (api, _options = {}) => {
|
|
742
873
|
const entries = [];
|
|
743
874
|
const toolKeys = [];
|
|
744
|
-
return {
|
|
745
|
-
processRoute({ routeKey, route,
|
|
875
|
+
return (0, kizunajs_generator.walkApi)(api, {
|
|
876
|
+
processRoute({ routeKey, route, routeGroups, deprecated, deprecationMessage, hidden }) {
|
|
746
877
|
if (route.tool) toolKeys.push({
|
|
747
878
|
key: routeKey,
|
|
748
879
|
origin: "route"
|
|
@@ -752,9 +883,10 @@ const routeMap = (0, kizunajs_generator.createGenerator)((_options, _api) => {
|
|
|
752
883
|
method: route.method,
|
|
753
884
|
path: route.path,
|
|
754
885
|
auth: describeAuth(route.auth),
|
|
755
|
-
|
|
886
|
+
groups: routeGroups,
|
|
756
887
|
deprecated: deprecated ? deprecationMessage ?? "" : void 0,
|
|
757
|
-
sunset: describeSunset(route.sunset)
|
|
888
|
+
sunset: describeSunset(route.sunset),
|
|
889
|
+
...hidden ? { hidden: true } : {}
|
|
758
890
|
});
|
|
759
891
|
},
|
|
760
892
|
finalize() {
|
|
@@ -764,8 +896,8 @@ const routeMap = (0, kizunajs_generator.createGenerator)((_options, _api) => {
|
|
|
764
896
|
tool: names.get(entry.key) ?? (toolKeys.some((tool) => tool.key === entry.key) ? (0, kizunajs_generator.toToolName)(entry.key) : void 0)
|
|
765
897
|
}));
|
|
766
898
|
}
|
|
767
|
-
};
|
|
768
|
-
}
|
|
899
|
+
});
|
|
900
|
+
};
|
|
769
901
|
const pad = (text, width) => text + " ".repeat(Math.max(0, width - text.length));
|
|
770
902
|
/**
|
|
771
903
|
* The route map as aligned columns, one route per line.
|
|
@@ -778,9 +910,11 @@ const formatRoutes = (entries) => {
|
|
|
778
910
|
return entries.map((entry) => {
|
|
779
911
|
const notes = [
|
|
780
912
|
`auth: ${entry.auth}`,
|
|
913
|
+
entry.groups.length > 0 && `group: ${entry.groups.join(", ")}`,
|
|
781
914
|
entry.tool && `tool: ${entry.tool}`,
|
|
782
915
|
entry.deprecated !== void 0 && `deprecated${entry.deprecated ? `, ${entry.deprecated}` : ""}`,
|
|
783
|
-
entry.sunset && `sunset ${entry.sunset}
|
|
916
|
+
entry.sunset && `sunset ${entry.sunset}`,
|
|
917
|
+
entry.hidden && "hidden"
|
|
784
918
|
].filter(Boolean);
|
|
785
919
|
return `${pad(entry.method, methodWidth)} ${pad(entry.path, pathWidth)} ${pad(entry.key, keyWidth)} ${notes.join(" ")}`.trimEnd();
|
|
786
920
|
}).join("\n");
|
|
@@ -798,10 +932,10 @@ Object.defineProperty(exports, "__toESM", {
|
|
|
798
932
|
return __toESM;
|
|
799
933
|
}
|
|
800
934
|
});
|
|
801
|
-
Object.defineProperty(exports, "
|
|
935
|
+
Object.defineProperty(exports, "checkFiles", {
|
|
802
936
|
enumerable: true,
|
|
803
937
|
get: function() {
|
|
804
|
-
return
|
|
938
|
+
return checkFiles;
|
|
805
939
|
}
|
|
806
940
|
});
|
|
807
941
|
Object.defineProperty(exports, "diffAgainst", {
|
|
@@ -822,6 +956,12 @@ Object.defineProperty(exports, "diffSchemas", {
|
|
|
822
956
|
return diffSchemas;
|
|
823
957
|
}
|
|
824
958
|
});
|
|
959
|
+
Object.defineProperty(exports, "diffSnapshots", {
|
|
960
|
+
enumerable: true,
|
|
961
|
+
get: function() {
|
|
962
|
+
return diffSnapshots;
|
|
963
|
+
}
|
|
964
|
+
});
|
|
825
965
|
Object.defineProperty(exports, "formatChange", {
|
|
826
966
|
enumerable: true,
|
|
827
967
|
get: function() {
|
|
@@ -858,15 +998,33 @@ Object.defineProperty(exports, "loadConfig", {
|
|
|
858
998
|
return loadConfig;
|
|
859
999
|
}
|
|
860
1000
|
});
|
|
1001
|
+
Object.defineProperty(exports, "readSnapshot", {
|
|
1002
|
+
enumerable: true,
|
|
1003
|
+
get: function() {
|
|
1004
|
+
return readSnapshot;
|
|
1005
|
+
}
|
|
1006
|
+
});
|
|
861
1007
|
Object.defineProperty(exports, "routeMap", {
|
|
862
1008
|
enumerable: true,
|
|
863
1009
|
get: function() {
|
|
864
1010
|
return routeMap;
|
|
865
1011
|
}
|
|
866
1012
|
});
|
|
867
|
-
Object.defineProperty(exports, "
|
|
1013
|
+
Object.defineProperty(exports, "snapshotPathFor", {
|
|
1014
|
+
enumerable: true,
|
|
1015
|
+
get: function() {
|
|
1016
|
+
return snapshotPathFor;
|
|
1017
|
+
}
|
|
1018
|
+
});
|
|
1019
|
+
Object.defineProperty(exports, "snapshotTarget", {
|
|
1020
|
+
enumerable: true,
|
|
1021
|
+
get: function() {
|
|
1022
|
+
return snapshotTarget;
|
|
1023
|
+
}
|
|
1024
|
+
});
|
|
1025
|
+
Object.defineProperty(exports, "writeFiles", {
|
|
868
1026
|
enumerable: true,
|
|
869
1027
|
get: function() {
|
|
870
|
-
return
|
|
1028
|
+
return writeFiles;
|
|
871
1029
|
}
|
|
872
1030
|
});
|