hennzau

21 commits
Updated 2026-04-25 12:39:37
hnz/src
hnz/src/sync.rs
use std::path::PathBuf;

use crate::Help;

pub fn sync(help: Help) -> Result<(), std::io::Error> {
    let base_path = help.get_one::<PathBuf>("BASE_PATH").expect("required");

    let md_path = help.get_one::<PathBuf>("MD_PATH").expect("required");
    let json_path = help.get_one::<PathBuf>("JSON_PATH").expect("required");

    for entry in std::fs::read_dir(md_path)? {
        let entry = entry?;

        if entry.file_name() == "_index.md"
            || entry.file_name() == ".gitkeep"
            || !entry.file_type()?.is_file()
        {
            continue;
        }

        if !base_path
            .join(entry.path().file_stem().expect("file required"))
            .exists()
        {
            std::fs::remove_file(entry.path())?;
        }
    }

    for entry in std::fs::read_dir(json_path)? {
        let entry = entry?;

        if entry.file_name() == ".gitkeep" || !entry.file_type()?.is_file() {
            continue;
        }

        if !base_path
            .join(entry.path().file_stem().expect("file required"))
            .exists()
        {
            std::fs::remove_file(entry.path())?;
        }
    }

    Ok(())
}