From 6f5241afb5bd1d6a1a8724bf06cc6f205c279402 Mon Sep 17 00:00:00 2001 From: Soccera1 <129828376+Soccera1@users.noreply.github.com> Date: Sat, 18 Jul 2026 09:27:17 +1000 Subject: Refactor to single call --- src/commands/pwd.js | 123 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100644 src/commands/pwd.js (limited to 'src/commands/pwd.js') diff --git a/src/commands/pwd.js b/src/commands/pwd.js new file mode 100644 index 0000000..177b089 --- /dev/null +++ b/src/commands/pwd.js @@ -0,0 +1,123 @@ +#!/usr/bin/env bun + +import { realpath } from "node:fs/promises"; +import { isAbsolute } from "node:path"; +import { parseOptions } from "../shared/common.js"; +import { UsageError, stderr, stdout } from "../shared/diagnostics.js"; +import { processCwdOrNull } from "../shared/filesystem.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export const PWD_LONG_OPTIONS = ["logical", "physical", "help", "version"]; + +export function pwdMetaOption(args) { + for (const arg of args) { + if (arg === "--") return null; + if (!arg.startsWith("-") || arg === "-") continue; + if (arg.startsWith("--")) { + const option = normalizePwdLongOption(arg); + const name = option.slice(2).split("=", 1)[0]; + if (!PWD_LONG_OPTIONS.includes(name) || option.includes("=")) return null; + if (option === "--help" || option === "--version") return option; + continue; + } + for (let j = 1; j < arg.length; j++) if (arg[j] !== "L" && arg[j] !== "P") return null; + } + return null; +} + +export async function pwd(args) { + args = normalizePwdLongOptions(args); + const requestedMode = pwdRequestedMode(args); + const { operands } = parseOptions(args, { short: { L: false, P: false }, long: { logical: false, physical: false, help: false, version: false } }); + if (operands.length) stderr("pwd: ignoring non-option arguments\n"); + const wrapperPwd = syntacticallyValidLogicalPwd(process.env.BNU_LONG_PWD); + if (wrapperPwd != null) { + stdout(`${wrapperPwd}\n`); + return 0; + } + const cwd = processCwdOrNull(); + if (cwd == null) { + const logical = syntacticallyValidLogicalPwd(process.env.PWD); + if (logical != null) { + stdout(`${logical}\n`); + return 0; + } + throw new UsageError("failed to find current directory"); + } + const physical = await realpath(cwd); + const useLogical = requestedMode === "logical" || (requestedMode == null && process.env.POSIXLY_CORRECT); + if (useLogical) { + const logical = await validLogicalPwd(process.env.PWD, physical); + stdout(`${logical ?? physical}\n`); + } else { + stdout(`${physical}\n`); + } + return 0; +} + +export function pwdRequestedMode(args) { + let mode = null; + let end = false; + for (const arg of args) { + if (end) continue; + if (arg === "--") { + end = true; + continue; + } + if (arg === "-" || !arg.startsWith("-")) continue; + if (arg.startsWith("--")) { + if (arg === "--logical") mode = "logical"; + else if (arg === "--physical") mode = "physical"; + continue; + } + for (let j = 1; j < arg.length; j++) { + if (arg[j] === "L") mode = "logical"; + else if (arg[j] === "P") mode = "physical"; + } + } + return mode; +} + +export function normalizePwdLongOptions(args) { + const out = []; + let end = false; + for (const arg of args) { + if (end || arg === "--" || !arg.startsWith("--")) { + out.push(arg); + if (arg === "--") end = true; + continue; + } + out.push(normalizePwdLongOption(arg)); + } + return out; +} + +export function normalizePwdLongOption(arg) { + const body = arg.slice(2); + const eq = body.indexOf("="); + const name = eq === -1 ? body : body.slice(0, eq); + if (!name) return arg; + const match = PWD_LONG_OPTIONS.find((option) => option.startsWith(name)); + if (!match) return arg; + return eq === -1 ? `--${match}` : `--${match}=${body.slice(eq + 1)}`; +} + +export function syntacticallyValidLogicalPwd(value) { + if (!value || !isAbsolute(value)) return null; + if (value.split("/").some((part) => part === "." || part === "..")) return null; + return value; +} + +export async function validLogicalPwd(value, physical) { + if (syntacticallyValidLogicalPwd(value) == null) return null; + try { + return await realpath(value) === physical ? value : null; + } catch { + return null; + } +} + +const singleCall = defineCommand("pwd", pwd, pwdMetaOption); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); -- cgit v1.3