browserslist/queries/
current_node.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
use super::{Distrib, QueryResult};
use crate::error::Error;

pub(super) fn current_node() -> QueryResult {
    #[cfg(target_arch = "wasm32")]
    {
        use js_sys::{global, Reflect};

        let obj_process = Reflect::get(&global(), &"process".into())
            .map_err(|_| Error::UnsupportedCurrentNode)?;
        let obj_versions = Reflect::get(&obj_process, &"versions".into())
            .map_err(|_| Error::UnsupportedCurrentNode)?;
        let version = Reflect::get(&obj_versions, &"node".into())
            .map_err(|_| Error::UnsupportedCurrentNode)?
            .as_string()
            .ok_or(Error::UnsupportedCurrentNode)?;
        Ok(vec![Distrib::new("node", version)])
    }

    #[cfg(not(target_arch = "wasm32"))]
    {
        use std::process::Command;

        let output = Command::new("node")
            .arg("-v")
            .output()
            .map_err(|_| Error::UnsupportedCurrentNode)?;
        let version = String::from_utf8_lossy(&output.stdout)
            .trim()
            .trim_start_matches('v')
            .to_owned();

        Ok(vec![Distrib::new("node", version)])
    }
}

#[cfg(test)]
mod tests {
    use crate::{opts::Opts, test::run_compare};
    use test_case::test_case;

    #[test_case("current node"; "basic")]
    #[test_case("Current Node"; "case insensitive")]
    #[test_case("current      node"; "more spaces")]
    fn valid(query: &str) {
        run_compare(query, &Opts::new());
    }
}