codebase-onboarder 0.4.1 → 0.6.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/README.md +74 -2
- package/cli/explorer/advanced.js +457 -0
- package/cli/explorer/app.js +391 -0
- package/cli/explorer/commands.js +369 -0
- package/cli/explorer/github.js +75 -0
- package/cli/explorer/graphs.js +178 -0
- package/cli/explorer/session.js +251 -0
- package/cli/explorer/views.js +650 -0
- package/cli/explorer/wrap.js +49 -0
- package/cli/main.js +82 -10
- package/cli/ui.js +31 -2
- package/package.json +1 -1
- package/public/js/about.js +20 -7
- package/server/gitRemote.js +61 -0
- package/server/layout.js +41 -14
- package/shared/analyzer/github.js +81 -0
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
// One repository, loaded into memory, the way the website loads it.
|
|
2
|
+
//
|
|
3
|
+
// This is the whole reason the terminal app can stand in for the site: it runs
|
|
4
|
+
// the *same* modules in the *same* order as `server/apiScan.js` — FileSource →
|
|
5
|
+
// scanRepo → detectManifest → computeFacts → buildSearchIndex — and then the
|
|
6
|
+
// same projections the browser's views are projections of (health, layers,
|
|
7
|
+
// patterns, security, stack, tour). Nothing here re-implements analysis, so the
|
|
8
|
+
// two surfaces cannot drift into telling different stories about one repo.
|
|
9
|
+
//
|
|
10
|
+
// Everything below the loader is pure data plus a few lookups. The loader is
|
|
11
|
+
// the only async part, and it is the only part that touches the filesystem.
|
|
12
|
+
|
|
13
|
+
import { promises as fs } from 'node:fs';
|
|
14
|
+
import path from 'node:path';
|
|
15
|
+
|
|
16
|
+
import { nodeFileSource } from '../../server/fileSourceNode.js';
|
|
17
|
+
import { buildSearchIndex } from '../../server/searchIndex.js';
|
|
18
|
+
import { searchDocuments } from '../../server/apiSearch.js';
|
|
19
|
+
import { expandHome, resolveInside } from '../../server/paths.js';
|
|
20
|
+
import { assertGitUrl, cloneRepo, removeClone, repoNameFromUrl } from '../../server/gitClone.js';
|
|
21
|
+
import { gitRemoteOrigin } from '../../server/gitRemote.js';
|
|
22
|
+
import { scanRepo } from '../../shared/analyzer/scan.js';
|
|
23
|
+
import { detectManifest } from '../../shared/analyzer/services.js';
|
|
24
|
+
import { computeFacts, roleOf } from '../../shared/analyzer/graph.js';
|
|
25
|
+
import { analyzeHealth } from '../../shared/analyzer/health.js';
|
|
26
|
+
import { computeLayers, detectPatterns, couplingMatrix } from '../../shared/analyzer/patterns.js';
|
|
27
|
+
import { summarizeSecurity } from '../../shared/analyzer/security.js';
|
|
28
|
+
import { analyzeStack } from '../../shared/analyzer/stack.js';
|
|
29
|
+
import { buildTourStops } from '../../shared/analyzer/tour.js';
|
|
30
|
+
import { explainFile, explainFolder, explainOverview } from '../../shared/analyzer/explainLocal.js';
|
|
31
|
+
import { languageLabel } from '../../shared/analyzer/languages/index.js';
|
|
32
|
+
|
|
33
|
+
// Does this argument name a repository to fetch, rather than a folder on this
|
|
34
|
+
// machine? The check is deliberately narrow: only the URL shapes git itself
|
|
35
|
+
// accepts, so a folder that happens to be called `ssh://stuff` cannot exist on a
|
|
36
|
+
// filesystem anyway, while `owner/repo` is NOT accepted — too many real folders
|
|
37
|
+
// are named like that (`src/server`, `app/models`) and silently cloning
|
|
38
|
+
// `github.com/src/server` because someone typed `cd src/server` would be a
|
|
39
|
+
// genuinely bad failure. The shorthand, if it is wanted, has to be asked for.
|
|
40
|
+
export function isGitUrl(target) {
|
|
41
|
+
return /^(https?:\/\/|git@|ssh:\/\/)/.test(String(target || '').trim());
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// Open a local folder or clone a remote one — the same three ways in the web
|
|
45
|
+
// app's landing page offers, narrowed to the two that make sense for a command
|
|
46
|
+
// line. A clone is left in place when the session ends; `closeRemote` removes it,
|
|
47
|
+
// and the session calls that on `cd` and on exit, so nothing is orphaned in the
|
|
48
|
+
// temp dir.
|
|
49
|
+
export async function openRepoOrClone(target, { onProgress } = {}) {
|
|
50
|
+
const raw = String(target || '').trim();
|
|
51
|
+
if (!isGitUrl(raw)) return openRepo(raw, { onProgress });
|
|
52
|
+
|
|
53
|
+
const url = assertGitUrl(raw);
|
|
54
|
+
if (onProgress) onProgress({ phase: 'clone', url });
|
|
55
|
+
const clone = await cloneRepo(url);
|
|
56
|
+
try {
|
|
57
|
+
const repo = await openRepo(clone.dir, { onProgress });
|
|
58
|
+
// The repo's own name is what a person recognizes; the temp path is kept so
|
|
59
|
+
// `about` can still print a path someone could go and look at.
|
|
60
|
+
return {
|
|
61
|
+
...repo,
|
|
62
|
+
name: repoNameFromUrl(url) || repo.name,
|
|
63
|
+
gitUrl: url,
|
|
64
|
+
tempId: path.basename(clone.dir),
|
|
65
|
+
cloneDir: clone.dir,
|
|
66
|
+
};
|
|
67
|
+
} catch (err) {
|
|
68
|
+
// A clone that was never loaded has no session to clean it up later, so it
|
|
69
|
+
// has to go now or it is a temp directory nobody owns.
|
|
70
|
+
await removeClone(clone.dir);
|
|
71
|
+
throw err;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// The URL this repo should be asked about on GitHub, or null.
|
|
76
|
+
//
|
|
77
|
+
// A repo this session cloned knows its own URL. A repo someone was standing in
|
|
78
|
+
// does not, and git does. Asking git here rather than in the `github` command
|
|
79
|
+
// keeps the answer on the repo object, so every consumer gets it the same way.
|
|
80
|
+
export async function remoteUrlFor(repo) {
|
|
81
|
+
if (repo.gitUrl) return repo.gitUrl;
|
|
82
|
+
if (repo.remoteChecked) return null;
|
|
83
|
+
repo.remoteChecked = true;
|
|
84
|
+
const found = await gitRemoteOrigin(repo.root);
|
|
85
|
+
if (found.ok) repo.gitUrl = found.url;
|
|
86
|
+
return repo.gitUrl || null;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// Drop a temp clone, if this repo is one. Safe to call on a local repo: it
|
|
90
|
+
// checks the shape of the directory rather than trusting a flag, so a bug
|
|
91
|
+
// elsewhere cannot turn "clean up" into "delete the folder the user is in".
|
|
92
|
+
export async function closeRemote(repo) {
|
|
93
|
+
if (repo?.cloneDir) await removeClone(repo.cloneDir);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// Scan a directory and return the loaded repo, or throw an Error whose message
|
|
97
|
+
// is safe to print straight to the user. `target` may be `~`, a relative path,
|
|
98
|
+
// or anything `expandHome` understands; it defaults to the current directory.
|
|
99
|
+
export async function openRepo(target = '.', { onProgress } = {}) {
|
|
100
|
+
const root = expandHome(String(target || '.'));
|
|
101
|
+
const stat = await fs.stat(root).catch(() => null);
|
|
102
|
+
if (!stat) throw new Error('No such folder: ' + root);
|
|
103
|
+
if (!stat.isDirectory()) throw new Error('Not a folder: ' + root);
|
|
104
|
+
|
|
105
|
+
const source = nodeFileSource(root);
|
|
106
|
+
const scan = await scanRepo(source, { onProgress });
|
|
107
|
+
const manifest = await detectManifest(source);
|
|
108
|
+
const facts = computeFacts(scan, manifest);
|
|
109
|
+
const searchIndex = await buildSearchIndex(source, scan.files.map((f) => f.path));
|
|
110
|
+
|
|
111
|
+
// The layers projection is an input to patterns, not a parallel view of the
|
|
112
|
+
// same thing — `detectPatterns` reads the layer depths to place a file in the
|
|
113
|
+
// architecture. Compute once, share. The coupling matrix is the same kind of
|
|
114
|
+
// thing: a projection of `facts.folderEdges` that both the site and the
|
|
115
|
+
// terminal's heat grid read, so it is computed once here rather than per view.
|
|
116
|
+
const layers = computeLayers(scan, facts);
|
|
117
|
+
|
|
118
|
+
return {
|
|
119
|
+
root,
|
|
120
|
+
name: scan.name || path.basename(root),
|
|
121
|
+
scan,
|
|
122
|
+
facts,
|
|
123
|
+
manifest,
|
|
124
|
+
searchIndex,
|
|
125
|
+
layers,
|
|
126
|
+
coupling: couplingMatrix(scan, facts),
|
|
127
|
+
health: analyzeHealth(scan, facts),
|
|
128
|
+
patterns: detectPatterns(scan, facts, manifest, layers),
|
|
129
|
+
security: summarizeSecurity(scan),
|
|
130
|
+
stack: analyzeStack(manifest, scan.stats.languages || {}),
|
|
131
|
+
tour: buildTourStops(scan, facts),
|
|
132
|
+
languages: languageRows(scan),
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// LOC per language, biggest first, with the display label the site uses — so
|
|
137
|
+
// `JavaScript` here and `JavaScript` there are the same string.
|
|
138
|
+
function languageRows(scan) {
|
|
139
|
+
return Object.entries(scan.stats.languages || {})
|
|
140
|
+
.map(([id, loc]) => ({ id, loc, label: languageLabel(id) }))
|
|
141
|
+
.sort((a, b) => b.loc - a.loc || a.label.localeCompare(b.label));
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// The one place a typed path becomes a real file. People type `logger.js`, not
|
|
145
|
+
// `server/logger.js`, and a tool that says "no such file" for a file that is
|
|
146
|
+
// right there is worse than no tool. So: exact, then folder, then suffix, then
|
|
147
|
+
// basename, then a substring. Ambiguity is reported with the candidates rather
|
|
148
|
+
// than guessed at — silently picking the wrong `index.js` is the failure mode
|
|
149
|
+
// that makes people stop trusting a map.
|
|
150
|
+
export function resolveTarget(repo, typed) {
|
|
151
|
+
const raw = String(typed || '').trim().replace(/^\.\//, '');
|
|
152
|
+
if (!raw) return { error: 'Name a file or folder.' };
|
|
153
|
+
|
|
154
|
+
// `.` and `./` are the root, which is a real folder and a common thing to type
|
|
155
|
+
// (`tree .`, `explain .`). Without this they fell through to the substring
|
|
156
|
+
// match and offered eight unrelated files as "did you mean", which is worse
|
|
157
|
+
// than useless — it is actively misleading. The root folder's path is `''`.
|
|
158
|
+
if (raw === '.' || raw === './') return { folder: ROOT_FOLDER };
|
|
159
|
+
|
|
160
|
+
const files = repo.scan.files;
|
|
161
|
+
if (!repo.byPath) repo.byPath = new Map(files.map((f) => [f.path, f]));
|
|
162
|
+
if (repo.byPath.has(raw)) return { file: repo.byPath.get(raw) };
|
|
163
|
+
|
|
164
|
+
const folder = folderFor(repo, raw);
|
|
165
|
+
if (folder) return { folder };
|
|
166
|
+
|
|
167
|
+
const lower = raw.toLowerCase();
|
|
168
|
+
const suffix = files.filter((f) => f.path.endsWith('/' + raw));
|
|
169
|
+
if (suffix.length === 1) return { file: suffix[0] };
|
|
170
|
+
if (suffix.length > 1) return { error: 'Which one?', candidates: suffix.map((f) => f.path) };
|
|
171
|
+
|
|
172
|
+
const base = files.filter((f) => f.name.toLowerCase() === lower);
|
|
173
|
+
if (base.length === 1) return { file: base[0] };
|
|
174
|
+
if (base.length > 1) return { error: 'Which one?', candidates: base.map((f) => f.path) };
|
|
175
|
+
|
|
176
|
+
const loose = files.filter((f) => f.path.toLowerCase().includes(lower));
|
|
177
|
+
if (loose.length) {
|
|
178
|
+
loose.sort((a, b) => a.path.length - b.path.length);
|
|
179
|
+
return { error: 'No exact match. Did you mean:', candidates: loose.slice(0, 8).map((f) => f.path) };
|
|
180
|
+
}
|
|
181
|
+
return { error: 'Nothing in this repo matches "' + raw + '".' };
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// The root folder is a real answer, not a sentinel: `tree .` and `explain .`
|
|
185
|
+
// both need one, and it carries the same fields `resolveTarget` returns for any
|
|
186
|
+
// other folder so the callers need no special case.
|
|
187
|
+
const ROOT_FOLDER = Object.freeze({ name: '', path: '', loc: 0, comment: 0, blank: 0, size: 0, langs: {} });
|
|
188
|
+
|
|
189
|
+
function folderFor(repo, typed) {
|
|
190
|
+
const clean = typed.replace(/\/+$/, '');
|
|
191
|
+
if (clean === '') return ROOT_FOLDER;
|
|
192
|
+
const exact = repo.scan.folders.find((d) => d.path === clean);
|
|
193
|
+
if (exact) return exact;
|
|
194
|
+
const matches = repo.scan.folders.filter((d) => d.path.endsWith('/' + clean) || d.name === clean);
|
|
195
|
+
return matches.length === 1 ? matches[0] : null;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
// Read a file through the same containment check the HTTP route uses. A tool
|
|
199
|
+
// whose job is showing you one repository has no business reading any path on
|
|
200
|
+
// the machine, and going through `resolveInside` means there is no second,
|
|
201
|
+
// looser door to get there.
|
|
202
|
+
export async function readRepoFile(repo, repoPath) {
|
|
203
|
+
const abs = resolveInside(repo.root, repoPath);
|
|
204
|
+
if (!abs) throw new Error('That path is outside the repository.');
|
|
205
|
+
return fs.readFile(abs, 'utf8');
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
// The website's search, verbatim: same TF-IDF ranking, same query language
|
|
209
|
+
// (`ext:`, `path:`, `-exclude`, `"phrases"`, `/regex/`), same result shape. A
|
|
210
|
+
// query that works in one surface works in the other, because it is the same
|
|
211
|
+
// function.
|
|
212
|
+
export function searchRepo(repo, query, { limit = 12 } = {}) {
|
|
213
|
+
return searchDocuments(repo.searchIndex, query, { limit });
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// A number a person typed, or nothing. `show f 1.5 2.5` used to print
|
|
217
|
+
// `1.5–3 of 344`: `Number('1.5')` passed the `|| 1` fallback, the label
|
|
218
|
+
// interpolated the fraction, and only `slice()` coerced it. A line range is
|
|
219
|
+
// either a whole number of lines or it is a mistake, and a mistake gets a
|
|
220
|
+
// message rather than a wrong heading.
|
|
221
|
+
export function lineNumber(value, fallback) {
|
|
222
|
+
if (value === undefined || value === null || value === '') return fallback;
|
|
223
|
+
const n = Number(value);
|
|
224
|
+
return Number.isInteger(n) && n > 0 ? n : null;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
export function explainRepoFile(repo, file) {
|
|
228
|
+
return explainFile(file.path, file, repo.facts);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
export function explainRepoFolder(repo, folder) {
|
|
232
|
+
return explainFolder(folder.path, repo.scan, repo.facts);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
export function explainRepoOverview(repo) {
|
|
236
|
+
return explainOverview(repo.scan, repo.facts, repo.manifest);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
// Short facts about a file, in the order `docs.js` states them, so the terminal
|
|
240
|
+
// and the generated docs never describe the same file differently.
|
|
241
|
+
export function fileFacts(repo, file) {
|
|
242
|
+
return {
|
|
243
|
+
role: roleOf(file.path, repo.facts),
|
|
244
|
+
loc: file.loc || 0,
|
|
245
|
+
complexity: file.complexity || 0,
|
|
246
|
+
fanIn: repo.facts.fanIn[file.path] || 0,
|
|
247
|
+
fanOut: repo.facts.fanOut[file.path] || 0,
|
|
248
|
+
inCycle: (repo.facts.inCycle || []).includes(file.path),
|
|
249
|
+
findings: (file.findings || []).length,
|
|
250
|
+
};
|
|
251
|
+
}
|