From adfaa2afe2e13500832b2c78c7140df7cb152c3f Mon Sep 17 00:00:00 2001 From: JOLIMAITRE Matthieu Date: Mon, 26 May 2025 23:09:47 +0200 Subject: [PATCH 1/2] Add service. --- src/lib/conf.ts | 2 +- src/lib/debounce.ts | 14 +++++++++++++ src/lib/repo.ts | 50 +++++++++++++++++++++++++++++++++++++++++++++ src/service.ts | 20 ++++++++++++++++-- src/todo.ts | 3 +++ todo | 0 6 files changed, 86 insertions(+), 3 deletions(-) create mode 100644 src/lib/debounce.ts create mode 100644 src/lib/repo.ts create mode 100644 todo diff --git a/src/lib/conf.ts b/src/lib/conf.ts index e6996bb..82821a9 100644 --- a/src/lib/conf.ts +++ b/src/lib/conf.ts @@ -29,7 +29,7 @@ export async function config_default() { "$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", + remote_url: "ssh://forgejo@barnulf.net/mb/todo.git", } as ConfigType } diff --git a/src/lib/debounce.ts b/src/lib/debounce.ts new file mode 100644 index 0000000..4b9d0f0 --- /dev/null +++ b/src/lib/debounce.ts @@ -0,0 +1,14 @@ +export class Debouncer { + last = 0 + + public constructor( + public timeout: number, + ) {} + + public should_skip() { + const now = Date.now() + const delay = now - this.last + this.last = now + return (delay < this.timeout) + } +} diff --git a/src/lib/repo.ts b/src/lib/repo.ts new file mode 100644 index 0000000..b19b44d --- /dev/null +++ b/src/lib/repo.ts @@ -0,0 +1,50 @@ +import { exists } from "https://deno.land/std@0.224.0/fs/mod.ts" +import { join } from "https://deno.land/std@0.224.0/path/mod.ts" +import { crayon } from "https://deno.land/x/crayon@3.3.3/mod.ts" + +export async function fetch_repo(repo_path: string, remote_url: string) { + if (!await exists(join(repo_path, ".git"))) await create_repo(repo_path, remote_url) + const status = await run("git", "-C", repo_path, "pull") + if (!status.success) warn("Failed to fetch : " + new TextDecoder().decode(status.stderr)) + await run("git", "-C", repo_path, "push") +} + +async function create_repo(repo_path: string, remote_url: string) { + await Deno.mkdir(repo_path, { recursive: true }) + const status = await run("git", "clone", remote_url, repo_path) + if (!status.success) warn("Failed to clone : " + new TextDecoder().decode(status.stderr)) +} + +async function run(cmd: string, ...args: string[]) { + return await new Deno.Command(cmd, { args, stderr: "piped", stdout: "piped" }).output() +} + +export async function link_todo(repo_path: string, todo_path: string) { + const status = await run("ln", "-sf", join(repo_path, "todo"), todo_path) + if (!status.success) warn("Failed to link : " + new TextDecoder().decode(status.stderr)) +} + +function warn(text: string) { + console.warn(crayon.yellow(text)) +} + +function decode(beuffeur: Uint8Array) { + return new TextDecoder().decode(beuffeur) +} + +export async function push_repo(repo_path: string) { + console.log(repo_path) + const status = await run("git", "-C", repo_path, "status", "--short") + if (!status.success) { + console.error("Failed to stat : " + decode(status.stderr)) + return + } + if (decode(status.stdout) === "") return + await run("git", "-C", repo_path, "add", "-A") + await run("git", "-C", repo_path, "commit", "-m", "update " + await hostname()) + await run("git", "-C", repo_path, "push") +} + +async function hostname() { + return (await Deno.readTextFile("/etc/hostname")).trim() +} diff --git a/src/service.ts b/src/service.ts index 92a31ab..7796d50 100755 --- a/src/service.ts +++ b/src/service.ts @@ -1,6 +1,22 @@ -#!/usr/bin/env -S deno run +#!/usr/bin/env -S deno run --allow-all + +import { join } from "https://deno.land/std@0.224.0/path/mod.ts" +import { read_conf } from "./lib/conf.ts" +import { Debouncer } from "./lib/debounce.ts" +import { fetch_repo, push_repo } from "./lib/repo.ts" +import { log } from "./lib/utils.ts" await main() async function main() { - // + const config = await read_conf() + await fetch_repo(config.repo_path, config.remote_url) + const debouncer = new Debouncer(200) + const repo_file = join(config.repo_path, "todo") + log("Watching file", repo_file) + for await (const _ of Deno.watchFs(repo_file)) { + if (debouncer.should_skip()) continue + log("Commiting event " + Date.now()) + await fetch_repo(config.repo_path, config.remote_url) + await push_repo(config.repo_path) + } } diff --git a/src/todo.ts b/src/todo.ts index 2cb7ac3..5e2ff60 100755 --- a/src/todo.ts +++ b/src/todo.ts @@ -2,11 +2,14 @@ import { read_conf } from "./lib/conf.ts" import { print_todo } from "./lib/display.ts" +import { fetch_repo, link_todo } from "./lib/repo.ts" import { read_todo } from "./lib/task.ts" await main() async function main() { const config = await read_conf() + await fetch_repo(config.repo_path, config.remote_url) + await link_todo(config.repo_path, config.file_path) const todo = await read_todo(config.file_path) print_todo(todo) } diff --git a/todo b/todo new file mode 100644 index 0000000..e69de29 From b0c07ef3a3d5f1945571a83f508b02932adb551e Mon Sep 17 00:00:00 2001 From: JOLIMAITRE Matthieu Date: Mon, 26 May 2025 23:10:11 +0200 Subject: [PATCH 2/2] Remove trash. --- todo | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 todo diff --git a/todo b/todo deleted file mode 100644 index e69de29..0000000