Initialization.
This commit is contained in:
commit
4094afff3d
13 changed files with 517 additions and 0 deletions
45
src/lib/conf.ts
Normal file
45
src/lib/conf.ts
Normal file
|
@ -0,0 +1,45 @@
|
|||
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://git.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)))
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue