mirror of
https://github.com/Llewellynvdm/starship.git
synced 2025-01-13 10:25:33 +00:00
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 <mv@3yourmind.com>
This commit is contained in:
parent
8302a3ccb4
commit
b5fd517972
@ -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. |
|
||||
|
@ -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,
|
||||
|
@ -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<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 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<Module<'a>> {
|
||||
})
|
||||
.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<Module<'a>> {
|
||||
_ => 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()
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user