diff --git a/src/lib.ts b/src/lib.ts index 5d17a17..2dfc821 100644 --- a/src/lib.ts +++ b/src/lib.ts @@ -13,7 +13,6 @@ export class UserError extends Error { } export const schemestring = "alt1lite"; export const weborigin = "https://runeapps.org"; -export const configFile = "./config.json"; //needed because node-fetch tries to be correct by choking on BOM export async function readJsonWithBOM(res: { text(): Promise }) { diff --git a/src/settings.ts b/src/settings.ts index a3ef5b8..4ccc8d0 100644 --- a/src/settings.ts +++ b/src/settings.ts @@ -1,6 +1,8 @@ import Checks, { UservarType } from "../tempexternal/typecheck"; import * as fs from "fs"; -import { configFile, readJsonWithBOM, weborigin } from "./lib"; +import * as os from 'os'; +import * as path from 'path'; +import { readJsonWithBOM, weborigin } from "./lib"; import fetch from "node-fetch"; import { CaptureMode } from "./native"; import { TypedEmitter } from "./typedemitter"; @@ -51,6 +53,42 @@ type SettingsEvents = { changed: [] } +function getPlatformConfigPath(filename: string): string { + const platform = process.platform; + const home = os.homedir(); + const appName = "alt1"; + + if (platform === 'win32') { + const appData = process.env.APPDATA || path.join(home, 'AppData', 'Roaming'); + return path.join(appData, appName, filename); + } + else if (platform === 'darwin') { + return path.join(home, 'Library', 'Preferences', appName, filename); + } + else { + // Linux and other Unix-like systems + const xdgConfig = process.env.XDG_CONFIG_HOME || path.join(home, '.config'); + return path.join(xdgConfig, appName, filename); + } +} + +const configFileName = "config.json"; +const configFile = process.env.ALT1_CONFIG_FILE || + (process.env.NODE_ENV === "development" ? + path.join(__dirname, configFileName) : + getPlatformConfigPath(configFileName)); + + +// Ensure the config directory exists +try { + const configDir = path.dirname(configFile); + if (!fs.existsSync(configDir)) { + fs.mkdirSync(configDir, { recursive: true }); + } +} catch (e) { + console.error("Could not create config directory:", e); +} + class ManagedSettings extends TypedEmitter { settings: Settings; appconfig: AppConfig; @@ -69,15 +107,18 @@ class ManagedSettings extends TypedEmitter { * Called on top-level, where await is not possible. Default settings are loaded in the background. */ loadOrFetch() { + console.log("Reading from path"); + console.log(this.path); try { let file = JSON.parse(fs.readFileSync(this.path, "utf8")); this.settings = checkSettings.load(file, { defaultOnError: true }); this.appconfig.setBookmarks(this.settings.bookmarks); } catch (e) { console.log("couldn't load config"); + console.log(e); this.settings = checkSettings.default(); this.appconfig.setBookmarks(this.settings.bookmarks); - + fetch(`${weborigin}/data/alt1/defaultapps.json`).then(r => readJsonWithBOM(r)).then(async (r: { folder: string, name: string, url: string }[]) => { for (let appbase of r) { await this.appconfig.identifyApp(new URL(`${weborigin}${appbase.url}`)); @@ -97,8 +138,8 @@ class ManagedSettings extends TypedEmitter { } - set captureMode(mode: CaptureMode) { - if (!Object.keys(checkSettings.props.captureMode.opts).includes(mode)) { + set captureMode(mode: CaptureMode) { + if (!Object.keys(checkSettings.props.captureMode.opts).includes(mode)) { console.log("unknown capture mode", mode); return; } @@ -114,4 +155,4 @@ class ManagedSettings extends TypedEmitter { } } -export var settings = new ManagedSettings(configFile); \ No newline at end of file +export var settings = new ManagedSettings(configFile);