@xnetjs/cli 0.0.2

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Chris Smothers
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,49 @@
1
+ # @xnetjs/cli
2
+
3
+ CLI tools for schema migration workflows, schema diffing, and data integrity diagnostics.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ pnpm add -D @xnetjs/cli
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```bash
14
+ # Show commands
15
+ pnpm --filter @xnetjs/cli dev -- --help
16
+
17
+ # Analyze schema changes
18
+ pnpm --filter @xnetjs/cli dev -- migrate analyze --from Task@1.0.0 --to Task@2.0.0 --schema-file ./schemas.json
19
+
20
+ # Generate migration lens code
21
+ pnpm --filter @xnetjs/cli dev -- migrate generate --from Task@1.0.0 --to Task@2.0.0 -o ./migrations/task-v1-v2.ts
22
+
23
+ # Extract and diff schemas (CI-friendly)
24
+ pnpm --filter @xnetjs/cli dev -- schema extract --output schemas.json
25
+ pnpm --filter @xnetjs/cli dev -- schema diff schemas-main.json schemas-pr.json --fail-on-breaking
26
+
27
+ # Run integrity checks
28
+ pnpm --filter @xnetjs/cli dev -- doctor --quick
29
+ ```
30
+
31
+ ## Commands
32
+
33
+ - `migrate` -- Analyze schema deltas, generate lens code, and run migration flows
34
+ - `schema` -- Extract schemas and diff schema snapshots for CI gates
35
+ - `doctor` -- Run integrity checks, repair helpers, and import/export diagnostics
36
+
37
+ ## Programmatic API
38
+
39
+ You can also use utilities directly:
40
+
41
+ ```ts
42
+ import { diffSchemas, generateLensCode } from '@xnetjs/cli'
43
+ ```
44
+
45
+ ## Testing
46
+
47
+ ```bash
48
+ pnpm --filter @xnetjs/cli test
49
+ ```
@@ -0,0 +1,314 @@
1
+ // src/utils/lens-generator.ts
2
+ function indent(code, level) {
3
+ const spaces = " ".repeat(level);
4
+ return code.split("\n").map((line) => line.trim() ? spaces + line : line).join("\n");
5
+ }
6
+ function generateOperationCode(change) {
7
+ if (!change.suggestedLens) return null;
8
+ if (change.suggestedLens.startsWith("// TODO")) return null;
9
+ return change.suggestedLens;
10
+ }
11
+ function generateComment(change) {
12
+ const riskBadge = change.risk === "breaking" ? "// BREAKING: " : change.risk === "caution" ? "// CAUTION: " : "// ";
13
+ return `${riskBadge}${change.description}`;
14
+ }
15
+ function generateLensCode(options) {
16
+ const { diff, sourceIRI, targetIRI, includeComments = true, includeTodos = true } = options;
17
+ const operations = [];
18
+ const manualItems = [];
19
+ let isComplete = true;
20
+ for (const change of diff.changes) {
21
+ if (change.risk === "safe" && !change.suggestedLens) {
22
+ continue;
23
+ }
24
+ const operationCode = generateOperationCode(change);
25
+ if (operationCode) {
26
+ if (includeComments) {
27
+ operations.push(generateComment(change));
28
+ }
29
+ operations.push(operationCode + ",");
30
+ } else if (change.risk !== "safe") {
31
+ isComplete = false;
32
+ manualItems.push(change.description);
33
+ if (includeTodos) {
34
+ operations.push(`// TODO: ${change.description}`);
35
+ if (change.suggestedLens) {
36
+ operations.push(change.suggestedLens);
37
+ }
38
+ operations.push(`// transform('${change.property}', (v) => /* ... */, (v) => /* ... */),`);
39
+ }
40
+ }
41
+ }
42
+ const code = `/**
43
+ * Migration lens: ${sourceIRI} -> ${targetIRI}
44
+ *
45
+ * Generated by: xnet migrate generate
46
+ * Risk level: ${diff.overallRisk.toUpperCase()}
47
+ * Changes: ${diff.summary.safe} safe, ${diff.summary.caution} caution, ${diff.summary.breaking} breaking
48
+ *${!isComplete ? "\n * NOTE: This lens requires manual completion. See TODO comments below." : ""}
49
+ */
50
+
51
+ import { composeLens, rename, convert, addDefault, remove, transform } from '@xnetjs/data'
52
+ import type { SchemaIRI } from '@xnetjs/data'
53
+
54
+ const SOURCE: SchemaIRI = '${sourceIRI}' as SchemaIRI
55
+ const TARGET: SchemaIRI = '${targetIRI}' as SchemaIRI
56
+
57
+ /**
58
+ * Migrate from ${sourceIRI.split("@")[0].split("/").pop()}@${diff.fromVersion} to @${diff.toVersion}
59
+ */
60
+ export const lens = composeLens(
61
+ SOURCE,
62
+ TARGET,
63
+ ${indent(operations.join("\n"), 1)}
64
+ )
65
+
66
+ export default lens
67
+ `;
68
+ return {
69
+ code,
70
+ isComplete,
71
+ manualItems
72
+ };
73
+ }
74
+ function generateLensSnippet(diff) {
75
+ const operations = [];
76
+ for (const change of diff.changes) {
77
+ const operationCode = generateOperationCode(change);
78
+ if (operationCode) {
79
+ operations.push(operationCode);
80
+ }
81
+ }
82
+ if (operations.length === 0) {
83
+ return "// No migration operations needed";
84
+ }
85
+ return `composeLens(
86
+ ${indent(operations.join(",\n"), 1)}
87
+ )`;
88
+ }
89
+
90
+ // src/utils/schema-diff.ts
91
+ function getPropertyMap(schema) {
92
+ const map = /* @__PURE__ */ new Map();
93
+ for (const prop of schema.properties) {
94
+ map.set(prop.name, prop);
95
+ }
96
+ return map;
97
+ }
98
+ function getDefaultValue(def) {
99
+ switch (def.type) {
100
+ case "text":
101
+ return "''";
102
+ case "number":
103
+ return "0";
104
+ case "checkbox":
105
+ return "false";
106
+ case "date":
107
+ return "null";
108
+ case "select":
109
+ return def.options?.[0] ? `'${def.options[0].value}'` : "null";
110
+ case "multiSelect":
111
+ return "[]";
112
+ case "person":
113
+ return "[]";
114
+ case "relation":
115
+ return "[]";
116
+ case "url":
117
+ return "null";
118
+ case "email":
119
+ return "null";
120
+ case "phone":
121
+ return "null";
122
+ case "file":
123
+ return "[]";
124
+ default:
125
+ return "null";
126
+ }
127
+ }
128
+ function areTypesCompatible(oldType, newType) {
129
+ if (oldType === newType) return true;
130
+ const wideningPairs = [
131
+ ["text", "url"],
132
+ ["text", "email"],
133
+ ["text", "phone"]
134
+ ];
135
+ return wideningPairs.some(([from, to]) => oldType === to && newType === from);
136
+ }
137
+ function detectPotentialRename(removedProps, addedProps) {
138
+ const renames = [];
139
+ for (const [removedName, removedDef] of removedProps) {
140
+ for (const [addedName, addedDef] of addedProps) {
141
+ if (removedDef.type === addedDef.type) {
142
+ let confidence = 0.5;
143
+ const removedLower = removedName.toLowerCase();
144
+ const addedLower = addedName.toLowerCase();
145
+ if (removedLower.includes(addedLower) || addedLower.includes(removedLower)) {
146
+ confidence += 0.2;
147
+ }
148
+ if (removedDef.required === addedDef.required) {
149
+ confidence += 0.1;
150
+ }
151
+ if (removedDef.type === "select" && addedDef.type === "select") {
152
+ const oldOptions = new Set(removedDef.options?.map((o) => o.value) ?? []);
153
+ const newOptions = new Set(addedDef.options?.map((o) => o.value) ?? []);
154
+ const intersection = new Set([...oldOptions].filter((x) => newOptions.has(x)));
155
+ if (intersection.size === oldOptions.size && oldOptions.size === newOptions.size) {
156
+ confidence += 0.2;
157
+ }
158
+ }
159
+ if (confidence >= 0.5) {
160
+ renames.push({ from: removedName, to: addedName, confidence });
161
+ }
162
+ }
163
+ }
164
+ }
165
+ renames.sort((a, b) => b.confidence - a.confidence);
166
+ const usedFrom = /* @__PURE__ */ new Set();
167
+ const usedTo = /* @__PURE__ */ new Set();
168
+ return renames.filter((r) => {
169
+ if (usedFrom.has(r.from) || usedTo.has(r.to)) return false;
170
+ usedFrom.add(r.from);
171
+ usedTo.add(r.to);
172
+ return true;
173
+ });
174
+ }
175
+ function diffSchemas(oldSchema, newSchema) {
176
+ const changes = [];
177
+ const oldProps = getPropertyMap(oldSchema);
178
+ const newProps = getPropertyMap(newSchema);
179
+ const removedProps = /* @__PURE__ */ new Map();
180
+ const addedProps = /* @__PURE__ */ new Map();
181
+ for (const [name, def] of oldProps) {
182
+ if (!newProps.has(name)) {
183
+ removedProps.set(name, def);
184
+ }
185
+ }
186
+ for (const [name, def] of newProps) {
187
+ if (!oldProps.has(name)) {
188
+ addedProps.set(name, def);
189
+ }
190
+ }
191
+ const potentialRenames = detectPotentialRename(removedProps, addedProps);
192
+ for (const { from, to } of potentialRenames) {
193
+ const oldDef = removedProps.get(from);
194
+ const newDef = addedProps.get(to);
195
+ changes.push({
196
+ type: "rename",
197
+ property: from,
198
+ newProperty: to,
199
+ risk: "breaking",
200
+ description: `Renamed property "${from}" to "${to}"`,
201
+ suggestedLens: `rename('${from}', '${to}')`,
202
+ oldDefinition: oldDef,
203
+ newDefinition: newDef
204
+ });
205
+ removedProps.delete(from);
206
+ addedProps.delete(to);
207
+ }
208
+ for (const [name, def] of removedProps) {
209
+ changes.push({
210
+ type: "remove",
211
+ property: name,
212
+ risk: "caution",
213
+ description: `Removed property "${name}" (${def.type})`,
214
+ suggestedLens: `remove('${name}')`,
215
+ oldDefinition: def
216
+ });
217
+ }
218
+ for (const [name, def] of addedProps) {
219
+ const isRequired = def.required === true;
220
+ changes.push({
221
+ type: "add",
222
+ property: name,
223
+ risk: isRequired ? "caution" : "safe",
224
+ description: isRequired ? `Added required property "${name}" (${def.type}) - needs default value` : `Added optional property "${name}" (${def.type})`,
225
+ suggestedLens: isRequired ? `addDefault('${name}', ${getDefaultValue(def)})` : void 0,
226
+ newDefinition: def
227
+ });
228
+ }
229
+ for (const [name, oldDef] of oldProps) {
230
+ const newDef = newProps.get(name);
231
+ if (!newDef) continue;
232
+ if (oldDef.type !== newDef.type) {
233
+ const compatible = areTypesCompatible(oldDef.type, newDef.type);
234
+ changes.push({
235
+ type: "modify",
236
+ property: name,
237
+ risk: compatible ? "caution" : "breaking",
238
+ description: `Changed type of "${name}" from ${oldDef.type} to ${newDef.type}`,
239
+ suggestedLens: compatible ? void 0 : `// TODO: Custom transform for ${oldDef.type} -> ${newDef.type}`,
240
+ oldDefinition: oldDef,
241
+ newDefinition: newDef
242
+ });
243
+ continue;
244
+ }
245
+ if (oldDef.type === "select" && newDef.type === "select") {
246
+ const oldOptions = new Set(oldDef.options?.map((o) => o.value) ?? []);
247
+ const newOptions = new Set(newDef.options?.map((o) => o.value) ?? []);
248
+ const removedOptions = [...oldOptions].filter((o) => !newOptions.has(o));
249
+ if (removedOptions.length > 0) {
250
+ changes.push({
251
+ type: "modify",
252
+ property: name,
253
+ risk: "breaking",
254
+ description: `Removed select options from "${name}": ${removedOptions.join(", ")}`,
255
+ suggestedLens: `// TODO: Map removed options ${removedOptions.join(", ")} to valid values`,
256
+ oldDefinition: oldDef,
257
+ newDefinition: newDef
258
+ });
259
+ }
260
+ const addedOptions = [...newOptions].filter((o) => !oldOptions.has(o));
261
+ if (addedOptions.length > 0 && removedOptions.length === 0) {
262
+ changes.push({
263
+ type: "modify",
264
+ property: name,
265
+ risk: "safe",
266
+ description: `Added select options to "${name}": ${addedOptions.join(", ")}`,
267
+ oldDefinition: oldDef,
268
+ newDefinition: newDef
269
+ });
270
+ }
271
+ }
272
+ if (!oldDef.required && newDef.required) {
273
+ changes.push({
274
+ type: "modify",
275
+ property: name,
276
+ risk: "caution",
277
+ description: `Made "${name}" required (was optional) - needs default for existing null values`,
278
+ suggestedLens: `addDefault('${name}', ${getDefaultValue(newDef)})`,
279
+ oldDefinition: oldDef,
280
+ newDefinition: newDef
281
+ });
282
+ } else if (oldDef.required && !newDef.required) {
283
+ changes.push({
284
+ type: "modify",
285
+ property: name,
286
+ risk: "safe",
287
+ description: `Made "${name}" optional (was required)`,
288
+ oldDefinition: oldDef,
289
+ newDefinition: newDef
290
+ });
291
+ }
292
+ }
293
+ const summary = {
294
+ safe: changes.filter((c) => c.risk === "safe").length,
295
+ caution: changes.filter((c) => c.risk === "caution").length,
296
+ breaking: changes.filter((c) => c.risk === "breaking").length
297
+ };
298
+ const overallRisk = summary.breaking > 0 ? "breaking" : summary.caution > 0 ? "caution" : "safe";
299
+ const autoMigratable = changes.filter((c) => c.risk !== "safe").every((c) => c.suggestedLens && !c.suggestedLens.startsWith("// TODO"));
300
+ return {
301
+ fromVersion: oldSchema.version,
302
+ toVersion: newSchema.version,
303
+ changes,
304
+ overallRisk,
305
+ autoMigratable,
306
+ summary
307
+ };
308
+ }
309
+
310
+ export {
311
+ generateLensCode,
312
+ generateLensSnippet,
313
+ diffSchemas
314
+ };
package/dist/cli.d.ts ADDED
@@ -0,0 +1 @@
1
+ #!/usr/bin/env node