aboutsummaryrefslogtreecommitdiff
path: root/scripts/link-commands.js
diff options
context:
space:
mode:
authorSoccera1 <[email protected]>2026-06-15 21:10:43 +1000
committerSoccera1 <[email protected]>2026-07-13 17:02:02 +1000
commit7ea75fb4424af8df393dd75dfb9b14a08aa46959 (patch)
tree52772fa63c7ae0581b44d0c0b164252934acf5f0 /scripts/link-commands.js
downloadbnu-7ea75fb4424af8df393dd75dfb9b14a08aa46959.tar.gz
bnu-7ea75fb4424af8df393dd75dfb9b14a08aa46959.zip
Initial commit
Diffstat (limited to 'scripts/link-commands.js')
-rwxr-xr-xscripts/link-commands.js45
1 files changed, 45 insertions, 0 deletions
diff --git a/scripts/link-commands.js b/scripts/link-commands.js
new file mode 100755
index 0000000..617f64e
--- /dev/null
+++ b/scripts/link-commands.js
@@ -0,0 +1,45 @@
+#!/usr/bin/env bun
+import { chmod, mkdir, writeFile } from "node:fs/promises";
+import { dirname, resolve } from "node:path";
+import { fileURLToPath } from "node:url";
+import { commandNames } from "../src/coreutils.js";
+
+const root = resolve(dirname(fileURLToPath(import.meta.url)), "..");
+const targetDir = resolve(process.argv[2] ?? "dist/bin");
+const bnu = resolve(root, "bin/bnu.js");
+const bun = shellQuote(process.execPath);
+const bnuScript = shellQuote(bnu);
+
+await mkdir(targetDir, { recursive: true });
+
+for (const command of commandNames) {
+ const path = resolve(targetDir, command);
+ const script = command === "pwd"
+ ? `#!/bin/sh
+saved_pwd=$PWD
+actual_pwd=$(command pwd -P 2>/dev/null || :)
+test -n "$actual_pwd" && saved_pwd=$actual_pwd
+if [ \${#saved_pwd} -gt 4000 ]; then
+ cd / || exit 1
+ BNU_LONG_PWD=$saved_pwd PWD=$saved_pwd exec ${bun} ${bnuScript} pwd "$@"
+fi
+exec ${bun} ${bnuScript} pwd "$@"
+`
+ : command === "tail" || command === "tac" || command === "timeout"
+ ? `#!/bin/sh
+if [ ! -e /proc/$$/fd/0 ]; then
+ BNU_STDIN_CLOSED=1
+ export BNU_STDIN_CLOSED
+fi
+exec ${bun} ${bnuScript} ${command} "$@"
+`
+ : `#!/bin/sh\nexec ${bun} ${bnuScript} ${command} "$@"\n`;
+ await writeFile(path, script);
+ await chmod(path, 0o755);
+}
+
+console.log(`Linked ${commandNames.length} command wrappers into ${targetDir}`);
+
+function shellQuote(value) {
+ return `'${String(value).replaceAll("'", "'\\''")}'`;
+}