handle macos more gracefully

This commit is contained in:
Danny Morabito 2025-02-24 23:00:23 +01:00
parent 1f95a4f0c8
commit 9bef51a027
Signed by: dannym
GPG key ID: 7CC8056A5A04557E

View file

@ -1,18 +1,28 @@
import { exists } from "jsr:@std/fs";
/**
* Return the path to Eve's configuration directory.
* Return the path to Eve's configuration directory and ensures its existence.
*
* The configuration directory is resolved in the following order:
* 1. The value of the `XDG_CONFIG_HOME` environment variable.
* 2. The value of the `HOME` environment variable, with `.config` appended.
* On macOS, the directory is located at "$HOME/Library/Application Support/eve/arx/Eve".
* On other systems, it defaults to "$XDG_CONFIG_HOME/arx/Eve" or
* "$HOME/.config/arx/Eve" if XDG_CONFIG_HOME is not set.
*
* If the resolved path does not exist, create it.
* If the directory does not exist, it is created automatically.
*
* @returns A promise that resolves to the path of the configuration directory.
*/
export async function getEveConfigHome(): Promise<string> {
const xdgConfigHome = Deno.env.get("XDG_CONFIG_HOME") ??
`${Deno.env.get("HOME")}/.config`;
const storagePath = `${xdgConfigHome}/arx/Eve`;
let storagePath: string;
if (Deno.build.os === "darwin") {
storagePath = `${
Deno.env.get("HOME")
}/Library/Application Support/eve/arx/Eve`;
} else {
const xdgConfigHome = Deno.env.get("XDG_CONFIG_HOME") ??
`${Deno.env.get("HOME")}/.config`;
storagePath = `${xdgConfigHome}/arx/Eve`;
}
if (!(await exists(storagePath))) {
await Deno.mkdir(storagePath, { recursive: true });
}