nestrs-cli-rs 0.1.0

Rust port of the Nest CLI for the nestrs organization.
Documentation
//! Upstream source: `../nest-cli/lib/runners/schematic.runner.ts`.

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

use super::{ProcessRunner, Runner, RunnerKind};

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct SchematicRunner {
    inner: ProcessRunner,
}

pub const SCHEMATICS_CLI_RELATIVE_PATH: &str =
    "node_modules/@angular-devkit/schematics-cli/bin/schematics.js";

pub fn find_closest_schematics_binary(cwd: impl AsRef<Path>) -> Result<PathBuf, String> {
    for directory in cwd.as_ref().ancestors() {
        let candidate = directory.join(SCHEMATICS_CLI_RELATIVE_PATH);
        if candidate.is_file() {
            return Ok(candidate);
        }
    }

    Err(format!(
        "'schematics' binary path could not be found. Expected {SCHEMATICS_CLI_RELATIVE_PATH} in this project or one of its parent directories."
    ))
}

impl SchematicRunner {
    pub fn new(schematics_binary: impl AsRef<Path>) -> Self {
        Self {
            inner: ProcessRunner::schematic(schematics_binary),
        }
    }

    pub fn inner(&self) -> &ProcessRunner {
        &self.inner
    }

    pub fn into_inner(self) -> ProcessRunner {
        self.inner
    }
}

impl Runner for SchematicRunner {
    fn kind(&self) -> RunnerKind {
        self.inner.kind()
    }

    fn binary(&self) -> &str {
        self.inner.binary()
    }

    fn prefix_args(&self) -> &[String] {
        self.inner.prefix_args()
    }
}