infernoflow 0.18.0 → 0.20.0
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/bin/infernoflow.mjs +72 -0
- package/dist/lib/commands/audit.mjs +335 -0
- package/dist/lib/commands/dashboard.mjs +248 -2
- package/dist/lib/commands/export.mjs +239 -0
- package/dist/lib/commands/health.mjs +309 -0
- package/dist/lib/commands/link.mjs +342 -0
- package/dist/lib/commands/monorepo.mjs +427 -0
- package/dist/lib/commands/scout.mjs +291 -0
- package/dist/lib/commands/snapshot.mjs +383 -0
- package/dist/lib/ui/errors.mjs +142 -0
- package/package.json +1 -1
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* infernoflow scout
|
|
3
|
+
*
|
|
4
|
+
* Scans your source files for undocumented capabilities — functions, routes,
|
|
5
|
+
* exports, controllers — that exist in code but are missing from the contract.
|
|
6
|
+
*
|
|
7
|
+
* Smarter than --adopt: it reads your existing contract, diffs against what
|
|
8
|
+
* it finds in source, and only surfaces genuine gaps.
|
|
9
|
+
*
|
|
10
|
+
* Usage:
|
|
11
|
+
* infernoflow scout Scan src/, lib/, app/ (auto-detected)
|
|
12
|
+
* infernoflow scout --dir src,api Custom scan directories
|
|
13
|
+
* infernoflow scout --apply Write discovered caps to capabilities.json
|
|
14
|
+
* infernoflow scout --json Machine-readable output
|
|
15
|
+
* infernoflow scout --min-confidence 0.7 Only show high-confidence hits
|
|
16
|
+
*
|
|
17
|
+
* Output:
|
|
18
|
+
* Lists undocumented capabilities with confidence score + source location.
|
|
19
|
+
* With --apply, merges them into the contract file.
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
import * as fs from "node:fs";
|
|
23
|
+
import * as path from "node:path";
|
|
24
|
+
import { done, warn, info, bold, cyan, gray, green, yellow, red } from "../ui/output.mjs";
|
|
25
|
+
|
|
26
|
+
// ── Pattern library ───────────────────────────────────────────────────────────
|
|
27
|
+
|
|
28
|
+
const PATTERNS = [
|
|
29
|
+
// Express / Fastify / Hono routes
|
|
30
|
+
{
|
|
31
|
+
name: "http-route",
|
|
32
|
+
regex: /(?:app|router|server)\s*\.\s*(get|post|put|patch|delete|head|options)\s*\(\s*['"`]([^'"`]+)['"`]/gi,
|
|
33
|
+
extract: (m) => ({ id: routeToId(m[2], m[1]), hint: `${m[1].toUpperCase()} ${m[2]}`, confidence: 0.85 }),
|
|
34
|
+
},
|
|
35
|
+
// Next.js / Nuxt API route files (path from filename)
|
|
36
|
+
{
|
|
37
|
+
name: "api-file",
|
|
38
|
+
filePattern: /[/\\](pages[/\\]api|app[/\\]api)[/\\](.+)\.(js|ts|mjs)$/,
|
|
39
|
+
extract: (filePath) => {
|
|
40
|
+
const m = filePath.match(/[/\\](pages[/\\]api|app[/\\]api)[/\\](.+)\.(js|ts|mjs)$/);
|
|
41
|
+
if (!m) return null;
|
|
42
|
+
const route = m[2].replace(/[/\\]index$/, "").replace(/\[([^\]]+)\]/g, ":$1");
|
|
43
|
+
return { id: routeToId("/" + route, "api"), hint: `Next.js API: /${route}`, confidence: 0.9 };
|
|
44
|
+
},
|
|
45
|
+
},
|
|
46
|
+
// Named exports (functions, classes, consts)
|
|
47
|
+
{
|
|
48
|
+
name: "export",
|
|
49
|
+
regex: /export\s+(?:async\s+)?(?:function|class|const|let)\s+([A-Z][A-Za-z0-9]+)/g,
|
|
50
|
+
extract: (m) => ({ id: camelToId(m[1]), hint: `export ${m[1]}`, confidence: 0.65 }),
|
|
51
|
+
},
|
|
52
|
+
// Default export object keys (controller-style)
|
|
53
|
+
{
|
|
54
|
+
name: "controller-method",
|
|
55
|
+
regex: /^\s{2,}(?:async\s+)?([a-z][A-Za-z0-9]+)\s*[:(]/gm,
|
|
56
|
+
extract: (m) => ({ id: camelToId(m[1]), hint: `method ${m[1]}`, confidence: 0.5 }),
|
|
57
|
+
minLen: 5,
|
|
58
|
+
},
|
|
59
|
+
// GraphQL resolver fields
|
|
60
|
+
{
|
|
61
|
+
name: "graphql-resolver",
|
|
62
|
+
regex: /['"`]?([A-Za-z][A-Za-z0-9]+)['"`]?\s*:\s*(?:async\s+)?\([^)]*\)\s*(?:=>|{)/g,
|
|
63
|
+
extract: (m) => ({ id: camelToId(m[1]), hint: `resolver ${m[1]}`, confidence: 0.6 }),
|
|
64
|
+
},
|
|
65
|
+
// Django / Flask URL patterns
|
|
66
|
+
{
|
|
67
|
+
name: "python-route",
|
|
68
|
+
regex: /(?:path|url|re_path)\s*\(\s*['"`]([^'"`]+)['"`]\s*,\s*([A-Za-z0-9_.]+)/g,
|
|
69
|
+
extract: (m) => ({ id: routeToId(m[1], "view"), hint: `view ${m[2]} @ ${m[1]}`, confidence: 0.8 }),
|
|
70
|
+
},
|
|
71
|
+
// Rails routes
|
|
72
|
+
{
|
|
73
|
+
name: "rails-route",
|
|
74
|
+
regex: /(?:get|post|put|patch|delete)\s+['"`]([^'"`]+)['"`]\s*,\s*to:\s*['"`]([^'"`]+)['"`]/g,
|
|
75
|
+
extract: (m) => ({ id: routeToId(m[1], "rails"), hint: `Rails: ${m[2]}`, confidence: 0.8 }),
|
|
76
|
+
},
|
|
77
|
+
];
|
|
78
|
+
|
|
79
|
+
// ── Helpers ───────────────────────────────────────────────────────────────────
|
|
80
|
+
|
|
81
|
+
function routeToId(route, method) {
|
|
82
|
+
return route
|
|
83
|
+
.replace(/^\//, "")
|
|
84
|
+
.replace(/\/:?[^/]+/g, "") // strip params
|
|
85
|
+
.replace(/\//g, "-")
|
|
86
|
+
.replace(/[^a-zA-Z0-9-]/g, "")
|
|
87
|
+
.replace(/-+/g, "-")
|
|
88
|
+
.replace(/^-|-$/g, "")
|
|
89
|
+
.toLowerCase() || method.toLowerCase() + "-root";
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function camelToId(name) {
|
|
93
|
+
return name
|
|
94
|
+
.replace(/([A-Z])/g, "-$1")
|
|
95
|
+
.toLowerCase()
|
|
96
|
+
.replace(/^-/, "")
|
|
97
|
+
.replace(/-+/g, "-");
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// ── File scanner ──────────────────────────────────────────────────────────────
|
|
101
|
+
|
|
102
|
+
const SOURCE_EXTENSIONS = new Set([".js", ".mjs", ".cjs", ".ts", ".tsx", ".jsx", ".py", ".rb"]);
|
|
103
|
+
const IGNORE_DIRS = new Set(["node_modules", ".git", "dist", "build", ".next", "__pycache__", "vendor", "coverage"]);
|
|
104
|
+
|
|
105
|
+
function* walkFiles(dir) {
|
|
106
|
+
if (!fs.existsSync(dir)) return;
|
|
107
|
+
const entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
108
|
+
for (const e of entries) {
|
|
109
|
+
if (IGNORE_DIRS.has(e.name)) continue;
|
|
110
|
+
const full = path.join(dir, e.name);
|
|
111
|
+
if (e.isDirectory()) {
|
|
112
|
+
yield* walkFiles(full);
|
|
113
|
+
} else if (e.isFile() && SOURCE_EXTENSIONS.has(path.extname(e.name))) {
|
|
114
|
+
yield full;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function scanFile(filePath) {
|
|
120
|
+
const hits = [];
|
|
121
|
+
|
|
122
|
+
// File-pattern rules (e.g. Next.js API files)
|
|
123
|
+
for (const pat of PATTERNS) {
|
|
124
|
+
if (pat.filePattern && pat.filePattern.test(filePath)) {
|
|
125
|
+
const result = pat.extract(filePath);
|
|
126
|
+
if (result) hits.push({ ...result, source: filePath, pattern: pat.name });
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
let content;
|
|
131
|
+
try { content = fs.readFileSync(filePath, "utf8"); } catch { return hits; }
|
|
132
|
+
|
|
133
|
+
for (const pat of PATTERNS) {
|
|
134
|
+
if (!pat.regex) continue;
|
|
135
|
+
pat.regex.lastIndex = 0;
|
|
136
|
+
let m;
|
|
137
|
+
while ((m = pat.regex.exec(content)) !== null) {
|
|
138
|
+
const result = pat.extract(m);
|
|
139
|
+
if (!result) continue;
|
|
140
|
+
if (pat.minLen && result.id.length < pat.minLen) continue;
|
|
141
|
+
hits.push({ ...result, source: filePath, pattern: pat.name });
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
return hits;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// ── Gap detection ─────────────────────────────────────────────────────────────
|
|
149
|
+
|
|
150
|
+
function readContract(infernoDir) {
|
|
151
|
+
for (const f of ["capabilities.json", "contract.json"]) {
|
|
152
|
+
const p = path.join(infernoDir, f);
|
|
153
|
+
if (!fs.existsSync(p)) continue;
|
|
154
|
+
try { return { file: f, data: JSON.parse(fs.readFileSync(p, "utf8")) }; } catch {}
|
|
155
|
+
}
|
|
156
|
+
return null;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function getKnownIds(contract) {
|
|
160
|
+
const caps = contract?.data?.capabilities || [];
|
|
161
|
+
return new Set(caps.map(c => (typeof c === "string" ? c : c.id || c.name || "").toLowerCase()));
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function dedupeHits(hits) {
|
|
165
|
+
const seen = new Map();
|
|
166
|
+
for (const h of hits) {
|
|
167
|
+
const key = h.id;
|
|
168
|
+
const existing = seen.get(key);
|
|
169
|
+
if (!existing || h.confidence > existing.confidence) {
|
|
170
|
+
seen.set(key, h);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
return [...seen.values()].sort((a, b) => b.confidence - a.confidence);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// ── Apply ─────────────────────────────────────────────────────────────────────
|
|
177
|
+
|
|
178
|
+
function applyToContract(infernoDir, contract, newCaps) {
|
|
179
|
+
const caps = contract.data.capabilities || [];
|
|
180
|
+
const added = [];
|
|
181
|
+
|
|
182
|
+
for (const cap of newCaps) {
|
|
183
|
+
const entry = {
|
|
184
|
+
id: cap.id,
|
|
185
|
+
description: cap.hint,
|
|
186
|
+
since: new Date().toISOString().slice(0, 10),
|
|
187
|
+
source: "scout",
|
|
188
|
+
};
|
|
189
|
+
caps.push(entry);
|
|
190
|
+
added.push(cap.id);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
contract.data.capabilities = caps;
|
|
194
|
+
fs.writeFileSync(
|
|
195
|
+
path.join(infernoDir, contract.file),
|
|
196
|
+
JSON.stringify(contract.data, null, 2) + "\n"
|
|
197
|
+
);
|
|
198
|
+
return added;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// ── Entry ─────────────────────────────────────────────────────────────────────
|
|
202
|
+
|
|
203
|
+
export async function scoutCommand(rawArgs) {
|
|
204
|
+
const args = rawArgs.slice(1);
|
|
205
|
+
const jsonMode = args.includes("--json");
|
|
206
|
+
const apply = args.includes("--apply");
|
|
207
|
+
const cwd = process.cwd();
|
|
208
|
+
const infernoDir = path.join(cwd, "inferno");
|
|
209
|
+
|
|
210
|
+
if (!fs.existsSync(infernoDir)) {
|
|
211
|
+
const msg = "inferno/ not found. Run: infernoflow init";
|
|
212
|
+
if (jsonMode) console.log(JSON.stringify({ ok: false, error: msg }));
|
|
213
|
+
else warn(msg);
|
|
214
|
+
process.exit(1);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
// Parse --dir flag
|
|
218
|
+
const dirIdx = args.indexOf("--dir");
|
|
219
|
+
const scanDirs = dirIdx !== -1
|
|
220
|
+
? args[dirIdx + 1].split(",").map(d => path.resolve(cwd, d.trim()))
|
|
221
|
+
: ["src", "lib", "app", "api", "pages", "routes", "controllers", "handlers"]
|
|
222
|
+
.map(d => path.join(cwd, d))
|
|
223
|
+
.filter(fs.existsSync);
|
|
224
|
+
|
|
225
|
+
// Parse --min-confidence
|
|
226
|
+
const confIdx = args.indexOf("--min-confidence");
|
|
227
|
+
const minConfidence = confIdx !== -1 ? parseFloat(args[confIdx + 1]) : 0.6;
|
|
228
|
+
|
|
229
|
+
if (!scanDirs.length) {
|
|
230
|
+
const msg = "No source directories found. Use --dir src,lib,api";
|
|
231
|
+
if (jsonMode) console.log(JSON.stringify({ ok: false, error: msg }));
|
|
232
|
+
else warn(msg);
|
|
233
|
+
process.exit(1);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
const contract = readContract(infernoDir);
|
|
237
|
+
const knownIds = getKnownIds(contract);
|
|
238
|
+
|
|
239
|
+
if (!jsonMode) {
|
|
240
|
+
info(`Scanning ${scanDirs.map(d => path.relative(cwd, d)).join(", ")} …`);
|
|
241
|
+
console.log();
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
// Collect hits from all source dirs
|
|
245
|
+
const allHits = [];
|
|
246
|
+
for (const dir of scanDirs) {
|
|
247
|
+
for (const file of walkFiles(dir)) {
|
|
248
|
+
allHits.push(...scanFile(file));
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
// Filter: unknown, above confidence threshold, non-trivial id
|
|
253
|
+
const candidates = dedupeHits(allHits).filter(h =>
|
|
254
|
+
!knownIds.has(h.id.toLowerCase()) &&
|
|
255
|
+
h.confidence >= minConfidence &&
|
|
256
|
+
h.id.length >= 3 &&
|
|
257
|
+
!["index", "app", "main", "root", "default", "handler"].includes(h.id)
|
|
258
|
+
);
|
|
259
|
+
|
|
260
|
+
if (jsonMode) {
|
|
261
|
+
console.log(JSON.stringify({ ok: true, found: candidates.length, candidates }));
|
|
262
|
+
return;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
if (!candidates.length) {
|
|
266
|
+
done("No undocumented capabilities found — contract looks complete.");
|
|
267
|
+
return;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
console.log(` ${bold(`${candidates.length} undocumented capability candidate${candidates.length !== 1 ? "s" : ""} found`)}`);
|
|
271
|
+
console.log();
|
|
272
|
+
|
|
273
|
+
const w = Math.max(...candidates.map(c => c.id.length), 10) + 2;
|
|
274
|
+
for (const c of candidates) {
|
|
275
|
+
const confStr = `${Math.round(c.confidence * 100)}%`;
|
|
276
|
+
const confColor = c.confidence >= 0.8 ? green : c.confidence >= 0.65 ? yellow : gray;
|
|
277
|
+
const rel = path.relative(cwd, c.source);
|
|
278
|
+
console.log(` ${bold(c.id.padEnd(w))} ${confColor(confStr.padEnd(5))} ${gray(c.hint)}`);
|
|
279
|
+
console.log(` ${" ".repeat(w + 7)}${gray(rel)}`);
|
|
280
|
+
}
|
|
281
|
+
console.log();
|
|
282
|
+
|
|
283
|
+
if (apply) {
|
|
284
|
+
const added = applyToContract(infernoDir, contract, candidates);
|
|
285
|
+
done(`Added ${bold(String(added.length))} capabilities to ${contract.file}`);
|
|
286
|
+
console.log();
|
|
287
|
+
} else {
|
|
288
|
+
info(`Run with ${cyan("--apply")} to add these to your contract.`);
|
|
289
|
+
console.log();
|
|
290
|
+
}
|
|
291
|
+
}
|
|
@@ -0,0 +1,383 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* infernoflow snapshot
|
|
3
|
+
*
|
|
4
|
+
* Named, timestamped snapshots of the full capability contract.
|
|
5
|
+
* Stored in inferno/snapshots/ — travel with the repo.
|
|
6
|
+
*
|
|
7
|
+
* Like git tags, but for the capability contract specifically.
|
|
8
|
+
*
|
|
9
|
+
* Usage:
|
|
10
|
+
* infernoflow snapshot save <name> Save current contract as a named snapshot
|
|
11
|
+
* infernoflow snapshot list List all snapshots
|
|
12
|
+
* infernoflow snapshot show <name> Print a snapshot's capabilities
|
|
13
|
+
* infernoflow snapshot diff <name1> <name2> Diff two snapshots (or name vs current)
|
|
14
|
+
* infernoflow snapshot restore <name> Overwrite contract with a snapshot
|
|
15
|
+
* infernoflow snapshot delete <name> Delete a snapshot
|
|
16
|
+
* infernoflow snapshot --json Machine-readable output on any subcommand
|
|
17
|
+
*
|
|
18
|
+
* Snapshot file format (inferno/snapshots/<name>.json):
|
|
19
|
+
* {
|
|
20
|
+
* "name": "v1.2-release",
|
|
21
|
+
* "savedAt": "2025-06-01T12:00:00Z",
|
|
22
|
+
* "capabilities": [...],
|
|
23
|
+
* "meta": { "version": "1.2.0", "capabilityCount": 12 }
|
|
24
|
+
* }
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
import * as fs from "node:fs";
|
|
28
|
+
import * as path from "node:path";
|
|
29
|
+
import { done, warn, info, bold, cyan, gray, green, yellow, red } from "../ui/output.mjs";
|
|
30
|
+
|
|
31
|
+
const SNAPSHOTS_DIR = "snapshots";
|
|
32
|
+
|
|
33
|
+
// ── Storage ───────────────────────────────────────────────────────────────────
|
|
34
|
+
|
|
35
|
+
function snapshotsDir(infernoDir) {
|
|
36
|
+
return path.join(infernoDir, SNAPSHOTS_DIR);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function snapshotPath(infernoDir, name) {
|
|
40
|
+
return path.join(infernoDir, SNAPSHOTS_DIR, `${name}.json`);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function ensureSnapshotsDir(infernoDir) {
|
|
44
|
+
const d = snapshotsDir(infernoDir);
|
|
45
|
+
if (!fs.existsSync(d)) fs.mkdirSync(d, { recursive: true });
|
|
46
|
+
return d;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function listSnapshots(infernoDir) {
|
|
50
|
+
const d = snapshotsDir(infernoDir);
|
|
51
|
+
if (!fs.existsSync(d)) return [];
|
|
52
|
+
return fs.readdirSync(d)
|
|
53
|
+
.filter(f => f.endsWith(".json"))
|
|
54
|
+
.map(f => {
|
|
55
|
+
try { return JSON.parse(fs.readFileSync(path.join(d, f), "utf8")); } catch { return null; }
|
|
56
|
+
})
|
|
57
|
+
.filter(Boolean)
|
|
58
|
+
.sort((a, b) => new Date(b.savedAt) - new Date(a.savedAt));
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function readSnapshot(infernoDir, name) {
|
|
62
|
+
const p = snapshotPath(infernoDir, name);
|
|
63
|
+
if (!fs.existsSync(p)) return null;
|
|
64
|
+
try { return JSON.parse(fs.readFileSync(p, "utf8")); } catch { return null; }
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function readContract(infernoDir) {
|
|
68
|
+
for (const f of ["contract.json", "capabilities.json"]) {
|
|
69
|
+
const p = path.join(infernoDir, f);
|
|
70
|
+
if (!fs.existsSync(p)) continue;
|
|
71
|
+
try { return { file: f, data: JSON.parse(fs.readFileSync(p, "utf8")) }; } catch {}
|
|
72
|
+
}
|
|
73
|
+
return null;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function normaliseCaps(contract) {
|
|
77
|
+
const raw = contract?.capabilities || contract?.data?.capabilities || contract || [];
|
|
78
|
+
return raw.map(c => (typeof c === "string" ? { id: c } : c));
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function readPackageVersion(cwd) {
|
|
82
|
+
try { return JSON.parse(fs.readFileSync(path.join(cwd, "package.json"), "utf8")).version || ""; } catch { return ""; }
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// ── Diff engine ───────────────────────────────────────────────────────────────
|
|
86
|
+
|
|
87
|
+
function diffCaps(capsBefore, capsAfter) {
|
|
88
|
+
const beforeIds = new Map(capsBefore.map(c => [c.id, c]));
|
|
89
|
+
const afterIds = new Map(capsAfter.map(c => [c.id, c]));
|
|
90
|
+
|
|
91
|
+
const added = capsAfter.filter(c => !beforeIds.has(c.id));
|
|
92
|
+
const removed = capsBefore.filter(c => !afterIds.has(c.id));
|
|
93
|
+
const changed = capsAfter.filter(c => {
|
|
94
|
+
const prev = beforeIds.get(c.id);
|
|
95
|
+
if (!prev) return false;
|
|
96
|
+
return JSON.stringify(c) !== JSON.stringify(prev);
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
return { added, removed, changed };
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// ── Sub-commands ──────────────────────────────────────────────────────────────
|
|
103
|
+
|
|
104
|
+
function subcmdSave(name, infernoDir, cwd, jsonMode) {
|
|
105
|
+
if (!name || name.startsWith("--")) {
|
|
106
|
+
const msg = "Usage: infernoflow snapshot save <name>";
|
|
107
|
+
if (jsonMode) console.log(JSON.stringify({ ok: false, error: msg }));
|
|
108
|
+
else warn(msg);
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// Validate name (no spaces, no slashes)
|
|
113
|
+
if (!/^[a-zA-Z0-9._-]+$/.test(name)) {
|
|
114
|
+
const msg = `Invalid snapshot name "${name}" — use letters, digits, dots, dashes, underscores only`;
|
|
115
|
+
if (jsonMode) console.log(JSON.stringify({ ok: false, error: msg }));
|
|
116
|
+
else warn(msg);
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const contract = readContract(infernoDir);
|
|
121
|
+
if (!contract) {
|
|
122
|
+
const msg = "No contract found. Run: infernoflow init";
|
|
123
|
+
if (jsonMode) console.log(JSON.stringify({ ok: false, error: msg }));
|
|
124
|
+
else warn(msg);
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
ensureSnapshotsDir(infernoDir);
|
|
129
|
+
|
|
130
|
+
const caps = normaliseCaps(contract.data);
|
|
131
|
+
const snapshot = {
|
|
132
|
+
name,
|
|
133
|
+
savedAt: new Date().toISOString(),
|
|
134
|
+
capabilities: caps,
|
|
135
|
+
meta: {
|
|
136
|
+
version: readPackageVersion(cwd),
|
|
137
|
+
capabilityCount: caps.length,
|
|
138
|
+
contractFile: contract.file,
|
|
139
|
+
},
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
const p = snapshotPath(infernoDir, name);
|
|
143
|
+
const overwriting = fs.existsSync(p);
|
|
144
|
+
fs.writeFileSync(p, JSON.stringify(snapshot, null, 2) + "\n");
|
|
145
|
+
|
|
146
|
+
if (jsonMode) {
|
|
147
|
+
console.log(JSON.stringify({ ok: true, action: "saved", name, capabilities: caps.length }));
|
|
148
|
+
} else {
|
|
149
|
+
done(`${overwriting ? "Updated" : "Saved"} snapshot ${bold(cyan(name))} — ${bold(String(caps.length))} capabilities`);
|
|
150
|
+
console.log();
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
function subcmdList(infernoDir, jsonMode) {
|
|
155
|
+
const snapshots = listSnapshots(infernoDir);
|
|
156
|
+
|
|
157
|
+
if (jsonMode) {
|
|
158
|
+
console.log(JSON.stringify({ ok: true, snapshots: snapshots.map(s => ({ name: s.name, savedAt: s.savedAt, capabilities: s.meta?.capabilityCount ?? s.capabilities?.length ?? 0 })) }));
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
if (!snapshots.length) {
|
|
163
|
+
info("No snapshots yet. Use: infernoflow snapshot save <name>");
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
console.log();
|
|
168
|
+
console.log(` ${bold(`${snapshots.length} snapshot${snapshots.length !== 1 ? "s" : ""}`)}`);
|
|
169
|
+
console.log();
|
|
170
|
+
|
|
171
|
+
const w = Math.max(...snapshots.map(s => s.name.length), 8) + 2;
|
|
172
|
+
for (const s of snapshots) {
|
|
173
|
+
const date = new Date(s.savedAt).toLocaleString();
|
|
174
|
+
const caps = s.meta?.capabilityCount ?? s.capabilities?.length ?? "?";
|
|
175
|
+
console.log(` ${bold(s.name.padEnd(w))} ${gray(date)} ${cyan(String(caps))} caps`);
|
|
176
|
+
}
|
|
177
|
+
console.log();
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
function subcmdShow(name, infernoDir, jsonMode) {
|
|
181
|
+
if (!name || name.startsWith("--")) {
|
|
182
|
+
const msg = "Usage: infernoflow snapshot show <name>";
|
|
183
|
+
if (jsonMode) console.log(JSON.stringify({ ok: false, error: msg }));
|
|
184
|
+
else warn(msg);
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
const snap = readSnapshot(infernoDir, name);
|
|
189
|
+
if (!snap) {
|
|
190
|
+
const msg = `Snapshot not found: ${name}`;
|
|
191
|
+
if (jsonMode) console.log(JSON.stringify({ ok: false, error: msg }));
|
|
192
|
+
else warn(msg);
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
if (jsonMode) {
|
|
197
|
+
console.log(JSON.stringify({ ok: true, snapshot: snap }));
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
console.log();
|
|
202
|
+
console.log(` ${bold("Snapshot:")} ${cyan(snap.name)}`);
|
|
203
|
+
console.log(` ${bold("Saved:")} ${gray(new Date(snap.savedAt).toLocaleString())}`);
|
|
204
|
+
console.log(` ${bold("Version:")} ${gray(snap.meta?.version || "—")}`);
|
|
205
|
+
console.log(` ${bold("Caps:")} ${snap.capabilities.length}`);
|
|
206
|
+
console.log();
|
|
207
|
+
|
|
208
|
+
const caps = normaliseCaps(snap.capabilities);
|
|
209
|
+
for (const c of caps) {
|
|
210
|
+
console.log(` ${cyan("·")} ${bold(c.id)}${c.description ? gray(" " + c.description) : ""}`);
|
|
211
|
+
}
|
|
212
|
+
console.log();
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
function subcmdDiff(nameA, nameB, infernoDir, jsonMode) {
|
|
216
|
+
if (!nameA) {
|
|
217
|
+
const msg = "Usage: infernoflow snapshot diff <name1> [<name2>] (omit name2 to diff against current)";
|
|
218
|
+
if (jsonMode) console.log(JSON.stringify({ ok: false, error: msg }));
|
|
219
|
+
else warn(msg);
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
const snapA = readSnapshot(infernoDir, nameA);
|
|
224
|
+
if (!snapA) {
|
|
225
|
+
const msg = `Snapshot not found: ${nameA}`;
|
|
226
|
+
if (jsonMode) console.log(JSON.stringify({ ok: false, error: msg }));
|
|
227
|
+
else warn(msg);
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
let capsBefore = normaliseCaps(snapA.capabilities);
|
|
232
|
+
let capsAfter;
|
|
233
|
+
let labelAfter;
|
|
234
|
+
|
|
235
|
+
if (nameB) {
|
|
236
|
+
const snapB = readSnapshot(infernoDir, nameB);
|
|
237
|
+
if (!snapB) {
|
|
238
|
+
const msg = `Snapshot not found: ${nameB}`;
|
|
239
|
+
if (jsonMode) console.log(JSON.stringify({ ok: false, error: msg }));
|
|
240
|
+
else warn(msg);
|
|
241
|
+
return;
|
|
242
|
+
}
|
|
243
|
+
capsAfter = normaliseCaps(snapB.capabilities);
|
|
244
|
+
labelAfter = nameB;
|
|
245
|
+
} else {
|
|
246
|
+
const contract = readContract(infernoDir);
|
|
247
|
+
if (!contract) {
|
|
248
|
+
const msg = "No current contract found.";
|
|
249
|
+
if (jsonMode) console.log(JSON.stringify({ ok: false, error: msg }));
|
|
250
|
+
else warn(msg);
|
|
251
|
+
return;
|
|
252
|
+
}
|
|
253
|
+
capsAfter = normaliseCaps(contract.data);
|
|
254
|
+
labelAfter = "current";
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
const diff = diffCaps(capsBefore, capsAfter);
|
|
258
|
+
|
|
259
|
+
if (jsonMode) {
|
|
260
|
+
console.log(JSON.stringify({ ok: true, from: nameA, to: labelAfter, ...diff }));
|
|
261
|
+
return;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
console.log();
|
|
265
|
+
console.log(` ${bold("Diff:")} ${cyan(nameA)} → ${cyan(labelAfter)}`);
|
|
266
|
+
console.log();
|
|
267
|
+
|
|
268
|
+
if (!diff.added.length && !diff.removed.length && !diff.changed.length) {
|
|
269
|
+
console.log(` ${gray("No differences — snapshots are identical")}`);
|
|
270
|
+
console.log();
|
|
271
|
+
return;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
if (diff.added.length) {
|
|
275
|
+
console.log(` ${green("+")} ${bold(`${diff.added.length} added`)}`);
|
|
276
|
+
diff.added.forEach(c => console.log(` ${green("+")} ${c.id}`));
|
|
277
|
+
console.log();
|
|
278
|
+
}
|
|
279
|
+
if (diff.removed.length) {
|
|
280
|
+
console.log(` ${red("-")} ${bold(`${diff.removed.length} removed`)}`);
|
|
281
|
+
diff.removed.forEach(c => console.log(` ${red("-")} ${c.id}`));
|
|
282
|
+
console.log();
|
|
283
|
+
}
|
|
284
|
+
if (diff.changed.length) {
|
|
285
|
+
console.log(` ${yellow("~")} ${bold(`${diff.changed.length} changed`)}`);
|
|
286
|
+
diff.changed.forEach(c => console.log(` ${yellow("~")} ${c.id}`));
|
|
287
|
+
console.log();
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
function subcmdRestore(name, infernoDir, jsonMode) {
|
|
292
|
+
if (!name || name.startsWith("--")) {
|
|
293
|
+
const msg = "Usage: infernoflow snapshot restore <name>";
|
|
294
|
+
if (jsonMode) console.log(JSON.stringify({ ok: false, error: msg }));
|
|
295
|
+
else warn(msg);
|
|
296
|
+
return;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
const snap = readSnapshot(infernoDir, name);
|
|
300
|
+
if (!snap) {
|
|
301
|
+
const msg = `Snapshot not found: ${name}`;
|
|
302
|
+
if (jsonMode) console.log(JSON.stringify({ ok: false, error: msg }));
|
|
303
|
+
else warn(msg);
|
|
304
|
+
return;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
const contract = readContract(infernoDir);
|
|
308
|
+
const contractFile = contract?.file || "capabilities.json";
|
|
309
|
+
const contractPath = path.join(infernoDir, contractFile);
|
|
310
|
+
|
|
311
|
+
const data = contract?.data || {};
|
|
312
|
+
data.capabilities = snap.capabilities;
|
|
313
|
+
fs.writeFileSync(contractPath, JSON.stringify(data, null, 2) + "\n");
|
|
314
|
+
|
|
315
|
+
if (jsonMode) {
|
|
316
|
+
console.log(JSON.stringify({ ok: true, action: "restored", name, capabilities: snap.capabilities.length }));
|
|
317
|
+
} else {
|
|
318
|
+
done(`Restored ${bold(cyan(name))} → ${bold(contractFile)} (${snap.capabilities.length} capabilities)`);
|
|
319
|
+
console.log();
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
function subcmdDelete(name, infernoDir, jsonMode) {
|
|
324
|
+
if (!name || name.startsWith("--")) {
|
|
325
|
+
const msg = "Usage: infernoflow snapshot delete <name>";
|
|
326
|
+
if (jsonMode) console.log(JSON.stringify({ ok: false, error: msg }));
|
|
327
|
+
else warn(msg);
|
|
328
|
+
return;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
const p = snapshotPath(infernoDir, name);
|
|
332
|
+
if (!fs.existsSync(p)) {
|
|
333
|
+
const msg = `Snapshot not found: ${name}`;
|
|
334
|
+
if (jsonMode) console.log(JSON.stringify({ ok: false, error: msg }));
|
|
335
|
+
else warn(msg);
|
|
336
|
+
return;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
fs.unlinkSync(p);
|
|
340
|
+
|
|
341
|
+
if (jsonMode) {
|
|
342
|
+
console.log(JSON.stringify({ ok: true, action: "deleted", name }));
|
|
343
|
+
} else {
|
|
344
|
+
done(`Deleted snapshot ${bold(name)}`);
|
|
345
|
+
console.log();
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
// ── Entry ─────────────────────────────────────────────────────────────────────
|
|
350
|
+
|
|
351
|
+
export async function snapshotCommand(rawArgs) {
|
|
352
|
+
const args = rawArgs.slice(1);
|
|
353
|
+
const jsonMode = args.includes("--json");
|
|
354
|
+
const cwd = process.cwd();
|
|
355
|
+
const infernoDir = path.join(cwd, "inferno");
|
|
356
|
+
|
|
357
|
+
if (!fs.existsSync(infernoDir)) {
|
|
358
|
+
const msg = "inferno/ not found. Run: infernoflow init";
|
|
359
|
+
if (jsonMode) console.log(JSON.stringify({ ok: false, error: msg }));
|
|
360
|
+
else warn(msg);
|
|
361
|
+
process.exit(1);
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
const subcmd = args.find(a => !a.startsWith("--"));
|
|
365
|
+
|
|
366
|
+
if (!subcmd || subcmd === "list") {
|
|
367
|
+
return subcmdList(infernoDir, jsonMode);
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
const positional = args.filter(a => !a.startsWith("--"));
|
|
371
|
+
|
|
372
|
+
if (subcmd === "save") return subcmdSave(positional[1], infernoDir, cwd, jsonMode);
|
|
373
|
+
if (subcmd === "show") return subcmdShow(positional[1], infernoDir, jsonMode);
|
|
374
|
+
if (subcmd === "restore") return subcmdRestore(positional[1], infernoDir, jsonMode);
|
|
375
|
+
if (subcmd === "delete") return subcmdDelete(positional[1], infernoDir, jsonMode);
|
|
376
|
+
|
|
377
|
+
if (subcmd === "diff") {
|
|
378
|
+
return subcmdDiff(positional[1], positional[2], infernoDir, jsonMode);
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
warn(`Unknown snapshot sub-command: ${subcmd}`);
|
|
382
|
+
info("Available: save | list | show | diff | restore | delete");
|
|
383
|
+
}
|