// SPDX-FileCopyrightText: Amolith <amolith@secluded.site>
//
// SPDX-License-Identifier: GPL-3.0-or-later

import { resolve } from "node:path";

/**
 * Expand a leading ~ in a file path to the user's home directory.
 * Use for paths outside the workspace (e.g. system_prompt_path).
 * Workspace-sandboxed paths should NOT use this.
 */
export function expandHomePath(filePath: string): string {
  const home = process.env["HOME"];
  if (!home) return filePath;

  if (filePath === "~") return home;
  if (filePath.startsWith("~/")) return resolve(home, filePath.slice(2));

  return filePath;
}
