@carrieros/cli 0.2.0 → 0.3.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 +142 -57
- package/dist/commands/attach.js +45 -0
- package/dist/commands/btw.js +78 -0
- package/dist/commands/changes.js +167 -0
- package/dist/commands/chat.js +80 -14
- package/dist/commands/context.js +160 -0
- package/dist/commands/doctor.js +46 -1
- package/dist/commands/files.js +84 -23
- package/dist/commands/help.js +1 -0
- package/dist/commands/history.js +88 -23
- package/dist/commands/login.js +31 -7
- package/dist/commands/memory.js +162 -0
- package/dist/commands/mode-model.js +39 -3
- package/dist/commands/modes.js +4 -1
- package/dist/commands/project.js +106 -0
- package/dist/commands/repo.js +146 -0
- package/dist/commands/repos.js +61 -0
- package/dist/commands/review.js +92 -0
- package/dist/commands/search.js +92 -0
- package/dist/commands/sessions.js +219 -65
- package/dist/commands/settings.js +87 -0
- package/dist/commands/update.js +159 -0
- package/dist/commands/upload.js +204 -31
- package/dist/commands/usage.js +212 -16
- package/dist/ide/editor-context.js +172 -0
- package/dist/ide/ide-session.js +94 -0
- package/dist/ide/tool-host.js +109 -0
- package/dist/main.js +26 -1
- package/dist/services/api.js +140 -6
- package/dist/services/chat-stream.js +145 -0
- package/dist/services/checkpoints.js +116 -0
- package/dist/services/client-tools.js +260 -0
- package/dist/services/command-coordinator.js +200 -0
- package/dist/services/command-policy.js +404 -0
- package/dist/services/config.js +247 -22
- package/dist/services/context.js +265 -20
- package/dist/services/device-auth.js +42 -30
- package/dist/services/diff.js +196 -0
- package/dist/services/env-policy.js +108 -0
- package/dist/services/file-detect.js +104 -0
- package/dist/services/file-resolver.js +199 -0
- package/dist/services/file-upload.js +126 -0
- package/dist/services/git-tools.js +248 -0
- package/dist/services/glob-match.js +164 -0
- package/dist/services/helper-bin.js +64 -0
- package/dist/services/ignore-rules.js +189 -0
- package/dist/services/job-helper.js +307 -0
- package/dist/services/local-files.js +517 -0
- package/dist/services/local-tools.js +432 -0
- package/dist/services/login-flow.js +15 -8
- package/dist/services/meta.js +3 -1
- package/dist/services/mutation-coordinator.js +161 -0
- package/dist/services/mutations.js +493 -0
- package/dist/services/open-url.js +68 -0
- package/dist/services/output.js +5 -2
- package/dist/services/packaged.js +24 -0
- package/dist/services/process-runner.js +406 -0
- package/dist/services/project-context.js +298 -0
- package/dist/services/redaction.js +201 -0
- package/dist/services/secure-store.js +197 -0
- package/dist/services/session-resolver.js +108 -0
- package/dist/services/sia.js +51 -0
- package/dist/services/update.js +286 -0
- package/dist/services/workspace-registry.js +309 -0
- package/dist/services/workspace.js +573 -0
- package/dist/shell/shell-chat.js +1028 -57
- package/dist/shell/shell-commands.js +200 -11
- package/dist/shell/shell-prompt.js +81 -8
- package/dist/shell/shell-renderer.js +7 -19
- package/dist/shell/shell-router.js +19 -3
- package/dist/shell/shell.js +185 -17
- package/dist/types/index.js +3 -1
- package/dist/ui/approval.js +81 -0
- package/dist/ui/banner.js +39 -0
- package/dist/ui/btw-overlay.js +230 -0
- package/dist/ui/change-review.js +87 -0
- package/dist/ui/chat-renderer.js +206 -13
- package/dist/ui/colors.js +4 -0
- package/dist/ui/command-filter.js +26 -5
- package/dist/ui/command-palette.js +17 -4
- package/dist/ui/command-registry.js +84 -9
- package/dist/ui/command-review.js +138 -0
- package/dist/ui/composer-layout.js +41 -0
- package/dist/ui/conversation.js +398 -0
- package/dist/ui/interrupt.js +35 -0
- package/dist/ui/live.js +3 -1
- package/dist/ui/markdown.js +63 -9
- package/dist/ui/message.js +14 -31
- package/dist/ui/screen-prompt.js +181 -0
- package/dist/ui/screen.js +1 -1
- package/dist/ui/select.js +185 -29
- package/dist/ui/sia-qa.js +213 -0
- package/dist/ui/text-prompt.js +74 -0
- package/dist/ui/trust.js +93 -0
- package/dist/ui/ui.js +4 -1
- package/package.json +13 -2
- package/dist/ui/activity.js +0 -77
|
@@ -0,0 +1,517 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { compileGlob, toPosix } from "./glob-match.js";
|
|
4
|
+
import { IgnoreMatcher, isLowSignalFile } from "./ignore-rules.js";
|
|
5
|
+
import { activeRepository } from "./workspace-registry.js";
|
|
6
|
+
import { getWorkspace } from "./workspace.js";
|
|
7
|
+
/**
|
|
8
|
+
* Reading the machine the CLI is standing on.
|
|
9
|
+
*
|
|
10
|
+
* This is the thick half of "thick client for the filesystem, thin client for
|
|
11
|
+
* reasoning": listing, reading, globbing and searching all happen here, and
|
|
12
|
+
* none of it goes near a model. Nothing in this module uploads, and nothing
|
|
13
|
+
* writes — every operation is read-only, by construction rather than by
|
|
14
|
+
* convention, because the same containment boundary will be load-bearing when
|
|
15
|
+
* writes eventually arrive.
|
|
16
|
+
*
|
|
17
|
+
* The shapes are deliberately structured rather than string-in/string-out
|
|
18
|
+
* (`readFile({ path })`, not `readFile(path)`): a later phase turns these into
|
|
19
|
+
* the executors behind backend tool calls, and the argument objects are already
|
|
20
|
+
* the tool payloads.
|
|
21
|
+
*
|
|
22
|
+
* Relative paths are POSIX-separated everywhere they leave this module, which
|
|
23
|
+
* matches the backend's `codebase_map` and means a local tree and an uploaded
|
|
24
|
+
* ZIP of the same project describe files the same way.
|
|
25
|
+
*/
|
|
26
|
+
// ── Budgets ──────────────────────────────────────────────────────────────────
|
|
27
|
+
// A monorepo will happily hand you a million files. Everything here is bounded
|
|
28
|
+
// so a mistyped search cannot pin the terminal, and every limit reports when it
|
|
29
|
+
// bites rather than silently truncating.
|
|
30
|
+
/** Directory entries examined in one traversal. */
|
|
31
|
+
const MAX_VISITED = 20_000;
|
|
32
|
+
/** Read into memory for display. Beyond this the read truncates and says so. */
|
|
33
|
+
const MAX_READ_BYTES = 1024 * 1024;
|
|
34
|
+
/** Files larger than this are skipped by search — cost without much signal. */
|
|
35
|
+
const MAX_SEARCH_FILE_BYTES = 2 * 1024 * 1024;
|
|
36
|
+
/** Bytes sampled to decide whether a file is text. */
|
|
37
|
+
const SNIFF_BYTES = 8192;
|
|
38
|
+
/** Default depth for the tree view. */
|
|
39
|
+
const DEFAULT_TREE_DEPTH = 3;
|
|
40
|
+
export class LocalFileError extends Error {
|
|
41
|
+
code;
|
|
42
|
+
constructor(code, message) {
|
|
43
|
+
super(message);
|
|
44
|
+
this.code = code;
|
|
45
|
+
this.name = "LocalFileError";
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
// ── Root and containment ─────────────────────────────────────────────────────
|
|
49
|
+
/**
|
|
50
|
+
* The root every local operation is relative to.
|
|
51
|
+
*
|
|
52
|
+
* The repository root when there is one, so `/files` from `src/components`
|
|
53
|
+
* describes the project rather than the corner of it you happen to be standing
|
|
54
|
+
* in. Otherwise the working directory — Git is not a requirement for reading
|
|
55
|
+
* your own files.
|
|
56
|
+
*/
|
|
57
|
+
export function workspaceRoot() {
|
|
58
|
+
// One repository is always the root of everything this module does. In a
|
|
59
|
+
// multi-repository workspace that is the ACTIVE repository — never the
|
|
60
|
+
// folder holding them, which would widen containment to every sibling.
|
|
61
|
+
try {
|
|
62
|
+
return activeRepository().root;
|
|
63
|
+
}
|
|
64
|
+
catch {
|
|
65
|
+
const ws = getWorkspace();
|
|
66
|
+
return ws.repository.root ?? ws.cwd;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
/** Windows compares paths case-insensitively; matches the rule in ui/trust.ts. */
|
|
70
|
+
function normalizeForCompare(p) {
|
|
71
|
+
const resolved = path.resolve(p);
|
|
72
|
+
return process.platform === "win32" ? resolved.toLowerCase() : resolved;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Is `candidate` the root itself, or beneath it?
|
|
76
|
+
*
|
|
77
|
+
* The separator check is the part that matters: a plain `startsWith` would
|
|
78
|
+
* accept `D:\project-secrets` as inside `D:\project`.
|
|
79
|
+
*/
|
|
80
|
+
export function isInside(root, candidate) {
|
|
81
|
+
const r = normalizeForCompare(root);
|
|
82
|
+
const c = normalizeForCompare(candidate);
|
|
83
|
+
return c === r || c.startsWith(r.endsWith(path.sep) ? r : r + path.sep);
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Resolve a user-supplied path against the workspace root and refuse anything
|
|
87
|
+
* that leaves it.
|
|
88
|
+
*
|
|
89
|
+
* Checked twice on purpose. The first check catches `..` and absolute paths.
|
|
90
|
+
* The second re-checks after `realpathSync`, which is what catches a symlink or
|
|
91
|
+
* a Windows junction whose own path looks innocent but whose target is not —
|
|
92
|
+
* the check that would otherwise be a hole the moment someone clones a
|
|
93
|
+
* repository containing a link to `C:\Users`.
|
|
94
|
+
*/
|
|
95
|
+
export function resolveInside(input, root = workspaceRoot()) {
|
|
96
|
+
const abs = path.resolve(root, input);
|
|
97
|
+
if (!isInside(root, abs)) {
|
|
98
|
+
throw new LocalFileError("OUTSIDE_WORKSPACE", `Outside the workspace: ${input}`);
|
|
99
|
+
}
|
|
100
|
+
try {
|
|
101
|
+
const real = fs.realpathSync.native(abs);
|
|
102
|
+
if (!isInside(root, real)) {
|
|
103
|
+
throw new LocalFileError("OUTSIDE_WORKSPACE", `${input} links outside the workspace.`);
|
|
104
|
+
}
|
|
105
|
+
return real;
|
|
106
|
+
}
|
|
107
|
+
catch (err) {
|
|
108
|
+
if (err instanceof LocalFileError)
|
|
109
|
+
throw err;
|
|
110
|
+
// realpath fails on a path that does not exist yet; the lexical check above
|
|
111
|
+
// already established it would be inside, so let the caller's stat report it.
|
|
112
|
+
return abs;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Walk the workspace, honouring the ignore policy.
|
|
117
|
+
*
|
|
118
|
+
* Recursive rather than an explicit stack because the .gitignore rules nest the
|
|
119
|
+
* same way the directories do: a rule set is pushed on the way into a directory
|
|
120
|
+
* and truncated on the way out, so a file is judged by every .gitignore at or
|
|
121
|
+
* above it. Depth is bounded, so the recursion is too.
|
|
122
|
+
*
|
|
123
|
+
* Symlinked directories are never descended into. On Windows that also covers
|
|
124
|
+
* junctions and reparse points, which Node reports as symbolic links — the
|
|
125
|
+
* usual way a naive walker ends up looping forever or wandering onto C:.
|
|
126
|
+
*/
|
|
127
|
+
export function walkRepository(opts = {}) {
|
|
128
|
+
const root = opts.root ?? workspaceRoot();
|
|
129
|
+
const maxDepth = opts.maxDepth ?? Infinity;
|
|
130
|
+
const maxEntries = opts.maxEntries ?? MAX_VISITED;
|
|
131
|
+
const entries = [];
|
|
132
|
+
const ignore = new IgnoreMatcher();
|
|
133
|
+
const state = { visited: 0, skipped: 0, truncated: false, stop: false };
|
|
134
|
+
const startRel = opts.start ? toPosix(opts.start) : "";
|
|
135
|
+
const startAbs = startRel ? resolveInside(startRel, root) : root;
|
|
136
|
+
const descend = (absDir, relDir, depth) => {
|
|
137
|
+
if (state.stop)
|
|
138
|
+
return;
|
|
139
|
+
if (opts.signal?.aborted) {
|
|
140
|
+
state.stop = true;
|
|
141
|
+
state.truncated = true;
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
let dirents;
|
|
145
|
+
try {
|
|
146
|
+
dirents = fs.readdirSync(absDir, { withFileTypes: true });
|
|
147
|
+
}
|
|
148
|
+
catch {
|
|
149
|
+
state.skipped += 1;
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
// A .gitignore here governs this directory and everything under it. Free to
|
|
153
|
+
// notice — the listing already named it.
|
|
154
|
+
const restore = ignore.depth;
|
|
155
|
+
if (dirents.some((d) => d.isFile() && d.name === ".gitignore")) {
|
|
156
|
+
try {
|
|
157
|
+
ignore.push(fs.readFileSync(path.join(absDir, ".gitignore"), "utf8"), relDir);
|
|
158
|
+
}
|
|
159
|
+
catch {
|
|
160
|
+
/* unreadable .gitignore — carry on with the hard excludes */
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
// Directories first, then files, each alphabetical: a stable, readable tree.
|
|
164
|
+
dirents.sort((a, b) => {
|
|
165
|
+
const ad = a.isDirectory() ? 0 : 1;
|
|
166
|
+
const bd = b.isDirectory() ? 0 : 1;
|
|
167
|
+
return ad !== bd ? ad - bd : a.name.localeCompare(b.name);
|
|
168
|
+
});
|
|
169
|
+
for (const d of dirents) {
|
|
170
|
+
if (state.stop)
|
|
171
|
+
break;
|
|
172
|
+
if (opts.signal?.aborted) {
|
|
173
|
+
state.stop = true;
|
|
174
|
+
state.truncated = true;
|
|
175
|
+
break;
|
|
176
|
+
}
|
|
177
|
+
state.visited += 1;
|
|
178
|
+
if (state.visited > maxEntries) {
|
|
179
|
+
state.truncated = true;
|
|
180
|
+
state.stop = true;
|
|
181
|
+
break;
|
|
182
|
+
}
|
|
183
|
+
const rel = relDir ? `${relDir}/${d.name}` : d.name;
|
|
184
|
+
const abs = path.join(absDir, d.name);
|
|
185
|
+
const isDir = d.isDirectory();
|
|
186
|
+
if (ignore.shouldIgnore(rel, { isDirectory: isDir }))
|
|
187
|
+
continue;
|
|
188
|
+
// Never follow a link out of the tree, and never risk a cycle.
|
|
189
|
+
if (d.isSymbolicLink())
|
|
190
|
+
continue;
|
|
191
|
+
if (isDir) {
|
|
192
|
+
if (opts.includeDirectories !== false) {
|
|
193
|
+
const entry = { path: rel, size: 0, type: "dir", depth };
|
|
194
|
+
entries.push(entry);
|
|
195
|
+
if (opts.onEntry?.(entry) === false) {
|
|
196
|
+
state.stop = true;
|
|
197
|
+
break;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
if (depth < maxDepth)
|
|
201
|
+
descend(abs, rel, depth + 1);
|
|
202
|
+
continue;
|
|
203
|
+
}
|
|
204
|
+
if (!d.isFile())
|
|
205
|
+
continue; // sockets, FIFOs, devices
|
|
206
|
+
let size = 0;
|
|
207
|
+
let modified;
|
|
208
|
+
try {
|
|
209
|
+
const st = fs.statSync(abs);
|
|
210
|
+
size = st.size;
|
|
211
|
+
modified = st.mtimeMs;
|
|
212
|
+
}
|
|
213
|
+
catch {
|
|
214
|
+
state.skipped += 1;
|
|
215
|
+
continue;
|
|
216
|
+
}
|
|
217
|
+
const entry = { path: rel, size, type: "file", depth, modified };
|
|
218
|
+
entries.push(entry);
|
|
219
|
+
if (opts.onEntry?.(entry) === false) {
|
|
220
|
+
state.stop = true;
|
|
221
|
+
break;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
ignore.truncate(restore);
|
|
225
|
+
};
|
|
226
|
+
descend(startAbs, startRel, 1);
|
|
227
|
+
return { entries, truncated: state.truncated, visited: state.visited, skipped: state.skipped };
|
|
228
|
+
}
|
|
229
|
+
/** A bounded tree of the workspace, shaped for display. */
|
|
230
|
+
export function listDirectory(opts = {}) {
|
|
231
|
+
const limit = opts.limit ?? 200;
|
|
232
|
+
const collected = [];
|
|
233
|
+
const res = walkRepository({
|
|
234
|
+
root: opts.root,
|
|
235
|
+
start: opts.path,
|
|
236
|
+
maxDepth: opts.depth ?? DEFAULT_TREE_DEPTH,
|
|
237
|
+
includeDirectories: true,
|
|
238
|
+
signal: opts.signal,
|
|
239
|
+
onEntry: (e) => {
|
|
240
|
+
collected.push(e);
|
|
241
|
+
return collected.length < limit;
|
|
242
|
+
}
|
|
243
|
+
});
|
|
244
|
+
return {
|
|
245
|
+
...res,
|
|
246
|
+
entries: collected,
|
|
247
|
+
truncated: res.truncated || collected.length >= limit
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
// ── Metadata ─────────────────────────────────────────────────────────────────
|
|
251
|
+
const CATEGORY = {
|
|
252
|
+
".ts": "TypeScript",
|
|
253
|
+
".tsx": "TypeScript (React)",
|
|
254
|
+
".js": "JavaScript",
|
|
255
|
+
".jsx": "JavaScript (React)",
|
|
256
|
+
".mjs": "JavaScript",
|
|
257
|
+
".cjs": "JavaScript",
|
|
258
|
+
".json": "JSON",
|
|
259
|
+
".md": "Markdown",
|
|
260
|
+
".py": "Python",
|
|
261
|
+
".rs": "Rust",
|
|
262
|
+
".go": "Go",
|
|
263
|
+
".java": "Java",
|
|
264
|
+
".rb": "Ruby",
|
|
265
|
+
".php": "PHP",
|
|
266
|
+
".cs": "C#",
|
|
267
|
+
".c": "C",
|
|
268
|
+
".h": "C header",
|
|
269
|
+
".cpp": "C++",
|
|
270
|
+
".css": "CSS",
|
|
271
|
+
".scss": "Sass",
|
|
272
|
+
".html": "HTML",
|
|
273
|
+
".yml": "YAML",
|
|
274
|
+
".yaml": "YAML",
|
|
275
|
+
".toml": "TOML",
|
|
276
|
+
".sql": "SQL",
|
|
277
|
+
".sh": "Shell",
|
|
278
|
+
".ps1": "PowerShell",
|
|
279
|
+
".txt": "Text",
|
|
280
|
+
".png": "Image",
|
|
281
|
+
".jpg": "Image",
|
|
282
|
+
".jpeg": "Image",
|
|
283
|
+
".gif": "Image",
|
|
284
|
+
".svg": "Image",
|
|
285
|
+
".ico": "Image",
|
|
286
|
+
".pdf": "PDF",
|
|
287
|
+
".zip": "Archive",
|
|
288
|
+
".exe": "Executable",
|
|
289
|
+
".dll": "Library"
|
|
290
|
+
};
|
|
291
|
+
export function categorize(name) {
|
|
292
|
+
const ext = path.extname(name).toLowerCase();
|
|
293
|
+
return CATEGORY[ext] ?? (ext ? ext.slice(1).toUpperCase() : "Unknown");
|
|
294
|
+
}
|
|
295
|
+
export function getFileMetadata(input) {
|
|
296
|
+
const root = input.root ?? workspaceRoot();
|
|
297
|
+
const abs = resolveInside(input.path, root);
|
|
298
|
+
let st;
|
|
299
|
+
try {
|
|
300
|
+
st = fs.statSync(abs);
|
|
301
|
+
}
|
|
302
|
+
catch {
|
|
303
|
+
throw new LocalFileError("NOT_FOUND", `No such file: ${input.path}`);
|
|
304
|
+
}
|
|
305
|
+
const rel = toPosix(path.relative(root, abs)) || toPosix(path.basename(abs));
|
|
306
|
+
return {
|
|
307
|
+
path: rel,
|
|
308
|
+
size: st.isDirectory() ? 0 : st.size,
|
|
309
|
+
type: st.isDirectory() ? "dir" : "file",
|
|
310
|
+
depth: rel.split("/").length,
|
|
311
|
+
modified: st.mtimeMs,
|
|
312
|
+
extension: path.extname(abs).toLowerCase(),
|
|
313
|
+
category: st.isDirectory() ? "Directory" : categorize(abs)
|
|
314
|
+
};
|
|
315
|
+
}
|
|
316
|
+
// ── Reading ──────────────────────────────────────────────────────────────────
|
|
317
|
+
/**
|
|
318
|
+
* Does this look like binary?
|
|
319
|
+
*
|
|
320
|
+
* A NUL byte in the first few KB, which is the same heuristic Git uses. Cheap,
|
|
321
|
+
* and wrong only for exotic encodings the CLI would not render usefully anyway.
|
|
322
|
+
*/
|
|
323
|
+
function looksBinary(buf) {
|
|
324
|
+
const end = Math.min(buf.length, SNIFF_BYTES);
|
|
325
|
+
for (let i = 0; i < end; i += 1)
|
|
326
|
+
if (buf[i] === 0)
|
|
327
|
+
return true;
|
|
328
|
+
return false;
|
|
329
|
+
}
|
|
330
|
+
/**
|
|
331
|
+
* Read a text file from inside the workspace.
|
|
332
|
+
*
|
|
333
|
+
* Size is checked with `stat` BEFORE opening, so an enormous file is never
|
|
334
|
+
* pulled into memory just to discover it was enormous. Beyond the cap the read
|
|
335
|
+
* takes the first slice and says it did — silently returning a partial file
|
|
336
|
+
* would be worse than refusing.
|
|
337
|
+
*/
|
|
338
|
+
export function readFile(input) {
|
|
339
|
+
const root = input.root ?? workspaceRoot();
|
|
340
|
+
const abs = resolveInside(input.path, root);
|
|
341
|
+
const cap = input.maxBytes ?? MAX_READ_BYTES;
|
|
342
|
+
let st;
|
|
343
|
+
try {
|
|
344
|
+
st = fs.statSync(abs);
|
|
345
|
+
}
|
|
346
|
+
catch {
|
|
347
|
+
throw new LocalFileError("NOT_FOUND", `No such file: ${input.path}`);
|
|
348
|
+
}
|
|
349
|
+
if (st.isDirectory()) {
|
|
350
|
+
throw new LocalFileError("NOT_A_FILE", `${input.path} is a directory.`);
|
|
351
|
+
}
|
|
352
|
+
const wanted = Math.min(st.size, cap);
|
|
353
|
+
let buf;
|
|
354
|
+
try {
|
|
355
|
+
const fd = fs.openSync(abs, "r");
|
|
356
|
+
try {
|
|
357
|
+
buf = Buffer.alloc(wanted);
|
|
358
|
+
fs.readSync(fd, buf, 0, wanted, 0);
|
|
359
|
+
}
|
|
360
|
+
finally {
|
|
361
|
+
fs.closeSync(fd);
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
catch {
|
|
365
|
+
throw new LocalFileError("UNREADABLE", `Could not read ${input.path}.`);
|
|
366
|
+
}
|
|
367
|
+
if (looksBinary(buf)) {
|
|
368
|
+
throw new LocalFileError("BINARY", `${input.path} is not a text file.`);
|
|
369
|
+
}
|
|
370
|
+
return {
|
|
371
|
+
path: toPosix(path.relative(root, abs)),
|
|
372
|
+
text: buf.toString("utf8"),
|
|
373
|
+
size: st.size,
|
|
374
|
+
truncated: st.size > wanted
|
|
375
|
+
};
|
|
376
|
+
}
|
|
377
|
+
// ── Glob ─────────────────────────────────────────────────────────────────────
|
|
378
|
+
/**
|
|
379
|
+
* Would a walk of the workspace skip this path?
|
|
380
|
+
*
|
|
381
|
+
* The walker answers that implicitly by never descending; a direct read has no
|
|
382
|
+
* walk, so it asks here instead. Same matcher, same rules: every .gitignore from
|
|
383
|
+
* the root down to the file's own directory, plus the hard excludes. Without
|
|
384
|
+
* this a read addressed straight at `node_modules/…` or a gitignored `.env`
|
|
385
|
+
* would succeed even though no listing or search would ever have surfaced it.
|
|
386
|
+
*/
|
|
387
|
+
export function isPathIgnored(relPath, root = workspaceRoot()) {
|
|
388
|
+
const parts = toPosix(relPath).split("/").filter((p) => p && p !== ".");
|
|
389
|
+
const ignore = new IgnoreMatcher();
|
|
390
|
+
const pushGitignore = (absDir, relDir) => {
|
|
391
|
+
try {
|
|
392
|
+
ignore.push(fs.readFileSync(path.join(absDir, ".gitignore"), "utf8"), relDir);
|
|
393
|
+
}
|
|
394
|
+
catch {
|
|
395
|
+
/* no .gitignore at this level */
|
|
396
|
+
}
|
|
397
|
+
};
|
|
398
|
+
pushGitignore(root, "");
|
|
399
|
+
let absDir = root;
|
|
400
|
+
for (let i = 0; i < parts.length; i += 1) {
|
|
401
|
+
const rel = parts.slice(0, i + 1).join("/");
|
|
402
|
+
const isLast = i === parts.length - 1;
|
|
403
|
+
if (ignore.shouldIgnore(rel, { isDirectory: !isLast }))
|
|
404
|
+
return true;
|
|
405
|
+
if (!isLast) {
|
|
406
|
+
absDir = path.join(absDir, parts[i]);
|
|
407
|
+
pushGitignore(absDir, rel);
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
return false;
|
|
411
|
+
}
|
|
412
|
+
/**
|
|
413
|
+
* Files matching a glob, rooted at the workspace.
|
|
414
|
+
*
|
|
415
|
+
* The pattern is matched against the relative path, so it cannot address
|
|
416
|
+
* anything outside the root no matter what it contains — there is no path
|
|
417
|
+
* resolution here for `..` to act on.
|
|
418
|
+
*/
|
|
419
|
+
export function globFiles(opts) {
|
|
420
|
+
const re = compileGlob(toPosix(opts.pattern));
|
|
421
|
+
const limit = opts.limit ?? 500;
|
|
422
|
+
const paths = [];
|
|
423
|
+
const res = walkRepository({
|
|
424
|
+
root: opts.root,
|
|
425
|
+
includeDirectories: false,
|
|
426
|
+
signal: opts.signal,
|
|
427
|
+
onEntry: (e) => {
|
|
428
|
+
if (e.type === "file" && re.test(e.path))
|
|
429
|
+
paths.push(e.path);
|
|
430
|
+
return paths.length < limit;
|
|
431
|
+
}
|
|
432
|
+
});
|
|
433
|
+
return { paths, truncated: res.truncated || paths.length >= limit };
|
|
434
|
+
}
|
|
435
|
+
/**
|
|
436
|
+
* Plain text search across the workspace.
|
|
437
|
+
*
|
|
438
|
+
* Literal substring matching, not a regular expression and emphatically not
|
|
439
|
+
* semantic search — a query is what it says it is. Binary files are sniffed and
|
|
440
|
+
* skipped rather than scanned as text, large files are skipped by size, and
|
|
441
|
+
* low-signal files (lockfiles, minified assets, images) never make it here at
|
|
442
|
+
* all because the ignore vocabulary already removed them.
|
|
443
|
+
*/
|
|
444
|
+
export function searchFiles(opts) {
|
|
445
|
+
const limit = opts.limit ?? 50;
|
|
446
|
+
const perFile = opts.maxPerFile ?? 3;
|
|
447
|
+
const needle = opts.caseSensitive ? opts.query : opts.query.toLowerCase();
|
|
448
|
+
const globRe = opts.glob ? compileGlob(toPosix(opts.glob)) : null;
|
|
449
|
+
const matches = [];
|
|
450
|
+
let filesScanned = 0;
|
|
451
|
+
// Hoisted: this is per-search, not per-file.
|
|
452
|
+
const root = opts.root ?? workspaceRoot();
|
|
453
|
+
const res = walkRepository({
|
|
454
|
+
// The walk MUST cover the same repository the reads below resolve against —
|
|
455
|
+
// hoisted `root` and an unrooted walk would list one repository's files and
|
|
456
|
+
// read another's.
|
|
457
|
+
root,
|
|
458
|
+
includeDirectories: false,
|
|
459
|
+
signal: opts.signal,
|
|
460
|
+
onEntry: (e) => {
|
|
461
|
+
if (e.type !== "file")
|
|
462
|
+
return true;
|
|
463
|
+
if (globRe && !globRe.test(e.path))
|
|
464
|
+
return true;
|
|
465
|
+
if (isLowSignalFile(e.path.slice(e.path.lastIndexOf("/") + 1)))
|
|
466
|
+
return true;
|
|
467
|
+
if (e.size > MAX_SEARCH_FILE_BYTES)
|
|
468
|
+
return true;
|
|
469
|
+
let buf;
|
|
470
|
+
try {
|
|
471
|
+
buf = fs.readFileSync(path.join(root, e.path));
|
|
472
|
+
}
|
|
473
|
+
catch {
|
|
474
|
+
return true; // unreadable — skip, never fail the whole search
|
|
475
|
+
}
|
|
476
|
+
if (looksBinary(buf))
|
|
477
|
+
return true;
|
|
478
|
+
filesScanned += 1;
|
|
479
|
+
const text = buf.toString("utf8");
|
|
480
|
+
// Reject the whole file before splitting it. Most files do not contain
|
|
481
|
+
// the term at all, and lowercasing them line by line to discover that
|
|
482
|
+
// was the entire cost of a search: 2.5s on a 1000-file repository, spent
|
|
483
|
+
// almost entirely on allocating strings nobody ever looked at.
|
|
484
|
+
const whole = opts.caseSensitive ? text : text.toLowerCase();
|
|
485
|
+
if (!whole.includes(needle))
|
|
486
|
+
return true;
|
|
487
|
+
const lines = text.split(/\r?\n/);
|
|
488
|
+
let hits = 0;
|
|
489
|
+
for (let i = 0; i < lines.length; i += 1) {
|
|
490
|
+
const hay = opts.caseSensitive ? lines[i] : lines[i].toLowerCase();
|
|
491
|
+
if (!hay.includes(needle))
|
|
492
|
+
continue;
|
|
493
|
+
matches.push({ path: e.path, line: i + 1, text: lines[i].trim() });
|
|
494
|
+
hits += 1;
|
|
495
|
+
if (matches.length >= limit)
|
|
496
|
+
return false;
|
|
497
|
+
if (hits >= perFile)
|
|
498
|
+
break;
|
|
499
|
+
}
|
|
500
|
+
return true;
|
|
501
|
+
}
|
|
502
|
+
});
|
|
503
|
+
return {
|
|
504
|
+
matches,
|
|
505
|
+
filesScanned,
|
|
506
|
+
truncated: res.truncated || matches.length >= limit,
|
|
507
|
+
aborted: Boolean(opts.signal?.aborted)
|
|
508
|
+
};
|
|
509
|
+
}
|
|
510
|
+
/** "14.2 KB" — sizes read in the terminal, not in bytes. */
|
|
511
|
+
export function formatSize(bytes) {
|
|
512
|
+
if (bytes < 1024)
|
|
513
|
+
return `${bytes} B`;
|
|
514
|
+
if (bytes < 1024 * 1024)
|
|
515
|
+
return `${(bytes / 1024).toFixed(1)} KB`;
|
|
516
|
+
return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
|
|
517
|
+
}
|