@kizunajs/cli 2.0.0-beta.0 → 2.0.0-beta.10
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 +32 -12
- package/dist/cli.mjs +32 -12
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +71 -20
- package/dist/index.d.mts +71 -20
- package/dist/index.mjs +1 -1
- package/dist/{route-map-D7cBgYmT.mjs → route-map-C484dhbj.mjs} +243 -141
- package/dist/{route-map-DW2UUkD_.cjs → route-map-I400Eb9_.cjs} +265 -139
- package/package.json +6 -4
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { createJiti } from "jiti";
|
|
2
2
|
import { apiEntries } from "kizunajs";
|
|
3
3
|
import { createGenerator, deriveToolNames, flattenJobs, readDef, readObjectShape, resolveResponseBody, toToolName, unwrapOptionalWrappers } from "kizunajs/generator";
|
|
4
|
+
import { basename, dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
|
|
5
|
+
import { parse, stringify } from "yaml";
|
|
4
6
|
import { flattenRoutes } from "kizunajs/adapter";
|
|
5
|
-
import {
|
|
6
|
-
import { dirname, isAbsolute, join, relative, resolve } from "node:path";
|
|
7
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
7
8
|
import ts from "typescript";
|
|
8
9
|
import { execFileSync } from "node:child_process";
|
|
9
10
|
//#region src/load-config.ts
|
|
@@ -16,53 +17,30 @@ import { execFileSync } from "node:child_process";
|
|
|
16
17
|
* a config.
|
|
17
18
|
*/
|
|
18
19
|
const loadConfig = async (configPath) => {
|
|
19
|
-
return apiEntries(await createJiti(import.meta.url, {
|
|
20
|
+
return apiEntries(await createJiti(import.meta.url, {
|
|
21
|
+
interopDefault: true,
|
|
22
|
+
jsx: true,
|
|
23
|
+
tsconfigPaths: true
|
|
24
|
+
}).import(configPath)).map(([name, entry]) => ({
|
|
20
25
|
name,
|
|
21
26
|
api: entry.api,
|
|
22
27
|
clients: entry.clients ?? [],
|
|
23
|
-
typesOutput: entry.typescript?.outputFile
|
|
28
|
+
typesOutput: entry.typescript?.outputFile,
|
|
29
|
+
diff: entry.diff ?? {}
|
|
24
30
|
}));
|
|
25
31
|
};
|
|
26
32
|
//#endregion
|
|
27
33
|
//#region src/diff-schemas.ts
|
|
28
|
-
const shapeOf = (schema) => {
|
|
29
|
-
const { inner } = unwrapOptionalWrappers(schema);
|
|
30
|
-
const raw = readObjectShape(inner);
|
|
31
|
-
if (!raw) return void 0;
|
|
32
|
-
const fields = /* @__PURE__ */ new Map();
|
|
33
|
-
for (const [key, field] of Object.entries(raw)) fields.set(key, {
|
|
34
|
-
schema: field,
|
|
35
|
-
optional: unwrapOptionalWrappers(field).optional
|
|
36
|
-
});
|
|
37
|
-
return { fields };
|
|
38
|
-
};
|
|
39
|
-
/**
|
|
40
|
-
* What a schema accepts, flattened enough to compare: its kind, and its values
|
|
41
|
-
* when it is closed.
|
|
42
|
-
*/
|
|
43
|
-
const describe = (schema) => {
|
|
44
|
-
const { inner } = unwrapOptionalWrappers(schema);
|
|
45
|
-
const def = readDef(inner);
|
|
46
|
-
if (def.type === "enum") return {
|
|
47
|
-
kind: "enum",
|
|
48
|
-
values: Object.values(def.entries ?? {}).map(String).sort()
|
|
49
|
-
};
|
|
50
|
-
if (def.type === "literal") return {
|
|
51
|
-
kind: "enum",
|
|
52
|
-
values: (def.values ?? []).map(String).sort()
|
|
53
|
-
};
|
|
54
|
-
if (def.type === "array" && def.element) return { kind: `array<${describe(def.element).kind}>` };
|
|
55
|
-
return { kind: def.type ?? "unknown" };
|
|
56
|
-
};
|
|
57
34
|
const joinPath = (parent, key) => parent === "" ? key : `${parent}.${key}`;
|
|
35
|
+
const fieldsOf = (schema) => schema.kind === "object" ? schema.fields : void 0;
|
|
36
|
+
const elementOf = (schema) => schema.kind === "array" ? schema.element : void 0;
|
|
37
|
+
const valuesOf = (schema) => schema.kind === "enum" ? schema.values : void 0;
|
|
58
38
|
/**
|
|
59
|
-
*
|
|
60
|
-
* rather than as the whole array changing shape.
|
|
39
|
+
* What a reader sees when the kinds differ, so `array` reads as `array<string>`.
|
|
61
40
|
*/
|
|
62
|
-
const
|
|
63
|
-
const
|
|
64
|
-
|
|
65
|
-
return def.type === "array" ? def.element : void 0;
|
|
41
|
+
const describeKind = (schema) => {
|
|
42
|
+
const element = elementOf(schema);
|
|
43
|
+
return element ? `array<${describeKind(element)}>` : schema.kind;
|
|
66
44
|
};
|
|
67
45
|
/**
|
|
68
46
|
* What changed between two versions of one schema, and whether it breaks the
|
|
@@ -84,11 +62,11 @@ const diffSchemas = (before, after, direction, path = "") => {
|
|
|
84
62
|
const beforeElement = elementOf(before);
|
|
85
63
|
const afterElement = elementOf(after);
|
|
86
64
|
if (beforeElement && afterElement) return diffSchemas(beforeElement, afterElement, direction, `${path}[]`);
|
|
87
|
-
const
|
|
88
|
-
const
|
|
89
|
-
if (
|
|
90
|
-
for (const [key, field] of
|
|
91
|
-
const existing =
|
|
65
|
+
const beforeFields = fieldsOf(before);
|
|
66
|
+
const afterFields = fieldsOf(after);
|
|
67
|
+
if (beforeFields && afterFields) {
|
|
68
|
+
for (const [key, field] of Object.entries(afterFields)) {
|
|
69
|
+
const existing = beforeFields[key];
|
|
92
70
|
const here = joinPath(path, key);
|
|
93
71
|
if (!existing) {
|
|
94
72
|
if (!field.optional) changes.push({
|
|
@@ -105,8 +83,8 @@ const diffSchemas = (before, after, direction, path = "") => {
|
|
|
105
83
|
});
|
|
106
84
|
changes.push(...diffSchemas(existing.schema, field.schema, direction, here));
|
|
107
85
|
}
|
|
108
|
-
for (const
|
|
109
|
-
if (
|
|
86
|
+
for (const key of Object.keys(beforeFields)) {
|
|
87
|
+
if (afterFields[key]) continue;
|
|
110
88
|
const here = joinPath(path, key);
|
|
111
89
|
changes.push({
|
|
112
90
|
breaking: direction === "response",
|
|
@@ -116,19 +94,16 @@ const diffSchemas = (before, after, direction, path = "") => {
|
|
|
116
94
|
}
|
|
117
95
|
return changes;
|
|
118
96
|
}
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
if (was.values && now.values) {
|
|
130
|
-
const removed = was.values.filter((value) => !now.values?.includes(value));
|
|
131
|
-
const added = now.values.filter((value) => !was.values?.includes(value));
|
|
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
|
+
const was = valuesOf(before);
|
|
103
|
+
const now = valuesOf(after);
|
|
104
|
+
if (was && now) {
|
|
105
|
+
const removed = was.filter((value) => !now.includes(value));
|
|
106
|
+
const added = now.filter((value) => !was.includes(value));
|
|
132
107
|
if (removed.length > 0) changes.push({
|
|
133
108
|
breaking: true,
|
|
134
109
|
path,
|
|
@@ -140,53 +115,145 @@ const diffSchemas = (before, after, direction, path = "") => {
|
|
|
140
115
|
summary: direction === "response" ? `${path || "the body"} can now be ${added.join(", ")}` : `${path || "the body"} also accepts ${added.join(", ")}`
|
|
141
116
|
});
|
|
142
117
|
}
|
|
118
|
+
const wasRefined = before.refinements ?? 0;
|
|
119
|
+
const isRefined = after.refinements ?? 0;
|
|
120
|
+
if (wasRefined !== isRefined) changes.push({
|
|
121
|
+
breaking: direction === "request" && isRefined > wasRefined,
|
|
122
|
+
path,
|
|
123
|
+
summary: isRefined > wasRefined ? `${path || "the body"} is validated more tightly` : `${path || "the body"} is validated less tightly`
|
|
124
|
+
});
|
|
143
125
|
return changes;
|
|
144
126
|
};
|
|
145
127
|
//#endregion
|
|
146
|
-
//#region src/
|
|
147
|
-
|
|
148
|
-
|
|
128
|
+
//#region src/snapshot.ts
|
|
129
|
+
/**
|
|
130
|
+
* How many `.refine()` or `.superRefine()` calls a schema carries. The
|
|
131
|
+
* predicate itself cannot be compared across two files, but its arrival or
|
|
132
|
+
* departure can.
|
|
133
|
+
*/
|
|
134
|
+
const refinementCount = (schema) => {
|
|
135
|
+
const checks = readDef(schema).checks;
|
|
136
|
+
if (!checks) return void 0;
|
|
137
|
+
const custom = checks.filter((check) => check._zod?.def?.check === "custom").length;
|
|
138
|
+
return custom === 0 ? void 0 : custom;
|
|
139
|
+
};
|
|
140
|
+
/**
|
|
141
|
+
* A Zod schema reduced to the shape a comparison reads.
|
|
142
|
+
*/
|
|
143
|
+
const toSchemaNode = (schema) => {
|
|
144
|
+
const { inner } = unwrapOptionalWrappers(schema);
|
|
145
|
+
const def = readDef(inner);
|
|
146
|
+
const refinements = refinementCount(inner);
|
|
147
|
+
const carry = refinements === void 0 ? {} : { refinements };
|
|
148
|
+
const shape = readObjectShape(inner);
|
|
149
|
+
if (shape) {
|
|
150
|
+
const fields = {};
|
|
151
|
+
for (const [key, field] of Object.entries(shape)) fields[key] = {
|
|
152
|
+
optional: unwrapOptionalWrappers(field).optional,
|
|
153
|
+
schema: toSchemaNode(field)
|
|
154
|
+
};
|
|
155
|
+
return {
|
|
156
|
+
kind: "object",
|
|
157
|
+
fields,
|
|
158
|
+
...carry
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
if (def.type === "array" && def.element) return {
|
|
162
|
+
kind: "array",
|
|
163
|
+
element: toSchemaNode(def.element),
|
|
164
|
+
...carry
|
|
165
|
+
};
|
|
166
|
+
if (def.type === "enum") return {
|
|
167
|
+
kind: "enum",
|
|
168
|
+
values: Object.values(def.entries ?? {}).map(String).sort(),
|
|
169
|
+
...carry
|
|
170
|
+
};
|
|
171
|
+
if (def.type === "literal") return {
|
|
172
|
+
kind: "enum",
|
|
173
|
+
values: (def.values ?? []).map(String).sort(),
|
|
174
|
+
...carry
|
|
175
|
+
};
|
|
176
|
+
return {
|
|
177
|
+
kind: def.type ?? "unknown",
|
|
178
|
+
...carry
|
|
179
|
+
};
|
|
180
|
+
};
|
|
181
|
+
const sunsetOf = (route) => {
|
|
182
|
+
const declared = route.sunset;
|
|
183
|
+
if (declared === void 0) return void 0;
|
|
184
|
+
return typeof declared === "string" ? declared : declared.date;
|
|
185
|
+
};
|
|
186
|
+
const routeSnapshots = createGenerator(() => {
|
|
187
|
+
const routes = {};
|
|
149
188
|
return {
|
|
150
189
|
processRoute({ routeKey, route, deprecated }) {
|
|
151
|
-
|
|
190
|
+
const sunset = sunsetOf(route);
|
|
191
|
+
const responses = {};
|
|
192
|
+
for (const [status, response] of Object.entries(route.responses)) {
|
|
193
|
+
const body = resolveResponseBody(response);
|
|
194
|
+
responses[status] = body === void 0 ? null : toSchemaNode(body);
|
|
195
|
+
}
|
|
196
|
+
routes[routeKey] = {
|
|
152
197
|
method: route.method,
|
|
153
198
|
path: route.path,
|
|
154
|
-
statuses: Object.keys(route.responses).map(Number).sort(),
|
|
199
|
+
statuses: Object.keys(route.responses).map(Number).sort((a, b) => a - b),
|
|
155
200
|
deprecated,
|
|
156
|
-
...
|
|
157
|
-
...route.body ? { body: route.body } : {},
|
|
158
|
-
...route.query ? { query: route.query } : {},
|
|
159
|
-
...route.headers ? { headers: route.headers } : {},
|
|
160
|
-
responses
|
|
161
|
-
}
|
|
201
|
+
...sunset === void 0 ? {} : { sunset },
|
|
202
|
+
...route.body ? { body: toSchemaNode(route.body) } : {},
|
|
203
|
+
...route.query ? { query: toSchemaNode(route.query) } : {},
|
|
204
|
+
...route.headers ? { headers: toSchemaNode(route.headers) } : {},
|
|
205
|
+
responses
|
|
206
|
+
};
|
|
162
207
|
},
|
|
163
|
-
finalize: () =>
|
|
208
|
+
finalize: () => routes
|
|
164
209
|
};
|
|
165
210
|
});
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
211
|
+
/**
|
|
212
|
+
* Everything `kizuna diff` compares, as plain data.
|
|
213
|
+
*
|
|
214
|
+
* Writing this at each commit is what lets a comparison read the other side
|
|
215
|
+
* out of git rather than checking it out and running it.
|
|
216
|
+
*/
|
|
217
|
+
const toSnapshot = (api) => {
|
|
218
|
+
const declared = api.jobs;
|
|
219
|
+
const jobs = declared ? flattenJobs(declared).map((job) => job.jobKey).sort() : [];
|
|
220
|
+
const tools = Object.fromEntries(flattenRoutes(api.routes).filter(({ route }) => route.tool !== void 0 && route.tool !== false).map(({ routeKey }) => [routeKey, toToolName(routeKey)]));
|
|
221
|
+
return {
|
|
222
|
+
routes: routeSnapshots(api, {}),
|
|
223
|
+
jobs,
|
|
224
|
+
tools
|
|
225
|
+
};
|
|
175
226
|
};
|
|
176
227
|
/**
|
|
177
|
-
*
|
|
228
|
+
* YAML rather than JSON, so the file a reviewer opens in a pull request reads
|
|
229
|
+
* as the API rather than as punctuation.
|
|
178
230
|
*/
|
|
179
|
-
const
|
|
231
|
+
const renderSnapshot = (api) => stringify(toSnapshot(api), { lineWidth: 0 });
|
|
180
232
|
/**
|
|
181
|
-
*
|
|
182
|
-
*
|
|
183
|
-
*
|
|
184
|
-
|
|
233
|
+
* Where a config's snapshot lives. `.kizuna/` sits beside the config, so CI
|
|
234
|
+
* always knows where to look without being told, and a repository holding
|
|
235
|
+
* several configs keeps one snapshot per config.
|
|
236
|
+
*/
|
|
237
|
+
const snapshotPathFor = (configPath) => {
|
|
238
|
+
const declared = basename(configPath).replace(/\.config\.[cm]?[jt]s$/, "").replace(/^kizuna\./, "");
|
|
239
|
+
const name = declared === "" || declared === "kizuna" ? "snapshot" : `${declared}.snapshot`;
|
|
240
|
+
return join(dirname(configPath), ".kizuna", `${name}.yaml`);
|
|
241
|
+
};
|
|
242
|
+
/**
|
|
243
|
+
* The snapshot as a client target, so `kizuna generate` writes it and
|
|
244
|
+
* `kizuna generate --check` refuses to let it go stale.
|
|
185
245
|
*/
|
|
186
|
-
const
|
|
246
|
+
const snapshotTarget = (configPath) => ({
|
|
247
|
+
kind: "snapshot",
|
|
248
|
+
output: snapshotPathFor(configPath),
|
|
249
|
+
generate: renderSnapshot
|
|
250
|
+
});
|
|
251
|
+
//#endregion
|
|
252
|
+
//#region src/diff-apis.ts
|
|
253
|
+
const diffSnapshots = (before, after, options = {}) => {
|
|
187
254
|
const changes = [];
|
|
188
|
-
const beforeRoutes =
|
|
189
|
-
const afterRoutes =
|
|
255
|
+
const beforeRoutes = new Map(Object.entries(before.routes));
|
|
256
|
+
const afterRoutes = new Map(Object.entries(after.routes));
|
|
190
257
|
const removed = [...beforeRoutes.keys()].filter((key) => !afterRoutes.has(key));
|
|
191
258
|
const added = [...afterRoutes.keys()].filter((key) => !beforeRoutes.has(key));
|
|
192
259
|
const renames = /* @__PURE__ */ new Map();
|
|
@@ -203,10 +270,10 @@ const diffApis = (before, after) => {
|
|
|
203
270
|
if (match) renames.set(match, goneKey);
|
|
204
271
|
}
|
|
205
272
|
for (const [toKey, fromKey] of renames) changes.push({
|
|
206
|
-
level: "breaking",
|
|
273
|
+
level: options.dottedKeys ? "breaking" : "changed",
|
|
207
274
|
key: fromKey,
|
|
208
275
|
summary: `${fromKey} renamed to ${toKey}`,
|
|
209
|
-
detail: "
|
|
276
|
+
detail: "the generated client method changes name, the HTTP surface does not"
|
|
210
277
|
});
|
|
211
278
|
for (const key of removed) {
|
|
212
279
|
if ([...renames.values()].includes(key)) continue;
|
|
@@ -279,10 +346,10 @@ const diffApis = (before, after) => {
|
|
|
279
346
|
key,
|
|
280
347
|
summary: `${httpLabel(arrived, key)} ${change.summary}`
|
|
281
348
|
});
|
|
282
|
-
for (const [status, was] of gone.responses) {
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
for (const change of diffSchemas(was, now, "response",
|
|
349
|
+
for (const [status, was] of Object.entries(gone.responses)) {
|
|
350
|
+
if (!(status in arrived.responses)) continue;
|
|
351
|
+
const now = arrived.responses[status];
|
|
352
|
+
for (const change of diffSchemas(was ?? void 0, now ?? void 0, "response", status)) changes.push({
|
|
286
353
|
level: change.breaking ? "breaking" : "changed",
|
|
287
354
|
key,
|
|
288
355
|
summary: `${httpLabel(arrived, key)} ${change.summary}`
|
|
@@ -299,8 +366,8 @@ const diffApis = (before, after) => {
|
|
|
299
366
|
summary: `${httpLabel(arrived, key)} sunsets on ${arrived.sunset}`
|
|
300
367
|
});
|
|
301
368
|
}
|
|
302
|
-
for (const key of
|
|
303
|
-
if (
|
|
369
|
+
for (const key of options.jobs ? before.jobs : []) {
|
|
370
|
+
if (after.jobs.includes(key)) continue;
|
|
304
371
|
changes.push({
|
|
305
372
|
level: "breaking",
|
|
306
373
|
key,
|
|
@@ -308,10 +375,8 @@ const diffApis = (before, after) => {
|
|
|
308
375
|
detail: "whatever calls POST /jobs/run with that key stops working"
|
|
309
376
|
});
|
|
310
377
|
}
|
|
311
|
-
const
|
|
312
|
-
|
|
313
|
-
for (const [key, name] of beforeTools) {
|
|
314
|
-
const arrived = afterTools.get(key);
|
|
378
|
+
for (const [key, name] of options.tools ? Object.entries(before.tools) : []) {
|
|
379
|
+
const arrived = after.tools[key];
|
|
315
380
|
if (arrived === void 0) {
|
|
316
381
|
changes.push({
|
|
317
382
|
level: "breaking",
|
|
@@ -336,6 +401,13 @@ const diffApis = (before, after) => {
|
|
|
336
401
|
return changes.sort((left, right) => order[left.level] - order[right.level] || left.key.localeCompare(right.key));
|
|
337
402
|
};
|
|
338
403
|
/**
|
|
404
|
+
* What changed between two apis, for a caller holding both in memory.
|
|
405
|
+
*
|
|
406
|
+
* `kizuna diff` reads snapshots instead, so it never has to load the other
|
|
407
|
+
* side's code.
|
|
408
|
+
*/
|
|
409
|
+
const diffApis = (before, after, options = {}) => diffSnapshots(toSnapshot(before), toSnapshot(after), options);
|
|
410
|
+
/**
|
|
339
411
|
* One block per change, ready to print: the level, the summary, and the
|
|
340
412
|
* detail indented beneath it.
|
|
341
413
|
*/
|
|
@@ -404,7 +476,7 @@ const formatStale = (stale, command = "kizuna generate") => {
|
|
|
404
476
|
return client.reason === "missing" ? ` ${where} has not been generated` : ` ${where} is behind the config`;
|
|
405
477
|
});
|
|
406
478
|
return [
|
|
407
|
-
stale.length === 1 ? "A generated
|
|
479
|
+
stale.length === 1 ? "A generated file is out of date:" : `${stale.length} generated files are out of date:`,
|
|
408
480
|
...lines,
|
|
409
481
|
"",
|
|
410
482
|
`Run \`${command}\` and commit the result.`
|
|
@@ -565,22 +637,32 @@ const HEADER = `/**
|
|
|
565
637
|
* and re-run \`kizuna generate\` to regenerate this file.
|
|
566
638
|
*/`;
|
|
567
639
|
/**
|
|
640
|
+
* Points a relative specifier at the same file from somewhere else, so a
|
|
641
|
+
* `Config` written outside the config's own directory still resolves.
|
|
642
|
+
*/
|
|
643
|
+
const rebaseSpecifier = (specifier, configDir, outputDir) => {
|
|
644
|
+
if (!specifier.startsWith("./") && !specifier.startsWith("../")) return specifier;
|
|
645
|
+
const rebased = relative(outputDir, resolve(configDir, specifier)).split(sep).join("/");
|
|
646
|
+
return rebased.startsWith(".") ? rebased : `./${rebased}`;
|
|
647
|
+
};
|
|
648
|
+
/**
|
|
568
649
|
* The `import type` lines the interface needs, grouped by module and written in
|
|
569
650
|
* the order the config imported them.
|
|
570
651
|
*/
|
|
571
|
-
const renderImports = (imports, used) => {
|
|
652
|
+
const renderImports = (imports, used, rebase) => {
|
|
572
653
|
const byModule = /* @__PURE__ */ new Map();
|
|
573
654
|
const defaults = [];
|
|
574
655
|
for (const [local, imported] of imports) {
|
|
575
656
|
if (!used.has(local)) continue;
|
|
657
|
+
const module = rebase(imported.module);
|
|
576
658
|
if (imported.isDefault) {
|
|
577
|
-
defaults.push(`import type ${local} from '${
|
|
659
|
+
defaults.push(`import type ${local} from '${module}';`);
|
|
578
660
|
continue;
|
|
579
661
|
}
|
|
580
662
|
const specifier = imported.exported === local ? local : `${imported.exported} as ${local}`;
|
|
581
|
-
const existing = byModule.get(
|
|
663
|
+
const existing = byModule.get(module);
|
|
582
664
|
if (existing) existing.push(specifier);
|
|
583
|
-
else byModule.set(
|
|
665
|
+
else byModule.set(module, [specifier]);
|
|
584
666
|
}
|
|
585
667
|
const named = [...byModule].map(([module, names]) => `import type { ${names.join(", ")} } from '${module}';`);
|
|
586
668
|
return [...defaults, ...named].join("\n");
|
|
@@ -592,8 +674,11 @@ const renderImports = (imports, used) => {
|
|
|
592
674
|
* Read from the config's syntax rather than by running it: the generated file
|
|
593
675
|
* imports the same names the config did, and a config that loads is not a
|
|
594
676
|
* prerequisite for typing the routes it serves.
|
|
677
|
+
*
|
|
678
|
+
* `outputPath` is what the imports are written relative to. Left out, they stay
|
|
679
|
+
* relative to the config.
|
|
595
680
|
*/
|
|
596
|
-
const generateConfigTypes = (configSource, fileName = "kizuna.config.ts") => {
|
|
681
|
+
const generateConfigTypes = (configSource, fileName = "kizuna.config.ts", outputPath) => {
|
|
597
682
|
const source = ts.createSourceFile(fileName, configSource, ts.ScriptTarget.Latest, true);
|
|
598
683
|
const imports = collectImports(source);
|
|
599
684
|
const configObject = findConfigObject(source);
|
|
@@ -620,7 +705,9 @@ const generateConfigTypes = (configSource, fileName = "kizuna.config.ts") => {
|
|
|
620
705
|
} else if (key === "plugins") lines.push(` ${key}: ${typeOfPluginList(value, used)};`);
|
|
621
706
|
else lines.push(` ${key}: ${typeOfValue(value, used, key)};`);
|
|
622
707
|
}
|
|
623
|
-
const
|
|
708
|
+
const configDir = dirname(resolve(fileName));
|
|
709
|
+
const outputDir = outputPath === void 0 ? configDir : dirname(resolve(outputPath));
|
|
710
|
+
const importBlock = renderImports(imports, used, (specifier) => outputDir === configDir ? specifier : rebaseSpecifier(specifier, configDir, outputDir));
|
|
624
711
|
const body = `export interface Config {\n${lines.join("\n")}\n}`;
|
|
625
712
|
return `${importBlock === "" ? HEADER : `${HEADER}\n${importBlock}`}\n\n${body}\n`;
|
|
626
713
|
};
|
|
@@ -628,44 +715,59 @@ const generateConfigTypes = (configSource, fileName = "kizuna.config.ts") => {
|
|
|
628
715
|
//#region src/diff-against.ts
|
|
629
716
|
const git = (cwd, ...args) => execFileSync("git", args, {
|
|
630
717
|
cwd,
|
|
631
|
-
encoding: "utf8"
|
|
718
|
+
encoding: "utf8",
|
|
719
|
+
stdio: [
|
|
720
|
+
"ignore",
|
|
721
|
+
"pipe",
|
|
722
|
+
"ignore"
|
|
723
|
+
]
|
|
632
724
|
}).trim();
|
|
633
725
|
/**
|
|
634
|
-
*
|
|
635
|
-
*
|
|
636
|
-
* The ref is checked out into a worktree inside the repository, which costs a
|
|
637
|
-
* few files rather than a clone. Nothing is installed there: module resolution
|
|
638
|
-
* walks up to the repository's own `node_modules`, so the base tree needs only
|
|
639
|
-
* its source.
|
|
640
|
-
*
|
|
641
|
-
* @example
|
|
642
|
-
* const changes = await diffAgainst('main', './src/contract.ts');
|
|
726
|
+
* The repository holding the snapshot, falling back to the one the caller ran in.
|
|
643
727
|
*/
|
|
644
|
-
const
|
|
645
|
-
const { cwd = process.cwd() } = options;
|
|
646
|
-
const root = git(cwd, "rev-parse", "--show-toplevel");
|
|
647
|
-
const absolute = isAbsolute(configPath) ? configPath : resolve(cwd, configPath);
|
|
648
|
-
const fromRoot = relative(root, absolute);
|
|
649
|
-
if (fromRoot.startsWith("..")) throw new Error(`${configPath} is outside the repository at ${root}`);
|
|
650
|
-
const worktree = mkdtempSync(join(root, ".kizuna-diff-"));
|
|
728
|
+
const repositoryFor = (directory, cwd) => {
|
|
651
729
|
try {
|
|
652
|
-
git(
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
}
|
|
662
|
-
rmSync(worktree, {
|
|
663
|
-
recursive: true,
|
|
664
|
-
force: true
|
|
665
|
-
});
|
|
666
|
-
}
|
|
730
|
+
return git(directory, "rev-parse", "--show-toplevel");
|
|
731
|
+
} catch {
|
|
732
|
+
return git(cwd, "rev-parse", "--show-toplevel");
|
|
733
|
+
}
|
|
734
|
+
};
|
|
735
|
+
const parseSnapshot = (contents, source) => {
|
|
736
|
+
try {
|
|
737
|
+
return parse(contents);
|
|
738
|
+
} catch {
|
|
739
|
+
throw new Error(`${source} is not a Kizuna snapshot. Run \`kizuna generate\` to write one.`);
|
|
667
740
|
}
|
|
668
741
|
};
|
|
742
|
+
/**
|
|
743
|
+
* Reads a path out of a ref without checking anything out.
|
|
744
|
+
*/
|
|
745
|
+
const showAtRef = (root, ref, path) => {
|
|
746
|
+
try {
|
|
747
|
+
return git(root, "show", `${ref}:${path}`);
|
|
748
|
+
} catch {
|
|
749
|
+
throw new Error(`${path} does not exist at ${ref}. It was added or renamed since, so there is nothing to compare against.`);
|
|
750
|
+
}
|
|
751
|
+
};
|
|
752
|
+
const readSnapshot = (path) => {
|
|
753
|
+
if (!existsSync(path)) throw new Error(`No snapshot at ${path}. Run \`kizuna generate\` to write one.`);
|
|
754
|
+
return parseSnapshot(readFileSync(path, "utf8"), path);
|
|
755
|
+
};
|
|
756
|
+
/**
|
|
757
|
+
* Compares a snapshot against the same file at a git ref.
|
|
758
|
+
*
|
|
759
|
+
* The ref is read with `git show`, so nothing is checked out, installed or
|
|
760
|
+
* executed for the other side. A snapshot written a year ago compares fine
|
|
761
|
+
* against one written today.
|
|
762
|
+
*/
|
|
763
|
+
const diffAgainst = (ref, snapshotPath, options = {}) => {
|
|
764
|
+
const { cwd = process.cwd(), ...diff } = options;
|
|
765
|
+
const absolute = isAbsolute(snapshotPath) ? snapshotPath : resolve(cwd, snapshotPath);
|
|
766
|
+
const root = repositoryFor(dirname(absolute), cwd);
|
|
767
|
+
const fromRoot = relative(root, absolute);
|
|
768
|
+
if (fromRoot.startsWith("..")) throw new Error(`${snapshotPath} is outside the repository at ${root}`);
|
|
769
|
+
return diffSnapshots(parseSnapshot(showAtRef(root, ref, fromRoot.split("\\").join("/")), `${fromRoot} at ${ref}`), readSnapshot(absolute), diff);
|
|
770
|
+
};
|
|
669
771
|
//#endregion
|
|
670
772
|
//#region src/route-map.ts
|
|
671
773
|
const permissionList = (requires) => {
|
|
@@ -744,4 +846,4 @@ const formatRoutes = (entries) => {
|
|
|
744
846
|
}).join("\n");
|
|
745
847
|
};
|
|
746
848
|
//#endregion
|
|
747
|
-
export {
|
|
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, writeClients as l, snapshotPathFor as m, routeMap as n, generateConfigTypes as o, hasBreakingChange as p, diffAgainst as r, checkClients as s, formatRoutes as t, diffApis as u };
|