@fabersep/atlas 0.0.0-stage → 0.1.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/CHANGELOG.md +30 -0
- package/LICENSE +202 -0
- package/README.md +139 -2
- package/dist/packs/csharp/calls.d.ts +4 -0
- package/dist/packs/csharp/calls.js +167 -0
- package/dist/packs/csharp/declarations.d.ts +4 -0
- package/dist/packs/csharp/declarations.js +142 -0
- package/dist/packs/csharp/pack.d.ts +2 -0
- package/dist/packs/csharp/pack.js +39 -0
- package/dist/packs/csharp/resolve.d.ts +2 -0
- package/dist/packs/csharp/resolve.js +35 -0
- package/dist/packs/csharp/signals.d.ts +7 -0
- package/dist/packs/csharp/signals.js +143 -0
- package/dist/packs/dart/calls.d.ts +4 -0
- package/dist/packs/dart/calls.js +130 -0
- package/dist/packs/dart/declarations.d.ts +11 -0
- package/dist/packs/dart/declarations.js +79 -0
- package/dist/packs/dart/members.d.ts +4 -0
- package/dist/packs/dart/members.js +62 -0
- package/dist/packs/dart/pack.d.ts +2 -0
- package/dist/packs/dart/pack.js +37 -0
- package/dist/packs/dart/resolve.d.ts +2 -0
- package/dist/packs/dart/resolve.js +32 -0
- package/dist/packs/index.d.ts +3 -0
- package/dist/packs/index.js +8 -0
- package/dist/packs/json/pack.d.ts +2 -0
- package/dist/packs/json/pack.js +27 -0
- package/dist/packs/msbuild/pack.d.ts +2 -0
- package/dist/packs/msbuild/pack.js +75 -0
- package/dist/packs/msbuild/xml.d.ts +16 -0
- package/dist/packs/msbuild/xml.js +123 -0
- package/dist/packs/typescript/calls.d.ts +4 -0
- package/dist/packs/typescript/calls.js +201 -0
- package/dist/packs/typescript/declarations.d.ts +9 -0
- package/dist/packs/typescript/declarations.js +74 -0
- package/dist/packs/typescript/members.d.ts +4 -0
- package/dist/packs/typescript/members.js +92 -0
- package/dist/packs/typescript/pack.d.ts +2 -0
- package/dist/packs/typescript/pack.js +53 -0
- package/dist/packs/typescript/resolve.d.ts +2 -0
- package/dist/packs/typescript/resolve.js +68 -0
- package/dist/packs/typescript/signals.d.ts +7 -0
- package/dist/packs/typescript/signals.js +186 -0
- package/dist/packs/yaml/pack.d.ts +2 -0
- package/dist/packs/yaml/pack.js +35 -0
- package/dist/src/cli.d.ts +2 -0
- package/dist/src/cli.js +371 -0
- package/dist/src/graph/build.d.ts +17 -0
- package/dist/src/graph/build.js +41 -0
- package/dist/src/graph/metrics.d.ts +12 -0
- package/dist/src/graph/metrics.js +75 -0
- package/dist/src/graph/topology.d.ts +5 -0
- package/dist/src/graph/topology.js +54 -0
- package/dist/src/health/compare.d.ts +2 -0
- package/dist/src/health/compare.js +38 -0
- package/dist/src/health/duplicates.d.ts +11 -0
- package/dist/src/health/duplicates.js +66 -0
- package/dist/src/health/health.d.ts +12 -0
- package/dist/src/health/health.js +94 -0
- package/dist/src/health/types.d.ts +101 -0
- package/dist/src/health/types.js +5 -0
- package/dist/src/index.d.ts +23 -0
- package/dist/src/index.js +20 -0
- package/dist/src/init/init.d.ts +73 -0
- package/dist/src/init/init.js +305 -0
- package/dist/src/init/scout.d.ts +45 -0
- package/dist/src/init/scout.js +208 -0
- package/dist/src/jsonc.d.ts +5 -0
- package/dist/src/jsonc.js +34 -0
- package/dist/src/mcp/server.d.ts +266 -0
- package/dist/src/mcp/server.js +414 -0
- package/dist/src/metrics/doctor.d.ts +15 -0
- package/dist/src/metrics/doctor.js +125 -0
- package/dist/src/metrics/log.d.ts +43 -0
- package/dist/src/metrics/log.js +101 -0
- package/dist/src/metrics/stats.d.ts +43 -0
- package/dist/src/metrics/stats.js +172 -0
- package/dist/src/model.d.ts +178 -0
- package/dist/src/model.js +2 -0
- package/dist/src/outputs.d.ts +16 -0
- package/dist/src/outputs.js +48 -0
- package/dist/src/packs/calls.d.ts +9 -0
- package/dist/src/packs/calls.js +119 -0
- package/dist/src/packs/dataShape.d.ts +6 -0
- package/dist/src/packs/dataShape.js +54 -0
- package/dist/src/packs/pack.d.ts +51 -0
- package/dist/src/packs/pack.js +3 -0
- package/dist/src/packs/packFiles.d.ts +5 -0
- package/dist/src/packs/packFiles.js +12 -0
- package/dist/src/packs/symbols.d.ts +39 -0
- package/dist/src/packs/symbols.js +98 -0
- package/dist/src/packs/treeSitter.d.ts +8 -0
- package/dist/src/packs/treeSitter.js +107 -0
- package/dist/src/queries/flow.d.ts +42 -0
- package/dist/src/queries/flow.js +391 -0
- package/dist/src/queries/map.d.ts +28 -0
- package/dist/src/queries/map.js +102 -0
- package/dist/src/queries/neighborhood.d.ts +35 -0
- package/dist/src/queries/neighborhood.js +60 -0
- package/dist/src/scan/audit.d.ts +2 -0
- package/dist/src/scan/audit.js +41 -0
- package/dist/src/scan/records.d.ts +12 -0
- package/dist/src/scan/records.js +42 -0
- package/dist/src/scan/resolveImports.d.ts +3 -0
- package/dist/src/scan/resolveImports.js +47 -0
- package/dist/src/scan/roles.d.ts +5 -0
- package/dist/src/scan/roles.js +87 -0
- package/dist/src/scan/scan.d.ts +24 -0
- package/dist/src/scan/scan.js +72 -0
- package/dist/src/text/briefs.d.ts +13 -0
- package/dist/src/text/briefs.js +43 -0
- package/dist/src/text/collapse.d.ts +10 -0
- package/dist/src/text/collapse.js +58 -0
- package/dist/src/text/declarations.d.ts +12 -0
- package/dist/src/text/declarations.js +120 -0
- package/dist/src/text/structure.d.ts +2 -0
- package/dist/src/text/structure.js +15 -0
- package/dist/src/text/tree.d.ts +20 -0
- package/dist/src/text/tree.js +49 -0
- package/dist/src/tokens.d.ts +4 -0
- package/dist/src/tokens.js +9 -0
- package/dist/src/version.d.ts +10 -0
- package/dist/src/version.js +11 -0
- package/dist/src/walk/ignoreRules.d.ts +17 -0
- package/dist/src/walk/ignoreRules.js +72 -0
- package/dist/src/walk/walk.d.ts +11 -0
- package/dist/src/walk/walk.js +29 -0
- package/grammars/LICENSE-tree-sitter-dart +20 -0
- package/grammars/README.md +11 -0
- package/grammars/tree-sitter-dart.wasm +0 -0
- package/package.json +68 -4
- package/packs/csharp/README.md +9 -0
- package/packs/csharp/imports.scm +3 -0
- package/packs/csharp/symbols.scm +17 -0
- package/packs/dart/imports.scm +5 -0
- package/packs/dart/symbols.scm +13 -0
- package/packs/msbuild/README.md +11 -0
- package/packs/typescript/exports.scm +16 -0
- package/packs/typescript/imports.scm +9 -0
- package/packs/typescript/symbols.scm +19 -0
- package/schema/ast_code_map.schema.json +144 -0
- package/schema/schema-lock.json +7 -0
- package/schema/topology.schema.json +87 -0
package/dist/src/cli.js
ADDED
|
@@ -0,0 +1,371 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { PACKS } from '../packs/index.js';
|
|
5
|
+
import { rankHubs } from './graph/topology.js';
|
|
6
|
+
import { compareHealth } from './health/compare.js';
|
|
7
|
+
import { checkLimits, health } from './health/health.js';
|
|
8
|
+
import { HealthLimitsError } from './health/types.js';
|
|
9
|
+
import { formatInitPlan, planOrApplyInit } from './init/init.js';
|
|
10
|
+
import { defaultUserHome, discoverLiveAgyModels, formatScoutStatus, readCurrentScoutConfigs, runInteractiveScout, setScoutConfig, } from './init/scout.js';
|
|
11
|
+
import { ensureFreshIndex, projectRootFrom, serveMcp } from './mcp/server.js';
|
|
12
|
+
import { formatDoctor, runDoctor } from './metrics/doctor.js';
|
|
13
|
+
import { computeStats, formatShareCard, formatStats } from './metrics/stats.js';
|
|
14
|
+
import { readCodeMap, readTopology } from './outputs.js';
|
|
15
|
+
import { flow, impact, renderFlow, UnknownSymbolError } from './queries/flow.js';
|
|
16
|
+
import { renderMap } from './queries/map.js';
|
|
17
|
+
import { neighborhood } from './queries/neighborhood.js';
|
|
18
|
+
import { scan } from './scan/scan.js';
|
|
19
|
+
import { briefsText, neighborhoodList, neighborhoodText } from './text/briefs.js';
|
|
20
|
+
import { GENERATOR_VERSION } from './version.js';
|
|
21
|
+
const USAGE = `Usage: faber-atlas <command> [options] (faber-atlas --version prints the version)
|
|
22
|
+
|
|
23
|
+
scan <folder> [--out <dir>] [--exclude <path>]... [--full] [--json]
|
|
24
|
+
Scan a folder and write ast_code_map.json, topology.json, TopologyFULL.txt and
|
|
25
|
+
project_structure.md to --out (default <folder>/.faber-atlas). Unchanged files are not parsed
|
|
26
|
+
again unless --full is given.
|
|
27
|
+
map [<subfolder or file>] [--out <dir>] [--zoom 0|1|2] [--include-tests] [--fields] [--full] [--depth <n>] [--folders]
|
|
28
|
+
Print the repo map of the scanned folder, a subfolder, or one file's declarations.
|
|
29
|
+
--zoom 0: folders with file counts and roles. --zoom 1: files + top-level symbols (tests
|
|
30
|
+
excluded unless --include-tests). --zoom 2: method-level compact map (default).
|
|
31
|
+
neighborhood <file>... [--budget <tokens>] [--contents [--root <folder>]] [--list] [--out <dir>] [--json]
|
|
32
|
+
The files around the given ones with their briefs (declarations without bodies, data shapes).
|
|
33
|
+
Without --budget: every file they import and every file importing them. With --budget: also
|
|
34
|
+
the next ring, most central first, while they fit. --contents prints whole files instead,
|
|
35
|
+
read from --root (default: the folder that holds --out). --list prints only the paths.
|
|
36
|
+
brief <file>... [--out <dir>]
|
|
37
|
+
The briefs of the given files.
|
|
38
|
+
flow <symbol> [--depth <n>] [--include-tests] [--out <dir>] [--json]
|
|
39
|
+
Linearized downstream call, event, job, and implementation flow tree from <symbol> (default depth 4).
|
|
40
|
+
impact <symbol> [--depth <n>] [--include-tests] [--out <dir>] [--json]
|
|
41
|
+
Upstream callers and entrypoints that reach <symbol> (default depth 4).
|
|
42
|
+
hubs [--top <n>] [--out <dir>] [--json]
|
|
43
|
+
The most connected files.
|
|
44
|
+
health <folder> --max-file-lines <n> --max-function-lines <n> --hard-function-lines <n>
|
|
45
|
+
--max-nesting-depth <n> --min-duplicate-lines <n> [--out <dir>] [--json]
|
|
46
|
+
Files and functions over the limits (length and nesting depth), duplicated blocks and import cycles of the scanned folder
|
|
47
|
+
(--out defaults to <folder>/.faber-atlas). Every limit is required; none is assumed.
|
|
48
|
+
health-compare <before.json> <after.json> [--json]
|
|
49
|
+
What changed between two health reports made with the same limits: limits crossed, new
|
|
50
|
+
duplicates, new cycles, and the totals before and after.
|
|
51
|
+
mcp [--root <folder>] [--out <dir>] [--no-fresh]
|
|
52
|
+
Start a stdio MCP server exposing map, brief, neighborhood, flow, impact, hubs, and health.
|
|
53
|
+
Without --root it maps the project the AI tool has open (its workspace root, else the git
|
|
54
|
+
root above the folder it starts in), never the whole home folder. Runs an incremental scan
|
|
55
|
+
before each query unless --no-fresh is given.
|
|
56
|
+
init [--engine <name>]... [--scout-model <m>] [--scout-effort <low|medium|high>] [--no-scout] [--agents-md] [--root <folder>] [--preview | --undo] [--json]
|
|
57
|
+
One-time setup for every project: the MCP server and a read-only Scout for Claude Code,
|
|
58
|
+
Antigravity and Cursor (Claude Desktop with --engine claude-desktop, mapping --root).
|
|
59
|
+
Applies directly (with .faber-atlas.bak backups); --preview shows the changes without writing,
|
|
60
|
+
--undo removes them. --agents-md also adds a navigation block to this project's AGENTS.md.
|
|
61
|
+
scout [--engine <claude-code|antigravity>] [--model <name>] [--effort <low|medium|high>] [--discover] [--json]
|
|
62
|
+
Inspect or configure the read-only Scout model and reasoning effort per environment.
|
|
63
|
+
Run interactively without flags to pick from supported tiers, or pass --model/--effort directly.
|
|
64
|
+
stats [--out <dir>] [--days <n>] [--share] [--json]
|
|
65
|
+
Show local, zero-telemetry token compression, zoom sizes, scan latency, and MCP usage.
|
|
66
|
+
Pass --share for a strictly anonymized Markdown benchmark card.
|
|
67
|
+
doctor [--root <folder>] [--out <dir>] [--json]
|
|
68
|
+
Read-only health check of Node runtime, index schema/freshness, parse health, and .gitignore.
|
|
69
|
+
|
|
70
|
+
Queries read the outputs in --out (default ./.faber-atlas) and never parse again unless --fresh is given
|
|
71
|
+
(with --root <folder>, defaulting to the parent of --out).
|
|
72
|
+
`;
|
|
73
|
+
const LIMIT_FLAGS = {
|
|
74
|
+
maxFileLines: 'max-file-lines', maxFunctionLines: 'max-function-lines', hardFunctionLines: 'hard-function-lines',
|
|
75
|
+
maxNestingDepth: 'max-nesting-depth', minDuplicateLines: 'min-duplicate-lines',
|
|
76
|
+
};
|
|
77
|
+
const VALUE_FLAGS = new Set([
|
|
78
|
+
'out', 'exclude', 'depth', 'budget', 'top', 'root', 'zoom', 'engine', 'days',
|
|
79
|
+
'model', 'effort', 'scout-model', 'scout-effort',
|
|
80
|
+
...Object.values(LIMIT_FLAGS),
|
|
81
|
+
]);
|
|
82
|
+
function parseArgs(argv) {
|
|
83
|
+
const args = { positional: [], values: new Map(), flags: new Set() };
|
|
84
|
+
for (let i = 0; i < argv.length; i++) {
|
|
85
|
+
const name = argv[i].startsWith('--') ? argv[i].slice(2) : undefined;
|
|
86
|
+
if (!name)
|
|
87
|
+
args.positional.push(argv[i]);
|
|
88
|
+
else if (VALUE_FLAGS.has(name))
|
|
89
|
+
args.values.set(name, [...(args.values.get(name) ?? []), argv[++i]]);
|
|
90
|
+
else
|
|
91
|
+
args.flags.add(name);
|
|
92
|
+
}
|
|
93
|
+
return args;
|
|
94
|
+
}
|
|
95
|
+
const value = (args, name) => args.values.get(name)?.[0];
|
|
96
|
+
const numberValue = (args, name) => (value(args, name) === undefined ? undefined : Number(value(args, name)));
|
|
97
|
+
const outDir = (args) => path.resolve(value(args, 'out') ?? '.faber-atlas');
|
|
98
|
+
const rootDir = (args) => path.resolve(value(args, 'root') ?? path.dirname(outDir(args)));
|
|
99
|
+
async function loadMap(args) {
|
|
100
|
+
const out = outDir(args);
|
|
101
|
+
if (args.flags.has('fresh'))
|
|
102
|
+
return (await ensureFreshIndex(rootDir(args), out)).map;
|
|
103
|
+
return readCodeMap(out);
|
|
104
|
+
}
|
|
105
|
+
async function loadIndex(args) {
|
|
106
|
+
const out = outDir(args);
|
|
107
|
+
if (args.flags.has('fresh'))
|
|
108
|
+
return ensureFreshIndex(rootDir(args), out);
|
|
109
|
+
return { map: readCodeMap(out), topology: readTopology(out) };
|
|
110
|
+
}
|
|
111
|
+
function printSummary(s) {
|
|
112
|
+
console.log(`Scanned ${s.files} files (${s.parsed} parsed, ${s.reused} unchanged) in ${s.durationMs} ms; ${s.symbols} declarations.`);
|
|
113
|
+
for (const f of s.failed)
|
|
114
|
+
console.log(` failed ${f.path}: ${f.error}`);
|
|
115
|
+
const unsupported = Object.entries(s.unsupported).map(([ext, n]) => `${ext} (${n})`);
|
|
116
|
+
if (unsupported.length)
|
|
117
|
+
console.log(` unsupported (listed without declarations): ${unsupported.join(', ')}`);
|
|
118
|
+
}
|
|
119
|
+
async function runScan(args) {
|
|
120
|
+
const folder = args.positional[1];
|
|
121
|
+
if (!folder) {
|
|
122
|
+
console.error('scan needs a folder.');
|
|
123
|
+
return 2;
|
|
124
|
+
}
|
|
125
|
+
const out = value(args, 'out');
|
|
126
|
+
const result = await scan({ root: folder, out: out && path.resolve(out), exclude: args.values.get('exclude'), full: args.flags.has('full'), packs: PACKS });
|
|
127
|
+
if (args.flags.has('json'))
|
|
128
|
+
console.log(JSON.stringify({ out: result.out, generatorVersion: result.map.generatorVersion, schemaVersion: result.map.schemaVersion, summary: result.map.summary }, null, 2));
|
|
129
|
+
else
|
|
130
|
+
printSummary(result.map.summary);
|
|
131
|
+
return 0;
|
|
132
|
+
}
|
|
133
|
+
async function runNeighborhood(args) {
|
|
134
|
+
const budget = numberValue(args, 'budget');
|
|
135
|
+
const files = args.positional.slice(1);
|
|
136
|
+
if (!files.length || Number.isNaN(budget)) {
|
|
137
|
+
console.error('neighborhood needs files, and --budget <tokens> must be a number.');
|
|
138
|
+
return 2;
|
|
139
|
+
}
|
|
140
|
+
const contents = args.flags.has('contents');
|
|
141
|
+
const { map, topology } = await loadIndex(args);
|
|
142
|
+
const result = neighborhood(map, topology, files, { budget, contents });
|
|
143
|
+
if (args.flags.has('json'))
|
|
144
|
+
console.log(JSON.stringify(result, null, 2));
|
|
145
|
+
else if (args.flags.has('list'))
|
|
146
|
+
console.log(neighborhoodList(result));
|
|
147
|
+
else
|
|
148
|
+
console.log(neighborhoodText(result, contents ? { root: rootDir(args) } : undefined));
|
|
149
|
+
return 0;
|
|
150
|
+
}
|
|
151
|
+
async function runBrief(args) {
|
|
152
|
+
const files = args.positional.slice(1);
|
|
153
|
+
if (!files.length) {
|
|
154
|
+
console.error('brief needs files.');
|
|
155
|
+
return 2;
|
|
156
|
+
}
|
|
157
|
+
console.log(briefsText(await loadMap(args), files));
|
|
158
|
+
return 0;
|
|
159
|
+
}
|
|
160
|
+
async function runFlow(args, direction) {
|
|
161
|
+
const symbol = args.positional[1];
|
|
162
|
+
const depth = numberValue(args, 'depth');
|
|
163
|
+
if (!symbol || Number.isNaN(depth)) {
|
|
164
|
+
console.error(`${direction === 'downstream' ? 'flow' : 'impact'} needs a symbol, and --depth must be a number.`);
|
|
165
|
+
return 2;
|
|
166
|
+
}
|
|
167
|
+
const map = await loadMap(args);
|
|
168
|
+
const opts = { depth, includeTests: args.flags.has('include-tests') };
|
|
169
|
+
const result = direction === 'downstream' ? flow(map, symbol, opts) : impact(map, symbol, opts);
|
|
170
|
+
if (args.flags.has('json'))
|
|
171
|
+
console.log(JSON.stringify(result, null, 2));
|
|
172
|
+
else
|
|
173
|
+
console.log(renderFlow(result));
|
|
174
|
+
return 0;
|
|
175
|
+
}
|
|
176
|
+
async function runHubs(args) {
|
|
177
|
+
const { topology } = await loadIndex(args);
|
|
178
|
+
const hubs = rankHubs(topology.nodes, numberValue(args, 'top') ?? 10);
|
|
179
|
+
if (args.flags.has('json'))
|
|
180
|
+
console.log(JSON.stringify(hubs, null, 2));
|
|
181
|
+
else
|
|
182
|
+
hubs.forEach((h, i) => console.log(`#${i + 1} ${h.path} score ${h.centralityScore} in ${h.inDegree} out ${h.outDegree} pagerank ${h.pageRank}`));
|
|
183
|
+
return 0;
|
|
184
|
+
}
|
|
185
|
+
function runHealth(args) {
|
|
186
|
+
const folder = args.positional[1];
|
|
187
|
+
if (!folder) {
|
|
188
|
+
console.error('health needs a folder.');
|
|
189
|
+
return 2;
|
|
190
|
+
}
|
|
191
|
+
const limits = checkLimits(Object.fromEntries(Object.entries(LIMIT_FLAGS).map(([name, flag]) => [name, numberValue(args, flag)])));
|
|
192
|
+
const out = path.resolve(value(args, 'out') ?? path.join(folder, '.faber-atlas'));
|
|
193
|
+
const report = health(readCodeMap(out), readTopology(out), path.resolve(folder), limits, PACKS);
|
|
194
|
+
if (args.flags.has('json')) {
|
|
195
|
+
console.log(JSON.stringify(report, null, 2));
|
|
196
|
+
return 0;
|
|
197
|
+
}
|
|
198
|
+
const t = report.totals;
|
|
199
|
+
console.log(`${t.files} files, ${t.lines} lines; ${t.filesOverLimit} files over ${limits.maxFileLines} lines; ${t.functionsOverSoft} functions over ${limits.maxFunctionLines} and ${t.functionsOverHard} over ${limits.hardFunctionLines} lines; ${t.functionsOverNesting} functions nested deeper than ${limits.maxNestingDepth}; ${t.duplicateBlocks} duplicated blocks; ${t.cycles} import cycles; ${t.notMeasured} files not measured.`);
|
|
200
|
+
return 0;
|
|
201
|
+
}
|
|
202
|
+
function runHealthCompare(args) {
|
|
203
|
+
const [before, after] = args.positional.slice(1);
|
|
204
|
+
if (!before || !after) {
|
|
205
|
+
console.error('health-compare needs two health reports.');
|
|
206
|
+
return 2;
|
|
207
|
+
}
|
|
208
|
+
const readReport = (file) => JSON.parse(fs.readFileSync(file, 'utf8'));
|
|
209
|
+
const result = compareHealth(readReport(before), readReport(after));
|
|
210
|
+
if (args.flags.has('json')) {
|
|
211
|
+
console.log(JSON.stringify(result, null, 2));
|
|
212
|
+
return 0;
|
|
213
|
+
}
|
|
214
|
+
console.log(`${result.files.length} files and ${result.functions.length} functions crossed a length limit; ${result.nesting.length} functions crossed the nesting limit; ${result.duplicates.length} new duplicated blocks; ${result.cycles.length} new import cycles; ${result.notMeasured.length} changed files not measured.`);
|
|
215
|
+
return 0;
|
|
216
|
+
}
|
|
217
|
+
async function main(argv) {
|
|
218
|
+
if (argv[0] === '--version' || argv[0] === '-v' || argv[0] === 'version') {
|
|
219
|
+
console.log(GENERATOR_VERSION);
|
|
220
|
+
return 0;
|
|
221
|
+
}
|
|
222
|
+
const args = parseArgs(argv);
|
|
223
|
+
switch (args.positional[0]) {
|
|
224
|
+
case 'scan': return runScan(args);
|
|
225
|
+
case 'map': {
|
|
226
|
+
const zoomRaw = numberValue(args, 'zoom');
|
|
227
|
+
if (zoomRaw !== undefined && zoomRaw !== 0 && zoomRaw !== 1 && zoomRaw !== 2) {
|
|
228
|
+
console.error('map --zoom must be 0, 1, or 2.');
|
|
229
|
+
return 2;
|
|
230
|
+
}
|
|
231
|
+
console.log(renderMap(await loadMap(args), {
|
|
232
|
+
folder: args.positional[1],
|
|
233
|
+
fields: args.flags.has('fields'),
|
|
234
|
+
full: args.flags.has('full'),
|
|
235
|
+
depth: numberValue(args, 'depth'),
|
|
236
|
+
foldersOnly: args.flags.has('folders'),
|
|
237
|
+
zoom: zoomRaw,
|
|
238
|
+
includeTests: args.flags.has('include-tests'),
|
|
239
|
+
}));
|
|
240
|
+
return 0;
|
|
241
|
+
}
|
|
242
|
+
case 'neighborhood': return runNeighborhood(args);
|
|
243
|
+
case 'brief': return runBrief(args);
|
|
244
|
+
case 'flow': return runFlow(args, 'downstream');
|
|
245
|
+
case 'impact': return runFlow(args, 'upstream');
|
|
246
|
+
case 'hubs': return runHubs(args);
|
|
247
|
+
case 'health': return runHealth(args);
|
|
248
|
+
case 'health-compare': return runHealthCompare(args);
|
|
249
|
+
case 'mcp':
|
|
250
|
+
await serveMcp({
|
|
251
|
+
root: value(args, 'root'),
|
|
252
|
+
out: value(args, 'out'),
|
|
253
|
+
fresh: !args.flags.has('no-fresh'),
|
|
254
|
+
});
|
|
255
|
+
return 0;
|
|
256
|
+
case 'init': {
|
|
257
|
+
const engines = args.values.get('engine');
|
|
258
|
+
const preview = args.flags.has('preview') || args.flags.has('dry-run');
|
|
259
|
+
const undo = args.flags.has('undo');
|
|
260
|
+
const scoutEffort = value(args, 'scout-effort');
|
|
261
|
+
if (scoutEffort && scoutEffort !== 'low' && scoutEffort !== 'medium' && scoutEffort !== 'high') {
|
|
262
|
+
console.error('init --scout-effort must be low, medium, or high.');
|
|
263
|
+
return 2;
|
|
264
|
+
}
|
|
265
|
+
const result = planOrApplyInit({
|
|
266
|
+
root: value(args, 'root') ?? args.positional[1],
|
|
267
|
+
engines,
|
|
268
|
+
agentsMd: args.flags.has('agents-md'),
|
|
269
|
+
scout: !args.flags.has('no-scout'),
|
|
270
|
+
scoutModel: value(args, 'scout-model'),
|
|
271
|
+
scoutEffort,
|
|
272
|
+
preview,
|
|
273
|
+
undo,
|
|
274
|
+
});
|
|
275
|
+
if (args.flags.has('json'))
|
|
276
|
+
console.log(JSON.stringify(result, null, 2));
|
|
277
|
+
else
|
|
278
|
+
console.log(formatInitPlan(result));
|
|
279
|
+
return 0;
|
|
280
|
+
}
|
|
281
|
+
case 'scout': {
|
|
282
|
+
const userHome = defaultUserHome();
|
|
283
|
+
const engine = value(args, 'engine');
|
|
284
|
+
if (engine && engine !== 'claude-code' && engine !== 'antigravity') {
|
|
285
|
+
console.error('scout --engine must be claude-code or antigravity.');
|
|
286
|
+
return 2;
|
|
287
|
+
}
|
|
288
|
+
const model = value(args, 'model');
|
|
289
|
+
const effort = value(args, 'effort');
|
|
290
|
+
if (effort && effort !== 'low' && effort !== 'medium' && effort !== 'high') {
|
|
291
|
+
console.error('scout --effort must be low, medium, or high.');
|
|
292
|
+
return 2;
|
|
293
|
+
}
|
|
294
|
+
if (args.flags.has('discover')) {
|
|
295
|
+
const live = discoverLiveAgyModels();
|
|
296
|
+
if (args.flags.has('json')) {
|
|
297
|
+
console.log(JSON.stringify(live, null, 2));
|
|
298
|
+
}
|
|
299
|
+
else if (live.ok) {
|
|
300
|
+
console.log('Live Antigravity models (agy models):\n' + live.lines.join('\n'));
|
|
301
|
+
}
|
|
302
|
+
else {
|
|
303
|
+
console.log(live.note ?? 'Live discovery unavailable.');
|
|
304
|
+
}
|
|
305
|
+
return 0;
|
|
306
|
+
}
|
|
307
|
+
if (model || effort) {
|
|
308
|
+
const targetEngine = engine ?? 'claude-code';
|
|
309
|
+
const saved = setScoutConfig({
|
|
310
|
+
userHome,
|
|
311
|
+
engine: targetEngine,
|
|
312
|
+
model: model ?? (targetEngine === 'claude-code' ? 'haiku' : 'flash'),
|
|
313
|
+
effort,
|
|
314
|
+
});
|
|
315
|
+
if (args.flags.has('json'))
|
|
316
|
+
console.log(JSON.stringify(saved, null, 2));
|
|
317
|
+
else
|
|
318
|
+
console.log(`Updated ${saved.engine} Scout -> model: ${saved.model}${saved.effort ? `, effort: ${saved.effort}` : ''} (${saved.filePath})`);
|
|
319
|
+
return 0;
|
|
320
|
+
}
|
|
321
|
+
if (process.stdin.isTTY && !args.flags.has('json') && !args.flags.has('status')) {
|
|
322
|
+
await runInteractiveScout(userHome, engine);
|
|
323
|
+
return 0;
|
|
324
|
+
}
|
|
325
|
+
const configs = readCurrentScoutConfigs(userHome);
|
|
326
|
+
if (args.flags.has('json'))
|
|
327
|
+
console.log(JSON.stringify(configs, null, 2));
|
|
328
|
+
else
|
|
329
|
+
console.log(formatScoutStatus(configs));
|
|
330
|
+
return 0;
|
|
331
|
+
}
|
|
332
|
+
case 'stats': {
|
|
333
|
+
const days = numberValue(args, 'days');
|
|
334
|
+
if (days !== undefined && (Number.isNaN(days) || days <= 0)) {
|
|
335
|
+
console.error('stats --days must be a positive number.');
|
|
336
|
+
return 2;
|
|
337
|
+
}
|
|
338
|
+
const out = value(args, 'out')
|
|
339
|
+
? path.resolve(value(args, 'out'))
|
|
340
|
+
: path.resolve(value(args, 'root') ?? projectRootFrom('.'), '.faber-atlas');
|
|
341
|
+
const stats = computeStats(out, { days });
|
|
342
|
+
if (args.flags.has('share')) {
|
|
343
|
+
if (args.flags.has('json'))
|
|
344
|
+
console.log(JSON.stringify(stats, null, 2));
|
|
345
|
+
else
|
|
346
|
+
console.log(formatShareCard(stats));
|
|
347
|
+
}
|
|
348
|
+
else if (args.flags.has('json')) {
|
|
349
|
+
console.log(JSON.stringify(stats, null, 2));
|
|
350
|
+
}
|
|
351
|
+
else {
|
|
352
|
+
console.log(formatStats(stats));
|
|
353
|
+
}
|
|
354
|
+
return 0;
|
|
355
|
+
}
|
|
356
|
+
case 'doctor': {
|
|
357
|
+
const root = value(args, 'root') ?? args.positional[1] ?? projectRootFrom('.');
|
|
358
|
+
const out = value(args, 'out');
|
|
359
|
+
const report = runDoctor(root, out);
|
|
360
|
+
if (args.flags.has('json'))
|
|
361
|
+
console.log(JSON.stringify(report, null, 2));
|
|
362
|
+
else
|
|
363
|
+
console.log(formatDoctor(report));
|
|
364
|
+
return report.ok ? 0 : 1;
|
|
365
|
+
}
|
|
366
|
+
default:
|
|
367
|
+
console.log(USAGE);
|
|
368
|
+
return args.positional[0] ? 2 : 0;
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
main(process.argv.slice(2)).then((code) => { process.exitCode = code; }, (err) => { console.error(`faber-atlas: ${err.message}`); process.exitCode = err instanceof HealthLimitsError || err instanceof UnknownSymbolError ? 2 : 1; });
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { FileRecord, GraphEdge, NodeType } from '../model.js';
|
|
2
|
+
/** Nodes and edges of the topology graph: files that import, their top-level declarations, and the links between them. */
|
|
3
|
+
export interface RawNode {
|
|
4
|
+
id: string;
|
|
5
|
+
label: string;
|
|
6
|
+
path: string;
|
|
7
|
+
type: NodeType;
|
|
8
|
+
language?: string;
|
|
9
|
+
line?: number;
|
|
10
|
+
tokenEstimate?: number;
|
|
11
|
+
}
|
|
12
|
+
export declare const fileId: (path: string) => string;
|
|
13
|
+
/** The graph of the files whose language has imports (`codeLanguages`); data files and unsupported files are not part of it. */
|
|
14
|
+
export declare function buildGraph(records: FileRecord[], codeLanguages: Set<string>): {
|
|
15
|
+
nodes: RawNode[];
|
|
16
|
+
edges: GraphEdge[];
|
|
17
|
+
};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
const SYMBOL_NODE_KINDS = new Set(['class', 'interface', 'enum', 'mixin', 'extension', 'type', 'function']);
|
|
2
|
+
export const fileId = (path) => `file:${path}`;
|
|
3
|
+
const symbolId = (path, name) => `symbol:${path}#${name}`;
|
|
4
|
+
/** `State<HomeScreen>` names the type `State`; `http.Client` names `Client`. */
|
|
5
|
+
const typeName = (ref) => ref.replace(/<.*$/s, '').split('.').pop().trim();
|
|
6
|
+
function fileNode(r) {
|
|
7
|
+
return { id: fileId(r.path), label: r.path.split('/').pop(), path: r.path, type: 'file', language: r.language, tokenEstimate: r.rawTokens };
|
|
8
|
+
}
|
|
9
|
+
function symbolNodes(r) {
|
|
10
|
+
return r.symbols
|
|
11
|
+
.filter((s) => SYMBOL_NODE_KINDS.has(s.kind))
|
|
12
|
+
.map((s) => ({ id: symbolId(r.path, s.name), label: s.name, path: r.path, type: s.kind, line: s.lineStart }));
|
|
13
|
+
}
|
|
14
|
+
/** Import edges between project files, once per pair. */
|
|
15
|
+
function importEdges(r, files) {
|
|
16
|
+
const targets = new Set(r.imports.filter((i) => i.kind === 'file' && i.target && files.has(i.target) && i.target !== r.path).map((i) => i.target));
|
|
17
|
+
return [...targets].sort().map((t) => ({ source: fileId(r.path), target: fileId(t), type: 'imports' }));
|
|
18
|
+
}
|
|
19
|
+
/** extends, with and implements: to a declaration of the same file or of a file it imports, found by name. */
|
|
20
|
+
function heritageEdges(r, byFile) {
|
|
21
|
+
const visible = [r.path, ...r.imports.filter((i) => i.kind === 'file' && i.target).map((i) => i.target)];
|
|
22
|
+
const find = (name) => visible.find((p) => byFile.get(p)?.has(name));
|
|
23
|
+
const link = (s, refs, type) => (refs ?? []).map(typeName).flatMap((name) => {
|
|
24
|
+
const at = find(name);
|
|
25
|
+
return at ? [{ source: symbolId(r.path, s.name), target: symbolId(at, name), type }] : [];
|
|
26
|
+
});
|
|
27
|
+
return r.symbols.flatMap((s) => [...link(s, [...(s.extends ?? []), ...(s.mixins ?? [])], 'extends'), ...link(s, s.implements, 'implements')]);
|
|
28
|
+
}
|
|
29
|
+
/** The graph of the files whose language has imports (`codeLanguages`); data files and unsupported files are not part of it. */
|
|
30
|
+
export function buildGraph(records, codeLanguages) {
|
|
31
|
+
const code = records.filter((r) => codeLanguages.has(r.language));
|
|
32
|
+
const files = new Set(code.map((r) => r.path));
|
|
33
|
+
const declared = new Map(code.map((r) => [r.path, new Set(symbolNodes(r).map((n) => n.label))]));
|
|
34
|
+
const nodes = code.flatMap((r) => [fileNode(r), ...symbolNodes(r)]);
|
|
35
|
+
const edges = code.flatMap((r) => [
|
|
36
|
+
...importEdges(r, files),
|
|
37
|
+
...symbolNodes(r).map((n) => ({ source: fileId(r.path), target: n.id, type: 'defines' })),
|
|
38
|
+
...heritageEdges(r, declared),
|
|
39
|
+
]);
|
|
40
|
+
return { nodes, edges };
|
|
41
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { GraphEdge } from '../model.js';
|
|
2
|
+
/** In- and out-degree per node, counting only the given edge types. */
|
|
3
|
+
export declare function degrees(edges: GraphEdge[], types: Set<string>): {
|
|
4
|
+
inDegree: Map<string, number>;
|
|
5
|
+
outDegree: Map<string, number>;
|
|
6
|
+
};
|
|
7
|
+
/** PageRank over directed edges among `ids` (a node that links nowhere shares its rank with every node), rounded to 6 places. */
|
|
8
|
+
export declare function pageRank(ids: string[], edges: GraphEdge[]): Map<string, number>;
|
|
9
|
+
/** Import cycles found by a depth-first search in path order, each starting at its smallest file, at most 20. */
|
|
10
|
+
export declare function cycles(ids: string[], edges: GraphEdge[]): string[][];
|
|
11
|
+
/** A file's cluster: its folder, at most two levels deep (`src/orders`, `lib/screens`); `.` for the root. */
|
|
12
|
+
export declare function clusterOf(path: string): string;
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/** Graph measures: degrees, PageRank, cycles and clusters. Every result is deterministic for the same graph. */
|
|
2
|
+
const DAMPING = 0.85;
|
|
3
|
+
const ITERATIONS = 100;
|
|
4
|
+
const TOLERANCE = 1e-10;
|
|
5
|
+
const MAX_CYCLES = 20;
|
|
6
|
+
/** In- and out-degree per node, counting only the given edge types. */
|
|
7
|
+
export function degrees(edges, types) {
|
|
8
|
+
const inDegree = new Map();
|
|
9
|
+
const outDegree = new Map();
|
|
10
|
+
for (const e of edges.filter((e) => types.has(e.type))) {
|
|
11
|
+
outDegree.set(e.source, (outDegree.get(e.source) ?? 0) + 1);
|
|
12
|
+
inDegree.set(e.target, (inDegree.get(e.target) ?? 0) + 1);
|
|
13
|
+
}
|
|
14
|
+
return { inDegree, outDegree };
|
|
15
|
+
}
|
|
16
|
+
/** PageRank over directed edges among `ids` (a node that links nowhere shares its rank with every node), rounded to 6 places. */
|
|
17
|
+
export function pageRank(ids, edges) {
|
|
18
|
+
const n = ids.length;
|
|
19
|
+
const out = new Map(ids.map((id) => [id, []]));
|
|
20
|
+
for (const e of edges)
|
|
21
|
+
if (out.has(e.source) && out.has(e.target))
|
|
22
|
+
out.get(e.source).push(e.target);
|
|
23
|
+
let rank = new Map(ids.map((id) => [id, 1 / n]));
|
|
24
|
+
for (let i = 0; i < ITERATIONS; i++) {
|
|
25
|
+
const dangling = ids.filter((id) => out.get(id).length === 0).reduce((sum, id) => sum + rank.get(id), 0);
|
|
26
|
+
const next = new Map(ids.map((id) => [id, (1 - DAMPING) / n + (DAMPING * dangling) / n]));
|
|
27
|
+
for (const id of ids)
|
|
28
|
+
for (const t of out.get(id))
|
|
29
|
+
next.set(t, next.get(t) + (DAMPING * rank.get(id)) / out.get(id).length);
|
|
30
|
+
const change = ids.reduce((sum, id) => sum + Math.abs(next.get(id) - rank.get(id)), 0);
|
|
31
|
+
rank = next;
|
|
32
|
+
if (change < TOLERANCE)
|
|
33
|
+
break;
|
|
34
|
+
}
|
|
35
|
+
return new Map(ids.map((id) => [id, Number(rank.get(id).toFixed(6))]));
|
|
36
|
+
}
|
|
37
|
+
/** Import cycles found by a depth-first search in path order, each starting at its smallest file, at most 20. */
|
|
38
|
+
export function cycles(ids, edges) {
|
|
39
|
+
const adj = new Map(ids.map((id) => [id, []]));
|
|
40
|
+
for (const e of edges)
|
|
41
|
+
if (adj.has(e.source) && adj.has(e.target))
|
|
42
|
+
adj.get(e.source).push(e.target);
|
|
43
|
+
adj.forEach((targets) => targets.sort());
|
|
44
|
+
const found = new Map();
|
|
45
|
+
const state = new Map();
|
|
46
|
+
const stack = [];
|
|
47
|
+
const visit = (id) => {
|
|
48
|
+
state.set(id, 'open');
|
|
49
|
+
stack.push(id);
|
|
50
|
+
for (const t of adj.get(id)) {
|
|
51
|
+
if (state.get(t) === 'open')
|
|
52
|
+
record(found, stack.slice(stack.indexOf(t)));
|
|
53
|
+
else if (!state.has(t))
|
|
54
|
+
visit(t);
|
|
55
|
+
}
|
|
56
|
+
stack.pop();
|
|
57
|
+
state.set(id, 'done');
|
|
58
|
+
};
|
|
59
|
+
for (const id of [...ids].sort())
|
|
60
|
+
if (!state.has(id))
|
|
61
|
+
visit(id);
|
|
62
|
+
return [...found.values()].slice(0, MAX_CYCLES);
|
|
63
|
+
}
|
|
64
|
+
function record(found, loop) {
|
|
65
|
+
const start = loop.indexOf([...loop].sort()[0]);
|
|
66
|
+
const rotated = [...loop.slice(start), ...loop.slice(0, start)];
|
|
67
|
+
const key = rotated.join('>');
|
|
68
|
+
if (!found.has(key))
|
|
69
|
+
found.set(key, [...rotated, rotated[0]]);
|
|
70
|
+
}
|
|
71
|
+
/** A file's cluster: its folder, at most two levels deep (`src/orders`, `lib/screens`); `.` for the root. */
|
|
72
|
+
export function clusterOf(path) {
|
|
73
|
+
const parts = path.split('/').slice(0, -1);
|
|
74
|
+
return parts.length ? parts.slice(0, 2).join('/') : '.';
|
|
75
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { FileRecord, GraphNode, Hub, Topology } from '../model.js';
|
|
2
|
+
import type { Generator } from '../model.js';
|
|
3
|
+
/** Files by centrality score, then PageRank, then path. */
|
|
4
|
+
export declare function rankHubs(nodes: GraphNode[], top: number): Hub[];
|
|
5
|
+
export declare function buildTopology(records: FileRecord[], codeLanguages: Set<string>, generator: Generator): Topology;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { buildGraph } from './build.js';
|
|
2
|
+
import { clusterOf, cycles, degrees, pageRank } from './metrics.js';
|
|
3
|
+
/**
|
|
4
|
+
* The topology graph (topology.json). File degrees count import edges; a declaration's degrees count the
|
|
5
|
+
* extends and implements edges. `centralityScore` is twice the in-degree plus the out-degree; `pageRank` ranks files
|
|
6
|
+
* by the import graph.
|
|
7
|
+
*/
|
|
8
|
+
const TOP_HUBS = 10;
|
|
9
|
+
const FILE_EDGES = new Set(['imports']);
|
|
10
|
+
const SYMBOL_EDGES = new Set(['extends', 'implements']);
|
|
11
|
+
const toHub = (n) => ({ id: n.id, path: n.path, inDegree: n.inDegree, outDegree: n.outDegree, centralityScore: n.centralityScore, pageRank: n.pageRank });
|
|
12
|
+
/** Files by centrality score, then PageRank, then path. */
|
|
13
|
+
export function rankHubs(nodes, top) {
|
|
14
|
+
return nodes
|
|
15
|
+
.filter((n) => n.type === 'file')
|
|
16
|
+
.sort((a, b) => b.centralityScore - a.centralityScore || b.pageRank - a.pageRank || (a.path < b.path ? -1 : 1))
|
|
17
|
+
.slice(0, top)
|
|
18
|
+
.map(toHub);
|
|
19
|
+
}
|
|
20
|
+
function clusters(nodes) {
|
|
21
|
+
const counts = new Map();
|
|
22
|
+
for (const n of nodes)
|
|
23
|
+
counts.set(n.cluster, (counts.get(n.cluster) ?? 0) + 1);
|
|
24
|
+
return [...counts].map(([name, nodeCount]) => ({ name, nodeCount })).sort((a, b) => b.nodeCount - a.nodeCount || (a.name < b.name ? -1 : 1));
|
|
25
|
+
}
|
|
26
|
+
export function buildTopology(records, codeLanguages, generator) {
|
|
27
|
+
const { nodes: raw, edges } = buildGraph(records, codeLanguages);
|
|
28
|
+
const fileIds = raw.filter((n) => n.type === 'file').map((n) => n.id);
|
|
29
|
+
const fileDegrees = degrees(edges, FILE_EDGES);
|
|
30
|
+
const symbolDegrees = degrees(edges, SYMBOL_EDGES);
|
|
31
|
+
const ranks = pageRank(fileIds, edges.filter((e) => e.type === 'imports'));
|
|
32
|
+
const nodes = raw.map((n) => {
|
|
33
|
+
const d = n.type === 'file' ? fileDegrees : symbolDegrees;
|
|
34
|
+
const inDegree = d.inDegree.get(n.id) ?? 0;
|
|
35
|
+
const outDegree = d.outDegree.get(n.id) ?? 0;
|
|
36
|
+
return { ...n, inDegree, outDegree, centralityScore: inDegree * 2 + outDegree, pageRank: ranks.get(n.id) ?? 0, cluster: clusterOf(n.path) };
|
|
37
|
+
});
|
|
38
|
+
const count = nodes.length;
|
|
39
|
+
return {
|
|
40
|
+
...generator,
|
|
41
|
+
summary: {
|
|
42
|
+
totalFiles: fileIds.length,
|
|
43
|
+
totalSymbols: count - fileIds.length,
|
|
44
|
+
totalNodes: count,
|
|
45
|
+
totalEdges: edges.length,
|
|
46
|
+
density: count > 1 ? Number((edges.length / (count * (count - 1))).toFixed(5)) : 0,
|
|
47
|
+
circularDependencies: cycles(fileIds, edges.filter((e) => e.type === 'imports')).map((c) => ({ cycle: c.map((id) => id.replace(/^file:/, '')) })),
|
|
48
|
+
clusters: clusters(nodes),
|
|
49
|
+
topHubs: rankHubs(nodes, TOP_HUBS),
|
|
50
|
+
},
|
|
51
|
+
nodes,
|
|
52
|
+
edges,
|
|
53
|
+
};
|
|
54
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { LIMIT_NAMES, HealthLimitsError } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* What a change did to code health (FR-07, CIN-18): of two reports of the same project made with the same limits, the
|
|
4
|
+
* files and functions that crossed a limit (length or nesting), the new duplicated blocks and import cycles, and the
|
|
5
|
+
* totals before and after.
|
|
6
|
+
*/
|
|
7
|
+
const RANK = { soft: 1, hard: 2 };
|
|
8
|
+
const functionKey = (f) => `${f.path}\u0000${f.name}`;
|
|
9
|
+
/** A cycle in a stable form, so the same cycle found from another start file is the same cycle. */
|
|
10
|
+
function cycleKey(cycle) {
|
|
11
|
+
const nodes = cycle[0] === cycle[cycle.length - 1] ? cycle.slice(0, -1) : cycle;
|
|
12
|
+
const start = nodes.indexOf([...nodes].sort()[0]);
|
|
13
|
+
return [...nodes.slice(start), ...nodes.slice(0, start)].join('\u0000');
|
|
14
|
+
}
|
|
15
|
+
function sameLimits(before, after) {
|
|
16
|
+
const differ = LIMIT_NAMES.filter((name) => before.limits[name] !== after.limits[name]);
|
|
17
|
+
if (differ.length)
|
|
18
|
+
throw new HealthLimitsError(`The two reports were made with different limits (${differ.join(', ')}); make both with the same limits.`);
|
|
19
|
+
}
|
|
20
|
+
export function compareHealth(before, after) {
|
|
21
|
+
sameLimits(before, after);
|
|
22
|
+
const filesBefore = new Set(before.files.map((f) => f.path));
|
|
23
|
+
const functionsBefore = new Map(before.functions.map((f) => [functionKey(f), RANK[f.limit]]));
|
|
24
|
+
const deepBefore = new Set(before.nesting.map(functionKey));
|
|
25
|
+
const keysBefore = new Set(before.duplicates.flatMap((d) => d.keys));
|
|
26
|
+
const cyclesBefore = new Set(before.cycles.map(cycleKey));
|
|
27
|
+
const linesBefore = new Map(before.notMeasured.map((f) => [f.path, f.lines]));
|
|
28
|
+
return {
|
|
29
|
+
limits: after.limits,
|
|
30
|
+
files: after.files.filter((f) => !filesBefore.has(f.path)),
|
|
31
|
+
functions: after.functions.filter((f) => (functionsBefore.get(functionKey(f)) ?? 0) < RANK[f.limit]),
|
|
32
|
+
nesting: after.nesting.filter((f) => !deepBefore.has(functionKey(f))),
|
|
33
|
+
duplicates: after.duplicates.filter((d) => !d.keys.some((k) => keysBefore.has(k))),
|
|
34
|
+
cycles: after.cycles.filter((c) => !cyclesBefore.has(cycleKey(c))),
|
|
35
|
+
notMeasured: after.notMeasured.filter((f) => linesBefore.get(f.path) !== f.lines),
|
|
36
|
+
totals: { before: before.totals, after: after.totals },
|
|
37
|
+
};
|
|
38
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { DuplicateBlock } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Duplicated blocks: the same normalized run of significant lines in two places. Blank lines and lines of brackets or
|
|
4
|
+
* separators only are not significant; whitespace inside a line is collapsed. Each window of `minLines` significant
|
|
5
|
+
* lines is hashed; consecutive duplicated windows of one file form a block, and blocks sharing a window are copies.
|
|
6
|
+
*/
|
|
7
|
+
export interface SourceFile {
|
|
8
|
+
path: string;
|
|
9
|
+
content: string;
|
|
10
|
+
}
|
|
11
|
+
export declare function findDuplicates(files: SourceFile[], minLines: number): DuplicateBlock[];
|