cloudaxe-opencode/packages/opencode/src/cli/cmd/debug/ripgrep.ts

95 lines
2.6 KiB
TypeScript
Raw Normal View History

import { EOL } from "os"
import { AppRuntime } from "../../../effect/app-runtime"
2025-07-01 16:06:38 +00:00
import { Ripgrep } from "../../../file/ripgrep"
import { Instance } from "../../../project/instance"
2025-07-01 16:06:38 +00:00
import { bootstrap } from "../../bootstrap"
import { cmd } from "../cmd"
2025-07-01 16:06:38 +00:00
export const RipgrepCommand = cmd({
2025-07-01 02:46:42 +00:00
command: "rg",
describe: "ripgrep debugging utilities",
2025-11-08 01:59:02 +00:00
builder: (yargs) => yargs.command(TreeCommand).command(FilesCommand).command(SearchCommand).demandCommand(),
2025-07-01 02:46:42 +00:00
async handler() {},
})
const TreeCommand = cmd({
command: "tree",
describe: "show file tree using ripgrep",
2025-07-01 02:46:42 +00:00
builder: (yargs) =>
yargs.option("limit", {
type: "number",
}),
async handler(args) {
await bootstrap(process.cwd(), async () => {
2025-11-08 01:59:02 +00:00
process.stdout.write((await Ripgrep.tree({ cwd: Instance.directory, limit: args.limit })) + EOL)
2025-07-01 02:46:42 +00:00
})
},
})
2025-06-30 20:45:13 +00:00
const FilesCommand = cmd({
command: "files",
describe: "list files using ripgrep",
2025-06-30 20:45:13 +00:00
builder: (yargs) =>
yargs
.option("query", {
type: "string",
description: "Filter files by query",
})
.option("glob", {
type: "string",
description: "Glob pattern to match files",
})
.option("limit", {
type: "number",
description: "Limit number of results",
}),
async handler(args) {
await bootstrap(process.cwd(), async () => {
const files: string[] = []
for await (const file of await Ripgrep.files({
cwd: Instance.directory,
2025-07-08 22:14:24 +00:00
glob: args.glob ? [args.glob] : undefined,
})) {
files.push(file)
if (args.limit && files.length >= args.limit) break
}
process.stdout.write(files.join(EOL) + EOL)
2025-06-30 20:45:13 +00:00
})
},
})
2025-07-01 02:46:42 +00:00
const SearchCommand = cmd({
command: "search <pattern>",
describe: "search file contents using ripgrep",
2025-07-01 02:46:42 +00:00
builder: (yargs) =>
yargs
.positional("pattern", {
type: "string",
demandOption: true,
description: "Search pattern",
})
.option("glob", {
type: "array",
description: "File glob patterns",
})
.option("limit", {
type: "number",
description: "Limit number of results",
}),
async handler(args) {
await bootstrap(process.cwd(), async () => {
const results = await AppRuntime.runPromise(
Ripgrep.Service.use((svc) =>
svc.search({
cwd: Instance.directory,
pattern: args.pattern,
glob: args.glob as string[] | undefined,
limit: args.limit,
}),
),
)
process.stdout.write(JSON.stringify(results.items, null, 2) + EOL)
2025-07-01 02:46:42 +00:00
})
},
})