Skip to main content

torrust_linting/
utils.rs

1use std::process::Command;
2
3use anyhow::Result;
4use tracing::{error, info};
5
6/// Check if a command is available in the system PATH
7///
8/// # Errors
9///
10/// Returns false if the command is not found or if the check fails.
11#[must_use]
12pub fn is_command_available(command: &str) -> bool {
13    Command::new("which")
14        .arg(command)
15        .output()
16        .is_ok_and(|output| output.status.success())
17}
18
19/// Install a tool using npm globally
20///
21/// # Errors
22///
23/// Returns an error if npm is not available or if the installation fails.
24pub fn install_npm_tool(tool: &str) -> Result<()> {
25    info!("Installing {tool}...");
26
27    let output = Command::new("npm").args(["install", "-g", tool]).output()?;
28
29    if output.status.success() {
30        info!("{tool} installed successfully");
31        Ok(())
32    } else {
33        let stderr = String::from_utf8_lossy(&output.stderr);
34        error!("Failed to install {tool}: {stderr}");
35        Err(anyhow::anyhow!("Failed to install {tool}"))
36    }
37}