ines-core 0.0.2

Rust bundler/framework for website with WebComponent
Documentation
mod component;
pub mod error;

use error::Result;
use std::ffi::OsStr;

use crate::project::builder::component::Component;
use crate::project::include::REDIRECT_HTML;
use std::path::PathBuf;

pub fn scan_components(path: PathBuf) -> Result<Vec<Component>> {
    let dir = std::fs::read_dir(&path)?;
    let mut components = vec![];

    for entry in dir.flatten() {
        if entry.file_type()?.is_file() {
            if let Some("html") = entry.path().extension().and_then(OsStr::to_str) {
                if std::fs::exists(entry.path().with_extension("js"))? {
                    components.push(Component::new(
                        entry.path().with_extension("js"),
                        entry.path(),
                    ));
                }
            }
        } else if entry.file_type()?.is_dir() {
            components.extend(scan_components(entry.path())?);
        }
    }

    Ok(components)
}

pub fn build_html(index: PathBuf, html: String) -> Result<String> {
    let index = std::fs::read_to_string(&index)?;
    Ok(index.replace("#![ROOT]", &html))
}

pub fn copy(src: PathBuf, dest: PathBuf) -> Result<()> {
    if src.is_dir() {
        std::fs::create_dir_all(&dest)?;
        for entry in std::fs::read_dir(&src)?.flatten() {
            copy(src.join(entry.file_name()), dest.join(entry.file_name()))?;
        }
    } else {
        std::fs::copy(src, dest)?;
    }

    Ok(())
}

pub fn generate_redirect(path: &str) -> String {
    REDIRECT_HTML.replace("#![PATH]", path)
}