forked from anomalyco/opencode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile.ts
More file actions
97 lines (91 loc) · 2.52 KB
/
file.ts
File metadata and controls
97 lines (91 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import { EOL } from "os"
import { File } from "../../../file"
import { bootstrap } from "../../bootstrap"
import { cmd } from "../cmd"
import { Ripgrep } from "@/file/ripgrep"
const FileSearchCommand = cmd({
command: "search <query>",
describe: "search files by query",
builder: (yargs) =>
yargs.positional("query", {
type: "string",
demandOption: true,
description: "Search query",
}),
async handler(args) {
await bootstrap(process.cwd(), async () => {
const results = await File.search({ query: args.query })
process.stdout.write(results.join(EOL) + EOL)
})
},
})
const FileReadCommand = cmd({
command: "read <path>",
describe: "read file contents as JSON",
builder: (yargs) =>
yargs.positional("path", {
type: "string",
demandOption: true,
description: "File path to read",
}),
async handler(args) {
await bootstrap(process.cwd(), async () => {
const content = await File.read(args.path)
process.stdout.write(JSON.stringify(content, null, 2) + EOL)
})
},
})
const FileStatusCommand = cmd({
command: "status",
describe: "show file status information",
builder: (yargs) => yargs,
async handler() {
await bootstrap(process.cwd(), async () => {
const status = await File.status()
process.stdout.write(JSON.stringify(status, null, 2) + EOL)
})
},
})
const FileListCommand = cmd({
command: "list <path>",
describe: "list files in a directory",
builder: (yargs) =>
yargs.positional("path", {
type: "string",
demandOption: true,
description: "File path to list",
}),
async handler(args) {
await bootstrap(process.cwd(), async () => {
const files = await File.list(args.path)
process.stdout.write(JSON.stringify(files, null, 2) + EOL)
})
},
})
const FileTreeCommand = cmd({
command: "tree [dir]",
describe: "show directory tree",
builder: (yargs) =>
yargs.positional("dir", {
type: "string",
description: "Directory to tree",
default: process.cwd(),
}),
async handler(args) {
const files = await Ripgrep.tree({ cwd: args.dir, limit: 200 })
console.log(JSON.stringify(files, null, 2))
},
})
export const FileCommand = cmd({
command: "file",
describe: "file system debugging utilities",
builder: (yargs) =>
yargs
.command(FileReadCommand)
.command(FileStatusCommand)
.command(FileListCommand)
.command(FileSearchCommand)
.command(FileTreeCommand)
.demandCommand(),
async handler() {},
})