darling_npm/
lib.rs

1use darling_api as darling;
2
3pub struct Npm;
4
5pub static PACKAGE_MANAGER: Npm = Npm;
6
7impl darling::PackageManager for Npm {
8    fn name(&self) -> String {
9        "npm".to_owned()
10    }
11
12    fn install(&self, _context: &darling::Context, package: &darling::InstallationEntry) -> anyhow::Result<()> {
13        std::process::Command::new("npm")
14            .arg("install")
15            .arg("-g")
16            .arg(&package.name)
17            .spawn()?
18            .wait()?;
19
20        Ok(())
21    }
22
23    fn uninstall(&self, _context: &darling::Context, package: &darling::InstallationEntry) -> anyhow::Result<()> {
24        std::process::Command::new("npm")
25            .arg("uninstall")
26            .arg("-g")
27            .arg(&package.name)
28            .spawn()?
29            .wait()?;
30
31        Ok(())
32    }
33
34    fn get_all_explicit(&self, _context: &darling::Context) -> anyhow::Result<Vec<(String, String)>> {
35        let output = String::from_utf8(
36            std::process::Command::new("npm")
37                .arg("list")
38                .arg("-g")
39                .arg("--depth")
40                .arg("0")
41                .output()?
42                .stdout,
43        )?;
44        Ok(output
45            .lines()
46            .filter_map(|line| {
47                regex_macro::regex!(r"(\S+)@(\S+)")
48                    .captures(line)
49                    .map(|captures| (captures[1].to_owned(), captures[2].to_owned()))
50            })
51            .collect::<Vec<_>>())
52    }
53}