diff options
author | Matan Kushner <hello@matchai.me> | 2019-10-04 22:30:46 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-10-04 22:30:46 +0900 |
commit | 05210b9510b797f7738d5b2d51e8a6877f2d5283 (patch) | |
tree | 7399401dba9373f61035dbbd055f4137cd20f705 /src/modules/nodejs.rs | |
parent | e90a3768da7882db092b38d141cf8e19fabbee56 (diff) |
refactor: Go from Rust workspaces to a package with nested packages (#480)
Diffstat (limited to 'src/modules/nodejs.rs')
-rw-r--r-- | src/modules/nodejs.rs | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/src/modules/nodejs.rs b/src/modules/nodejs.rs new file mode 100644 index 000000000..b30f2f531 --- /dev/null +++ b/src/modules/nodejs.rs @@ -0,0 +1,49 @@ +use ansi_term::Color; +use std::process::Command; + +use super::{Context, Module}; + +/// Creates a module with the current Node.js version +/// +/// Will display the Node.js version if any of the following criteria are met: +/// - Current directory contains a `.js` file +/// - Current directory contains a `package.json` file +/// - Current directory contains a `node_modules` directory +pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> { + let is_js_project = context + .try_begin_scan()? + .set_files(&["package.json"]) + .set_extensions(&["js"]) + .set_folders(&["node_modules"]) + .is_match(); + + if !is_js_project { + return None; + } + + match get_node_version() { + Some(node_version) => { + const NODE_CHAR: &str = "⬢ "; + + let mut module = context.new_module("nodejs"); + let module_style = module + .config_value_style("style") + .unwrap_or_else(|| Color::Green.bold()); + module.set_style(module_style); + + let formatted_version = node_version.trim(); + module.new_segment("symbol", NODE_CHAR); + module.new_segment("version", formatted_version); + + Some(module) + } + None => None, + } +} + +fn get_node_version() -> Option<String> { + match Command::new("node").arg("--version").output() { + Ok(output) => Some(String::from_utf8(output.stdout).unwrap()), + Err(_) => None, + } +} |