File size: 1,123 Bytes
88c4c60 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | import fs from "node:fs";
import path from "path";
import os from "os";
const APP_NAME = "9router";
function defaultDir() {
if (process.platform === "win32") {
return path.join(process.env.APPDATA || path.join(os.homedir(), "AppData", "Roaming"), APP_NAME);
}
return path.join(os.homedir(), `.${APP_NAME}`);
}
export function getDataDir() {
const configured = process.env.DATA_DIR;
if (!configured) return defaultDir();
// On Windows, ignore Unix-style absolute paths (e.g. /var/lib/...) that come
// from a Linux-targeted .env or Docker config — they are not valid here.
if (process.platform === "win32" && /^\//.test(configured)) {
console.warn(`[DATA_DIR] '${configured}' is a Unix path on Windows → fallback to default`);
return defaultDir();
}
try {
fs.mkdirSync(configured, { recursive: true });
return configured;
} catch (e) {
if (e?.code === "EACCES" || e?.code === "EPERM") {
console.warn(`[DATA_DIR] '${configured}' not writable → fallback ~/.${APP_NAME}`);
return defaultDir();
}
throw e;
}
}
export const DATA_DIR = getDataDir();
|