From 648c762e06108b7f4adebeae326adfe817b70362 Mon Sep 17 00:00:00 2001 From: Angel Wang Date: Sun, 21 Jun 2026 15:27:23 -0600 Subject: [PATCH] feat: attempt cross-platform compatibility with better config paths --- deno.json | 3 ++- deno.lock | 11 +++++++++-- utils.ts | 49 +++++++++++++++++++++++++++++++++++++------------ 3 files changed, 48 insertions(+), 15 deletions(-) diff --git a/deno.json b/deno.json index 7bd9097..4fa808d 100644 --- a/deno.json +++ b/deno.json @@ -6,7 +6,8 @@ "apply": "deno run -A apply/index.ts" }, "imports": { - "@std/assert": "jsr:@std/assert@1" + "@std/assert": "jsr:@std/assert@1", + "@std/path": "jsr:@std/path@^1.1.5" }, "fmt": { "semiColons": false, "indentWidth": 4, "useTabs": true, "lineWidth": 120 } } diff --git a/deno.lock b/deno.lock index d7a96ad..6adbed6 100644 --- a/deno.lock +++ b/deno.lock @@ -10,11 +10,17 @@ "@std/assert@1.0.19": { "integrity": "eaada96ee120cb980bc47e040f82814d786fe8162ecc53c91d8df60b8755991e", "dependencies": [ - "jsr:@std/internal" + "jsr:@std/internal@^1.0.12" ] }, "@std/internal@1.0.14": { "integrity": "291516b3d4c35024d6ffbc0a9df5bf4c64116e05b50012cf846710152d2ffdf7" + }, + "@std/path@1.1.5": { + "integrity": "ccea00982ea28c36becaf6e62f855406c76a8c32d462f66f415bbb7d83a271bc", + "dependencies": [ + "jsr:@std/internal@^1.0.14" + ] } }, "npm": { @@ -28,7 +34,8 @@ }, "workspace": { "dependencies": [ - "jsr:@std/assert@1" + "jsr:@std/assert@1", + "jsr:@std/path@^1.1.5" ], "packageJson": { "dependencies": [ diff --git a/utils.ts b/utils.ts index cf086e5..17d4d55 100644 --- a/utils.ts +++ b/utils.ts @@ -1,11 +1,33 @@ +import * as path from "@std/path" import "zx/globals" -export const configDir = process.env.XDG_CONFIG_HOME - ? `${process.env.XDG_CONFIG_HOME}/rainwall` - : `${os.homedir()}/.config/rainwall` -export const cacheDir = process.env.XDG_CACHE_HOME - ? `${process.env.XDG_CACHE_HOME}/rainwall` - : `${os.homedir()}/.cache/rainwall` +function getConfigPath() { + if (Deno.build.os == "windows") { + return path.fromFileUrl(`file:///${os.homedir}/AppData/Local/rainwall`) + } else if (Deno.build.os == "darwin") { + return path.fromFileUrl(`file:///${os.homedir()}/Library/Preferences/rainwall`) + } else { + return process.env.XDG_CONFIG_HOME + ? path.fromFileUrl(`file:///${process.env.XDG_CONFIG_HOME}/rainwall`) + : path.fromFileUrl(`file:///${os.homedir()}/.config/rainwall`) + } +} + +function getCachePath() { + if (Deno.build.os == "windows") { + return path.fromFileUrl(`file:///${os.homedir}/AppData/Local/Temp/rainwall`) + } else if (Deno.build.os == "darwin") { + return path.fromFileUrl(`file:///${os.homedir()}/Library/Caches/rainwall`) + } else { + return process.env.XDG_CACHE_HOME + ? path.fromFileUrl(`file:///${process.env.XDG_CACHE_HOME}/rainwall`) + : path.fromFileUrl(`file:///${os.homedir()}/.cache/rainwall`) + } +} + +export const configDir = getConfigPath() + +export const cacheDir = getCachePath() if (!fs.statSync(configDir)) { fs.mkdirSync(configDir) @@ -22,7 +44,7 @@ export async function loadConfig(pathToConfig: string, defaultConfig: object) { encoding: "utf8", }) if (stringConfig == undefined) { - console.warn( + console.log( `Current config blank, generating a new one... you may need to stop this process and edit the config file.`, ) config = defaultConfig @@ -31,11 +53,10 @@ export async function loadConfig(pathToConfig: string, defaultConfig: object) { config = JSON.parse(stringConfig) } } catch (err) { - console.warn( - `Got ${ - (err as Error).message - } when trying to load config, generating a new one... you may need to stop this process and edit the config file.`, - ) + console.warn(chalkWarn( + `Got ${(err as Error).message} when trying to load config, generating a new one... + you may need to stop this process and edit the config file.`, + )) config = defaultConfig writeDefaultConfig(pathToConfig, defaultConfig) } @@ -48,6 +69,10 @@ async function writeDefaultConfig(path: string, data: object) { console.info(`Generated default config at ${path}!`) } +export const chalkError = chalk.bold.red +export const chalkWarn = chalk.yellow +export const chalkDebug = chalk.gray + export function clamp(number: number, min: number, max: number) { return Math.max(min, Math.min(number, max)) } -- 2.51.2