nestrs-cli-rs 0.1.0

Rust port of the Nest CLI for the nestrs organization.
Documentation
//! Upstream source: `../nest-cli/lib/compiler/helpers/delete-out-dir.ts`.

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

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct DeleteOutDirPlan {
    pub out_dir: PathBuf,
    pub enabled: bool,
    pub ts_build_info_file: Option<PathBuf>,
}

pub fn delete_out_dir_if_enabled(plan: &DeleteOutDirPlan) -> std::io::Result<bool> {
    if !plan.enabled {
        return Ok(false);
    }
    remove_dir_all_force(&plan.out_dir)?;
    if let Some(ts_build_info_file) = &plan.ts_build_info_file {
        remove_file_force(ts_build_info_file)?;
    }
    Ok(true)
}

fn remove_dir_all_force(path: &Path) -> std::io::Result<()> {
    match fs::remove_dir_all(path) {
        Ok(()) => Ok(()),
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(()),
        Err(error) => Err(error),
    }
}

fn remove_file_force(path: &Path) -> std::io::Result<()> {
    match fs::remove_file(path) {
        Ok(()) => Ok(()),
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(()),
        Err(error) => Err(error),
    }
}