aboutsummaryrefslogtreecommitdiff
path: root/src/commands/link.js
diff options
context:
space:
mode:
authorLily <[email protected]>2026-07-28 16:12:21 +1000
committerGitHub <[email protected]>2026-07-28 16:12:21 +1000
commitc11e17573334dfb487a8e1cc25fa84fa9e138f5c (patch)
treee91dbd19b524f9432da6bcb86b1fc193e0d02e26 /src/commands/link.js
parent6a2f098e72e71437154ec00506e8e736aa8e1026 (diff)
downloadbnu-c11e17573334dfb487a8e1cc25fa84fa9e138f5c.tar.gz
bnu-c11e17573334dfb487a8e1cc25fa84fa9e138f5c.zip
Refactor coreutils into single-call command modules (#1)
* Refactor to single call
Diffstat (limited to 'src/commands/link.js')
-rw-r--r--src/commands/link.js31
1 files changed, 31 insertions, 0 deletions
diff --git a/src/commands/link.js b/src/commands/link.js
new file mode 100644
index 0000000..65b77b3
--- /dev/null
+++ b/src/commands/link.js
@@ -0,0 +1,31 @@
+#!/usr/bin/env bun
+
+import { link as fsLink } from "node:fs/promises";
+import { helpVersionOnlyMetaOption, localeQuotedEscapedDiagnostic, normalizeHelpVersionOnlyLongOptions, parseOptions } from "../shared/common.js";
+import { UsageError, fail } from "../shared/diagnostics.js";
+import { errnoMessage, linkQuotedName } from "../shared/filesystem.js";
+import { defineCommand, runAsMain } from "../shared/command.js";
+
+export async function linkCmd(args) {
+ args = normalizeHelpVersionOnlyLongOptions(args);
+ const { operands } = parseOptions(args, { long: { help: false, version: false } });
+ if (!operands.length) throw new UsageError("missing operand", true);
+ if (operands.length < 2) throw new UsageError(`missing operand after '${operands[0]}'`, true);
+ if (operands.length > 2) throw new UsageError(`extra operand ${localeQuotedEscapedDiagnostic(operands[2])}`, true);
+ try {
+ await fsLink(operands[0], operands[1]);
+ } catch (error) {
+ return fail("link", `cannot create link ${linkQuotedName(operands[1])} to ${linkQuotedName(operands[0])}: ${linkErrorMessage(error)}`);
+ }
+ return 0;
+}
+
+export function linkErrorMessage(error) {
+ if (error?.code === "EEXIST") return "File exists";
+ return errnoMessage(error);
+}
+
+const singleCall = defineCommand("link", linkCmd, helpVersionOnlyMetaOption);
+export default singleCall;
+
+if (import.meta.main) await runAsMain(singleCall);