diff options
| author | Soccera1 <[email protected]> | 2026-07-18 09:27:17 +1000 |
|---|---|---|
| committer | Soccera1 <[email protected]> | 2026-07-18 09:27:17 +1000 |
| commit | 6f5241afb5bd1d6a1a8724bf06cc6f205c279402 (patch) | |
| tree | 2aa852e968f6b89f2c8458333ce3e7c38b3c2450 /src/shared/command.js | |
| parent | a753f669c9bedfccb9f7f400da018bb487dca5d0 (diff) | |
| download | bnu-6f5241afb5bd1d6a1a8724bf06cc6f205c279402.tar.gz bnu-6f5241afb5bd1d6a1a8724bf06cc6f205c279402.zip | |
Refactor to single call
Diffstat (limited to 'src/shared/command.js')
| -rw-r--r-- | src/shared/command.js | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/src/shared/command.js b/src/shared/command.js new file mode 100644 index 0000000..0d7666d --- /dev/null +++ b/src/shared/command.js @@ -0,0 +1,28 @@ +import { readFileSync } from "node:fs"; +import { executeCommand } from "./runtime.js"; +import { hasCommand } from "./catalog.js"; + +export function defineCommand(name, implementation, metaOption) { + if (!hasCommand(name)) throw new Error(`Unknown BNU command: ${name}`); + return (args) => executeCommand(name, implementation, metaOption, args); +} + +export async function runAsMain(command, args = singleCallArgs()) { + globalThis[Symbol.for("bnu.cli")] = true; + process.exit(await command(args)); +} + +// Bun consumes a leading `--` between the script name and its arguments. GNU +// utilities need to see that separator because it changes option parsing. +function singleCallArgs() { + const args = Bun.argv.slice(2); + try { + const parts = readFileSync("/proc/self/cmdline").subarray(0, -1).toString("binary").split("\0"); + let scriptIndex = parts.indexOf(Bun.argv[1]); + if (scriptIndex === -1 && parts.length > 1) scriptIndex = 1; + if (parts[scriptIndex + 1] === "--" && args[0] !== "--") return ["--", ...args]; + } catch { + // Non-Linux platforms retain Bun's decoded argument list. + } + return args; +} |
