Initial commit
This commit is contained in:
commit
2fcdb11783
7 changed files with 518 additions and 0 deletions
102
src/cli.ts
Normal file
102
src/cli.ts
Normal file
|
@ -0,0 +1,102 @@
|
|||
import { parseArgs } from "node:util";
|
||||
import { edit, ls, newScript, runScript, which } from "./scriptFile";
|
||||
|
||||
const KNOWN_COMMANDS = [
|
||||
"ls",
|
||||
"help",
|
||||
"new",
|
||||
"create",
|
||||
"which",
|
||||
"x",
|
||||
"run",
|
||||
"edit",
|
||||
] as const;
|
||||
type Command = (typeof KNOWN_COMMANDS)[number];
|
||||
|
||||
function isCommand(command: string): command is Command {
|
||||
return KNOWN_COMMANDS.includes(command as Command);
|
||||
}
|
||||
|
||||
function help() {
|
||||
console.log(
|
||||
`
|
||||
Usage: rundir [command]
|
||||
|
||||
Commands:
|
||||
new <name> - Create a new script in the rundir (alias: create)
|
||||
edit <name> - Open a script in the editor
|
||||
x <name> - Run a script from the nearest rundir (alias: run)
|
||||
which - Print the absolute path of the rundir
|
||||
ls - List all scripts in the rundir
|
||||
help - Show this help message
|
||||
`.trim(),
|
||||
);
|
||||
}
|
||||
|
||||
function readArgs(): string[] {
|
||||
const { positionals } = parseArgs({
|
||||
args: Bun.argv,
|
||||
allowPositionals: true,
|
||||
strict: true,
|
||||
});
|
||||
|
||||
return positionals.slice(2);
|
||||
}
|
||||
|
||||
const args = readArgs();
|
||||
|
||||
// Default functionality is to list
|
||||
if (args.length === 0) {
|
||||
await ls();
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
const command = args[0];
|
||||
|
||||
if (!isCommand(command)) {
|
||||
console.error(`Unknown command: ${args[0]}`);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
switch (command) {
|
||||
case "ls":
|
||||
await ls();
|
||||
break;
|
||||
case "help":
|
||||
help();
|
||||
break;
|
||||
case "which":
|
||||
which();
|
||||
break;
|
||||
case "new":
|
||||
case "create":
|
||||
if (args.length < 2) {
|
||||
console.error("Missing script name");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
await newScript(args[1]);
|
||||
break;
|
||||
case "x":
|
||||
case "run":
|
||||
if (args.length < 2) {
|
||||
console.error("Missing script name");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
await runScript(args[1]);
|
||||
break;
|
||||
case "edit":
|
||||
if (args.length < 2) {
|
||||
console.error("Missing script name");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
edit(args[1]);
|
||||
break;
|
||||
default:
|
||||
console.error(`Unknown command "${command}"`);
|
||||
break;
|
||||
}
|
||||
|
||||
process.exit(0);
|
Loading…
Add table
Add a link
Reference in a new issue