| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- import { EOL } from "os"
- import { Ripgrep } from "../../../file/ripgrep"
- import { Instance } from "../../../project/instance"
- import { bootstrap } from "../../bootstrap"
- import { cmd } from "../cmd"
- export const RipgrepCommand = cmd({
- command: "rg",
- builder: (yargs) => yargs.command(TreeCommand).command(FilesCommand).command(SearchCommand).demandCommand(),
- async handler() {},
- })
- const TreeCommand = cmd({
- command: "tree",
- builder: (yargs) =>
- yargs.option("limit", {
- type: "number",
- }),
- async handler(args) {
- await bootstrap(process.cwd(), async () => {
- process.stdout.write(await Ripgrep.tree({ cwd: Instance.directory, limit: args.limit }) + EOL)
- })
- },
- })
- const FilesCommand = cmd({
- command: "files",
- 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 Ripgrep.files({
- cwd: Instance.directory,
- glob: args.glob ? [args.glob] : undefined,
- })) {
- files.push(file)
- if (args.limit && files.length >= args.limit) break
- }
- process.stdout.write(files.join(EOL) + EOL)
- })
- },
- })
- const SearchCommand = cmd({
- command: "search <pattern>",
- 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) {
- const results = await Ripgrep.search({
- cwd: process.cwd(),
- pattern: args.pattern,
- glob: args.glob as string[] | undefined,
- limit: args.limit,
- })
- process.stdout.write(JSON.stringify(results, null, 2) + EOL)
- },
- })
|