A command-line journaling application
at exploded 80 lines 1.9 kB view raw
1import * as path from "std/path/mod.ts"; 2import * as toml from "std/toml/mod.ts"; 3 4export interface Config { 5 editor: string[]; 6 default: string; 7 journals: { 8 [name: string]: { 9 journal: string; 10 }; 11 }; 12} 13 14function homeDir(): string { 15 if (Deno.build.os === "windows") { 16 const home = Deno.env.get("USERPROFILE"); 17 if (home == null) throw new Error("USERPROFILE missing"); 18 return home; 19 } else { 20 const home = Deno.env.get("HOME"); 21 if (home == null) throw new Error("$HOME missing"); 22 return home; 23 } 24} 25 26function configDir(): string { 27 if (Deno.build.os === "windows") { 28 const profile = Deno.env.get("APPDATA"); 29 if (profile == null) throw new Error("APPDATA missing"); 30 return profile; 31 } else { 32 let configRoot = Deno.env.get("XDG_CONFIG_HOME"); 33 if (configRoot == null) { 34 configRoot = path.join(homeDir(), ".config"); 35 } 36 return configRoot; 37 } 38} 39 40function defaultConfigPath(): string { 41 return path.join(configDir(), "hayom", "hayom.toml"); 42} 43 44function defaultJournalPath(): string { 45 return path.join( 46 homeDir(), 47 Deno.build.os === "windows" ? "hayom.md" : ".hayom.md", 48 ); 49} 50 51function defaultEditor(): string[] { 52 const editor = Deno.env.get("EDITOR"); 53 if (editor != null) return editor.split(" "); 54 55 if (Deno.build.os == "windows") return ["cmd", "/c", "start"]; 56 else return ["nano"]; 57} 58 59export function loadConfig(): Config { 60 const defaultConfig: Config = { 61 default: "default", 62 editor: defaultEditor(), 63 journals: { 64 default: { 65 journal: defaultJournalPath(), 66 }, 67 }, 68 }; 69 70 try { 71 const userConfig = toml.parse(Deno.readTextFileSync(defaultConfigPath())); 72 const mergedConfig = { ...defaultConfig, ...userConfig }; 73 if (typeof userConfig.editor === "string") { 74 mergedConfig.editor = userConfig.editor.split(" "); 75 } 76 return mergedConfig; 77 } catch { 78 return defaultConfig; 79 } 80}