@getchomp/cli 0.0.0-stage → 0.2.43
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 +21 -0
- package/README.md +39 -2
- package/dist/commands/analyze.d.ts +49 -0
- package/dist/commands/analyze.js +280 -0
- package/dist/commands/connect.d.ts +12 -0
- package/dist/commands/connect.js +255 -0
- package/dist/commands/export.d.ts +29 -0
- package/dist/commands/export.js +356 -0
- package/dist/commands/health.d.ts +19 -0
- package/dist/commands/health.js +259 -0
- package/dist/commands/init.d.ts +40 -0
- package/dist/commands/init.js +659 -0
- package/dist/commands/mcp.d.ts +2 -0
- package/dist/commands/mcp.js +12 -0
- package/dist/commands/plugin.d.ts +8 -0
- package/dist/commands/plugin.js +198 -0
- package/dist/commands/ui.d.ts +20 -0
- package/dist/commands/ui.js +252 -0
- package/dist/commands/uninstall.d.ts +8 -0
- package/dist/commands/uninstall.js +246 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +37 -0
- package/dist/ports/portHydrator.d.ts +63 -0
- package/dist/ports/portHydrator.js +954 -0
- package/dist/ports/portHydratorWorker.d.ts +22 -0
- package/dist/ports/portHydratorWorker.js +75 -0
- package/dist/server/buildStatusTracker.d.ts +45 -0
- package/dist/server/buildStatusTracker.js +122 -0
- package/dist/server/graphLayoutState.d.ts +205 -0
- package/dist/server/graphLayoutState.js +930 -0
- package/dist/server/loopbackSecurity.d.ts +18 -0
- package/dist/server/loopbackSecurity.js +57 -0
- package/dist/server/routes/authSourceRoutes.d.ts +18 -0
- package/dist/server/routes/authSourceRoutes.js +231 -0
- package/dist/server/routes/layoutRoutes.d.ts +3 -0
- package/dist/server/routes/layoutRoutes.js +857 -0
- package/dist/server/routes/pluginRoutes.d.ts +3 -0
- package/dist/server/routes/pluginRoutes.js +368 -0
- package/dist/server/routes/viewRulesRoutes.d.ts +3 -0
- package/dist/server/routes/viewRulesRoutes.js +173 -0
- package/dist/server/uiHotkeys.d.ts +26 -0
- package/dist/server/uiHotkeys.js +160 -0
- package/dist/utils/branding.d.ts +126 -0
- package/dist/utils/branding.js +978 -0
- package/dist/utils/credentials.d.ts +22 -0
- package/dist/utils/credentials.js +126 -0
- package/dist/utils/git.d.ts +95 -0
- package/dist/utils/git.js +780 -0
- package/dist/utils/masterContext.d.ts +112 -0
- package/dist/utils/masterContext.js +642 -0
- package/dist/visualizer/cloudLayoutClient.d.ts +84 -0
- package/dist/visualizer/cloudLayoutClient.js +1200 -0
- package/dist/visualizer/layoutCache.d.ts +25 -0
- package/dist/visualizer/layoutCache.js +406 -0
- package/dist/visualizer/localEngineBridge.d.ts +50 -0
- package/dist/visualizer/localEngineBridge.js +80 -0
- package/dist/visualizer/pluginPresentationApi.d.ts +111 -0
- package/dist/visualizer/pluginPresentationApi.js +432 -0
- package/dist/visualizer/rulesConfig.d.ts +11 -0
- package/dist/visualizer/rulesConfig.js +266 -0
- package/dist/visualizer/sourceAccess.d.ts +45 -0
- package/dist/visualizer/sourceAccess.js +204 -0
- package/package.json +52 -3
|
@@ -0,0 +1,642 @@
|
|
|
1
|
+
import { chmodSync, existsSync, mkdirSync, readFileSync, readdirSync, statSync, unlinkSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { homedir } from "node:os";
|
|
3
|
+
import { basename, delimiter, dirname, resolve } from "node:path";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
import { execSync } from "node:child_process";
|
|
6
|
+
import { getGitRepoIdentity, isLinkedGitWorktree, resolveRepoName, sharesGitRepository, } from "./git.js";
|
|
7
|
+
export { getGitRepoIdentity, isLinkedGitWorktree, resolveRepoName, sharesGitRepository };
|
|
8
|
+
function toRepoId(repoName) {
|
|
9
|
+
return repoName.toLowerCase().replace(/[^a-z0-9_-]/g, "_");
|
|
10
|
+
}
|
|
11
|
+
export function getChompMasterRoot() {
|
|
12
|
+
if (process.env.CHOMP_MASTER_ROOT && existsSync(process.env.CHOMP_MASTER_ROOT)) {
|
|
13
|
+
return resolve(process.env.CHOMP_MASTER_ROOT);
|
|
14
|
+
}
|
|
15
|
+
const metaUrl = typeof import.meta !== "undefined" ? import.meta.url : undefined;
|
|
16
|
+
if (typeof metaUrl === "string" && metaUrl.startsWith("file:")) {
|
|
17
|
+
const __dirname = dirname(fileURLToPath(metaUrl));
|
|
18
|
+
return resolve(__dirname, "../../../..");
|
|
19
|
+
}
|
|
20
|
+
return process.cwd();
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Resolves the canonical primary checkout for the Chomp master repository.
|
|
24
|
+
* If the current CLI is running inside a linked Git worktree (`git worktree add`),
|
|
25
|
+
* maps back to the primary checkout root when it contains `packages/cli/src/index.ts`
|
|
26
|
+
* so global `~/.local/bin/chomp` shims and fallback context never get hijacked by
|
|
27
|
+
* ephemeral worktrees.
|
|
28
|
+
*/
|
|
29
|
+
export function getCanonicalChompMasterRoot(rawMasterRoot = getChompMasterRoot()) {
|
|
30
|
+
const identity = getGitRepoIdentity(rawMasterRoot);
|
|
31
|
+
if (identity?.isLinkedWorktree && identity.primaryRepoRoot) {
|
|
32
|
+
const primaryCliEntry = resolve(identity.primaryRepoRoot, "packages/cli/src/index.ts");
|
|
33
|
+
if (existsSync(primaryCliEntry)) {
|
|
34
|
+
return identity.primaryRepoRoot;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return resolve(rawMasterRoot);
|
|
38
|
+
}
|
|
39
|
+
export function isChompMasterMonorepo(root = getChompMasterRoot()) {
|
|
40
|
+
try {
|
|
41
|
+
return existsSync(resolve(root, "packages/cli/src/index.ts"));
|
|
42
|
+
}
|
|
43
|
+
catch {
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
function isUsableChompExecutable(binPath) {
|
|
48
|
+
if (!existsSync(binPath))
|
|
49
|
+
return false;
|
|
50
|
+
try {
|
|
51
|
+
const stat = statSync(binPath);
|
|
52
|
+
if (!stat.isFile())
|
|
53
|
+
return false;
|
|
54
|
+
// If this is an auto-generated master-repo shim, verify its target CLI file still exists
|
|
55
|
+
if (stat.size < 4096) {
|
|
56
|
+
const head = readFileSync(binPath, "utf8");
|
|
57
|
+
if (head.includes("Auto-generated Chomp Master CLI shim")) {
|
|
58
|
+
const match = /CHOMP_MASTER_ROOT="([^"]+)"/.exec(head);
|
|
59
|
+
if (match && !existsSync(resolve(match[1], "packages/cli/src/index.ts"))) {
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
return true;
|
|
65
|
+
}
|
|
66
|
+
catch {
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Resolves a globally installed `chomp` CLI binary on `$PATH` or `~/.local/bin/chomp`,
|
|
72
|
+
* ignoring ephemeral `npx` / `pnpm dlx` temporary cache directories.
|
|
73
|
+
*/
|
|
74
|
+
export function resolveGlobalChompBinary(env = process.env, homeDirOverride = homedir()) {
|
|
75
|
+
const localShim = resolve(homeDirOverride, ".local", "bin", "chomp");
|
|
76
|
+
if (isUsableChompExecutable(localShim)) {
|
|
77
|
+
return localShim;
|
|
78
|
+
}
|
|
79
|
+
const rawPath = env.PATH ?? env.Path ?? "";
|
|
80
|
+
const binNames = process.platform === "win32" ? ["chomp.cmd", "chomp.exe", "chomp"] : ["chomp"];
|
|
81
|
+
for (const rawDir of rawPath.split(delimiter)) {
|
|
82
|
+
const dir = rawDir.trim();
|
|
83
|
+
if (!dir)
|
|
84
|
+
continue;
|
|
85
|
+
const norm = dir.replace(/\\/g, "/");
|
|
86
|
+
if (norm.includes("/_npx/") ||
|
|
87
|
+
norm.includes("/pnpm/dlx/") ||
|
|
88
|
+
norm.includes("/node_modules/.bin")) {
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
for (const name of binNames) {
|
|
92
|
+
const candidate = resolve(dir, name);
|
|
93
|
+
if (isUsableChompExecutable(candidate)) {
|
|
94
|
+
return candidate;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return null;
|
|
99
|
+
}
|
|
100
|
+
export function hasPrivateResearchLedger() {
|
|
101
|
+
try {
|
|
102
|
+
return existsSync(resolve(getChompMasterRoot(), "fixtures/research/pattern_ledger.db"));
|
|
103
|
+
}
|
|
104
|
+
catch {
|
|
105
|
+
return false;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
export function getGlobalContextPath() {
|
|
109
|
+
if (process.env.CHOMP_GLOBAL_CONTEXT_PATH?.trim()) {
|
|
110
|
+
return resolve(process.env.CHOMP_GLOBAL_CONTEXT_PATH.trim());
|
|
111
|
+
}
|
|
112
|
+
return resolve(homedir(), ".chomp", "context.json");
|
|
113
|
+
}
|
|
114
|
+
export function getWorkspaceFallbackContextPath() {
|
|
115
|
+
return resolve(getCanonicalChompMasterRoot(), ".chomp", "master_context.json");
|
|
116
|
+
}
|
|
117
|
+
export function resolveGitRepoRoot(inputPath) {
|
|
118
|
+
const abs = resolve(inputPath);
|
|
119
|
+
const dir = existsSync(abs) && !statSync(abs).isDirectory() ? dirname(abs) : abs;
|
|
120
|
+
const masterRoot = getChompMasterRoot();
|
|
121
|
+
// 1. If dir is inside <masterRoot>/fixtures/cloned-repos/<repo> or <masterRoot>/fixtures/test-repos/<repo>,
|
|
122
|
+
// resolve strictly to that fixture repo root — never let `git rev-parse` walk up to masterRoot!
|
|
123
|
+
for (const fixtureBucket of ["cloned-repos", "test-repos"]) {
|
|
124
|
+
const bucketPrefix = resolve(masterRoot, "fixtures", fixtureBucket) + "/";
|
|
125
|
+
if (dir.startsWith(bucketPrefix)) {
|
|
126
|
+
const rel = dir.slice(bucketPrefix.length);
|
|
127
|
+
const repoFolder = rel.split("/")[0];
|
|
128
|
+
if (repoFolder) {
|
|
129
|
+
return resolve(masterRoot, "fixtures", fixtureBucket, repoFolder);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
if (existsSync(resolve(dir, ".git")) || existsSync(resolve(dir, ".chomp"))) {
|
|
134
|
+
return dir;
|
|
135
|
+
}
|
|
136
|
+
try {
|
|
137
|
+
const gitTop = execSync("git rev-parse --show-toplevel", {
|
|
138
|
+
cwd: dir,
|
|
139
|
+
encoding: "utf8",
|
|
140
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
141
|
+
}).trim();
|
|
142
|
+
if (resolve(gitTop) === resolve(masterRoot) &&
|
|
143
|
+
resolve(dir) !== resolve(masterRoot) &&
|
|
144
|
+
existsSync(resolve(dir, "package.json")) &&
|
|
145
|
+
!dir.startsWith(resolve(masterRoot, "packages") + "/") &&
|
|
146
|
+
!dir.startsWith(resolve(masterRoot, "apps") + "/")) {
|
|
147
|
+
return dir;
|
|
148
|
+
}
|
|
149
|
+
return gitTop;
|
|
150
|
+
}
|
|
151
|
+
catch {
|
|
152
|
+
return dir;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
export function readMasterContext() {
|
|
156
|
+
const rawCandidates = isChompMasterMonorepo()
|
|
157
|
+
? [getGlobalContextPath(), getWorkspaceFallbackContextPath()]
|
|
158
|
+
: [getGlobalContextPath()];
|
|
159
|
+
const candidates = rawCandidates
|
|
160
|
+
.filter((p) => existsSync(p))
|
|
161
|
+
.sort((a, b) => {
|
|
162
|
+
try {
|
|
163
|
+
return statSync(b).mtimeMs - statSync(a).mtimeMs;
|
|
164
|
+
}
|
|
165
|
+
catch {
|
|
166
|
+
return 0;
|
|
167
|
+
}
|
|
168
|
+
});
|
|
169
|
+
for (const ctxPath of candidates) {
|
|
170
|
+
try {
|
|
171
|
+
const parsed = JSON.parse(readFileSync(ctxPath, "utf8"));
|
|
172
|
+
if (parsed && typeof parsed.activeRepoPath === "string") {
|
|
173
|
+
const canonicalActiveName = resolveRepoName(parsed.activeRepoPath);
|
|
174
|
+
const activeRepoName = parsed.activeRepoName && parsed.activeRepoName !== basename(parsed.activeRepoPath)
|
|
175
|
+
? parsed.activeRepoName
|
|
176
|
+
: canonicalActiveName || parsed.activeRepoName || basename(parsed.activeRepoPath);
|
|
177
|
+
const activeRepoId = parsed.activeRepoId && parsed.activeRepoId !== toRepoId(basename(parsed.activeRepoPath))
|
|
178
|
+
? parsed.activeRepoId
|
|
179
|
+
: toRepoId(activeRepoName);
|
|
180
|
+
return {
|
|
181
|
+
activeRepoId,
|
|
182
|
+
activeRepoPath: parsed.activeRepoPath,
|
|
183
|
+
activeRepoName,
|
|
184
|
+
masterRoot: parsed.masterRoot || getChompMasterRoot(),
|
|
185
|
+
updatedAt: parsed.updatedAt || new Date().toISOString(),
|
|
186
|
+
recentRepos: Array.isArray(parsed.recentRepos)
|
|
187
|
+
? parsed.recentRepos.map((r) => {
|
|
188
|
+
const rPath = typeof r?.path === "string" ? r.path : "";
|
|
189
|
+
const canonicalRecentName = rPath ? resolveRepoName(rPath) : "";
|
|
190
|
+
const rName = r?.name && rPath && r.name !== basename(rPath)
|
|
191
|
+
? r.name
|
|
192
|
+
: canonicalRecentName || r?.name || basename(rPath);
|
|
193
|
+
const rId = r?.id && rPath && r.id !== toRepoId(basename(rPath))
|
|
194
|
+
? r.id
|
|
195
|
+
: toRepoId(rName);
|
|
196
|
+
return {
|
|
197
|
+
id: rId,
|
|
198
|
+
name: rName,
|
|
199
|
+
path: r?.path,
|
|
200
|
+
lastConnectedAt: r?.lastConnectedAt || new Date().toISOString(),
|
|
201
|
+
};
|
|
202
|
+
})
|
|
203
|
+
: [],
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
catch {
|
|
208
|
+
// Ignore malformed context file
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
return null;
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* Resolves the target repository path for CLI commands (`chomp ui`, `chomp analyze`, `chomp health`).
|
|
215
|
+
*
|
|
216
|
+
* - When `inputPath` is explicitly passed (`.`, `./subpath`, `express`, `chomp`), resolves that explicit target
|
|
217
|
+
* (including short names inside `fixtures/cloned-repos/<name>`).
|
|
218
|
+
* - When `inputPath` is omitted (`undefined`):
|
|
219
|
+
* 1. Linked Git worktrees ALWAYS resolve to the current worktree (`mode: "worktree"`), never jumping
|
|
220
|
+
* to `main` or another worktree.
|
|
221
|
+
* 2. If the caller's repo shares the same underlying Git repository (`commonDir`) as the tethered repo
|
|
222
|
+
* (e.g. `main` vs a linked worktree of `chomp`), always prefer the caller's checkout (`mode: "local"`).
|
|
223
|
+
* 3. When inside the primary Chomp master repository and tethered to a *different* external repository
|
|
224
|
+
* (e.g. `chomp connect express`), follow the Master Tether (`[PAT-CLI-14]`, `mode: "tethered"`).
|
|
225
|
+
* 4. When inside any other initialized Git/Chomp repository (e.g. `~/code/my-app`), prefer that local
|
|
226
|
+
* repository (`[PAT-CLI-13]`, `mode: "local"`).
|
|
227
|
+
* 5. When outside any repository and `preferTetherWhenNoArg` is true, fall back to the active Master Tether.
|
|
228
|
+
*/
|
|
229
|
+
export function resolveDefaultCliTarget(inputPath, baseDir = process.env.INIT_CWD ?? process.cwd(), preferTetherWhenNoArg = false) {
|
|
230
|
+
const masterRoot = getChompMasterRoot();
|
|
231
|
+
const canonicalMasterRoot = getCanonicalChompMasterRoot(masterRoot);
|
|
232
|
+
if (inputPath !== undefined && inputPath.trim().length > 0) {
|
|
233
|
+
const trimmed = inputPath.trim();
|
|
234
|
+
if (trimmed === "chomp" || trimmed === "master") {
|
|
235
|
+
return { targetPath: canonicalMasterRoot, explicit: true, mode: "explicit" };
|
|
236
|
+
}
|
|
237
|
+
if (trimmed.startsWith("~/")) {
|
|
238
|
+
const homeCandidate = resolve(homedir(), trimmed.slice(2));
|
|
239
|
+
if (existsSync(homeCandidate)) {
|
|
240
|
+
return { targetPath: homeCandidate, explicit: true, mode: "explicit" };
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
const localCandidate = resolve(baseDir, trimmed);
|
|
244
|
+
if (existsSync(localCandidate)) {
|
|
245
|
+
return { targetPath: localCandidate, explicit: true, mode: "explicit" };
|
|
246
|
+
}
|
|
247
|
+
const clonedCandidate = resolve(canonicalMasterRoot, "fixtures", "cloned-repos", trimmed);
|
|
248
|
+
if (existsSync(clonedCandidate)) {
|
|
249
|
+
return { targetPath: clonedCandidate, explicit: true, mode: "explicit" };
|
|
250
|
+
}
|
|
251
|
+
const testCandidate = resolve(canonicalMasterRoot, "fixtures", "test-repos", trimmed);
|
|
252
|
+
if (existsSync(testCandidate)) {
|
|
253
|
+
return { targetPath: testCandidate, explicit: true, mode: "explicit" };
|
|
254
|
+
}
|
|
255
|
+
const ctx = readMasterContext();
|
|
256
|
+
if (ctx) {
|
|
257
|
+
const normalized = trimmed.toLowerCase();
|
|
258
|
+
const normalizedId = toRepoId(trimmed);
|
|
259
|
+
if (ctx.activeRepoPath &&
|
|
260
|
+
existsSync(ctx.activeRepoPath) &&
|
|
261
|
+
(ctx.activeRepoName?.toLowerCase() === normalized ||
|
|
262
|
+
ctx.activeRepoId?.toLowerCase() === normalized ||
|
|
263
|
+
ctx.activeRepoId?.toLowerCase() === normalizedId ||
|
|
264
|
+
basename(ctx.activeRepoPath).toLowerCase() === normalized)) {
|
|
265
|
+
return { targetPath: resolve(ctx.activeRepoPath), explicit: true, mode: "explicit" };
|
|
266
|
+
}
|
|
267
|
+
for (const r of ctx.recentRepos ?? []) {
|
|
268
|
+
if (!r?.path || !existsSync(r.path))
|
|
269
|
+
continue;
|
|
270
|
+
if (r.name?.toLowerCase() === normalized ||
|
|
271
|
+
r.id?.toLowerCase() === normalized ||
|
|
272
|
+
r.id?.toLowerCase() === normalizedId ||
|
|
273
|
+
basename(r.path).toLowerCase() === normalized) {
|
|
274
|
+
return { targetPath: resolve(r.path), explicit: true, mode: "explicit" };
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
return { targetPath: localCandidate, explicit: true, mode: "explicit" };
|
|
279
|
+
}
|
|
280
|
+
const callerRepoRoot = resolveGitRepoRoot(baseDir);
|
|
281
|
+
const callerIsWorktree = isLinkedGitWorktree(callerRepoRoot);
|
|
282
|
+
const callerHasRepo = existsSync(resolve(callerRepoRoot, ".git")) ||
|
|
283
|
+
existsSync(resolve(callerRepoRoot, ".chomp"));
|
|
284
|
+
if (callerIsWorktree) {
|
|
285
|
+
return { targetPath: callerRepoRoot, explicit: false, mode: "worktree" };
|
|
286
|
+
}
|
|
287
|
+
const ctx = readMasterContext();
|
|
288
|
+
if (ctx?.activeRepoPath && existsSync(ctx.activeRepoPath)) {
|
|
289
|
+
// Never jump from a primary checkout (e.g. `main`) into a linked worktree of the same repo
|
|
290
|
+
if (sharesGitRepository(callerRepoRoot, ctx.activeRepoPath)) {
|
|
291
|
+
return { targetPath: callerRepoRoot, explicit: false, mode: "local" };
|
|
292
|
+
}
|
|
293
|
+
const inPrimaryMasterRepo = resolve(callerRepoRoot) === resolve(canonicalMasterRoot) ||
|
|
294
|
+
resolve(callerRepoRoot) === resolve(masterRoot);
|
|
295
|
+
if (inPrimaryMasterRepo) {
|
|
296
|
+
return { targetPath: ctx.activeRepoPath, explicit: false, mode: "tethered" };
|
|
297
|
+
}
|
|
298
|
+
if (!callerHasRepo && preferTetherWhenNoArg) {
|
|
299
|
+
return { targetPath: ctx.activeRepoPath, explicit: false, mode: "tethered" };
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
if (callerHasRepo) {
|
|
303
|
+
return { targetPath: callerRepoRoot, explicit: false, mode: "local" };
|
|
304
|
+
}
|
|
305
|
+
return { targetPath: resolve(baseDir, "."), explicit: false, mode: "local" };
|
|
306
|
+
}
|
|
307
|
+
export function setActiveChompTarget(targetPath, options = {}) {
|
|
308
|
+
const repoRoot = resolveGitRepoRoot(targetPath);
|
|
309
|
+
const repoName = resolveRepoName(repoRoot);
|
|
310
|
+
const repoId = toRepoId(repoName);
|
|
311
|
+
const canonicalMaster = getCanonicalChompMasterRoot();
|
|
312
|
+
const now = new Date().toISOString();
|
|
313
|
+
const prev = readMasterContext();
|
|
314
|
+
const existingRecents = prev?.recentRepos ?? [];
|
|
315
|
+
const filtered = existingRecents.filter((r) => r && r.path && resolve(r.path) !== repoRoot);
|
|
316
|
+
const recentRepos = [
|
|
317
|
+
{ id: repoId, name: repoName, path: repoRoot, lastConnectedAt: now },
|
|
318
|
+
...filtered,
|
|
319
|
+
].slice(0, 25);
|
|
320
|
+
const nextCtx = {
|
|
321
|
+
activeRepoId: repoId,
|
|
322
|
+
activeRepoPath: repoRoot,
|
|
323
|
+
activeRepoName: repoName,
|
|
324
|
+
masterRoot: canonicalMaster,
|
|
325
|
+
updatedAt: now,
|
|
326
|
+
recentRepos,
|
|
327
|
+
};
|
|
328
|
+
if (!options.allowWorktree && isLinkedGitWorktree(repoRoot)) {
|
|
329
|
+
return nextCtx;
|
|
330
|
+
}
|
|
331
|
+
const payload = JSON.stringify(nextCtx, null, 2) + "\n";
|
|
332
|
+
// Write to the canonical master workspace fallback only when inside the Chomp monorepo
|
|
333
|
+
if (isChompMasterMonorepo(canonicalMaster)) {
|
|
334
|
+
try {
|
|
335
|
+
const fallbackPath = getWorkspaceFallbackContextPath();
|
|
336
|
+
mkdirSync(dirname(fallbackPath), { recursive: true });
|
|
337
|
+
writeFileSync(fallbackPath, payload, "utf8");
|
|
338
|
+
}
|
|
339
|
+
catch {
|
|
340
|
+
// Ignore if master workspace is read-only
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
// Also write to ~/.chomp/context.json when permitted
|
|
344
|
+
try {
|
|
345
|
+
const ctxPath = getGlobalContextPath();
|
|
346
|
+
mkdirSync(dirname(ctxPath), { recursive: true });
|
|
347
|
+
writeFileSync(ctxPath, payload, "utf8");
|
|
348
|
+
}
|
|
349
|
+
catch {
|
|
350
|
+
// Ignore EPERM when running inside a restricted sandbox
|
|
351
|
+
}
|
|
352
|
+
return nextCtx;
|
|
353
|
+
}
|
|
354
|
+
/**
|
|
355
|
+
* Removes an uninstalled repository from `~/.chomp/context.json` (`recentRepos`),
|
|
356
|
+
* switching `activeRepoPath` to the next valid recent repo (or canonical master monorepo)
|
|
357
|
+
* when available, or clearing `~/.chomp/context.json` cleanly in standalone NPM installs
|
|
358
|
+
* so ephemeral `_npx/.../node_modules` paths are never persisted as active targets.
|
|
359
|
+
*/
|
|
360
|
+
export function clearRemovedChompTarget(removedPath) {
|
|
361
|
+
const repoRoot = resolve(removedPath);
|
|
362
|
+
const prev = readMasterContext();
|
|
363
|
+
if (!prev) {
|
|
364
|
+
return { switchedTo: null, cleared: false };
|
|
365
|
+
}
|
|
366
|
+
const remainingRecents = (prev.recentRepos ?? []).filter((r) => r && r.path && resolve(r.path) !== repoRoot && existsSync(r.path));
|
|
367
|
+
const wasActive = Boolean(prev.activeRepoPath) && resolve(prev.activeRepoPath) === repoRoot;
|
|
368
|
+
if (!wasActive) {
|
|
369
|
+
if (remainingRecents.length !== (prev.recentRepos ?? []).length) {
|
|
370
|
+
const updated = {
|
|
371
|
+
...prev,
|
|
372
|
+
updatedAt: new Date().toISOString(),
|
|
373
|
+
recentRepos: remainingRecents,
|
|
374
|
+
};
|
|
375
|
+
const payload = JSON.stringify(updated, null, 2) + "\n";
|
|
376
|
+
try {
|
|
377
|
+
const ctxPath = getGlobalContextPath();
|
|
378
|
+
mkdirSync(dirname(ctxPath), { recursive: true });
|
|
379
|
+
writeFileSync(ctxPath, payload, "utf8");
|
|
380
|
+
}
|
|
381
|
+
catch {
|
|
382
|
+
// Ignore sandbox write errors
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
return { switchedTo: null, cleared: false };
|
|
386
|
+
}
|
|
387
|
+
if (remainingRecents.length > 0) {
|
|
388
|
+
const nextRepo = remainingRecents[0];
|
|
389
|
+
const nextTarget = resolve(nextRepo.path);
|
|
390
|
+
const canonicalMaster = getCanonicalChompMasterRoot();
|
|
391
|
+
const updated = {
|
|
392
|
+
activeRepoId: nextRepo.id,
|
|
393
|
+
activeRepoPath: nextTarget,
|
|
394
|
+
activeRepoName: nextRepo.name,
|
|
395
|
+
masterRoot: canonicalMaster,
|
|
396
|
+
updatedAt: new Date().toISOString(),
|
|
397
|
+
recentRepos: remainingRecents,
|
|
398
|
+
};
|
|
399
|
+
const payload = JSON.stringify(updated, null, 2) + "\n";
|
|
400
|
+
if (isChompMasterMonorepo(canonicalMaster)) {
|
|
401
|
+
try {
|
|
402
|
+
const fallbackPath = getWorkspaceFallbackContextPath();
|
|
403
|
+
mkdirSync(dirname(fallbackPath), { recursive: true });
|
|
404
|
+
writeFileSync(fallbackPath, payload, "utf8");
|
|
405
|
+
}
|
|
406
|
+
catch {
|
|
407
|
+
// Ignore read-only workspace
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
try {
|
|
411
|
+
const ctxPath = getGlobalContextPath();
|
|
412
|
+
mkdirSync(dirname(ctxPath), { recursive: true });
|
|
413
|
+
writeFileSync(ctxPath, payload, "utf8");
|
|
414
|
+
}
|
|
415
|
+
catch {
|
|
416
|
+
// Ignore sandbox errors
|
|
417
|
+
}
|
|
418
|
+
return { switchedTo: nextTarget, cleared: false };
|
|
419
|
+
}
|
|
420
|
+
const canonicalMaster = getCanonicalChompMasterRoot();
|
|
421
|
+
if (isChompMasterMonorepo(canonicalMaster) &&
|
|
422
|
+
resolve(canonicalMaster) !== repoRoot) {
|
|
423
|
+
setActiveChompTarget(canonicalMaster);
|
|
424
|
+
return { switchedTo: resolve(canonicalMaster), cleared: false };
|
|
425
|
+
}
|
|
426
|
+
try {
|
|
427
|
+
const ctxPath = getGlobalContextPath();
|
|
428
|
+
if (existsSync(ctxPath)) {
|
|
429
|
+
unlinkSync(ctxPath);
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
catch {
|
|
433
|
+
// Ignore sandbox errors
|
|
434
|
+
}
|
|
435
|
+
return { switchedTo: null, cleared: true };
|
|
436
|
+
}
|
|
437
|
+
/**
|
|
438
|
+
* Resolves the `.git` directory for a repository or git worktree and adds `.chomp/`
|
|
439
|
+
* (and optional extra patterns) to `.git/info/exclude` so Chomp never dirties `.gitignore`
|
|
440
|
+
* or `git status` in external repositories.
|
|
441
|
+
*/
|
|
442
|
+
export function ensureGitExcludeHasChomp(repoRoot, extraEntries = []) {
|
|
443
|
+
const dotGit = resolve(repoRoot, ".git");
|
|
444
|
+
if (!existsSync(dotGit))
|
|
445
|
+
return false;
|
|
446
|
+
let gitDir = dotGit;
|
|
447
|
+
try {
|
|
448
|
+
const stat = statSync(dotGit);
|
|
449
|
+
if (!stat.isDirectory()) {
|
|
450
|
+
const pointer = readFileSync(dotGit, "utf8").trim();
|
|
451
|
+
const match = /^gitdir:\s*(.+)$/i.exec(pointer);
|
|
452
|
+
if (match) {
|
|
453
|
+
gitDir = resolve(repoRoot, match[1].trim());
|
|
454
|
+
}
|
|
455
|
+
else {
|
|
456
|
+
return false;
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
catch {
|
|
461
|
+
return false;
|
|
462
|
+
}
|
|
463
|
+
const infoDir = resolve(gitDir, "info");
|
|
464
|
+
if (!existsSync(infoDir)) {
|
|
465
|
+
try {
|
|
466
|
+
mkdirSync(infoDir, { recursive: true });
|
|
467
|
+
}
|
|
468
|
+
catch {
|
|
469
|
+
return false;
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
// If .gitignore already ignores .chomp, no need to modify .git/info/exclude
|
|
473
|
+
const gitignorePath = resolve(repoRoot, ".gitignore");
|
|
474
|
+
if (extraEntries.length === 0 && existsSync(gitignorePath)) {
|
|
475
|
+
try {
|
|
476
|
+
const gitignoreLines = readFileSync(gitignorePath, "utf8")
|
|
477
|
+
.split(/\r?\n/)
|
|
478
|
+
.map((l) => l.trim());
|
|
479
|
+
if (gitignoreLines.includes(".chomp") ||
|
|
480
|
+
gitignoreLines.includes(".chomp/") ||
|
|
481
|
+
gitignoreLines.includes("/.chomp") ||
|
|
482
|
+
gitignoreLines.includes("/.chomp/")) {
|
|
483
|
+
return false;
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
catch {
|
|
487
|
+
// continue
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
const excludePath = resolve(infoDir, "exclude");
|
|
491
|
+
const raw = existsSync(excludePath) ? readFileSync(excludePath, "utf8") : "";
|
|
492
|
+
const lines = raw.split(/\r?\n/).map((l) => l.trim());
|
|
493
|
+
const required = [".chomp/", ".chomp", ...extraEntries];
|
|
494
|
+
const missing = required.filter((entry) => !lines.includes(entry));
|
|
495
|
+
if (missing.length === 0)
|
|
496
|
+
return false;
|
|
497
|
+
const prefix = raw.length > 0 && !raw.endsWith("\n") ? `${raw}\n` : raw;
|
|
498
|
+
const block = `\n# --- BEGIN CHOMP IGNORE ---\n${missing.join("\n")}\n# --- END CHOMP IGNORE ---\n`;
|
|
499
|
+
try {
|
|
500
|
+
writeFileSync(excludePath, prefix + block, "utf8");
|
|
501
|
+
return true;
|
|
502
|
+
}
|
|
503
|
+
catch {
|
|
504
|
+
return false;
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
/**
|
|
508
|
+
* Discovers all cloned repos under `<masterRoot>/fixtures/cloned-repos/*` plus `recentRepos`.
|
|
509
|
+
*/
|
|
510
|
+
export function listKnownTargetRepos() {
|
|
511
|
+
const masterRoot = getChompMasterRoot();
|
|
512
|
+
const ctx = readMasterContext();
|
|
513
|
+
const activePath = ctx?.activeRepoPath ? resolve(ctx.activeRepoPath) : resolve(masterRoot);
|
|
514
|
+
const byPath = new Map();
|
|
515
|
+
const addCandidate = (repoPath, source) => {
|
|
516
|
+
const abs = resolve(repoPath);
|
|
517
|
+
if (!existsSync(abs))
|
|
518
|
+
return;
|
|
519
|
+
const isAct = activePath === abs;
|
|
520
|
+
const existing = byPath.get(abs);
|
|
521
|
+
if (existing) {
|
|
522
|
+
if (isAct)
|
|
523
|
+
existing.isActive = true;
|
|
524
|
+
if (source === "master" || source === "cloned-fixture") {
|
|
525
|
+
existing.source = source;
|
|
526
|
+
}
|
|
527
|
+
return;
|
|
528
|
+
}
|
|
529
|
+
byPath.set(abs, {
|
|
530
|
+
name: resolveRepoName(abs),
|
|
531
|
+
path: abs,
|
|
532
|
+
source,
|
|
533
|
+
isActive: isAct,
|
|
534
|
+
hasGraphDb: existsSync(resolve(abs, ".chomp", "graph.db")),
|
|
535
|
+
});
|
|
536
|
+
};
|
|
537
|
+
if (isChompMasterMonorepo(masterRoot)) {
|
|
538
|
+
addCandidate(masterRoot, "master");
|
|
539
|
+
}
|
|
540
|
+
if (activePath && (isChompMasterMonorepo(masterRoot) || activePath !== resolve(masterRoot))) {
|
|
541
|
+
addCandidate(activePath, "recent");
|
|
542
|
+
}
|
|
543
|
+
for (const r of ctx?.recentRepos ?? []) {
|
|
544
|
+
if (r?.path)
|
|
545
|
+
addCandidate(r.path, "recent");
|
|
546
|
+
}
|
|
547
|
+
const clonedDir = resolve(masterRoot, "fixtures", "cloned-repos");
|
|
548
|
+
if (existsSync(clonedDir)) {
|
|
549
|
+
try {
|
|
550
|
+
for (const entry of readdirSync(clonedDir, { withFileTypes: true })) {
|
|
551
|
+
if (entry.isDirectory() && !entry.name.startsWith(".")) {
|
|
552
|
+
addCandidate(resolve(clonedDir, entry.name), "cloned-fixture");
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
catch {
|
|
557
|
+
// Ignore read errors
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
return Array.from(byPath.values());
|
|
561
|
+
}
|
|
562
|
+
/**
|
|
563
|
+
* Installs global `chomp` and `chomp-research` CLI shims into `~/.local/bin` (which is on PATH)
|
|
564
|
+
* so the user can run `chomp init`, `chomp connect`, `chomp analyze`, `chomp health`,
|
|
565
|
+
* and `chomp ui` from any directory on the machine without installing `@getchomp/cli` into target repos.
|
|
566
|
+
*
|
|
567
|
+
* Worktree safety: resolves linked Git worktrees back to the primary checkout so ephemeral
|
|
568
|
+
* worktrees never hijack `~/.local/bin/chomp` unless `CHOMP_ALLOW_WORKTREE_SHIM=1` (or
|
|
569
|
+
* `options.allowWorktree === true`) is explicitly passed.
|
|
570
|
+
*/
|
|
571
|
+
export function installGlobalChompShims(binDir = resolve(homedir(), ".local", "bin"), options = {}) {
|
|
572
|
+
const allowWorktree = Boolean(options.allowWorktree || process.env.CHOMP_ALLOW_WORKTREE_SHIM === "1");
|
|
573
|
+
const rawMasterRoot = getChompMasterRoot();
|
|
574
|
+
const masterRoot = allowWorktree
|
|
575
|
+
? rawMasterRoot
|
|
576
|
+
: getCanonicalChompMasterRoot(rawMasterRoot);
|
|
577
|
+
const chompShimPath = resolve(binDir, "chomp");
|
|
578
|
+
const researchShimPath = resolve(binDir, "chomp-research");
|
|
579
|
+
if (!allowWorktree && isLinkedGitWorktree(masterRoot) && existsSync(chompShimPath)) {
|
|
580
|
+
return {
|
|
581
|
+
chompShimPath,
|
|
582
|
+
researchShimPath,
|
|
583
|
+
chompBin: chompShimPath,
|
|
584
|
+
researchBin: researchShimPath,
|
|
585
|
+
skippedWorktreeOverwrite: true,
|
|
586
|
+
targetMasterRoot: masterRoot,
|
|
587
|
+
};
|
|
588
|
+
}
|
|
589
|
+
// Published NPM installs (`npm i -g @getchomp/cli` or `npx @getchomp/cli`) have no live monorepo
|
|
590
|
+
// `packages/cli/src/index.ts`; never overwrite `~/.local/bin/chomp` with a broken tsx shim.
|
|
591
|
+
if (!isChompMasterMonorepo(masterRoot)) {
|
|
592
|
+
return {
|
|
593
|
+
chompShimPath,
|
|
594
|
+
researchShimPath,
|
|
595
|
+
chompBin: resolveGlobalChompBinary() ?? "chomp",
|
|
596
|
+
researchBin: researchShimPath,
|
|
597
|
+
targetMasterRoot: masterRoot,
|
|
598
|
+
};
|
|
599
|
+
}
|
|
600
|
+
const tsxBin = [
|
|
601
|
+
resolve(masterRoot, "packages/db/node_modules/.bin/tsx"),
|
|
602
|
+
resolve(masterRoot, "packages/cli/node_modules/.bin/tsx"),
|
|
603
|
+
resolve(masterRoot, "node_modules/.bin/tsx"),
|
|
604
|
+
].find((p) => existsSync(p)) ?? "npx tsx";
|
|
605
|
+
const cliEntry = resolve(masterRoot, "packages/cli/src/index.ts");
|
|
606
|
+
const researchEntry = resolve(masterRoot, "fixtures/research/research-cli/src/index.ts");
|
|
607
|
+
try {
|
|
608
|
+
if (!existsSync(binDir)) {
|
|
609
|
+
mkdirSync(binDir, { recursive: true });
|
|
610
|
+
}
|
|
611
|
+
const chompScript = [
|
|
612
|
+
"#!/usr/bin/env bash",
|
|
613
|
+
"# Auto-generated Chomp Master CLI shim — runs live TypeScript source from master repo",
|
|
614
|
+
'export INIT_CWD="${INIT_CWD:-$(pwd)}"',
|
|
615
|
+
`export CHOMP_MASTER_ROOT="${masterRoot}"`,
|
|
616
|
+
`exec "${tsxBin}" "${cliEntry}" "$@"`,
|
|
617
|
+
"",
|
|
618
|
+
].join("\n");
|
|
619
|
+
writeFileSync(chompShimPath, chompScript, "utf8");
|
|
620
|
+
chmodSync(chompShimPath, 0o755);
|
|
621
|
+
const researchScript = [
|
|
622
|
+
"#!/usr/bin/env bash",
|
|
623
|
+
"# Auto-generated Chomp Research CLI shim — runs live TypeScript source from master repo",
|
|
624
|
+
'export INIT_CWD="${INIT_CWD:-$(pwd)}"',
|
|
625
|
+
`export CHOMP_MASTER_ROOT="${masterRoot}"`,
|
|
626
|
+
`exec "${tsxBin}" "${researchEntry}" "$@"`,
|
|
627
|
+
"",
|
|
628
|
+
].join("\n");
|
|
629
|
+
writeFileSync(researchShimPath, researchScript, "utf8");
|
|
630
|
+
chmodSync(researchShimPath, 0o755);
|
|
631
|
+
}
|
|
632
|
+
catch {
|
|
633
|
+
// Ignore EPERM when running inside a restricted agent sandbox
|
|
634
|
+
}
|
|
635
|
+
return {
|
|
636
|
+
chompShimPath,
|
|
637
|
+
researchShimPath,
|
|
638
|
+
chompBin: chompShimPath,
|
|
639
|
+
researchBin: researchShimPath,
|
|
640
|
+
targetMasterRoot: masterRoot,
|
|
641
|
+
};
|
|
642
|
+
}
|