@kizunajs/cli 2.0.0-beta.4 → 2.0.0-beta.40
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 +57 -12
- package/dist/cli.mjs +57 -12
- package/dist/index.cjs +13 -12
- package/dist/index.d.cts +56 -23
- package/dist/index.d.mts +56 -23
- package/dist/index.mjs +13 -12
- package/dist/{route-map-yRIISgnC.cjs → route-map-BMcSul55.cjs} +202 -68
- package/dist/{route-map-CRWVBQGf.mjs → route-map-C-GvgV1C.mjs} +200 -66
- package/package.json +9 -9
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { _ as loadConfig, a as ConfigSyntaxError, c as formatStale, f as formatChange, g as diffSchemas, l as
|
|
2
|
-
import {
|
|
1
|
+
import { _ as loadConfig, a as ConfigSyntaxError, c as formatStale, f as formatChange, g as diffSchemas, l as writeFiles, n as routeMap, o as generateConfigTypes, p as hasBreakingChange, r as diffAgainst, s as checkFiles, t as formatRoutes, u as diffApis } from "./route-map-C-GvgV1C.mjs";
|
|
2
|
+
import { walkApi } from "kizunajs/generator";
|
|
3
3
|
//#region src/api-notices.ts
|
|
4
4
|
const readSunset = (route) => {
|
|
5
5
|
const declared = route.sunset;
|
|
@@ -7,14 +7,7 @@ const readSunset = (route) => {
|
|
|
7
7
|
return typeof declared === "string" ? declared : declared.date;
|
|
8
8
|
};
|
|
9
9
|
const daysBetween = (from, to) => Math.ceil((to.getTime() - from.getTime()) / 864e5);
|
|
10
|
-
|
|
11
|
-
* Every route that is deprecated or has a sunset date, soonest first, with
|
|
12
|
-
* undated deprecations last.
|
|
13
|
-
*
|
|
14
|
-
* Reads the same `deprecated` field the generated clients carry in an
|
|
15
|
-
* editor, so a watcher and a hover agree.
|
|
16
|
-
*/
|
|
17
|
-
const apiNotices = createGenerator((options) => {
|
|
10
|
+
const noticesWalk = (options) => {
|
|
18
11
|
const now = options.now ?? /* @__PURE__ */ new Date();
|
|
19
12
|
const notices = [];
|
|
20
13
|
return {
|
|
@@ -41,7 +34,15 @@ const apiNotices = createGenerator((options) => {
|
|
|
41
34
|
});
|
|
42
35
|
}
|
|
43
36
|
};
|
|
44
|
-
}
|
|
37
|
+
};
|
|
38
|
+
/**
|
|
39
|
+
* Every route that is deprecated or has a sunset date, soonest first, with
|
|
40
|
+
* undated deprecations last.
|
|
41
|
+
*
|
|
42
|
+
* Reads the same `deprecated` field the generated clients carry in an
|
|
43
|
+
* editor, so a watcher and a hover agree.
|
|
44
|
+
*/
|
|
45
|
+
const apiNotices = (api, options = {}) => walkApi(api, noticesWalk(options));
|
|
45
46
|
/**
|
|
46
47
|
* One line per notice, for a watcher to print.
|
|
47
48
|
*
|
|
@@ -61,4 +62,4 @@ const formatNotice = (notice) => {
|
|
|
61
62
|
return `${notice.method} ${notice.path} ${notice.routeKey} ${parts.join(", ")}${suffix}`;
|
|
62
63
|
};
|
|
63
64
|
//#endregion
|
|
64
|
-
export { ConfigSyntaxError, apiNotices,
|
|
65
|
+
export { ConfigSyntaxError, apiNotices, checkFiles, diffAgainst, diffApis, diffSchemas, formatChange, formatNotice, formatRoutes, formatStale, generateConfigTypes, hasBreakingChange, loadConfig, routeMap, writeFiles };
|
|
@@ -48,7 +48,9 @@ const loadConfig = async (configPath) => {
|
|
|
48
48
|
name,
|
|
49
49
|
api: entry.api,
|
|
50
50
|
clients: entry.clients ?? [],
|
|
51
|
+
generators: entry.generators ?? [],
|
|
51
52
|
typesOutput: entry.typescript?.outputFile,
|
|
53
|
+
typesExtensions: entry.typescript?.extend ?? [],
|
|
52
54
|
diff: entry.diff ?? {}
|
|
53
55
|
}));
|
|
54
56
|
};
|
|
@@ -58,12 +60,31 @@ const joinPath = (parent, key) => parent === "" ? key : `${parent}.${key}`;
|
|
|
58
60
|
const fieldsOf = (schema) => schema.kind === "object" ? schema.fields : void 0;
|
|
59
61
|
const elementOf = (schema) => schema.kind === "array" ? schema.element : void 0;
|
|
60
62
|
const valuesOf = (schema) => schema.kind === "enum" ? schema.values : void 0;
|
|
63
|
+
const modelOf = (schema) => schema.kind === "model" ? schema.model : void 0;
|
|
64
|
+
const isExtensible = (schema) => schema.extensible === true;
|
|
65
|
+
const optionsOf = (schema) => schema.kind === "union" ? schema.options : void 0;
|
|
61
66
|
/**
|
|
62
|
-
*
|
|
67
|
+
* Whether every value `narrow` allows is one `wide` allows too. A union holds
|
|
68
|
+
* whatever one of its options holds.
|
|
69
|
+
*/
|
|
70
|
+
const holds = (wide, narrow) => {
|
|
71
|
+
const narrowOptions = optionsOf(narrow);
|
|
72
|
+
if (narrowOptions) return narrowOptions.every((option) => holds(wide, option));
|
|
73
|
+
const wideOptions = optionsOf(wide);
|
|
74
|
+
if (wideOptions && wide.kind !== narrow.kind) return wideOptions.some((option) => holds(option, narrow));
|
|
75
|
+
if (wide.kind !== narrow.kind || modelOf(wide) !== modelOf(narrow)) return false;
|
|
76
|
+
if (narrow.nullable === true && wide.nullable !== true) return false;
|
|
77
|
+
return diffSchemas(narrow, wide, "request").every((change) => !change.breaking);
|
|
78
|
+
};
|
|
79
|
+
/**
|
|
80
|
+
* What a reader sees when the kinds differ, so `array` reads as `array<string>` and a union as `string | number`.
|
|
63
81
|
*/
|
|
64
82
|
const describeKind = (schema) => {
|
|
65
83
|
const element = elementOf(schema);
|
|
66
|
-
|
|
84
|
+
if (element) return `array<${describeKind(element)}>`;
|
|
85
|
+
const options = optionsOf(schema);
|
|
86
|
+
if (options) return options.map(describeKind).join(" | ");
|
|
87
|
+
return modelOf(schema) ?? schema.kind;
|
|
67
88
|
};
|
|
68
89
|
/**
|
|
69
90
|
* What changed between two versions of one schema, and whether it breaks the
|
|
@@ -81,10 +102,39 @@ const diffSchemas = (before, after, direction, path = "") => {
|
|
|
81
102
|
path,
|
|
82
103
|
summary: `${path || "the body"} is gone`
|
|
83
104
|
}];
|
|
105
|
+
const kindChanged = before.kind !== after.kind || modelOf(before) !== modelOf(after);
|
|
106
|
+
const bothUnions = optionsOf(before) !== void 0 && optionsOf(after) !== void 0;
|
|
107
|
+
if (kindChanged || bothUnions) {
|
|
108
|
+
const label = path || "the body";
|
|
109
|
+
const widened = holds(after, before);
|
|
110
|
+
const narrowed = holds(before, after);
|
|
111
|
+
if (!kindChanged && widened && narrowed) return [];
|
|
112
|
+
if (direction === "response" && widened && !kindChanged && isExtensible(after) || (direction === "request" ? widened : narrowed)) return [{
|
|
113
|
+
breaking: false,
|
|
114
|
+
path,
|
|
115
|
+
summary: `${label} ${direction === "request" ? "widens" : "narrows"} from ${describeKind(before)} to ${describeKind(after)}`,
|
|
116
|
+
detail: "the generated client type changes, the HTTP surface does not"
|
|
117
|
+
}];
|
|
118
|
+
return [{
|
|
119
|
+
breaking: true,
|
|
120
|
+
path,
|
|
121
|
+
summary: `${label} is ${describeKind(after)} instead of ${describeKind(before)}`
|
|
122
|
+
}];
|
|
123
|
+
}
|
|
84
124
|
const changes = [];
|
|
125
|
+
const wasNullable = before.nullable === true;
|
|
126
|
+
const isNullable = after.nullable === true;
|
|
127
|
+
if (wasNullable !== isNullable) changes.push({
|
|
128
|
+
breaking: direction === (isNullable ? "response" : "request"),
|
|
129
|
+
path,
|
|
130
|
+
summary: isNullable ? `${path || "the body"} can now be null` : `${path || "the body"} can no longer be null`
|
|
131
|
+
});
|
|
85
132
|
const beforeElement = elementOf(before);
|
|
86
133
|
const afterElement = elementOf(after);
|
|
87
|
-
if (beforeElement && afterElement)
|
|
134
|
+
if (beforeElement && afterElement) {
|
|
135
|
+
changes.push(...diffSchemas(beforeElement, afterElement, direction, `${path}[]`));
|
|
136
|
+
return changes;
|
|
137
|
+
}
|
|
88
138
|
const beforeFields = fieldsOf(before);
|
|
89
139
|
const afterFields = fieldsOf(after);
|
|
90
140
|
if (beforeFields && afterFields) {
|
|
@@ -104,6 +154,11 @@ const diffSchemas = (before, after, direction, path = "") => {
|
|
|
104
154
|
path: here,
|
|
105
155
|
summary: `${here} is no longer optional`
|
|
106
156
|
});
|
|
157
|
+
if (!existing.optional && field.optional) changes.push({
|
|
158
|
+
breaking: direction === "response",
|
|
159
|
+
path: here,
|
|
160
|
+
summary: direction === "response" ? `${here} may now be left out` : `${here} is now optional`
|
|
161
|
+
});
|
|
107
162
|
changes.push(...diffSchemas(existing.schema, field.schema, direction, here));
|
|
108
163
|
}
|
|
109
164
|
for (const key of Object.keys(beforeFields)) {
|
|
@@ -117,11 +172,6 @@ const diffSchemas = (before, after, direction, path = "") => {
|
|
|
117
172
|
}
|
|
118
173
|
return changes;
|
|
119
174
|
}
|
|
120
|
-
if (before.kind !== after.kind) return [{
|
|
121
|
-
breaking: true,
|
|
122
|
-
path,
|
|
123
|
-
summary: `${path || "the body"} is ${describeKind(after)} instead of ${describeKind(before)}`
|
|
124
|
-
}];
|
|
125
175
|
const was = valuesOf(before);
|
|
126
176
|
const now = valuesOf(after);
|
|
127
177
|
if (was && now) {
|
|
@@ -133,7 +183,7 @@ const diffSchemas = (before, after, direction, path = "") => {
|
|
|
133
183
|
summary: `${path || "the body"} no longer accepts ${removed.join(", ")}`
|
|
134
184
|
});
|
|
135
185
|
if (added.length > 0) changes.push({
|
|
136
|
-
breaking: direction === "response",
|
|
186
|
+
breaking: direction === "response" && !isExtensible(after),
|
|
137
187
|
path,
|
|
138
188
|
summary: direction === "response" ? `${path || "the body"} can now be ${added.join(", ")}` : `${path || "the body"} also accepts ${added.join(", ")}`
|
|
139
189
|
});
|
|
@@ -160,44 +210,75 @@ const refinementCount = (schema) => {
|
|
|
160
210
|
const custom = checks.filter((check) => check._zod?.def?.check === "custom").length;
|
|
161
211
|
return custom === 0 ? void 0 : custom;
|
|
162
212
|
};
|
|
213
|
+
const extensibleOf = (schema) => (0, kizunajs_generator.readMetaExtensible)(schema) ? { extensible: true } : {};
|
|
163
214
|
/**
|
|
164
|
-
* A Zod schema reduced to the shape a comparison reads.
|
|
215
|
+
* A Zod schema reduced to the shape a comparison reads. A model that holds
|
|
216
|
+
* itself is written out once, and inside itself as a reference by its title.
|
|
217
|
+
*
|
|
218
|
+
* `inside` is the objects the walk is in, so it can tell when it has gone round.
|
|
165
219
|
*/
|
|
166
|
-
const toSchemaNode = (schema) => {
|
|
167
|
-
const { inner } = (0, kizunajs_generator.unwrapOptionalWrappers)(schema);
|
|
220
|
+
const toSchemaNode = (schema, inside = []) => {
|
|
221
|
+
const { inner, nullable } = (0, kizunajs_generator.unwrapOptionalWrappers)(schema);
|
|
168
222
|
const def = (0, kizunajs_generator.readDef)(inner);
|
|
223
|
+
const nullability = nullable ? { nullable: true } : {};
|
|
224
|
+
const identity = (0, kizunajs_generator.readModelSchema)(inner) ?? inner;
|
|
225
|
+
if (inside.includes(identity)) return {
|
|
226
|
+
kind: "model",
|
|
227
|
+
...nullability,
|
|
228
|
+
model: (0, kizunajs_generator.readMetaId)(inner) ?? ""
|
|
229
|
+
};
|
|
230
|
+
const options = def.type === "union" ? def.options ?? [] : [];
|
|
231
|
+
const nonNull = options.filter((option) => (0, kizunajs_generator.readDef)(option).type !== "null");
|
|
232
|
+
if (nonNull.length === 1 && options.length > 1) return {
|
|
233
|
+
...toSchemaNode(nonNull[0], inside),
|
|
234
|
+
nullable: true
|
|
235
|
+
};
|
|
169
236
|
const refinements = refinementCount(inner);
|
|
170
237
|
const carry = refinements === void 0 ? {} : { refinements };
|
|
171
238
|
const shape = (0, kizunajs_generator.readObjectShape)(inner);
|
|
172
239
|
if (shape) {
|
|
173
240
|
const fields = {};
|
|
241
|
+
const within = [...inside, identity];
|
|
174
242
|
for (const [key, field] of Object.entries(shape)) fields[key] = {
|
|
175
243
|
optional: (0, kizunajs_generator.unwrapOptionalWrappers)(field).optional,
|
|
176
|
-
schema: toSchemaNode(field)
|
|
244
|
+
schema: toSchemaNode(field, within)
|
|
177
245
|
};
|
|
178
246
|
return {
|
|
179
247
|
kind: "object",
|
|
248
|
+
...nullability,
|
|
180
249
|
fields,
|
|
181
250
|
...carry
|
|
182
251
|
};
|
|
183
252
|
}
|
|
184
253
|
if (def.type === "array" && def.element) return {
|
|
185
254
|
kind: "array",
|
|
186
|
-
|
|
255
|
+
...nullability,
|
|
256
|
+
element: toSchemaNode(def.element, inside),
|
|
187
257
|
...carry
|
|
188
258
|
};
|
|
189
259
|
if (def.type === "enum") return {
|
|
190
260
|
kind: "enum",
|
|
261
|
+
...nullability,
|
|
191
262
|
values: Object.values(def.entries ?? {}).map(String).sort(),
|
|
263
|
+
...extensibleOf(inner),
|
|
192
264
|
...carry
|
|
193
265
|
};
|
|
194
266
|
if (def.type === "literal") return {
|
|
195
267
|
kind: "enum",
|
|
268
|
+
...nullability,
|
|
196
269
|
values: (def.values ?? []).map(String).sort(),
|
|
197
270
|
...carry
|
|
198
271
|
};
|
|
272
|
+
if (def.type === "union") return {
|
|
273
|
+
kind: "union",
|
|
274
|
+
...nullability,
|
|
275
|
+
options: options.map((option) => toSchemaNode(option, inside)),
|
|
276
|
+
...extensibleOf(inner),
|
|
277
|
+
...carry
|
|
278
|
+
};
|
|
199
279
|
return {
|
|
200
280
|
kind: def.type ?? "unknown",
|
|
281
|
+
...nullability,
|
|
201
282
|
...carry
|
|
202
283
|
};
|
|
203
284
|
};
|
|
@@ -206,10 +287,10 @@ const sunsetOf = (route) => {
|
|
|
206
287
|
if (declared === void 0) return void 0;
|
|
207
288
|
return typeof declared === "string" ? declared : declared.date;
|
|
208
289
|
};
|
|
209
|
-
const routeSnapshots = (
|
|
290
|
+
const routeSnapshots = (api) => {
|
|
210
291
|
const routes = {};
|
|
211
|
-
return {
|
|
212
|
-
processRoute({ routeKey, route, deprecated }) {
|
|
292
|
+
return (0, kizunajs_generator.walkApi)(api, {
|
|
293
|
+
processRoute({ routeKey, route, deprecated, hidden }) {
|
|
213
294
|
const sunset = sunsetOf(route);
|
|
214
295
|
const responses = {};
|
|
215
296
|
for (const [status, response] of Object.entries(route.responses)) {
|
|
@@ -221,6 +302,7 @@ const routeSnapshots = (0, kizunajs_generator.createGenerator)(() => {
|
|
|
221
302
|
path: route.path,
|
|
222
303
|
statuses: Object.keys(route.responses).map(Number).sort((a, b) => a - b),
|
|
223
304
|
deprecated,
|
|
305
|
+
...hidden ? { hidden: true } : {},
|
|
224
306
|
...sunset === void 0 ? {} : { sunset },
|
|
225
307
|
...route.body ? { body: toSchemaNode(route.body) } : {},
|
|
226
308
|
...route.query ? { query: toSchemaNode(route.query) } : {},
|
|
@@ -229,8 +311,8 @@ const routeSnapshots = (0, kizunajs_generator.createGenerator)(() => {
|
|
|
229
311
|
};
|
|
230
312
|
},
|
|
231
313
|
finalize: () => routes
|
|
232
|
-
};
|
|
233
|
-
}
|
|
314
|
+
});
|
|
315
|
+
};
|
|
234
316
|
/**
|
|
235
317
|
* Everything `kizuna diff` compares, as plain data.
|
|
236
318
|
*
|
|
@@ -242,7 +324,7 @@ const toSnapshot = (api) => {
|
|
|
242
324
|
const jobs = declared ? (0, kizunajs_generator.flattenJobs)(declared).map((job) => job.jobKey).sort() : [];
|
|
243
325
|
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)]));
|
|
244
326
|
return {
|
|
245
|
-
routes: routeSnapshots(api
|
|
327
|
+
routes: routeSnapshots(api),
|
|
246
328
|
jobs,
|
|
247
329
|
tools
|
|
248
330
|
};
|
|
@@ -259,7 +341,7 @@ const renderSnapshot = (api) => (0, yaml.stringify)(toSnapshot(api), { lineWidth
|
|
|
259
341
|
*/
|
|
260
342
|
const snapshotPathFor = (configPath) => {
|
|
261
343
|
const declared = (0, node_path.basename)(configPath).replace(/\.config\.[cm]?[jt]s$/, "").replace(/^kizuna\./, "");
|
|
262
|
-
const name = declared === "" || declared === "kizuna" ? "
|
|
344
|
+
const name = declared === "" || declared === "kizuna" ? "snapshot" : `${declared}.snapshot`;
|
|
263
345
|
return (0, node_path.join)((0, node_path.dirname)(configPath), ".kizuna", `${name}.yaml`);
|
|
264
346
|
};
|
|
265
347
|
/**
|
|
@@ -267,9 +349,8 @@ const snapshotPathFor = (configPath) => {
|
|
|
267
349
|
* `kizuna generate --check` refuses to let it go stale.
|
|
268
350
|
*/
|
|
269
351
|
const snapshotTarget = (configPath) => ({
|
|
270
|
-
kind: "diff",
|
|
271
352
|
output: snapshotPathFor(configPath),
|
|
272
|
-
|
|
353
|
+
render: renderSnapshot
|
|
273
354
|
});
|
|
274
355
|
//#endregion
|
|
275
356
|
//#region src/diff-apis.ts
|
|
@@ -367,7 +448,8 @@ const diffSnapshots = (before, after, options = {}) => {
|
|
|
367
448
|
for (const [direction, label, was, now] of inputs) for (const change of diffSchemas(was, now, direction, label)) changes.push({
|
|
368
449
|
level: change.breaking ? "breaking" : "changed",
|
|
369
450
|
key,
|
|
370
|
-
summary: `${httpLabel(arrived, key)} ${change.summary}
|
|
451
|
+
summary: `${httpLabel(arrived, key)} ${change.summary}`,
|
|
452
|
+
...change.detail === void 0 ? {} : { detail: change.detail }
|
|
371
453
|
});
|
|
372
454
|
for (const [status, was] of Object.entries(gone.responses)) {
|
|
373
455
|
if (!(status in arrived.responses)) continue;
|
|
@@ -375,9 +457,21 @@ const diffSnapshots = (before, after, options = {}) => {
|
|
|
375
457
|
for (const change of diffSchemas(was ?? void 0, now ?? void 0, "response", status)) changes.push({
|
|
376
458
|
level: change.breaking ? "breaking" : "changed",
|
|
377
459
|
key,
|
|
378
|
-
summary: `${httpLabel(arrived, key)} ${change.summary}
|
|
460
|
+
summary: `${httpLabel(arrived, key)} ${change.summary}`,
|
|
461
|
+
...change.detail === void 0 ? {} : { detail: change.detail }
|
|
379
462
|
});
|
|
380
463
|
}
|
|
464
|
+
if (gone.hidden !== true && arrived.hidden === true) changes.push({
|
|
465
|
+
level: "breaking",
|
|
466
|
+
key,
|
|
467
|
+
summary: `${httpLabel(arrived, key)} is now hidden`,
|
|
468
|
+
detail: "the server still serves it, but it is no longer in the generated clients or the OpenAPI document"
|
|
469
|
+
});
|
|
470
|
+
if (gone.hidden === true && arrived.hidden !== true) changes.push({
|
|
471
|
+
level: "added",
|
|
472
|
+
key,
|
|
473
|
+
summary: `${httpLabel(arrived, key)} is no longer hidden`
|
|
474
|
+
});
|
|
381
475
|
if (!gone.deprecated && arrived.deprecated) changes.push({
|
|
382
476
|
level: "changed",
|
|
383
477
|
key,
|
|
@@ -453,38 +547,37 @@ const currentContents = (output) => {
|
|
|
453
547
|
}
|
|
454
548
|
};
|
|
455
549
|
/**
|
|
456
|
-
* Writes every
|
|
457
|
-
* matches so watchers and build tools see no
|
|
550
|
+
* Writes every generated file, the clients and what plugins generate, leaving
|
|
551
|
+
* a file alone when it already matches so watchers and build tools see no
|
|
552
|
+
* change.
|
|
458
553
|
*/
|
|
459
|
-
const
|
|
460
|
-
const rendered =
|
|
461
|
-
const changed = currentContents(
|
|
554
|
+
const writeFiles = (contract, files) => files.map((file) => {
|
|
555
|
+
const rendered = file.render(contract);
|
|
556
|
+
const changed = currentContents(file.output) !== rendered;
|
|
462
557
|
if (changed) {
|
|
463
|
-
(0, node_fs.mkdirSync)((0, node_path.dirname)(
|
|
464
|
-
(0, node_fs.writeFileSync)(
|
|
558
|
+
(0, node_fs.mkdirSync)((0, node_path.dirname)(file.output), { recursive: true });
|
|
559
|
+
(0, node_fs.writeFileSync)(file.output, rendered);
|
|
465
560
|
}
|
|
466
561
|
return {
|
|
467
|
-
|
|
468
|
-
output: client.output,
|
|
562
|
+
output: file.output,
|
|
469
563
|
changed
|
|
470
564
|
};
|
|
471
565
|
});
|
|
472
566
|
/**
|
|
473
|
-
* Every
|
|
567
|
+
* Every generated file that no longer matches its api, without writing
|
|
568
|
+
* anything.
|
|
474
569
|
*
|
|
475
570
|
* This is what a pipeline runs: a generated client that has fallen behind is a
|
|
476
571
|
* compile error waiting to happen in whatever imports it.
|
|
477
572
|
*/
|
|
478
|
-
const
|
|
479
|
-
const current = currentContents(
|
|
573
|
+
const checkFiles = (contract, files) => files.flatMap((file) => {
|
|
574
|
+
const current = currentContents(file.output);
|
|
480
575
|
if (current === void 0) return [{
|
|
481
|
-
|
|
482
|
-
output: client.output,
|
|
576
|
+
output: file.output,
|
|
483
577
|
reason: "missing"
|
|
484
578
|
}];
|
|
485
|
-
if (current !==
|
|
486
|
-
|
|
487
|
-
output: client.output,
|
|
579
|
+
if (current !== file.render(contract)) return [{
|
|
580
|
+
output: file.output,
|
|
488
581
|
reason: "outdated"
|
|
489
582
|
}];
|
|
490
583
|
return [];
|
|
@@ -494,9 +587,9 @@ const checkClients = (contract, clients) => clients.flatMap((client) => {
|
|
|
494
587
|
* rather than printing a diff nobody reads.
|
|
495
588
|
*/
|
|
496
589
|
const formatStale = (stale, command = "kizuna generate") => {
|
|
497
|
-
const lines = stale.map((
|
|
498
|
-
const where = (0, node_path.relative)(process.cwd(),
|
|
499
|
-
return
|
|
590
|
+
const lines = stale.map((file) => {
|
|
591
|
+
const where = (0, node_path.relative)(process.cwd(), file.output);
|
|
592
|
+
return file.reason === "missing" ? ` ${where} has not been generated` : ` ${where} is behind the config`;
|
|
500
593
|
});
|
|
501
594
|
return [
|
|
502
595
|
stale.length === 1 ? "A generated file is out of date:" : `${stale.length} generated files are out of date:`,
|
|
@@ -514,7 +607,7 @@ const formatStale = (stale, command = "kizuna generate") => {
|
|
|
514
607
|
*/
|
|
515
608
|
const CONFIG_KEYS = [
|
|
516
609
|
"adapter",
|
|
517
|
-
"
|
|
610
|
+
"groups",
|
|
518
611
|
"auth",
|
|
519
612
|
"requestContext",
|
|
520
613
|
"validation",
|
|
@@ -623,12 +716,22 @@ const typeOfStringUnion = (expression, key) => {
|
|
|
623
716
|
return codes.length > 0 ? codes.join(" | ") : "never";
|
|
624
717
|
};
|
|
625
718
|
/**
|
|
719
|
+
* The repo's Prettier `printWidth`. A line the generator writes longer than this
|
|
720
|
+
* is one `pnpm format` would rewrite, and `--check` would then report the file
|
|
721
|
+
* as behind.
|
|
722
|
+
*/
|
|
723
|
+
const PRINT_WIDTH = 140;
|
|
724
|
+
/**
|
|
626
725
|
* Each plugin in order, as a tuple, so a handler reaches each one under its own
|
|
627
|
-
* slug.
|
|
726
|
+
* slug. It wraps one plugin per line when the line would run past
|
|
727
|
+
* {@link PRINT_WIDTH}, the way Prettier does.
|
|
628
728
|
*/
|
|
629
|
-
const typeOfPluginList = (expression, used) => {
|
|
729
|
+
const typeOfPluginList = (expression, used, prefix) => {
|
|
630
730
|
if (!typescript.default.isArrayLiteralExpression(expression)) throw new ConfigSyntaxError("`plugins` has to be an array, so each plugin keeps its place.");
|
|
631
|
-
|
|
731
|
+
const entries = expression.elements.map((element) => typeOfValue(element, used, "plugins"));
|
|
732
|
+
const inline = `[${entries.join(", ")}]`;
|
|
733
|
+
if (`${prefix}${inline};`.length <= PRINT_WIDTH) return inline;
|
|
734
|
+
return `[\n${entries.map((entry) => ` ${entry},`).join("\n")}\n ]`;
|
|
632
735
|
};
|
|
633
736
|
/**
|
|
634
737
|
* The `issueCodes` inside `validation`, or nothing when it declares none.
|
|
@@ -654,10 +757,35 @@ const authLines = (expression, used) => {
|
|
|
654
757
|
}
|
|
655
758
|
return lines;
|
|
656
759
|
};
|
|
760
|
+
/**
|
|
761
|
+
* A string as Prettier writes it: single quotes, unless the string holds more
|
|
762
|
+
* of them than double quotes.
|
|
763
|
+
*/
|
|
764
|
+
const stringLiteral = (value) => {
|
|
765
|
+
const quote = value.split("'").length > value.split("\"").length ? "\"" : "'";
|
|
766
|
+
return `${quote}${value.replaceAll("\\", "\\\\").replaceAll(quote, `\\${quote}`)}${quote}`;
|
|
767
|
+
};
|
|
768
|
+
const propertyName = (name) => /^[A-Za-z_$][\w$]*$/.test(name) ? name : stringLiteral(name);
|
|
769
|
+
/**
|
|
770
|
+
* One entry under `typescript.extend`. A list of strings becomes a
|
|
771
|
+
* union, wrapped one member per line when it would run past
|
|
772
|
+
* {@link PRINT_WIDTH}, the way Prettier does.
|
|
773
|
+
*/
|
|
774
|
+
const typeOfExtension = (values, indent) => {
|
|
775
|
+
const lines = Object.entries(values).map(([key, value]) => {
|
|
776
|
+
const prefix = `${indent} ${propertyName(key)}:`;
|
|
777
|
+
if (!Array.isArray(value)) return `${prefix} ${typeOfExtension(value, `${indent} `)};`;
|
|
778
|
+
const members = [...new Set(value)].map(stringLiteral);
|
|
779
|
+
if (members.length === 0) return `${prefix} never;`;
|
|
780
|
+
const inline = `${prefix} ${members.join(" | ")};`;
|
|
781
|
+
if (members.length === 1 || inline.length <= PRINT_WIDTH) return inline;
|
|
782
|
+
return `${prefix}\n${members.map((member) => `${indent} | ${member}`).join("\n")};`;
|
|
783
|
+
});
|
|
784
|
+
return lines.length === 0 ? "{}" : `{\n${lines.join("\n")}\n${indent}}`;
|
|
785
|
+
};
|
|
657
786
|
const HEADER = `/**
|
|
658
|
-
*
|
|
659
|
-
*
|
|
660
|
-
* and re-run \`kizuna generate\` to regenerate this file.
|
|
787
|
+
* Generated by \`kizuna generate\` from your Kizuna config. DO NOT EDIT MANUALLY.
|
|
788
|
+
* Every edit here is lost on the next run. Change the config and run \`kizuna generate\` again.
|
|
661
789
|
*/`;
|
|
662
790
|
/**
|
|
663
791
|
* Points a relative specifier at the same file from somewhere else, so a
|
|
@@ -696,12 +824,14 @@ const renderImports = (imports, used, rebase) => {
|
|
|
696
824
|
*
|
|
697
825
|
* Read from the config's syntax rather than by running it: the generated file
|
|
698
826
|
* imports the same names the config did, and a config that loads is not a
|
|
699
|
-
* prerequisite for typing the routes it serves.
|
|
827
|
+
* prerequisite for typing the routes it serves. What
|
|
828
|
+
* `typescript.extend` adds exists only once the config has run, so the caller passes
|
|
829
|
+
* it in.
|
|
700
830
|
*
|
|
701
831
|
* `outputPath` is what the imports are written relative to. Left out, they stay
|
|
702
832
|
* relative to the config.
|
|
703
833
|
*/
|
|
704
|
-
const generateConfigTypes = (configSource, fileName = "kizuna.config.ts", outputPath) => {
|
|
834
|
+
const generateConfigTypes = (configSource, fileName = "kizuna.config.ts", outputPath, extensions = []) => {
|
|
705
835
|
const source = typescript.default.createSourceFile(fileName, configSource, typescript.default.ScriptTarget.Latest, true);
|
|
706
836
|
const imports = collectImports(source);
|
|
707
837
|
const configObject = findConfigObject(source);
|
|
@@ -725,9 +855,10 @@ const generateConfigTypes = (configSource, fileName = "kizuna.config.ts", output
|
|
|
725
855
|
else if (key === "validation") {
|
|
726
856
|
const codes = issueCodesOf(value);
|
|
727
857
|
if (codes !== void 0) lines.push(` validation: {\n issueCodes: ${codes};\n };`);
|
|
728
|
-
} else if (key === "plugins") lines.push(` ${key}: ${typeOfPluginList(value, used)};`);
|
|
858
|
+
} else if (key === "plugins") lines.push(` ${key}: ${typeOfPluginList(value, used, ` ${key}: `)};`);
|
|
729
859
|
else lines.push(` ${key}: ${typeOfValue(value, used, key)};`);
|
|
730
860
|
}
|
|
861
|
+
for (const extension of extensions) for (const [name, values] of Object.entries(extension)) lines.push(` ${propertyName(name)}: ${typeOfExtension(values, " ")};`);
|
|
731
862
|
const configDir = (0, node_path.dirname)((0, node_path.resolve)(fileName));
|
|
732
863
|
const outputDir = outputPath === void 0 ? configDir : (0, node_path.dirname)((0, node_path.resolve)(outputPath));
|
|
733
864
|
const importBlock = renderImports(imports, used, (specifier) => outputDir === configDir ? specifier : rebaseSpecifier(specifier, configDir, outputDir));
|
|
@@ -759,7 +890,7 @@ const parseSnapshot = (contents, source) => {
|
|
|
759
890
|
try {
|
|
760
891
|
return (0, yaml.parse)(contents);
|
|
761
892
|
} catch {
|
|
762
|
-
throw new Error(`${source} is not a Kizuna
|
|
893
|
+
throw new Error(`${source} is not a Kizuna snapshot. Run \`kizuna generate\` to write one.`);
|
|
763
894
|
}
|
|
764
895
|
};
|
|
765
896
|
/**
|
|
@@ -821,11 +952,11 @@ const describeSunset = (sunset) => {
|
|
|
821
952
|
/**
|
|
822
953
|
* Every route an api serves, in the order it declares them.
|
|
823
954
|
*/
|
|
824
|
-
const routeMap = (
|
|
955
|
+
const routeMap = (api, _options = {}) => {
|
|
825
956
|
const entries = [];
|
|
826
957
|
const toolKeys = [];
|
|
827
|
-
return {
|
|
828
|
-
processRoute({ routeKey, route,
|
|
958
|
+
return (0, kizunajs_generator.walkApi)(api, {
|
|
959
|
+
processRoute({ routeKey, route, routeGroups, deprecated, deprecationMessage, hidden }) {
|
|
829
960
|
if (route.tool) toolKeys.push({
|
|
830
961
|
key: routeKey,
|
|
831
962
|
origin: "route"
|
|
@@ -835,9 +966,10 @@ const routeMap = (0, kizunajs_generator.createGenerator)((_options, _api) => {
|
|
|
835
966
|
method: route.method,
|
|
836
967
|
path: route.path,
|
|
837
968
|
auth: describeAuth(route.auth),
|
|
838
|
-
|
|
969
|
+
groups: routeGroups,
|
|
839
970
|
deprecated: deprecated ? deprecationMessage ?? "" : void 0,
|
|
840
|
-
sunset: describeSunset(route.sunset)
|
|
971
|
+
sunset: describeSunset(route.sunset),
|
|
972
|
+
...hidden ? { hidden: true } : {}
|
|
841
973
|
});
|
|
842
974
|
},
|
|
843
975
|
finalize() {
|
|
@@ -847,8 +979,8 @@ const routeMap = (0, kizunajs_generator.createGenerator)((_options, _api) => {
|
|
|
847
979
|
tool: names.get(entry.key) ?? (toolKeys.some((tool) => tool.key === entry.key) ? (0, kizunajs_generator.toToolName)(entry.key) : void 0)
|
|
848
980
|
}));
|
|
849
981
|
}
|
|
850
|
-
};
|
|
851
|
-
}
|
|
982
|
+
});
|
|
983
|
+
};
|
|
852
984
|
const pad = (text, width) => text + " ".repeat(Math.max(0, width - text.length));
|
|
853
985
|
/**
|
|
854
986
|
* The route map as aligned columns, one route per line.
|
|
@@ -861,9 +993,11 @@ const formatRoutes = (entries) => {
|
|
|
861
993
|
return entries.map((entry) => {
|
|
862
994
|
const notes = [
|
|
863
995
|
`auth: ${entry.auth}`,
|
|
996
|
+
entry.groups.length > 0 && `group: ${entry.groups.join(", ")}`,
|
|
864
997
|
entry.tool && `tool: ${entry.tool}`,
|
|
865
998
|
entry.deprecated !== void 0 && `deprecated${entry.deprecated ? `, ${entry.deprecated}` : ""}`,
|
|
866
|
-
entry.sunset && `sunset ${entry.sunset}
|
|
999
|
+
entry.sunset && `sunset ${entry.sunset}`,
|
|
1000
|
+
entry.hidden && "hidden"
|
|
867
1001
|
].filter(Boolean);
|
|
868
1002
|
return `${pad(entry.method, methodWidth)} ${pad(entry.path, pathWidth)} ${pad(entry.key, keyWidth)} ${notes.join(" ")}`.trimEnd();
|
|
869
1003
|
}).join("\n");
|
|
@@ -881,10 +1015,10 @@ Object.defineProperty(exports, "__toESM", {
|
|
|
881
1015
|
return __toESM;
|
|
882
1016
|
}
|
|
883
1017
|
});
|
|
884
|
-
Object.defineProperty(exports, "
|
|
1018
|
+
Object.defineProperty(exports, "checkFiles", {
|
|
885
1019
|
enumerable: true,
|
|
886
1020
|
get: function() {
|
|
887
|
-
return
|
|
1021
|
+
return checkFiles;
|
|
888
1022
|
}
|
|
889
1023
|
});
|
|
890
1024
|
Object.defineProperty(exports, "diffAgainst", {
|
|
@@ -971,9 +1105,9 @@ Object.defineProperty(exports, "snapshotTarget", {
|
|
|
971
1105
|
return snapshotTarget;
|
|
972
1106
|
}
|
|
973
1107
|
});
|
|
974
|
-
Object.defineProperty(exports, "
|
|
1108
|
+
Object.defineProperty(exports, "writeFiles", {
|
|
975
1109
|
enumerable: true,
|
|
976
1110
|
get: function() {
|
|
977
|
-
return
|
|
1111
|
+
return writeFiles;
|
|
978
1112
|
}
|
|
979
1113
|
});
|