ines-core 0.0.2

Rust bundler/framework for website with WebComponent
Documentation
use crate::project::builder::error::Result;
use std::ffi::OsStr;

use std::path::PathBuf;

pub struct Component {
    js: PathBuf,
    html: PathBuf,
}

impl Component {
    pub fn new(js: PathBuf, html: PathBuf) -> Self {
        Self { js, html }
    }

    pub fn name(&self) -> String {
        let name = self.js.with_extension("");
        let parts = name
            .file_name()
            .unwrap_or(OsStr::new(""))
            .to_str()
            .unwrap_or("")
            .split("-")
            .collect::<Vec<_>>();
        parts
            .into_iter()
            .map(|part| {
                let mut part = part.to_string();
                part.get_mut(0..1).map(|e| {
                    e.make_ascii_uppercase();
                    &*e
                });
                part
            })
            .collect::<Vec<_>>()
            .join("")
    }

    pub fn filename(&self) -> String {
        self.js
            .file_name()
            .unwrap_or(OsStr::new(""))
            .to_str()
            .unwrap_or("")
            .to_string()
    }

    pub fn get_hash(&self) -> String {
        sha256::digest(self.js.with_extension("").to_str().unwrap_or(""))
    }

    pub fn get_base(&self) -> PathBuf {
        self.js.parent().unwrap().to_path_buf()
    }

    pub fn build_js(&self) -> Result<String> {
        let content = std::fs::read_to_string(&self.js)?;
        let result = content.replace("super()", &format!("super('{}')", self.get_hash()));

        Ok(result)
    }

    pub fn build_template(&self) -> Result<String> {
        let content = std::fs::read_to_string(&self.html)?;

        Ok(format!(
            r#"<template id="{}">{content}</template>"#,
            self.get_hash()
        ))
    }
}