Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> }) {
Expand Down
51 changes: 46 additions & 5 deletions src/settings.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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<SettingsEvents> {
settings: Settings;
appconfig: AppConfig;
Expand All @@ -69,15 +107,18 @@ class ManagedSettings extends TypedEmitter<SettingsEvents> {
* 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}`));
Expand All @@ -97,8 +138,8 @@ class ManagedSettings extends TypedEmitter<SettingsEvents> {
}


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;
}
Expand All @@ -114,4 +155,4 @@ class ManagedSettings extends TypedEmitter<SettingsEvents> {
}
}

export var settings = new ManagedSettings(configFile);
export var settings = new ManagedSettings(configFile);