path.ts

 1// SPDX-FileCopyrightText: Amolith <amolith@secluded.site>
 2//
 3// SPDX-License-Identifier: GPL-3.0-or-later
 4
 5import { resolve } from "node:path";
 6
 7/**
 8 * Expand a leading ~ in a file path to the user's home directory.
 9 * Use for paths outside the workspace (e.g. system_prompt_path).
10 * Workspace-sandboxed paths should NOT use this.
11 */
12export function expandHomePath(filePath: string): string {
13  const home = process.env["HOME"];
14  if (!home) return filePath;
15
16  if (filePath === "~") return home;
17  if (filePath.startsWith("~/")) return resolve(home, filePath.slice(2));
18
19  return filePath;
20}