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/commands/link.js | |
| parent | a753f669c9bedfccb9f7f400da018bb487dca5d0 (diff) | |
| download | bnu-6f5241afb5bd1d6a1a8724bf06cc6f205c279402.tar.gz bnu-6f5241afb5bd1d6a1a8724bf06cc6f205c279402.zip | |
Refactor to single call
Diffstat (limited to 'src/commands/link.js')
| -rw-r--r-- | src/commands/link.js | 31 |
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); |
