45 lines
1.6 KiB
TypeScript
45 lines
1.6 KiB
TypeScript
import { z } from "npm:zod@3.24.4"
|
|
import { zodToJsonSchema } from "npm:zod-to-json-schema@3.24.4"
|
|
import { join, SEPARATOR } from "https://deno.land/std@0.224.0/path/mod.ts"
|
|
import { exists } from "https://deno.land/std@0.224.0/fs/mod.ts"
|
|
import { log } from "./utils.ts"
|
|
|
|
export type ConfigType = z.infer<ReturnType<typeof config_schema>>
|
|
export function config_schema() {
|
|
return z.object({
|
|
file_path: z.string(),
|
|
repo_path: z.string(),
|
|
remote_url: z.string(),
|
|
})
|
|
}
|
|
|
|
export function config_json_schema() {
|
|
return zodToJsonSchema(config_schema())
|
|
}
|
|
|
|
async function home_path() {
|
|
const from_env = Deno.env.get("FESEUR_HOME") ?? Deno.env.get("HOME")
|
|
if (from_env !== undefined) return from_env
|
|
const whoami = await new Deno.Command("whoami", { stdout: "piped" }).output()
|
|
return join(SEPARATOR, "home", new TextDecoder().decode(whoami.stdout))
|
|
}
|
|
|
|
export async function config_default() {
|
|
return {
|
|
"$schema": "https://git.barnulf.net/mb/feseur/raw/branch/master/asset/config_schema.json",
|
|
file_path: join(await home_path(), "todo"),
|
|
repo_path: join(await home_path(), ".local", "share", "feseur"),
|
|
remote_url: "ssh://forgejo@barnulf.net/mb/todo.git",
|
|
} as ConfigType
|
|
}
|
|
|
|
async function write_default_conf(path: string) {
|
|
log(`Writing default configuration to '${path}'.`)
|
|
await Deno.writeTextFile(path, JSON.stringify(await config_default(), null, 4))
|
|
}
|
|
|
|
export async function read_conf() {
|
|
const path = join(await home_path(), ".config", "feseur.json")
|
|
if (!await exists(path)) await write_default_conf(path)
|
|
return config_schema().parse(JSON.parse(await Deno.readTextFile(path)))
|
|
}
|