app/cli/lib/paths.ts
zzmq.dev@gmail.com/zz / app / cli / lib / paths.ts
@ 4942dbdb
Download
Ladder Summary
- file The file provides a utility for turning a user‑supplied path (such as an environment variable or CLI flag) into a proper absolute path, expanding a leading “~” to the current user’s home directory before calling `path.resolve`. It exports a single function, **`resolveUserPath(input: string): string`**, which performs this normalization. This ensures paths like “~/my-config.json” are correctly resolved to the home directory rather than a literal “~” folder.
// Normalizes a user-supplied path (an env var value -- ZZ_CONFIG,
// ZZ_CREDENTIALS, ZZ_ID_FILE -- or a future path-taking flag) to an
// absolute path. Node's fs functions resolve a relative path against
// process.cwd() silently and don't expand "~" at all, so without this a
// value like "~/my-config.json" would be read literally as a directory
// named "~" next to wherever the command happened to be run from,
// rather than the user's home directory. Internal paths this CLI
// computes itself (e.g. cli/lib/repo.ts's `~/.zz/repos/...`) are
// already absolute by construction (join() against homedir()) and
// don't need this -- it's specifically for normalizing input a user
// typed on the command line or in their environment.
import { homedir } from "node:os";
import { resolve } from "node:path";
export function resolveUserPath(input: string): string {
const expanded = input === "~" ? homedir() : input.startsWith("~/") ? resolve(homedir(), input.slice(2)) : input;
return resolve(expanded);
}