blade-cli 0.1.0

Fast, Node-free linter for Laravel Blade templates
//! Finding the templates to lint.

use std::path::{Path, PathBuf};

use ignore::WalkBuilder;

/// The files to lint: each path that's a file, plus every `*.blade.php` under each
/// directory, skipping hidden and `.gitignore`d paths. Sorted, without duplicates.
pub fn collect(paths: &[PathBuf]) -> Result<Vec<PathBuf>, String> {
    let mut files = Vec::new();
    for path in paths {
        if path.is_file() {
            files.push(path.clone());
        } else if path.is_dir() {
            for entry in WalkBuilder::new(path).require_git(false).build() {
                let entry = entry.map_err(|e| e.to_string())?;
                let is_blade = entry.file_name().to_string_lossy().ends_with(".blade.php");
                if is_blade && entry.file_type().is_some_and(|t| t.is_file()) {
                    files.push(entry.into_path());
                }
            }
        } else {
            return Err(format!("{}: no such file or directory", display(path)));
        }
    }
    files.sort();
    files.dedup();
    Ok(files)
}

/// A path as shown in output: `/` separators, no leading `./`.
pub fn display(path: &Path) -> String {
    let shown = path.to_string_lossy().replace('\\', "/");
    match shown.strip_prefix("./") {
        Some(rest) => rest.to_string(),
        None => shown,
    }
}