@getchomp/cli 0.0.0-stage → 0.2.42
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 +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 +654 -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 +250 -0
- package/dist/commands/uninstall.d.ts +8 -0
- package/dist/commands/uninstall.js +238 -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 +976 -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 +102 -0
- package/dist/utils/masterContext.js +559 -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 +51 -3
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, readFileSync, statSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { dirname, resolve } from "node:path";
|
|
3
|
+
import { getRegisteredPluginDefaultLayers } from "@getchomp/mcp";
|
|
4
|
+
import { DEFAULT_BUILT_IN_RULES, matchesCustomRule, } from "@getchomp/core";
|
|
5
|
+
export { matchesCustomRule, };
|
|
6
|
+
export function getDefaultOverlayLayers() {
|
|
7
|
+
return {
|
|
8
|
+
L1_PORTS: true,
|
|
9
|
+
...getRegisteredPluginDefaultLayers(),
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
export const DEFAULT_OVERLAY_LAYERS = getDefaultOverlayLayers();
|
|
13
|
+
export const DEFAULT_RULES_CONFIG = {
|
|
14
|
+
version: 1,
|
|
15
|
+
defaults: { ...DEFAULT_BUILT_IN_RULES },
|
|
16
|
+
customRules: [],
|
|
17
|
+
overlayLayers: { ...DEFAULT_OVERLAY_LAYERS },
|
|
18
|
+
};
|
|
19
|
+
export function normalizeRulesConfig(raw) {
|
|
20
|
+
const defaults = {
|
|
21
|
+
foldMiddleware: typeof raw?.defaults?.foldMiddleware === "boolean"
|
|
22
|
+
? raw.defaults.foldMiddleware
|
|
23
|
+
: DEFAULT_RULES_CONFIG.defaults.foldMiddleware,
|
|
24
|
+
foldComponents: typeof raw?.defaults?.foldComponents === "boolean"
|
|
25
|
+
? raw.defaults.foldComponents
|
|
26
|
+
: DEFAULT_RULES_CONFIG.defaults.foldComponents,
|
|
27
|
+
foldMicroNodes: typeof raw?.defaults?.foldMicroNodes === "boolean"
|
|
28
|
+
? raw.defaults.foldMicroNodes
|
|
29
|
+
: DEFAULT_RULES_CONFIG.defaults.foldMicroNodes,
|
|
30
|
+
};
|
|
31
|
+
const overlayLayers = getDefaultOverlayLayers();
|
|
32
|
+
if (raw?.overlayLayers && typeof raw.overlayLayers === "object") {
|
|
33
|
+
for (const [k, v] of Object.entries(raw.overlayLayers)) {
|
|
34
|
+
if (typeof v === "boolean") {
|
|
35
|
+
overlayLayers[k] = v;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
const customRules = Array.isArray(raw?.customRules)
|
|
40
|
+
? raw.customRules
|
|
41
|
+
.filter((r) => r && typeof r === "object" && typeof r.id === "string")
|
|
42
|
+
.map((r) => {
|
|
43
|
+
const action = r.action === "component_badge" ||
|
|
44
|
+
r.action === "micro_node" ||
|
|
45
|
+
r.action === "station_node"
|
|
46
|
+
? r.action
|
|
47
|
+
: "middleware_badge";
|
|
48
|
+
const badgeMode = r.badgeMode === "badge_on_node" ? "badge_on_node" : "fold_into_callers";
|
|
49
|
+
const match = {};
|
|
50
|
+
if (typeof r.match?.nodeId === "string" && r.match.nodeId.trim()) {
|
|
51
|
+
match.nodeId = r.match.nodeId.trim();
|
|
52
|
+
}
|
|
53
|
+
if (typeof r.match?.namePattern === "string" && r.match.namePattern.trim()) {
|
|
54
|
+
match.namePattern = r.match.namePattern.trim();
|
|
55
|
+
}
|
|
56
|
+
if (typeof r.match?.entityType === "string" && r.match.entityType.trim()) {
|
|
57
|
+
match.entityType = r.match.entityType.trim();
|
|
58
|
+
}
|
|
59
|
+
if (typeof r.match?.patternId === "string" && r.match.patternId.trim()) {
|
|
60
|
+
match.patternId = r.match.patternId.trim();
|
|
61
|
+
}
|
|
62
|
+
if (typeof r.match?.packageLabel === "string" && r.match.packageLabel.trim()) {
|
|
63
|
+
match.packageLabel = r.match.packageLabel.trim();
|
|
64
|
+
}
|
|
65
|
+
if (typeof r.match?.filePathPattern === "string" && r.match.filePathPattern.trim()) {
|
|
66
|
+
match.filePathPattern = r.match.filePathPattern.trim();
|
|
67
|
+
}
|
|
68
|
+
if (typeof r.match?.overlayLayer === "string" && r.match.overlayLayer.trim()) {
|
|
69
|
+
match.overlayLayer = r.match.overlayLayer.trim();
|
|
70
|
+
}
|
|
71
|
+
if (typeof r.match?.overlaySource === "string" && r.match.overlaySource.trim()) {
|
|
72
|
+
match.overlaySource = r.match.overlaySource.trim();
|
|
73
|
+
}
|
|
74
|
+
if (typeof r.match?.overlayField === "string" && r.match.overlayField.trim()) {
|
|
75
|
+
match.overlayField = r.match.overlayField.trim();
|
|
76
|
+
}
|
|
77
|
+
if (typeof r.match?.overlayValue === "string" ||
|
|
78
|
+
typeof r.match?.overlayValue === "number" ||
|
|
79
|
+
typeof r.match?.overlayValue === "boolean") {
|
|
80
|
+
match.overlayValue = r.match.overlayValue;
|
|
81
|
+
}
|
|
82
|
+
return {
|
|
83
|
+
id: r.id.trim(),
|
|
84
|
+
name: typeof r.name === "string" && r.name.trim() ? r.name.trim() : r.id.trim(),
|
|
85
|
+
...(typeof r.description === "string" && r.description.trim()
|
|
86
|
+
? { description: r.description.trim() }
|
|
87
|
+
: {}),
|
|
88
|
+
enabled: typeof r.enabled === "boolean" ? r.enabled : true,
|
|
89
|
+
match,
|
|
90
|
+
action,
|
|
91
|
+
...(typeof r.badgeLabel === "string" && r.badgeLabel.trim()
|
|
92
|
+
? { badgeLabel: r.badgeLabel.trim() }
|
|
93
|
+
: {}),
|
|
94
|
+
badgeMode,
|
|
95
|
+
createdBy: r.createdBy === "agent" ? "agent" : "user",
|
|
96
|
+
...(typeof r.createdAt === "string" ? { createdAt: r.createdAt } : {}),
|
|
97
|
+
};
|
|
98
|
+
})
|
|
99
|
+
: [];
|
|
100
|
+
return {
|
|
101
|
+
version: 1,
|
|
102
|
+
defaults,
|
|
103
|
+
customRules,
|
|
104
|
+
overlayLayers,
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
export function resolveRulesFilePath(chompDirOrDbPath) {
|
|
108
|
+
const dir = chompDirOrDbPath.endsWith(".db")
|
|
109
|
+
? dirname(chompDirOrDbPath)
|
|
110
|
+
: chompDirOrDbPath;
|
|
111
|
+
return resolve(dir, "rules.json");
|
|
112
|
+
}
|
|
113
|
+
function resolveRepoGitDirs(repoRoot) {
|
|
114
|
+
const dirs = [];
|
|
115
|
+
const dotGit = resolve(repoRoot, ".git");
|
|
116
|
+
if (!existsSync(dotGit))
|
|
117
|
+
return dirs;
|
|
118
|
+
try {
|
|
119
|
+
const st = statSync(dotGit);
|
|
120
|
+
let gitDir = dotGit;
|
|
121
|
+
if (st.isFile()) {
|
|
122
|
+
const ptr = readFileSync(dotGit, "utf-8").trim();
|
|
123
|
+
if (ptr.startsWith("gitdir:")) {
|
|
124
|
+
gitDir = resolve(repoRoot, ptr.slice("gitdir:".length).trim());
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
dirs.push(gitDir);
|
|
128
|
+
const commonDirFile = resolve(gitDir, "commondir");
|
|
129
|
+
if (existsSync(commonDirFile)) {
|
|
130
|
+
const rel = readFileSync(commonDirFile, "utf-8").trim();
|
|
131
|
+
if (rel) {
|
|
132
|
+
const commonDir = resolve(gitDir, rel);
|
|
133
|
+
if (!dirs.includes(commonDir))
|
|
134
|
+
dirs.push(commonDir);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
catch {
|
|
139
|
+
// Ignore git pointer resolution errors
|
|
140
|
+
}
|
|
141
|
+
return dirs;
|
|
142
|
+
}
|
|
143
|
+
export function detectSystemRules(chompDirOrDbPath) {
|
|
144
|
+
const chompDir = chompDirOrDbPath.endsWith(".db")
|
|
145
|
+
? dirname(chompDirOrDbPath)
|
|
146
|
+
: chompDirOrDbPath;
|
|
147
|
+
const repoRoot = dirname(chompDir);
|
|
148
|
+
const hookNames = ["post-commit", "post-merge", "post-checkout", "pre-commit"];
|
|
149
|
+
const candidateHookDirs = [
|
|
150
|
+
resolve(repoRoot, ".husky"),
|
|
151
|
+
...resolveRepoGitDirs(repoRoot).map((d) => resolve(d, "hooks")),
|
|
152
|
+
];
|
|
153
|
+
let gitSyncLinked = false;
|
|
154
|
+
try {
|
|
155
|
+
for (const hookDir of candidateHookDirs) {
|
|
156
|
+
for (const hookName of hookNames) {
|
|
157
|
+
const candidatePath = resolve(hookDir, hookName);
|
|
158
|
+
if (existsSync(candidatePath)) {
|
|
159
|
+
const content = readFileSync(candidatePath, "utf-8");
|
|
160
|
+
if (content.includes("chomp analyze")) {
|
|
161
|
+
gitSyncLinked = true;
|
|
162
|
+
break;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
if (gitSyncLinked)
|
|
167
|
+
break;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
catch {
|
|
171
|
+
gitSyncLinked = false;
|
|
172
|
+
}
|
|
173
|
+
const dbExists = existsSync(resolve(chompDir, "graph.db"));
|
|
174
|
+
const configPath = resolve(repoRoot, "chomp.config.json");
|
|
175
|
+
let configExists = false;
|
|
176
|
+
let configSummary = "Optional workspace exclusions, custom conventions, and plugin configuration.";
|
|
177
|
+
try {
|
|
178
|
+
if (existsSync(configPath)) {
|
|
179
|
+
configExists = true;
|
|
180
|
+
const parsed = JSON.parse(readFileSync(configPath, "utf-8"));
|
|
181
|
+
if (parsed && typeof parsed === "object") {
|
|
182
|
+
const activeKeys = ["excludeWorkspaces", "conventions", "shims", "designSystem"].filter((k) => parsed[k] !== undefined);
|
|
183
|
+
if (activeKeys.length > 0) {
|
|
184
|
+
configSummary = `Configured sections: ${activeKeys.join(", ")}.`;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
catch {
|
|
190
|
+
// Keep default summary on malformed JSON
|
|
191
|
+
}
|
|
192
|
+
return [
|
|
193
|
+
{
|
|
194
|
+
id: "graph-db-baseline",
|
|
195
|
+
name: "Local graph baseline",
|
|
196
|
+
description: "Stores the deterministic AST graph in .chomp/graph.db for instant layout and working-tree diffs.",
|
|
197
|
+
command: "chomp analyze .",
|
|
198
|
+
enabled: dbExists,
|
|
199
|
+
},
|
|
200
|
+
{
|
|
201
|
+
id: "git-sync-hooks",
|
|
202
|
+
name: "Git sync hooks",
|
|
203
|
+
description: "Runs chomp analyze automatically after commits, merges, and branch switches.",
|
|
204
|
+
command: "chomp analyze . --quiet",
|
|
205
|
+
enabled: gitSyncLinked,
|
|
206
|
+
},
|
|
207
|
+
{
|
|
208
|
+
id: "project-config",
|
|
209
|
+
name: "Project configuration",
|
|
210
|
+
description: configSummary,
|
|
211
|
+
command: "chomp.config.json",
|
|
212
|
+
enabled: configExists,
|
|
213
|
+
},
|
|
214
|
+
];
|
|
215
|
+
}
|
|
216
|
+
export function loadRulesConfig(chompDirOrDbPath) {
|
|
217
|
+
const filePath = resolveRulesFilePath(chompDirOrDbPath);
|
|
218
|
+
const systemRules = detectSystemRules(chompDirOrDbPath);
|
|
219
|
+
if (!existsSync(filePath)) {
|
|
220
|
+
return {
|
|
221
|
+
version: 1,
|
|
222
|
+
defaults: { ...DEFAULT_RULES_CONFIG.defaults },
|
|
223
|
+
customRules: [],
|
|
224
|
+
overlayLayers: getDefaultOverlayLayers(),
|
|
225
|
+
systemRules,
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
try {
|
|
229
|
+
const raw = JSON.parse(readFileSync(filePath, "utf-8"));
|
|
230
|
+
return {
|
|
231
|
+
...normalizeRulesConfig(raw),
|
|
232
|
+
systemRules,
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
catch {
|
|
236
|
+
return {
|
|
237
|
+
version: 1,
|
|
238
|
+
defaults: { ...DEFAULT_RULES_CONFIG.defaults },
|
|
239
|
+
customRules: [],
|
|
240
|
+
overlayLayers: getDefaultOverlayLayers(),
|
|
241
|
+
systemRules,
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
export function saveRulesConfig(chompDirOrDbPath, config) {
|
|
246
|
+
const filePath = resolveRulesFilePath(chompDirOrDbPath);
|
|
247
|
+
const normalized = normalizeRulesConfig(config);
|
|
248
|
+
mkdirSync(dirname(filePath), { recursive: true });
|
|
249
|
+
writeFileSync(filePath, JSON.stringify(normalized, null, 2), "utf-8");
|
|
250
|
+
return {
|
|
251
|
+
...normalized,
|
|
252
|
+
systemRules: detectSystemRules(chompDirOrDbPath),
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
export function getRulesFingerprint(chompDirOrDbPath) {
|
|
256
|
+
const filePath = resolveRulesFilePath(chompDirOrDbPath);
|
|
257
|
+
if (!existsSync(filePath))
|
|
258
|
+
return "rules:default";
|
|
259
|
+
try {
|
|
260
|
+
const st = statSync(filePath);
|
|
261
|
+
return `rules:${Math.floor(st.mtimeMs)}:${st.size}`;
|
|
262
|
+
}
|
|
263
|
+
catch {
|
|
264
|
+
return "rules:default";
|
|
265
|
+
}
|
|
266
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
export type EditorScheme = "cursor" | "windsurf" | "kiro" | "vscode";
|
|
2
|
+
/**
|
|
3
|
+
* Safely resolves a repo-relative file path strictly within `repoRoot` using `realpathSync`
|
|
4
|
+
* containment so path traversal (`..`) and symlink escapes outside the repo are rejected.
|
|
5
|
+
*/
|
|
6
|
+
export declare function resolveSafeRepoFile(repoRoot: string, relFilePath: string): string | null;
|
|
7
|
+
/**
|
|
8
|
+
* Reads a single 1-based source line from a validated repository file for localhost-only
|
|
9
|
+
* `UNKNOWN` / `LIKELY` call-shape tooltips. Never exposes full files or binary data.
|
|
10
|
+
*/
|
|
11
|
+
export declare function readSingleSourceLine(repoRoot: string, relFilePath: string, lineNumber: number): {
|
|
12
|
+
filePath: string;
|
|
13
|
+
lineNumber: number;
|
|
14
|
+
expression: string;
|
|
15
|
+
} | null;
|
|
16
|
+
/**
|
|
17
|
+
* Detects the active local IDE scheme from environment variables and workspace markers.
|
|
18
|
+
*/
|
|
19
|
+
export declare function detectPreferredEditorScheme(repoRoot: string, env?: NodeJS.ProcessEnv): EditorScheme;
|
|
20
|
+
/**
|
|
21
|
+
* Builds an IDE `file` deep-link URI (`cursor://file/...`, `vscode://file/...`, etc.).
|
|
22
|
+
*/
|
|
23
|
+
export declare function buildEditorUri(absFilePath: string, lineNumber?: number, scheme?: EditorScheme): string;
|
|
24
|
+
/**
|
|
25
|
+
* Builds a remote GitHub / GitLab blob URL (`#L<line>`) when `remoteUrl` is available.
|
|
26
|
+
*/
|
|
27
|
+
export declare function buildRemoteFileUrl(remoteUrl: string | undefined, commitSha: string | undefined, relFilePath: string, lineNumber?: number): string | null;
|
|
28
|
+
/**
|
|
29
|
+
* Opens a validated repository file in the developer's local IDE via safe `execFile`
|
|
30
|
+
* (`-g file:line`), returning both `editorUri` and `remoteFileUrl` fallbacks.
|
|
31
|
+
*/
|
|
32
|
+
export declare function openInLocalEditor(params: {
|
|
33
|
+
repoRoot: string;
|
|
34
|
+
relFilePath: string;
|
|
35
|
+
lineNumber?: number;
|
|
36
|
+
remoteUrl?: string;
|
|
37
|
+
commitSha?: string;
|
|
38
|
+
env?: NodeJS.ProcessEnv;
|
|
39
|
+
}): Promise<{
|
|
40
|
+
ok: boolean;
|
|
41
|
+
openedLocally: boolean;
|
|
42
|
+
editorScheme?: EditorScheme;
|
|
43
|
+
editorUri?: string;
|
|
44
|
+
remoteFileUrl?: string;
|
|
45
|
+
}>;
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
import { execFile } from "node:child_process";
|
|
2
|
+
import { existsSync, readFileSync, realpathSync, statSync } from "node:fs";
|
|
3
|
+
import { resolve, sep } from "node:path";
|
|
4
|
+
const MAX_SOURCE_FILE_BYTES = 2 * 1024 * 1024;
|
|
5
|
+
const MAX_EXPRESSION_LENGTH = 160;
|
|
6
|
+
/**
|
|
7
|
+
* Safely resolves a repo-relative file path strictly within `repoRoot` using `realpathSync`
|
|
8
|
+
* containment so path traversal (`..`) and symlink escapes outside the repo are rejected.
|
|
9
|
+
*/
|
|
10
|
+
export function resolveSafeRepoFile(repoRoot, relFilePath) {
|
|
11
|
+
if (!repoRoot || !relFilePath || typeof relFilePath !== "string")
|
|
12
|
+
return null;
|
|
13
|
+
if (relFilePath.includes("\0"))
|
|
14
|
+
return null;
|
|
15
|
+
const trimmed = relFilePath.trim().replace(/^file:\/\//i, "");
|
|
16
|
+
if (!trimmed)
|
|
17
|
+
return null;
|
|
18
|
+
try {
|
|
19
|
+
const realRoot = realpathSync(resolve(repoRoot));
|
|
20
|
+
const candidate = resolve(realRoot, trimmed);
|
|
21
|
+
if (!existsSync(candidate))
|
|
22
|
+
return null;
|
|
23
|
+
const realFile = realpathSync(candidate);
|
|
24
|
+
if (realFile !== realRoot && !realFile.startsWith(realRoot + sep)) {
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
27
|
+
const st = statSync(realFile);
|
|
28
|
+
if (!st.isFile())
|
|
29
|
+
return null;
|
|
30
|
+
return realFile;
|
|
31
|
+
}
|
|
32
|
+
catch {
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Reads a single 1-based source line from a validated repository file for localhost-only
|
|
38
|
+
* `UNKNOWN` / `LIKELY` call-shape tooltips. Never exposes full files or binary data.
|
|
39
|
+
*/
|
|
40
|
+
export function readSingleSourceLine(repoRoot, relFilePath, lineNumber) {
|
|
41
|
+
if (!Number.isInteger(lineNumber) || lineNumber < 1)
|
|
42
|
+
return null;
|
|
43
|
+
const realFile = resolveSafeRepoFile(repoRoot, relFilePath);
|
|
44
|
+
if (!realFile)
|
|
45
|
+
return null;
|
|
46
|
+
try {
|
|
47
|
+
const st = statSync(realFile);
|
|
48
|
+
if (st.size > MAX_SOURCE_FILE_BYTES)
|
|
49
|
+
return null;
|
|
50
|
+
const raw = readFileSync(realFile, "utf-8");
|
|
51
|
+
if (raw.includes("\0"))
|
|
52
|
+
return null;
|
|
53
|
+
const lines = raw.split(/\r?\n/);
|
|
54
|
+
if (lineNumber > lines.length)
|
|
55
|
+
return null;
|
|
56
|
+
const rawLine = (lines[lineNumber - 1] ?? "").replace(/\t/g, " ").trim();
|
|
57
|
+
if (!rawLine)
|
|
58
|
+
return null;
|
|
59
|
+
const expression = rawLine.length > MAX_EXPRESSION_LENGTH
|
|
60
|
+
? `${rawLine.slice(0, MAX_EXPRESSION_LENGTH - 1)}…`
|
|
61
|
+
: rawLine;
|
|
62
|
+
return {
|
|
63
|
+
filePath: relFilePath.trim(),
|
|
64
|
+
lineNumber,
|
|
65
|
+
expression,
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
catch {
|
|
69
|
+
return null;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Detects the active local IDE scheme from environment variables and workspace markers.
|
|
74
|
+
*/
|
|
75
|
+
export function detectPreferredEditorScheme(repoRoot, env = process.env) {
|
|
76
|
+
const explicit = (env.CHOMP_IDE ?? "").trim().toLowerCase();
|
|
77
|
+
if (explicit === "cursor" ||
|
|
78
|
+
explicit === "windsurf" ||
|
|
79
|
+
explicit === "kiro" ||
|
|
80
|
+
explicit === "vscode") {
|
|
81
|
+
return explicit;
|
|
82
|
+
}
|
|
83
|
+
const termProgram = (env.TERM_PROGRAM ?? "").toLowerCase();
|
|
84
|
+
if (env.CURSOR_TRACE_ID || termProgram.includes("cursor"))
|
|
85
|
+
return "cursor";
|
|
86
|
+
if (termProgram.includes("windsurf"))
|
|
87
|
+
return "windsurf";
|
|
88
|
+
if (termProgram.includes("kiro"))
|
|
89
|
+
return "kiro";
|
|
90
|
+
if (env.VSCODE_IPC_HOOK_CLI || termProgram.includes("vscode"))
|
|
91
|
+
return "vscode";
|
|
92
|
+
try {
|
|
93
|
+
const absRoot = resolve(repoRoot);
|
|
94
|
+
if (existsSync(resolve(absRoot, ".cursor")))
|
|
95
|
+
return "cursor";
|
|
96
|
+
if (existsSync(resolve(absRoot, ".windsurf")))
|
|
97
|
+
return "windsurf";
|
|
98
|
+
if (existsSync(resolve(absRoot, ".kiro")))
|
|
99
|
+
return "kiro";
|
|
100
|
+
if (existsSync(resolve(absRoot, ".vscode")))
|
|
101
|
+
return "vscode";
|
|
102
|
+
}
|
|
103
|
+
catch {
|
|
104
|
+
// Ignore fs errors
|
|
105
|
+
}
|
|
106
|
+
return "vscode";
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Builds an IDE `file` deep-link URI (`cursor://file/...`, `vscode://file/...`, etc.).
|
|
110
|
+
*/
|
|
111
|
+
export function buildEditorUri(absFilePath, lineNumber, scheme = "vscode") {
|
|
112
|
+
const posixAbs = absFilePath.replace(/\\/g, "/");
|
|
113
|
+
const withLeadingSlash = posixAbs.startsWith("/") ? posixAbs : `/${posixAbs}`;
|
|
114
|
+
const encodedPath = withLeadingSlash
|
|
115
|
+
.split("/")
|
|
116
|
+
.map((seg) => encodeURIComponent(seg))
|
|
117
|
+
.join("/");
|
|
118
|
+
const lineSuffix = typeof lineNumber === "number" && Number.isInteger(lineNumber) && lineNumber >= 1
|
|
119
|
+
? `:${lineNumber}:1`
|
|
120
|
+
: "";
|
|
121
|
+
return `${scheme}://file${encodedPath}${lineSuffix}`;
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Builds a remote GitHub / GitLab blob URL (`#L<line>`) when `remoteUrl` is available.
|
|
125
|
+
*/
|
|
126
|
+
export function buildRemoteFileUrl(remoteUrl, commitSha, relFilePath, lineNumber) {
|
|
127
|
+
if (!remoteUrl || !relFilePath)
|
|
128
|
+
return null;
|
|
129
|
+
const base = remoteUrl.trim().replace(/\/+$/, "");
|
|
130
|
+
if (!/^https?:\/\//i.test(base))
|
|
131
|
+
return null;
|
|
132
|
+
const cleanRel = relFilePath
|
|
133
|
+
.trim()
|
|
134
|
+
.replace(/\\/g, "/")
|
|
135
|
+
.replace(/^\/+/, "");
|
|
136
|
+
if (!cleanRel || cleanRel.includes(".."))
|
|
137
|
+
return null;
|
|
138
|
+
const ref = (commitSha ?? "").trim() || "HEAD";
|
|
139
|
+
const encodedRel = cleanRel
|
|
140
|
+
.split("/")
|
|
141
|
+
.map((seg) => encodeURIComponent(seg))
|
|
142
|
+
.join("/");
|
|
143
|
+
const isGitLab = /gitlab/i.test(base);
|
|
144
|
+
const blobPrefix = isGitLab ? "/-/blob" : "/blob";
|
|
145
|
+
const lineAnchor = typeof lineNumber === "number" && Number.isInteger(lineNumber) && lineNumber >= 1
|
|
146
|
+
? `#L${lineNumber}`
|
|
147
|
+
: "";
|
|
148
|
+
return `${base}${blobPrefix}/${encodeURIComponent(ref)}/${encodedRel}${lineAnchor}`;
|
|
149
|
+
}
|
|
150
|
+
function tryExecEditorCli(bin, targetArg) {
|
|
151
|
+
return new Promise((resolveDone) => {
|
|
152
|
+
try {
|
|
153
|
+
const child = execFile(bin, ["-g", targetArg], { timeout: 2500, windowsHide: true }, (err) => {
|
|
154
|
+
resolveDone(!err);
|
|
155
|
+
});
|
|
156
|
+
child.on("error", () => resolveDone(false));
|
|
157
|
+
}
|
|
158
|
+
catch {
|
|
159
|
+
resolveDone(false);
|
|
160
|
+
}
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Opens a validated repository file in the developer's local IDE via safe `execFile`
|
|
165
|
+
* (`-g file:line`), returning both `editorUri` and `remoteFileUrl` fallbacks.
|
|
166
|
+
*/
|
|
167
|
+
export async function openInLocalEditor(params) {
|
|
168
|
+
const { repoRoot, relFilePath, lineNumber, remoteUrl, commitSha, env } = params;
|
|
169
|
+
const remoteFileUrl = buildRemoteFileUrl(remoteUrl, commitSha, relFilePath, lineNumber) ?? undefined;
|
|
170
|
+
const realFile = resolveSafeRepoFile(repoRoot, relFilePath);
|
|
171
|
+
if (!realFile) {
|
|
172
|
+
return {
|
|
173
|
+
ok: false,
|
|
174
|
+
openedLocally: false,
|
|
175
|
+
...(remoteFileUrl ? { remoteFileUrl } : {}),
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
const scheme = detectPreferredEditorScheme(repoRoot, env);
|
|
179
|
+
const editorUri = buildEditorUri(realFile, lineNumber, scheme);
|
|
180
|
+
const lineArg = typeof lineNumber === "number" && Number.isInteger(lineNumber) && lineNumber >= 1
|
|
181
|
+
? `${realFile}:${lineNumber}`
|
|
182
|
+
: realFile;
|
|
183
|
+
const cliCandidates = scheme === "cursor"
|
|
184
|
+
? ["cursor", "code"]
|
|
185
|
+
: scheme === "windsurf"
|
|
186
|
+
? ["windsurf", "code"]
|
|
187
|
+
: scheme === "kiro"
|
|
188
|
+
? ["kiro", "code"]
|
|
189
|
+
: ["code", "cursor"];
|
|
190
|
+
let openedLocally = false;
|
|
191
|
+
for (const bin of cliCandidates) {
|
|
192
|
+
if (await tryExecEditorCli(bin, lineArg)) {
|
|
193
|
+
openedLocally = true;
|
|
194
|
+
break;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
return {
|
|
198
|
+
ok: true,
|
|
199
|
+
openedLocally,
|
|
200
|
+
editorScheme: scheme,
|
|
201
|
+
editorUri,
|
|
202
|
+
...(remoteFileUrl ? { remoteFileUrl } : {}),
|
|
203
|
+
};
|
|
204
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,54 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@getchomp/cli",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.2.42",
|
|
4
|
+
"description": "Chomp CLI and local server — map your codebase, check architecture health, and launch Chomp Map.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"homepage": "https://getchomp.dev",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"engines": {
|
|
9
|
+
"node": ">=18.18.0"
|
|
10
|
+
},
|
|
11
|
+
"main": "./dist/index.js",
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"default": "./dist/index.js"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"bin": {
|
|
20
|
+
"chomp": "./dist/index.js"
|
|
21
|
+
},
|
|
22
|
+
"publishConfig": {
|
|
23
|
+
"access": "public"
|
|
24
|
+
},
|
|
25
|
+
"files": [
|
|
26
|
+
"dist",
|
|
27
|
+
"README.md"
|
|
28
|
+
],
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"@modelcontextprotocol/sdk": "^1.30.0",
|
|
31
|
+
"better-sqlite3": "^11.0.0",
|
|
32
|
+
"commander": "^12.1.0",
|
|
33
|
+
"cors": "^2.8.6",
|
|
34
|
+
"dotenv": "^16.4.5",
|
|
35
|
+
"express": "^5.2.1",
|
|
36
|
+
"typescript": "^5.3.0",
|
|
37
|
+
"zod": "^3.23.8",
|
|
38
|
+
"@getchomp/core": "0.2.42",
|
|
39
|
+
"@getchomp/db": "0.2.42",
|
|
40
|
+
"@getchomp/mcp": "0.2.42"
|
|
41
|
+
},
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"@types/better-sqlite3": "^7.6.11",
|
|
44
|
+
"@types/cors": "^2.8.19",
|
|
45
|
+
"@types/express": "^5.0.6",
|
|
46
|
+
"tsx": "^4.19.0"
|
|
47
|
+
},
|
|
48
|
+
"scripts": {
|
|
49
|
+
"build": "tsc -p tsconfig.build.json",
|
|
50
|
+
"dev": "tsx src/index.ts",
|
|
51
|
+
"typecheck": "tsc --noEmit",
|
|
52
|
+
"test": "tsx --test src/utils/masterContext.test.ts src/visualizer/layoutCache.test.ts src/visualizer/rulesConfig.test.ts src/visualizer/pluginPresentationApi.test.ts src/visualizer/cloudLayoutClient.test.ts src/visualizer/sourceAccess.test.ts src/ports/portHydrator.test.ts src/ports/portHydratorWorker.test.ts src/visualizer/visualLinter.test.ts"
|
|
53
|
+
}
|
|
6
54
|
}
|