mirror of
https://github.com/Llewellynvdm/starship.git
synced 2025-01-14 11:03:11 +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 |
|
| 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. |
|
| `symbol` | `"⬢ "` | A format string representing the symbol of NodeJS. |
|
||||||
| `style` | `"bold green"` | The style for the module. |
|
| `style` | `"bold green"` | The style for the module. |
|
||||||
| `disabled` | `false` | Disables the `nodejs` module. |
|
| `disabled` | `false` | Disables the `nodejs` module. |
|
||||||
|
@ -14,7 +14,7 @@ pub struct NodejsConfig<'a> {
|
|||||||
impl<'a> RootModuleConfig<'a> for NodejsConfig<'a> {
|
impl<'a> RootModuleConfig<'a> for NodejsConfig<'a> {
|
||||||
fn new() -> Self {
|
fn new() -> Self {
|
||||||
NodejsConfig {
|
NodejsConfig {
|
||||||
format: "via [$symbol$version]($style) ",
|
format: "via [$symbol($version )]($style)",
|
||||||
symbol: "⬢ ",
|
symbol: "⬢ ",
|
||||||
style: "bold green",
|
style: "bold green",
|
||||||
disabled: false,
|
disabled: false,
|
||||||
|
@ -4,10 +4,12 @@ use crate::configs::nodejs::NodejsConfig;
|
|||||||
use crate::formatter::StringFormatter;
|
use crate::formatter::StringFormatter;
|
||||||
use crate::utils;
|
use crate::utils;
|
||||||
|
|
||||||
|
use once_cell::sync::Lazy;
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
use semver::Version;
|
use semver::Version;
|
||||||
use semver::VersionReq;
|
use semver::VersionReq;
|
||||||
use serde_json as json;
|
use serde_json as json;
|
||||||
|
use std::ops::Deref;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
|
||||||
/// Creates a module with the current Node.js version
|
/// 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 mut module = context.new_module("nodejs");
|
||||||
let config = NodejsConfig::try_load(module.config);
|
let config = NodejsConfig::try_load(module.config);
|
||||||
let nodejs_version = utils::exec_cmd("node", &["--version"])?.stdout;
|
let nodejs_version =
|
||||||
let engines_version = get_engines_version(&context.current_dir);
|
Lazy::new(|| utils::exec_cmd("node", &["--version"]).map(|cmd| cmd.stdout));
|
||||||
let in_engines_range = check_engines_version(&nodejs_version, engines_version);
|
|
||||||
let parsed = StringFormatter::new(config.format).and_then(|formatter| {
|
let parsed = StringFormatter::new(config.format).and_then(|formatter| {
|
||||||
formatter
|
formatter
|
||||||
.map_meta(|var, _| match var {
|
.map_meta(|var, _| match var {
|
||||||
@ -47,6 +48,9 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
|||||||
})
|
})
|
||||||
.map_style(|variable| match variable {
|
.map_style(|variable| match variable {
|
||||||
"style" => {
|
"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 {
|
if in_engines_range {
|
||||||
Some(Ok(config.style))
|
Some(Ok(config.style))
|
||||||
} else {
|
} else {
|
||||||
@ -56,7 +60,11 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
|||||||
_ => None,
|
_ => None,
|
||||||
})
|
})
|
||||||
.map(|variable| match variable {
|
.map(|variable| match variable {
|
||||||
"version" => Some(Ok(nodejs_version.trim())),
|
"version" => nodejs_version
|
||||||
|
.deref()
|
||||||
|
.as_ref()
|
||||||
|
.map(|version| version.trim())
|
||||||
|
.map(Ok),
|
||||||
_ => None,
|
_ => None,
|
||||||
})
|
})
|
||||||
.parse(None)
|
.parse(None)
|
||||||
@ -125,7 +133,7 @@ mod tests {
|
|||||||
File::create(dir.path().join("package.json"))?.sync_all()?;
|
File::create(dir.path().join("package.json"))?.sync_all()?;
|
||||||
|
|
||||||
let actual = ModuleRenderer::new("nodejs").path(dir.path()).collect();
|
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);
|
assert_eq!(expected, actual);
|
||||||
dir.close()
|
dir.close()
|
||||||
}
|
}
|
||||||
@ -149,7 +157,7 @@ mod tests {
|
|||||||
File::create(dir.path().join(".node-version"))?.sync_all()?;
|
File::create(dir.path().join(".node-version"))?.sync_all()?;
|
||||||
|
|
||||||
let actual = ModuleRenderer::new("nodejs").path(dir.path()).collect();
|
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);
|
assert_eq!(expected, actual);
|
||||||
dir.close()
|
dir.close()
|
||||||
}
|
}
|
||||||
@ -160,7 +168,7 @@ mod tests {
|
|||||||
File::create(dir.path().join("index.js"))?.sync_all()?;
|
File::create(dir.path().join("index.js"))?.sync_all()?;
|
||||||
|
|
||||||
let actual = ModuleRenderer::new("nodejs").path(dir.path()).collect();
|
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);
|
assert_eq!(expected, actual);
|
||||||
dir.close()
|
dir.close()
|
||||||
}
|
}
|
||||||
@ -171,7 +179,7 @@ mod tests {
|
|||||||
File::create(dir.path().join("index.mjs"))?.sync_all()?;
|
File::create(dir.path().join("index.mjs"))?.sync_all()?;
|
||||||
|
|
||||||
let actual = ModuleRenderer::new("nodejs").path(dir.path()).collect();
|
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);
|
assert_eq!(expected, actual);
|
||||||
dir.close()
|
dir.close()
|
||||||
}
|
}
|
||||||
@ -182,7 +190,7 @@ mod tests {
|
|||||||
File::create(dir.path().join("index.cjs"))?.sync_all()?;
|
File::create(dir.path().join("index.cjs"))?.sync_all()?;
|
||||||
|
|
||||||
let actual = ModuleRenderer::new("nodejs").path(dir.path()).collect();
|
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);
|
assert_eq!(expected, actual);
|
||||||
dir.close()
|
dir.close()
|
||||||
}
|
}
|
||||||
@ -193,7 +201,7 @@ mod tests {
|
|||||||
File::create(dir.path().join("index.ts"))?.sync_all()?;
|
File::create(dir.path().join("index.ts"))?.sync_all()?;
|
||||||
|
|
||||||
let actual = ModuleRenderer::new("nodejs").path(dir.path()).collect();
|
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);
|
assert_eq!(expected, actual);
|
||||||
dir.close()
|
dir.close()
|
||||||
}
|
}
|
||||||
@ -205,7 +213,7 @@ mod tests {
|
|||||||
fs::create_dir_all(&node_modules)?;
|
fs::create_dir_all(&node_modules)?;
|
||||||
|
|
||||||
let actual = ModuleRenderer::new("nodejs").path(dir.path()).collect();
|
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);
|
assert_eq!(expected, actual);
|
||||||
dir.close()
|
dir.close()
|
||||||
}
|
}
|
||||||
@ -224,7 +232,7 @@ mod tests {
|
|||||||
file.sync_all()?;
|
file.sync_all()?;
|
||||||
|
|
||||||
let actual = ModuleRenderer::new("nodejs").path(dir.path()).collect();
|
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);
|
assert_eq!(expected, actual);
|
||||||
dir.close()
|
dir.close()
|
||||||
}
|
}
|
||||||
@ -243,7 +251,7 @@ mod tests {
|
|||||||
file.sync_all()?;
|
file.sync_all()?;
|
||||||
|
|
||||||
let actual = ModuleRenderer::new("nodejs").path(dir.path()).collect();
|
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);
|
assert_eq!(expected, actual);
|
||||||
dir.close()
|
dir.close()
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user