37 lines
894 B
TypeScript
37 lines
894 B
TypeScript
// Show helpful information about a command.
|
|
//
|
|
// (Hopefully.)
|
|
|
|
import { commandPath, commands } from "@/commands"
|
|
|
|
export default async function (cmd: string): Promise<string> {
|
|
if (!cmd) return "usage: help <command>"
|
|
|
|
const path = commandPath(cmd)
|
|
if (!path) { return matchingCommands(cmd) }
|
|
|
|
const code = (await Bun.file(path).text()).split("\n")
|
|
let docs = []
|
|
|
|
for (const line of code) {
|
|
if (line.startsWith("///")) {
|
|
docs.push("Runs in the browser.\n")
|
|
continue
|
|
} else if (line.startsWith("//")) {
|
|
docs.push(line.slice(2).trim())
|
|
} else if (line.trim()) {
|
|
break
|
|
}
|
|
}
|
|
|
|
return docs.join("\n")
|
|
}
|
|
|
|
async function matchingCommands(cmd: string): Promise<string> {
|
|
let matched: string[] = []
|
|
for (const command of Object.keys(await commands())) {
|
|
if (command.startsWith(cmd)) matched.push(command)
|
|
}
|
|
|
|
return matched.join(" ")
|
|
} |