diff --git a/src/analyze/analysis.ts b/src/analyze/analysis.ts index 789aaf0..aa0cb67 100755 --- a/src/analyze/analysis.ts +++ b/src/analyze/analysis.ts @@ -1,4 +1,4 @@ -#!/usr/bin/env zx +import * as path from "@std/path" import "zx/globals" export interface AnalysisConfig { @@ -11,7 +11,7 @@ export interface ImageAnalysisData { } export const defaultConfig: AnalysisConfig = { - imageDir: `${os.homedir()}/Pictures`, + imageDir: path.fromFileUrl(`file:///${os.homedir()}/Pictures`), preAnalysisCommands: [], } diff --git a/src/analyze/index.ts b/src/analyze/index.ts index a8da2b1..dfeda27 100755 --- a/src/analyze/index.ts +++ b/src/analyze/index.ts @@ -1,14 +1,17 @@ -#!/usr/bin/env zx - -import { cacheDir, chalkDebug, configDir, loadConfig } from "../utils.ts" -import { type AnalysisConfig, analyzeImages, defaultConfig, type ImageAnalysisData, runPreAnalysis } from "./analysis.ts" +import { analyzeConfigPath, chalkDebug, loadConfig } from "../utils.ts" +import { + type AnalysisConfig, + analyzeImages, + defaultConfig, + type ImageAnalysisData, + runPreAnalysis, +} from "./analysis.ts" +import * as path from "@std/path" import "zx/globals" -const pathToConfig = `${configDir}/analyze-config.json` - -console.debug(chalkDebug(`Trying to load config from ${pathToConfig}...`)) +console.debug(chalkDebug(`Trying to load config from ${analyzeConfigPath}...`)) -const config = await loadConfig(pathToConfig, defaultConfig) as AnalysisConfig +const config = await loadConfig(analyzeConfigPath, defaultConfig) as AnalysisConfig if (config == undefined) { throw new Error("Couldn't initialize config!") @@ -16,7 +19,7 @@ if (config == undefined) { console.debug(chalkDebug(`Config: \n${JSON.stringify(config, null, 4)}`)) -await fs.cp(`${cacheDir}/analysis.json`, `${cacheDir}/analysis.json.prev`, { force: true }, (err: Error) => { +await fs.cp(analyzeConfigPath, `${analyzeConfigPath}.bak`, { force: true }, (err: Error) => { if (err) { console.log(err) } @@ -26,6 +29,6 @@ const imageData = { files: [] } as ImageAnalysisData await runPreAnalysis(config.preAnalysisCommands) -await analyzeImages(config.imageDir, imageData, `${cacheDir}/analysis.json`) +await analyzeImages(path.fromFileUrl(`file:///${config.imageDir}`), imageData, analyzeConfigPath) -await fs.rm(`${cacheDir}/analysis.json.prev`, { force: true }) +await fs.rm(path.fromFileUrl(`${analyzeConfigPath}.bak`), { force: true }) diff --git a/src/apply/index.ts b/src/apply/index.ts index 5829f85..453a3c6 100644 --- a/src/apply/index.ts +++ b/src/apply/index.ts @@ -1,15 +1,12 @@ import type { ImageAnalysisData } from "../analyze/analysis.ts" -import { cacheDir, chalkDebug, configDir, loadConfig, map, mapEaseOutExp, mapEaseOutQuint } from "../utils.ts" +import { applyConfigPath, cachePath, chalkDebug, loadConfig, map, mapEaseOutExp, mapEaseOutQuint } from "../utils.ts" import { type ApplicationConfig, defaultConfig, findMatchingImages, getOpenMeteoData } from "./application.ts" import * as SunCalc from "suncalc" import "zx/globals" -const pathToConfig = `${configDir}/apply-config.json` -const pathToCache = `${cacheDir}/analysis.json` - -console.debug(chalkDebug(`Trying to load application config from ${pathToConfig}...`)) +console.debug(chalkDebug(`Trying to load application config from ${applyConfigPath}...`)) const config = await loadConfig( - pathToConfig, + applyConfigPath, defaultConfig, ) as ApplicationConfig if (config == undefined) { @@ -66,12 +63,12 @@ const lightnessValue = mapEaseOutExp( console.info(`Calculated target colour oklch(${lightnessValue} ${chromaValue} ${hueValue})!`) -console.debug(chalkDebug(`Trying to open cache file ${pathToCache}...`)) -const cacheFile = await fs.promises.readFile(`${pathToCache}`, { +console.debug(chalkDebug(`Trying to open cache file ${cachePath}...`)) +const cacheFile = await fs.promises.readFile(`${cachePath}`, { encoding: "utf8", }) const imagesData = JSON.parse(cacheFile) as ImageAnalysisData -console.debug(chalkDebug(`Opened cache file ${pathToCache}!`)) +console.debug(chalkDebug(`Opened cache file ${cachePath}!`)) const targetImages = findMatchingImages(imagesData, hueValue, chromaValue, lightnessValue) const targetImage = targetImages[Math.floor(Math.random() * targetImages.length)] diff --git a/src/utils.ts b/src/utils.ts index c11135c..a2113dd 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -3,38 +3,37 @@ import "zx/globals" function getConfigPath() { if (Deno.build.os == "windows") { - return path.fromFileUrl(`file:///${os.homedir}/AppData/Local/rainwall`) + return `${os.homedir}/AppData/Local/rainwall` } else if (Deno.build.os == "darwin") { - return path.fromFileUrl(`file:///${os.homedir()}/Library/Preferences/rainwall`) + return `${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`) + ? `${process.env.XDG_CONFIG_HOME}/rainwall` + : `${os.homedir()}/.config/rainwall` } } function getCachePath() { if (Deno.build.os == "windows") { - return path.fromFileUrl(`file:///${os.homedir}/AppData/Local/Temp/rainwall`) + return `${os.homedir}/AppData/Local/Temp/rainwall` } else if (Deno.build.os == "darwin") { - return path.fromFileUrl(`file:///${os.homedir()}/Library/Caches/rainwall`) + return `${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`) + return process.env.XDG_CACHE_HOME ? `${process.env.XDG_CACHE_HOME}/rainwall` : `${os.homedir()}/.cache/rainwall` } } -export const configDir = getConfigPath() +export const applyConfigPath = path.fromFileUrl(`file:///${getConfigPath()}/apply-config.json`) +export const analyzeConfigPath = path.fromFileUrl(`file:///${getConfigPath()}/analyze-config.json`) +export const cachePath = path.fromFileUrl(`file:///${getCachePath()}/analysis.json`) -export const cacheDir = getCachePath() - -if (!fs.statSync(configDir)) { - fs.mkdirSync(configDir) +// ensure the config and cache directories exist +if (!fs.statSync(path.fromFileUrl(`file:///${getConfigPath()}`))) { + fs.mkdirSync(path.fromFileUrl(`file:///${getConfigPath()}`), { recursive: true }) } -if (!fs.statSync(cacheDir)) { - fs.mkdirSync(cacheDir) +if (!fs.statSync(path.fromFileUrl(`file:///${getCachePath()}`))) { + fs.mkdirSync(path.fromFileUrl(`file:///${getCachePath()}`), { recursive: true }) } export async function loadConfig(pathToConfig: string, defaultConfig: object) { @@ -90,9 +89,9 @@ export function mapEaseOutQuint(number: number, min: number, max: number, newMin } function easeOutExp(x: number, exp: number): number { - return Math.pow(x, exp); + return Math.pow(x, exp) } export function mapEaseOutExp(number: number, exp: number, min: number, max: number, newMin: number, newMax: number) { return easeOutExp((number - min) / (max - min), exp) * (newMax - newMin) + newMin -} \ No newline at end of file +}