@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
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { createJiti } from "jiti";
|
|
2
2
|
import { apiEntries } from "kizunajs";
|
|
3
|
-
import {
|
|
3
|
+
import { deriveToolNames, flattenJobs, readDef, readMetaExtensible, readMetaId, readModelSchema, readObjectShape, resolveResponseBody, toToolName, unwrapOptionalWrappers, walkApi } from "kizunajs/generator";
|
|
4
4
|
import { basename, dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
|
|
5
5
|
import { parse, stringify } from "yaml";
|
|
6
6
|
import { flattenRoutes } from "kizunajs/adapter";
|
|
@@ -25,7 +25,9 @@ const loadConfig = async (configPath) => {
|
|
|
25
25
|
name,
|
|
26
26
|
api: entry.api,
|
|
27
27
|
clients: entry.clients ?? [],
|
|
28
|
+
generators: entry.generators ?? [],
|
|
28
29
|
typesOutput: entry.typescript?.outputFile,
|
|
30
|
+
typesExtensions: entry.typescript?.extend ?? [],
|
|
29
31
|
diff: entry.diff ?? {}
|
|
30
32
|
}));
|
|
31
33
|
};
|
|
@@ -35,12 +37,31 @@ const joinPath = (parent, key) => parent === "" ? key : `${parent}.${key}`;
|
|
|
35
37
|
const fieldsOf = (schema) => schema.kind === "object" ? schema.fields : void 0;
|
|
36
38
|
const elementOf = (schema) => schema.kind === "array" ? schema.element : void 0;
|
|
37
39
|
const valuesOf = (schema) => schema.kind === "enum" ? schema.values : void 0;
|
|
40
|
+
const modelOf = (schema) => schema.kind === "model" ? schema.model : void 0;
|
|
41
|
+
const isExtensible = (schema) => schema.extensible === true;
|
|
42
|
+
const optionsOf = (schema) => schema.kind === "union" ? schema.options : void 0;
|
|
38
43
|
/**
|
|
39
|
-
*
|
|
44
|
+
* Whether every value `narrow` allows is one `wide` allows too. A union holds
|
|
45
|
+
* whatever one of its options holds.
|
|
46
|
+
*/
|
|
47
|
+
const holds = (wide, narrow) => {
|
|
48
|
+
const narrowOptions = optionsOf(narrow);
|
|
49
|
+
if (narrowOptions) return narrowOptions.every((option) => holds(wide, option));
|
|
50
|
+
const wideOptions = optionsOf(wide);
|
|
51
|
+
if (wideOptions && wide.kind !== narrow.kind) return wideOptions.some((option) => holds(option, narrow));
|
|
52
|
+
if (wide.kind !== narrow.kind || modelOf(wide) !== modelOf(narrow)) return false;
|
|
53
|
+
if (narrow.nullable === true && wide.nullable !== true) return false;
|
|
54
|
+
return diffSchemas(narrow, wide, "request").every((change) => !change.breaking);
|
|
55
|
+
};
|
|
56
|
+
/**
|
|
57
|
+
* What a reader sees when the kinds differ, so `array` reads as `array<string>` and a union as `string | number`.
|
|
40
58
|
*/
|
|
41
59
|
const describeKind = (schema) => {
|
|
42
60
|
const element = elementOf(schema);
|
|
43
|
-
|
|
61
|
+
if (element) return `array<${describeKind(element)}>`;
|
|
62
|
+
const options = optionsOf(schema);
|
|
63
|
+
if (options) return options.map(describeKind).join(" | ");
|
|
64
|
+
return modelOf(schema) ?? schema.kind;
|
|
44
65
|
};
|
|
45
66
|
/**
|
|
46
67
|
* What changed between two versions of one schema, and whether it breaks the
|
|
@@ -58,10 +79,39 @@ const diffSchemas = (before, after, direction, path = "") => {
|
|
|
58
79
|
path,
|
|
59
80
|
summary: `${path || "the body"} is gone`
|
|
60
81
|
}];
|
|
82
|
+
const kindChanged = before.kind !== after.kind || modelOf(before) !== modelOf(after);
|
|
83
|
+
const bothUnions = optionsOf(before) !== void 0 && optionsOf(after) !== void 0;
|
|
84
|
+
if (kindChanged || bothUnions) {
|
|
85
|
+
const label = path || "the body";
|
|
86
|
+
const widened = holds(after, before);
|
|
87
|
+
const narrowed = holds(before, after);
|
|
88
|
+
if (!kindChanged && widened && narrowed) return [];
|
|
89
|
+
if (direction === "response" && widened && !kindChanged && isExtensible(after) || (direction === "request" ? widened : narrowed)) return [{
|
|
90
|
+
breaking: false,
|
|
91
|
+
path,
|
|
92
|
+
summary: `${label} ${direction === "request" ? "widens" : "narrows"} from ${describeKind(before)} to ${describeKind(after)}`,
|
|
93
|
+
detail: "the generated client type changes, the HTTP surface does not"
|
|
94
|
+
}];
|
|
95
|
+
return [{
|
|
96
|
+
breaking: true,
|
|
97
|
+
path,
|
|
98
|
+
summary: `${label} is ${describeKind(after)} instead of ${describeKind(before)}`
|
|
99
|
+
}];
|
|
100
|
+
}
|
|
61
101
|
const changes = [];
|
|
102
|
+
const wasNullable = before.nullable === true;
|
|
103
|
+
const isNullable = after.nullable === true;
|
|
104
|
+
if (wasNullable !== isNullable) changes.push({
|
|
105
|
+
breaking: direction === (isNullable ? "response" : "request"),
|
|
106
|
+
path,
|
|
107
|
+
summary: isNullable ? `${path || "the body"} can now be null` : `${path || "the body"} can no longer be null`
|
|
108
|
+
});
|
|
62
109
|
const beforeElement = elementOf(before);
|
|
63
110
|
const afterElement = elementOf(after);
|
|
64
|
-
if (beforeElement && afterElement)
|
|
111
|
+
if (beforeElement && afterElement) {
|
|
112
|
+
changes.push(...diffSchemas(beforeElement, afterElement, direction, `${path}[]`));
|
|
113
|
+
return changes;
|
|
114
|
+
}
|
|
65
115
|
const beforeFields = fieldsOf(before);
|
|
66
116
|
const afterFields = fieldsOf(after);
|
|
67
117
|
if (beforeFields && afterFields) {
|
|
@@ -81,6 +131,11 @@ const diffSchemas = (before, after, direction, path = "") => {
|
|
|
81
131
|
path: here,
|
|
82
132
|
summary: `${here} is no longer optional`
|
|
83
133
|
});
|
|
134
|
+
if (!existing.optional && field.optional) changes.push({
|
|
135
|
+
breaking: direction === "response",
|
|
136
|
+
path: here,
|
|
137
|
+
summary: direction === "response" ? `${here} may now be left out` : `${here} is now optional`
|
|
138
|
+
});
|
|
84
139
|
changes.push(...diffSchemas(existing.schema, field.schema, direction, here));
|
|
85
140
|
}
|
|
86
141
|
for (const key of Object.keys(beforeFields)) {
|
|
@@ -94,11 +149,6 @@ const diffSchemas = (before, after, direction, path = "") => {
|
|
|
94
149
|
}
|
|
95
150
|
return changes;
|
|
96
151
|
}
|
|
97
|
-
if (before.kind !== after.kind) return [{
|
|
98
|
-
breaking: true,
|
|
99
|
-
path,
|
|
100
|
-
summary: `${path || "the body"} is ${describeKind(after)} instead of ${describeKind(before)}`
|
|
101
|
-
}];
|
|
102
152
|
const was = valuesOf(before);
|
|
103
153
|
const now = valuesOf(after);
|
|
104
154
|
if (was && now) {
|
|
@@ -110,7 +160,7 @@ const diffSchemas = (before, after, direction, path = "") => {
|
|
|
110
160
|
summary: `${path || "the body"} no longer accepts ${removed.join(", ")}`
|
|
111
161
|
});
|
|
112
162
|
if (added.length > 0) changes.push({
|
|
113
|
-
breaking: direction === "response",
|
|
163
|
+
breaking: direction === "response" && !isExtensible(after),
|
|
114
164
|
path,
|
|
115
165
|
summary: direction === "response" ? `${path || "the body"} can now be ${added.join(", ")}` : `${path || "the body"} also accepts ${added.join(", ")}`
|
|
116
166
|
});
|
|
@@ -137,44 +187,75 @@ const refinementCount = (schema) => {
|
|
|
137
187
|
const custom = checks.filter((check) => check._zod?.def?.check === "custom").length;
|
|
138
188
|
return custom === 0 ? void 0 : custom;
|
|
139
189
|
};
|
|
190
|
+
const extensibleOf = (schema) => readMetaExtensible(schema) ? { extensible: true } : {};
|
|
140
191
|
/**
|
|
141
|
-
* A Zod schema reduced to the shape a comparison reads.
|
|
192
|
+
* A Zod schema reduced to the shape a comparison reads. A model that holds
|
|
193
|
+
* itself is written out once, and inside itself as a reference by its title.
|
|
194
|
+
*
|
|
195
|
+
* `inside` is the objects the walk is in, so it can tell when it has gone round.
|
|
142
196
|
*/
|
|
143
|
-
const toSchemaNode = (schema) => {
|
|
144
|
-
const { inner } = unwrapOptionalWrappers(schema);
|
|
197
|
+
const toSchemaNode = (schema, inside = []) => {
|
|
198
|
+
const { inner, nullable } = unwrapOptionalWrappers(schema);
|
|
145
199
|
const def = readDef(inner);
|
|
200
|
+
const nullability = nullable ? { nullable: true } : {};
|
|
201
|
+
const identity = readModelSchema(inner) ?? inner;
|
|
202
|
+
if (inside.includes(identity)) return {
|
|
203
|
+
kind: "model",
|
|
204
|
+
...nullability,
|
|
205
|
+
model: readMetaId(inner) ?? ""
|
|
206
|
+
};
|
|
207
|
+
const options = def.type === "union" ? def.options ?? [] : [];
|
|
208
|
+
const nonNull = options.filter((option) => readDef(option).type !== "null");
|
|
209
|
+
if (nonNull.length === 1 && options.length > 1) return {
|
|
210
|
+
...toSchemaNode(nonNull[0], inside),
|
|
211
|
+
nullable: true
|
|
212
|
+
};
|
|
146
213
|
const refinements = refinementCount(inner);
|
|
147
214
|
const carry = refinements === void 0 ? {} : { refinements };
|
|
148
215
|
const shape = readObjectShape(inner);
|
|
149
216
|
if (shape) {
|
|
150
217
|
const fields = {};
|
|
218
|
+
const within = [...inside, identity];
|
|
151
219
|
for (const [key, field] of Object.entries(shape)) fields[key] = {
|
|
152
220
|
optional: unwrapOptionalWrappers(field).optional,
|
|
153
|
-
schema: toSchemaNode(field)
|
|
221
|
+
schema: toSchemaNode(field, within)
|
|
154
222
|
};
|
|
155
223
|
return {
|
|
156
224
|
kind: "object",
|
|
225
|
+
...nullability,
|
|
157
226
|
fields,
|
|
158
227
|
...carry
|
|
159
228
|
};
|
|
160
229
|
}
|
|
161
230
|
if (def.type === "array" && def.element) return {
|
|
162
231
|
kind: "array",
|
|
163
|
-
|
|
232
|
+
...nullability,
|
|
233
|
+
element: toSchemaNode(def.element, inside),
|
|
164
234
|
...carry
|
|
165
235
|
};
|
|
166
236
|
if (def.type === "enum") return {
|
|
167
237
|
kind: "enum",
|
|
238
|
+
...nullability,
|
|
168
239
|
values: Object.values(def.entries ?? {}).map(String).sort(),
|
|
240
|
+
...extensibleOf(inner),
|
|
169
241
|
...carry
|
|
170
242
|
};
|
|
171
243
|
if (def.type === "literal") return {
|
|
172
244
|
kind: "enum",
|
|
245
|
+
...nullability,
|
|
173
246
|
values: (def.values ?? []).map(String).sort(),
|
|
174
247
|
...carry
|
|
175
248
|
};
|
|
249
|
+
if (def.type === "union") return {
|
|
250
|
+
kind: "union",
|
|
251
|
+
...nullability,
|
|
252
|
+
options: options.map((option) => toSchemaNode(option, inside)),
|
|
253
|
+
...extensibleOf(inner),
|
|
254
|
+
...carry
|
|
255
|
+
};
|
|
176
256
|
return {
|
|
177
257
|
kind: def.type ?? "unknown",
|
|
258
|
+
...nullability,
|
|
178
259
|
...carry
|
|
179
260
|
};
|
|
180
261
|
};
|
|
@@ -183,10 +264,10 @@ const sunsetOf = (route) => {
|
|
|
183
264
|
if (declared === void 0) return void 0;
|
|
184
265
|
return typeof declared === "string" ? declared : declared.date;
|
|
185
266
|
};
|
|
186
|
-
const routeSnapshots =
|
|
267
|
+
const routeSnapshots = (api) => {
|
|
187
268
|
const routes = {};
|
|
188
|
-
return {
|
|
189
|
-
processRoute({ routeKey, route, deprecated }) {
|
|
269
|
+
return walkApi(api, {
|
|
270
|
+
processRoute({ routeKey, route, deprecated, hidden }) {
|
|
190
271
|
const sunset = sunsetOf(route);
|
|
191
272
|
const responses = {};
|
|
192
273
|
for (const [status, response] of Object.entries(route.responses)) {
|
|
@@ -198,6 +279,7 @@ const routeSnapshots = createGenerator(() => {
|
|
|
198
279
|
path: route.path,
|
|
199
280
|
statuses: Object.keys(route.responses).map(Number).sort((a, b) => a - b),
|
|
200
281
|
deprecated,
|
|
282
|
+
...hidden ? { hidden: true } : {},
|
|
201
283
|
...sunset === void 0 ? {} : { sunset },
|
|
202
284
|
...route.body ? { body: toSchemaNode(route.body) } : {},
|
|
203
285
|
...route.query ? { query: toSchemaNode(route.query) } : {},
|
|
@@ -206,8 +288,8 @@ const routeSnapshots = createGenerator(() => {
|
|
|
206
288
|
};
|
|
207
289
|
},
|
|
208
290
|
finalize: () => routes
|
|
209
|
-
};
|
|
210
|
-
}
|
|
291
|
+
});
|
|
292
|
+
};
|
|
211
293
|
/**
|
|
212
294
|
* Everything `kizuna diff` compares, as plain data.
|
|
213
295
|
*
|
|
@@ -219,7 +301,7 @@ const toSnapshot = (api) => {
|
|
|
219
301
|
const jobs = declared ? flattenJobs(declared).map((job) => job.jobKey).sort() : [];
|
|
220
302
|
const tools = Object.fromEntries(flattenRoutes(api.routes).filter(({ route }) => route.tool !== void 0 && route.tool !== false).map(({ routeKey }) => [routeKey, toToolName(routeKey)]));
|
|
221
303
|
return {
|
|
222
|
-
routes: routeSnapshots(api
|
|
304
|
+
routes: routeSnapshots(api),
|
|
223
305
|
jobs,
|
|
224
306
|
tools
|
|
225
307
|
};
|
|
@@ -236,7 +318,7 @@ const renderSnapshot = (api) => stringify(toSnapshot(api), { lineWidth: 0 });
|
|
|
236
318
|
*/
|
|
237
319
|
const snapshotPathFor = (configPath) => {
|
|
238
320
|
const declared = basename(configPath).replace(/\.config\.[cm]?[jt]s$/, "").replace(/^kizuna\./, "");
|
|
239
|
-
const name = declared === "" || declared === "kizuna" ? "
|
|
321
|
+
const name = declared === "" || declared === "kizuna" ? "snapshot" : `${declared}.snapshot`;
|
|
240
322
|
return join(dirname(configPath), ".kizuna", `${name}.yaml`);
|
|
241
323
|
};
|
|
242
324
|
/**
|
|
@@ -244,9 +326,8 @@ const snapshotPathFor = (configPath) => {
|
|
|
244
326
|
* `kizuna generate --check` refuses to let it go stale.
|
|
245
327
|
*/
|
|
246
328
|
const snapshotTarget = (configPath) => ({
|
|
247
|
-
kind: "diff",
|
|
248
329
|
output: snapshotPathFor(configPath),
|
|
249
|
-
|
|
330
|
+
render: renderSnapshot
|
|
250
331
|
});
|
|
251
332
|
//#endregion
|
|
252
333
|
//#region src/diff-apis.ts
|
|
@@ -344,7 +425,8 @@ const diffSnapshots = (before, after, options = {}) => {
|
|
|
344
425
|
for (const [direction, label, was, now] of inputs) for (const change of diffSchemas(was, now, direction, label)) changes.push({
|
|
345
426
|
level: change.breaking ? "breaking" : "changed",
|
|
346
427
|
key,
|
|
347
|
-
summary: `${httpLabel(arrived, key)} ${change.summary}
|
|
428
|
+
summary: `${httpLabel(arrived, key)} ${change.summary}`,
|
|
429
|
+
...change.detail === void 0 ? {} : { detail: change.detail }
|
|
348
430
|
});
|
|
349
431
|
for (const [status, was] of Object.entries(gone.responses)) {
|
|
350
432
|
if (!(status in arrived.responses)) continue;
|
|
@@ -352,9 +434,21 @@ const diffSnapshots = (before, after, options = {}) => {
|
|
|
352
434
|
for (const change of diffSchemas(was ?? void 0, now ?? void 0, "response", status)) changes.push({
|
|
353
435
|
level: change.breaking ? "breaking" : "changed",
|
|
354
436
|
key,
|
|
355
|
-
summary: `${httpLabel(arrived, key)} ${change.summary}
|
|
437
|
+
summary: `${httpLabel(arrived, key)} ${change.summary}`,
|
|
438
|
+
...change.detail === void 0 ? {} : { detail: change.detail }
|
|
356
439
|
});
|
|
357
440
|
}
|
|
441
|
+
if (gone.hidden !== true && arrived.hidden === true) changes.push({
|
|
442
|
+
level: "breaking",
|
|
443
|
+
key,
|
|
444
|
+
summary: `${httpLabel(arrived, key)} is now hidden`,
|
|
445
|
+
detail: "the server still serves it, but it is no longer in the generated clients or the OpenAPI document"
|
|
446
|
+
});
|
|
447
|
+
if (gone.hidden === true && arrived.hidden !== true) changes.push({
|
|
448
|
+
level: "added",
|
|
449
|
+
key,
|
|
450
|
+
summary: `${httpLabel(arrived, key)} is no longer hidden`
|
|
451
|
+
});
|
|
358
452
|
if (!gone.deprecated && arrived.deprecated) changes.push({
|
|
359
453
|
level: "changed",
|
|
360
454
|
key,
|
|
@@ -430,38 +524,37 @@ const currentContents = (output) => {
|
|
|
430
524
|
}
|
|
431
525
|
};
|
|
432
526
|
/**
|
|
433
|
-
* Writes every
|
|
434
|
-
* matches so watchers and build tools see no
|
|
527
|
+
* Writes every generated file, the clients and what plugins generate, leaving
|
|
528
|
+
* a file alone when it already matches so watchers and build tools see no
|
|
529
|
+
* change.
|
|
435
530
|
*/
|
|
436
|
-
const
|
|
437
|
-
const rendered =
|
|
438
|
-
const changed = currentContents(
|
|
531
|
+
const writeFiles = (contract, files) => files.map((file) => {
|
|
532
|
+
const rendered = file.render(contract);
|
|
533
|
+
const changed = currentContents(file.output) !== rendered;
|
|
439
534
|
if (changed) {
|
|
440
|
-
mkdirSync(dirname(
|
|
441
|
-
writeFileSync(
|
|
535
|
+
mkdirSync(dirname(file.output), { recursive: true });
|
|
536
|
+
writeFileSync(file.output, rendered);
|
|
442
537
|
}
|
|
443
538
|
return {
|
|
444
|
-
|
|
445
|
-
output: client.output,
|
|
539
|
+
output: file.output,
|
|
446
540
|
changed
|
|
447
541
|
};
|
|
448
542
|
});
|
|
449
543
|
/**
|
|
450
|
-
* Every
|
|
544
|
+
* Every generated file that no longer matches its api, without writing
|
|
545
|
+
* anything.
|
|
451
546
|
*
|
|
452
547
|
* This is what a pipeline runs: a generated client that has fallen behind is a
|
|
453
548
|
* compile error waiting to happen in whatever imports it.
|
|
454
549
|
*/
|
|
455
|
-
const
|
|
456
|
-
const current = currentContents(
|
|
550
|
+
const checkFiles = (contract, files) => files.flatMap((file) => {
|
|
551
|
+
const current = currentContents(file.output);
|
|
457
552
|
if (current === void 0) return [{
|
|
458
|
-
|
|
459
|
-
output: client.output,
|
|
553
|
+
output: file.output,
|
|
460
554
|
reason: "missing"
|
|
461
555
|
}];
|
|
462
|
-
if (current !==
|
|
463
|
-
|
|
464
|
-
output: client.output,
|
|
556
|
+
if (current !== file.render(contract)) return [{
|
|
557
|
+
output: file.output,
|
|
465
558
|
reason: "outdated"
|
|
466
559
|
}];
|
|
467
560
|
return [];
|
|
@@ -471,9 +564,9 @@ const checkClients = (contract, clients) => clients.flatMap((client) => {
|
|
|
471
564
|
* rather than printing a diff nobody reads.
|
|
472
565
|
*/
|
|
473
566
|
const formatStale = (stale, command = "kizuna generate") => {
|
|
474
|
-
const lines = stale.map((
|
|
475
|
-
const where = relative(process.cwd(),
|
|
476
|
-
return
|
|
567
|
+
const lines = stale.map((file) => {
|
|
568
|
+
const where = relative(process.cwd(), file.output);
|
|
569
|
+
return file.reason === "missing" ? ` ${where} has not been generated` : ` ${where} is behind the config`;
|
|
477
570
|
});
|
|
478
571
|
return [
|
|
479
572
|
stale.length === 1 ? "A generated file is out of date:" : `${stale.length} generated files are out of date:`,
|
|
@@ -491,7 +584,7 @@ const formatStale = (stale, command = "kizuna generate") => {
|
|
|
491
584
|
*/
|
|
492
585
|
const CONFIG_KEYS = [
|
|
493
586
|
"adapter",
|
|
494
|
-
"
|
|
587
|
+
"groups",
|
|
495
588
|
"auth",
|
|
496
589
|
"requestContext",
|
|
497
590
|
"validation",
|
|
@@ -600,12 +693,22 @@ const typeOfStringUnion = (expression, key) => {
|
|
|
600
693
|
return codes.length > 0 ? codes.join(" | ") : "never";
|
|
601
694
|
};
|
|
602
695
|
/**
|
|
696
|
+
* The repo's Prettier `printWidth`. A line the generator writes longer than this
|
|
697
|
+
* is one `pnpm format` would rewrite, and `--check` would then report the file
|
|
698
|
+
* as behind.
|
|
699
|
+
*/
|
|
700
|
+
const PRINT_WIDTH = 140;
|
|
701
|
+
/**
|
|
603
702
|
* Each plugin in order, as a tuple, so a handler reaches each one under its own
|
|
604
|
-
* slug.
|
|
703
|
+
* slug. It wraps one plugin per line when the line would run past
|
|
704
|
+
* {@link PRINT_WIDTH}, the way Prettier does.
|
|
605
705
|
*/
|
|
606
|
-
const typeOfPluginList = (expression, used) => {
|
|
706
|
+
const typeOfPluginList = (expression, used, prefix) => {
|
|
607
707
|
if (!ts.isArrayLiteralExpression(expression)) throw new ConfigSyntaxError("`plugins` has to be an array, so each plugin keeps its place.");
|
|
608
|
-
|
|
708
|
+
const entries = expression.elements.map((element) => typeOfValue(element, used, "plugins"));
|
|
709
|
+
const inline = `[${entries.join(", ")}]`;
|
|
710
|
+
if (`${prefix}${inline};`.length <= PRINT_WIDTH) return inline;
|
|
711
|
+
return `[\n${entries.map((entry) => ` ${entry},`).join("\n")}\n ]`;
|
|
609
712
|
};
|
|
610
713
|
/**
|
|
611
714
|
* The `issueCodes` inside `validation`, or nothing when it declares none.
|
|
@@ -631,10 +734,35 @@ const authLines = (expression, used) => {
|
|
|
631
734
|
}
|
|
632
735
|
return lines;
|
|
633
736
|
};
|
|
737
|
+
/**
|
|
738
|
+
* A string as Prettier writes it: single quotes, unless the string holds more
|
|
739
|
+
* of them than double quotes.
|
|
740
|
+
*/
|
|
741
|
+
const stringLiteral = (value) => {
|
|
742
|
+
const quote = value.split("'").length > value.split("\"").length ? "\"" : "'";
|
|
743
|
+
return `${quote}${value.replaceAll("\\", "\\\\").replaceAll(quote, `\\${quote}`)}${quote}`;
|
|
744
|
+
};
|
|
745
|
+
const propertyName = (name) => /^[A-Za-z_$][\w$]*$/.test(name) ? name : stringLiteral(name);
|
|
746
|
+
/**
|
|
747
|
+
* One entry under `typescript.extend`. A list of strings becomes a
|
|
748
|
+
* union, wrapped one member per line when it would run past
|
|
749
|
+
* {@link PRINT_WIDTH}, the way Prettier does.
|
|
750
|
+
*/
|
|
751
|
+
const typeOfExtension = (values, indent) => {
|
|
752
|
+
const lines = Object.entries(values).map(([key, value]) => {
|
|
753
|
+
const prefix = `${indent} ${propertyName(key)}:`;
|
|
754
|
+
if (!Array.isArray(value)) return `${prefix} ${typeOfExtension(value, `${indent} `)};`;
|
|
755
|
+
const members = [...new Set(value)].map(stringLiteral);
|
|
756
|
+
if (members.length === 0) return `${prefix} never;`;
|
|
757
|
+
const inline = `${prefix} ${members.join(" | ")};`;
|
|
758
|
+
if (members.length === 1 || inline.length <= PRINT_WIDTH) return inline;
|
|
759
|
+
return `${prefix}\n${members.map((member) => `${indent} | ${member}`).join("\n")};`;
|
|
760
|
+
});
|
|
761
|
+
return lines.length === 0 ? "{}" : `{\n${lines.join("\n")}\n${indent}}`;
|
|
762
|
+
};
|
|
634
763
|
const HEADER = `/**
|
|
635
|
-
*
|
|
636
|
-
*
|
|
637
|
-
* and re-run \`kizuna generate\` to regenerate this file.
|
|
764
|
+
* Generated by \`kizuna generate\` from your Kizuna config. DO NOT EDIT MANUALLY.
|
|
765
|
+
* Every edit here is lost on the next run. Change the config and run \`kizuna generate\` again.
|
|
638
766
|
*/`;
|
|
639
767
|
/**
|
|
640
768
|
* Points a relative specifier at the same file from somewhere else, so a
|
|
@@ -673,12 +801,14 @@ const renderImports = (imports, used, rebase) => {
|
|
|
673
801
|
*
|
|
674
802
|
* Read from the config's syntax rather than by running it: the generated file
|
|
675
803
|
* imports the same names the config did, and a config that loads is not a
|
|
676
|
-
* prerequisite for typing the routes it serves.
|
|
804
|
+
* prerequisite for typing the routes it serves. What
|
|
805
|
+
* `typescript.extend` adds exists only once the config has run, so the caller passes
|
|
806
|
+
* it in.
|
|
677
807
|
*
|
|
678
808
|
* `outputPath` is what the imports are written relative to. Left out, they stay
|
|
679
809
|
* relative to the config.
|
|
680
810
|
*/
|
|
681
|
-
const generateConfigTypes = (configSource, fileName = "kizuna.config.ts", outputPath) => {
|
|
811
|
+
const generateConfigTypes = (configSource, fileName = "kizuna.config.ts", outputPath, extensions = []) => {
|
|
682
812
|
const source = ts.createSourceFile(fileName, configSource, ts.ScriptTarget.Latest, true);
|
|
683
813
|
const imports = collectImports(source);
|
|
684
814
|
const configObject = findConfigObject(source);
|
|
@@ -702,9 +832,10 @@ const generateConfigTypes = (configSource, fileName = "kizuna.config.ts", output
|
|
|
702
832
|
else if (key === "validation") {
|
|
703
833
|
const codes = issueCodesOf(value);
|
|
704
834
|
if (codes !== void 0) lines.push(` validation: {\n issueCodes: ${codes};\n };`);
|
|
705
|
-
} else if (key === "plugins") lines.push(` ${key}: ${typeOfPluginList(value, used)};`);
|
|
835
|
+
} else if (key === "plugins") lines.push(` ${key}: ${typeOfPluginList(value, used, ` ${key}: `)};`);
|
|
706
836
|
else lines.push(` ${key}: ${typeOfValue(value, used, key)};`);
|
|
707
837
|
}
|
|
838
|
+
for (const extension of extensions) for (const [name, values] of Object.entries(extension)) lines.push(` ${propertyName(name)}: ${typeOfExtension(values, " ")};`);
|
|
708
839
|
const configDir = dirname(resolve(fileName));
|
|
709
840
|
const outputDir = outputPath === void 0 ? configDir : dirname(resolve(outputPath));
|
|
710
841
|
const importBlock = renderImports(imports, used, (specifier) => outputDir === configDir ? specifier : rebaseSpecifier(specifier, configDir, outputDir));
|
|
@@ -736,7 +867,7 @@ const parseSnapshot = (contents, source) => {
|
|
|
736
867
|
try {
|
|
737
868
|
return parse(contents);
|
|
738
869
|
} catch {
|
|
739
|
-
throw new Error(`${source} is not a Kizuna
|
|
870
|
+
throw new Error(`${source} is not a Kizuna snapshot. Run \`kizuna generate\` to write one.`);
|
|
740
871
|
}
|
|
741
872
|
};
|
|
742
873
|
/**
|
|
@@ -798,11 +929,11 @@ const describeSunset = (sunset) => {
|
|
|
798
929
|
/**
|
|
799
930
|
* Every route an api serves, in the order it declares them.
|
|
800
931
|
*/
|
|
801
|
-
const routeMap =
|
|
932
|
+
const routeMap = (api, _options = {}) => {
|
|
802
933
|
const entries = [];
|
|
803
934
|
const toolKeys = [];
|
|
804
|
-
return {
|
|
805
|
-
processRoute({ routeKey, route,
|
|
935
|
+
return walkApi(api, {
|
|
936
|
+
processRoute({ routeKey, route, routeGroups, deprecated, deprecationMessage, hidden }) {
|
|
806
937
|
if (route.tool) toolKeys.push({
|
|
807
938
|
key: routeKey,
|
|
808
939
|
origin: "route"
|
|
@@ -812,9 +943,10 @@ const routeMap = createGenerator((_options, _api) => {
|
|
|
812
943
|
method: route.method,
|
|
813
944
|
path: route.path,
|
|
814
945
|
auth: describeAuth(route.auth),
|
|
815
|
-
|
|
946
|
+
groups: routeGroups,
|
|
816
947
|
deprecated: deprecated ? deprecationMessage ?? "" : void 0,
|
|
817
|
-
sunset: describeSunset(route.sunset)
|
|
948
|
+
sunset: describeSunset(route.sunset),
|
|
949
|
+
...hidden ? { hidden: true } : {}
|
|
818
950
|
});
|
|
819
951
|
},
|
|
820
952
|
finalize() {
|
|
@@ -824,8 +956,8 @@ const routeMap = createGenerator((_options, _api) => {
|
|
|
824
956
|
tool: names.get(entry.key) ?? (toolKeys.some((tool) => tool.key === entry.key) ? toToolName(entry.key) : void 0)
|
|
825
957
|
}));
|
|
826
958
|
}
|
|
827
|
-
};
|
|
828
|
-
}
|
|
959
|
+
});
|
|
960
|
+
};
|
|
829
961
|
const pad = (text, width) => text + " ".repeat(Math.max(0, width - text.length));
|
|
830
962
|
/**
|
|
831
963
|
* The route map as aligned columns, one route per line.
|
|
@@ -838,12 +970,14 @@ const formatRoutes = (entries) => {
|
|
|
838
970
|
return entries.map((entry) => {
|
|
839
971
|
const notes = [
|
|
840
972
|
`auth: ${entry.auth}`,
|
|
973
|
+
entry.groups.length > 0 && `group: ${entry.groups.join(", ")}`,
|
|
841
974
|
entry.tool && `tool: ${entry.tool}`,
|
|
842
975
|
entry.deprecated !== void 0 && `deprecated${entry.deprecated ? `, ${entry.deprecated}` : ""}`,
|
|
843
|
-
entry.sunset && `sunset ${entry.sunset}
|
|
976
|
+
entry.sunset && `sunset ${entry.sunset}`,
|
|
977
|
+
entry.hidden && "hidden"
|
|
844
978
|
].filter(Boolean);
|
|
845
979
|
return `${pad(entry.method, methodWidth)} ${pad(entry.path, pathWidth)} ${pad(entry.key, keyWidth)} ${notes.join(" ")}`.trimEnd();
|
|
846
980
|
}).join("\n");
|
|
847
981
|
};
|
|
848
982
|
//#endregion
|
|
849
|
-
export { loadConfig as _, ConfigSyntaxError as a, formatStale as c, diffSnapshots as d, formatChange as f, diffSchemas as g, snapshotTarget as h, readSnapshot as i,
|
|
983
|
+
export { loadConfig as _, ConfigSyntaxError as a, formatStale as c, diffSnapshots as d, formatChange as f, diffSchemas as g, snapshotTarget as h, readSnapshot as i, writeFiles as l, snapshotPathFor as m, routeMap as n, generateConfigTypes as o, hasBreakingChange as p, diffAgainst as r, checkFiles as s, formatRoutes as t, diffApis as u };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kizunajs/cli",
|
|
3
|
-
"version": "2.0.0-beta.
|
|
3
|
+
"version": "2.0.0-beta.40",
|
|
4
4
|
"description": "Command line tooling for Kizuna.js",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"kizunajs",
|
|
@@ -58,18 +58,18 @@
|
|
|
58
58
|
"dependencies": {
|
|
59
59
|
"jiti": "^2.7.0",
|
|
60
60
|
"typescript": "^6.0.3",
|
|
61
|
-
"yaml": "^2.
|
|
61
|
+
"yaml": "^2.9.1"
|
|
62
62
|
},
|
|
63
63
|
"peerDependencies": {
|
|
64
|
-
"
|
|
65
|
-
"
|
|
64
|
+
"kizunajs": "2.0.0-beta.40",
|
|
65
|
+
"zod": "^4.0.0"
|
|
66
66
|
},
|
|
67
67
|
"devDependencies": {
|
|
68
|
-
"@types/node": "^24",
|
|
69
|
-
"@types/react": "^19.
|
|
70
|
-
"
|
|
71
|
-
"
|
|
72
|
-
"
|
|
68
|
+
"@types/node": "^24.19.2",
|
|
69
|
+
"@types/react": "^19.3.0",
|
|
70
|
+
"kizunajs": "2.0.0-beta.40",
|
|
71
|
+
"tsdown": "^0.21.10",
|
|
72
|
+
"zod": "^4.6.7"
|
|
73
73
|
},
|
|
74
74
|
"scripts": {
|
|
75
75
|
"build": "tsdown src/index.ts src/cli.ts --format esm,cjs --dts --clean --external kizunajs --external zod --external jiti --external typescript",
|