diff options
author | t-mangoe <m.takashi.raruba.101.112@gmail.com> | 2020-12-05 20:25:48 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-05 12:25:48 +0100 |
commit | 89588a73915d22b2b4f7d0d191ac162d37cdb966 (patch) | |
tree | 704f50920d91ad0484d46dcbac028784a53e4db3 /src | |
parent | 749245bda7521790cef72f1ecc824fb92c4b4449 (diff) |
feat(nodejs): check node engines version in package.json (#1847)
* check node engines version in package.json
* fix code, following review.
Diffstat (limited to 'src')
-rw-r--r-- | src/configs/nodejs.rs | 2 | ||||
-rw-r--r-- | src/modules/nodejs.rs | 84 |
2 files changed, 85 insertions, 1 deletions
diff --git a/src/configs/nodejs.rs b/src/configs/nodejs.rs index 355e2ff18..2867d535a 100644 --- a/src/configs/nodejs.rs +++ b/src/configs/nodejs.rs @@ -8,6 +8,7 @@ pub struct NodejsConfig<'a> { pub symbol: &'a str, pub style: &'a str, pub disabled: bool, + pub not_capable_style: &'a str, } impl<'a> RootModuleConfig<'a> for NodejsConfig<'a> { @@ -17,6 +18,7 @@ impl<'a> RootModuleConfig<'a> for NodejsConfig<'a> { symbol: "⬢ ", style: "bold green", disabled: false, + not_capable_style: "bold red", } } } diff --git a/src/modules/nodejs.rs b/src/modules/nodejs.rs index 1a3d087be..7c5c13026 100644 --- a/src/modules/nodejs.rs +++ b/src/modules/nodejs.rs @@ -4,6 +4,12 @@ use crate::configs::nodejs::NodejsConfig; use crate::formatter::StringFormatter; use crate::utils; +use regex::Regex; +use semver::Version; +use semver::VersionReq; +use serde_json as json; +use std::path::PathBuf; + /// Creates a module with the current Node.js version /// /// Will display the Node.js version if any of the following criteria are met: @@ -31,6 +37,8 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> { let mut module = context.new_module("nodejs"); let config = NodejsConfig::try_load(module.config); let nodejs_version = utils::exec_cmd("node", &["--version"])?.stdout; + let engines_version = get_engines_version(&context.current_dir); + let in_engines_range = check_engines_version(&nodejs_version, engines_version); let parsed = StringFormatter::new(config.format).and_then(|formatter| { formatter .map_meta(|var, _| match var { @@ -38,7 +46,13 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> { _ => None, }) .map_style(|variable| match variable { - "style" => Some(Ok(config.style)), + "style" => { + if in_engines_range { + Some(Ok(config.style)) + } else { + Some(Ok(config.not_capable_style)) + } + } _ => None, }) .map(|variable| match variable { @@ -59,12 +73,42 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> { Some(module) } +fn get_engines_version(base_dir: &PathBuf) -> Option<String> { + let json_str = utils::read_file(base_dir.join("package.json")).ok()?; + let package_json: json::Value = json::from_str(&json_str).ok()?; + let raw_version = package_json.get("engines")?.get("node")?.as_str()?; + Some(raw_version.to_string()) +} + +fn check_engines_version(nodejs_version: &str, engines_version: Option<String>) -> bool { + if engines_version.is_none() { + return true; + } + let r = match VersionReq::parse(&engines_version.unwrap()) { + Ok(r) => r, + Err(_e) => return true, + }; + let re = Regex::new(r"\d+\.\d+\.\d+").unwrap(); + let version = re + .captures(nodejs_version) + .unwrap() + .get(0) + .unwrap() + .as_str(); + let v = match Version::parse(version) { + Ok(v) => v, + Err(_e) => return true, + }; + r.matches(&v) +} + #[cfg(test)] mod tests { use crate::test::ModuleRenderer; use ansi_term::Color; use std::fs::{self, File}; use std::io; + use std::io::Write; #[test] fn folder_without_node_files() -> io::Result<()> { @@ -165,4 +209,42 @@ mod tests { assert_eq!(expected, actual); dir.close() } + + #[test] + fn engines_node_version_match() -> io::Result<()> { + let dir = tempfile::tempdir()?; + let mut file = File::create(dir.path().join("package.json"))?; + file.write_all( + b"{ + \"engines\":{ + \"node\":\">=12.0.0\" + } + }", + )?; + file.sync_all()?; + + let actual = ModuleRenderer::new("nodejs").path(dir.path()).collect(); + let expected = Some(format!("via {} ", Color::Green.bold().paint("⬢ v12.0.0"))); + assert_eq!(expected, actual); + dir.close() + } + + #[test] + fn engines_node_version_not_match() -> io::Result<()> { + let dir = tempfile::tempdir()?; + let mut file = File::create(dir.path().join("package.json"))?; + file.write_all( + b"{ + \"engines\":{ + \"node\":\"<12.0.0\" + } + }", + )?; + file.sync_all()?; + + let actual = ModuleRenderer::new("nodejs").path(dir.path()).collect(); + let expected = Some(format!("via {} ", Color::Red.bold().paint("⬢ v12.0.0"))); + assert_eq!(expected, actual); + dir.close() + } } |