From b5fd517972ffc3d0b2c108b0d3cc4218e10ce648 Mon Sep 17 00:00:00 2001 From: Moritz Vetter Date: Wed, 20 Jan 2021 19:03:48 +0100 Subject: [PATCH] perf(nodejs): evaluate nodejs format string lazily (#2160) * fix(nodejs): update format string * test(nodejs): adjust tests to new format strings * fix(nodejs): use once_cell's Lazy to implement hassleless lazy execution * chore(nodejs): run rustfmt Co-authored-by: Moritz Vetter --- docs/config/README.md | 2 +- src/configs/nodejs.rs | 2 +- src/modules/nodejs.rs | 34 +++++++++++++++++++++------------- 3 files changed, 23 insertions(+), 15 deletions(-) diff --git a/docs/config/README.md b/docs/config/README.md index 7cd9dcf8..db507b82 100644 --- a/docs/config/README.md +++ b/docs/config/README.md @@ -1760,7 +1760,7 @@ The module will be shown if any of the following conditions are met: | Option | Default | Description | | ---------- | ---------------------------------- | -------------------------------------------------- | -| `format` | `"via [$symbol$version]($style) "` | The format for the module. | +| `format` | `"via [$symbol($version )]($style)"` | The format for the module. | | `symbol` | `"⬢ "` | A format string representing the symbol of NodeJS. | | `style` | `"bold green"` | The style for the module. | | `disabled` | `false` | Disables the `nodejs` module. | diff --git a/src/configs/nodejs.rs b/src/configs/nodejs.rs index 2867d535..96972459 100644 --- a/src/configs/nodejs.rs +++ b/src/configs/nodejs.rs @@ -14,7 +14,7 @@ pub struct NodejsConfig<'a> { impl<'a> RootModuleConfig<'a> for NodejsConfig<'a> { fn new() -> Self { NodejsConfig { - format: "via [$symbol$version]($style) ", + format: "via [$symbol($version )]($style)", symbol: "⬢ ", style: "bold green", disabled: false, diff --git a/src/modules/nodejs.rs b/src/modules/nodejs.rs index e5c59f6c..137857ef 100644 --- a/src/modules/nodejs.rs +++ b/src/modules/nodejs.rs @@ -4,10 +4,12 @@ use crate::configs::nodejs::NodejsConfig; use crate::formatter::StringFormatter; use crate::utils; +use once_cell::sync::Lazy; use regex::Regex; use semver::Version; use semver::VersionReq; use serde_json as json; +use std::ops::Deref; use std::path::Path; /// Creates a module with the current Node.js version @@ -36,9 +38,8 @@ pub fn module<'a>(context: &'a Context) -> Option> { 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 nodejs_version = + Lazy::new(|| utils::exec_cmd("node", &["--version"]).map(|cmd| cmd.stdout)); let parsed = StringFormatter::new(config.format).and_then(|formatter| { formatter .map_meta(|var, _| match var { @@ -47,6 +48,9 @@ pub fn module<'a>(context: &'a Context) -> Option> { }) .map_style(|variable| match variable { "style" => { + let engines_version = get_engines_version(&context.current_dir); + let in_engines_range = + check_engines_version(nodejs_version.deref().as_ref()?, engines_version); if in_engines_range { Some(Ok(config.style)) } else { @@ -56,7 +60,11 @@ pub fn module<'a>(context: &'a Context) -> Option> { _ => None, }) .map(|variable| match variable { - "version" => Some(Ok(nodejs_version.trim())), + "version" => nodejs_version + .deref() + .as_ref() + .map(|version| version.trim()) + .map(Ok), _ => None, }) .parse(None) @@ -125,7 +133,7 @@ mod tests { File::create(dir.path().join("package.json"))?.sync_all()?; let actual = ModuleRenderer::new("nodejs").path(dir.path()).collect(); - let expected = Some(format!("via {} ", Color::Green.bold().paint("⬢ v12.0.0"))); + let expected = Some(format!("via {}", Color::Green.bold().paint("⬢ v12.0.0 "))); assert_eq!(expected, actual); dir.close() } @@ -149,7 +157,7 @@ mod tests { File::create(dir.path().join(".node-version"))?.sync_all()?; let actual = ModuleRenderer::new("nodejs").path(dir.path()).collect(); - let expected = Some(format!("via {} ", Color::Green.bold().paint("⬢ v12.0.0"))); + let expected = Some(format!("via {}", Color::Green.bold().paint("⬢ v12.0.0 "))); assert_eq!(expected, actual); dir.close() } @@ -160,7 +168,7 @@ mod tests { File::create(dir.path().join("index.js"))?.sync_all()?; let actual = ModuleRenderer::new("nodejs").path(dir.path()).collect(); - let expected = Some(format!("via {} ", Color::Green.bold().paint("⬢ v12.0.0"))); + let expected = Some(format!("via {}", Color::Green.bold().paint("⬢ v12.0.0 "))); assert_eq!(expected, actual); dir.close() } @@ -171,7 +179,7 @@ mod tests { File::create(dir.path().join("index.mjs"))?.sync_all()?; let actual = ModuleRenderer::new("nodejs").path(dir.path()).collect(); - let expected = Some(format!("via {} ", Color::Green.bold().paint("⬢ v12.0.0"))); + let expected = Some(format!("via {}", Color::Green.bold().paint("⬢ v12.0.0 "))); assert_eq!(expected, actual); dir.close() } @@ -182,7 +190,7 @@ mod tests { File::create(dir.path().join("index.cjs"))?.sync_all()?; let actual = ModuleRenderer::new("nodejs").path(dir.path()).collect(); - let expected = Some(format!("via {} ", Color::Green.bold().paint("⬢ v12.0.0"))); + let expected = Some(format!("via {}", Color::Green.bold().paint("⬢ v12.0.0 "))); assert_eq!(expected, actual); dir.close() } @@ -193,7 +201,7 @@ mod tests { File::create(dir.path().join("index.ts"))?.sync_all()?; let actual = ModuleRenderer::new("nodejs").path(dir.path()).collect(); - let expected = Some(format!("via {} ", Color::Green.bold().paint("⬢ v12.0.0"))); + let expected = Some(format!("via {}", Color::Green.bold().paint("⬢ v12.0.0 "))); assert_eq!(expected, actual); dir.close() } @@ -205,7 +213,7 @@ mod tests { fs::create_dir_all(&node_modules)?; let actual = ModuleRenderer::new("nodejs").path(dir.path()).collect(); - let expected = Some(format!("via {} ", Color::Green.bold().paint("⬢ v12.0.0"))); + let expected = Some(format!("via {}", Color::Green.bold().paint("⬢ v12.0.0 "))); assert_eq!(expected, actual); dir.close() } @@ -224,7 +232,7 @@ mod tests { file.sync_all()?; let actual = ModuleRenderer::new("nodejs").path(dir.path()).collect(); - let expected = Some(format!("via {} ", Color::Green.bold().paint("⬢ v12.0.0"))); + let expected = Some(format!("via {}", Color::Green.bold().paint("⬢ v12.0.0 "))); assert_eq!(expected, actual); dir.close() } @@ -243,7 +251,7 @@ mod tests { file.sync_all()?; let actual = ModuleRenderer::new("nodejs").path(dir.path()).collect(); - let expected = Some(format!("via {} ", Color::Red.bold().paint("⬢ v12.0.0"))); + let expected = Some(format!("via {}", Color::Red.bold().paint("⬢ v12.0.0 "))); assert_eq!(expected, actual); dir.close() }