2019-04-12 19:11:40 -04:00
|
|
|
use ansi_term::Color;
|
2019-04-11 19:31:30 -04:00
|
|
|
use std::process::Command;
|
2019-04-10 09:22:11 -04:00
|
|
|
|
2019-05-01 16:34:24 -04:00
|
|
|
use super::{Context, Module};
|
2019-04-19 16:57:14 -04:00
|
|
|
|
2019-07-19 16:18:52 -04:00
|
|
|
/// Creates a module with the current Node.js version
|
2019-04-12 17:49:20 -04:00
|
|
|
///
|
2019-04-11 20:04:04 -04:00
|
|
|
/// 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
|
2019-04-23 14:51:08 -04:00
|
|
|
/// - Current directory contains a `node_modules` directory
|
2019-07-02 16:12:53 -04:00
|
|
|
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
2019-05-12 13:37:23 -04:00
|
|
|
let is_js_project = context
|
|
|
|
|
.new_scan_dir()
|
|
|
|
|
.set_files(&["package.json"])
|
|
|
|
|
.set_extensions(&["js"])
|
|
|
|
|
.set_folders(&["node_modules"])
|
|
|
|
|
.scan();
|
|
|
|
|
|
2019-04-11 20:04:04 -04:00
|
|
|
if !is_js_project {
|
2019-04-12 23:06:48 -04:00
|
|
|
return None;
|
2019-04-11 19:31:30 -04:00
|
|
|
}
|
|
|
|
|
|
2019-04-23 14:51:08 -04:00
|
|
|
match get_node_version() {
|
|
|
|
|
Some(node_version) => {
|
2019-05-01 16:34:24 -04:00
|
|
|
const NODE_CHAR: &str = "⬢ ";
|
2019-04-23 14:51:08 -04:00
|
|
|
|
2019-08-13 12:30:59 -04:00
|
|
|
let mut module = context.new_module("nodejs")?;
|
2019-09-07 19:33:06 -05:00
|
|
|
let module_style = module
|
|
|
|
|
.config_value_style("style")
|
|
|
|
|
.unwrap_or_else(|| Color::Green.bold());
|
|
|
|
|
module.set_style(module_style);
|
2019-04-10 09:22:11 -04:00
|
|
|
|
2019-04-23 14:51:08 -04:00
|
|
|
let formatted_version = node_version.trim();
|
2019-05-01 16:34:24 -04:00
|
|
|
module.new_segment("symbol", NODE_CHAR);
|
|
|
|
|
module.new_segment("version", formatted_version);
|
2019-04-23 14:51:08 -04:00
|
|
|
|
2019-05-01 16:34:24 -04:00
|
|
|
Some(module)
|
2019-04-23 14:51:08 -04:00
|
|
|
}
|
|
|
|
|
None => None,
|
|
|
|
|
}
|
2019-04-10 09:22:11 -04:00
|
|
|
}
|
|
|
|
|
|
2019-04-23 14:51:08 -04:00
|
|
|
fn get_node_version() -> Option<String> {
|
|
|
|
|
match Command::new("node").arg("--version").output() {
|
|
|
|
|
Ok(output) => Some(String::from_utf8(output.stdout).unwrap()),
|
|
|
|
|
Err(_) => None,
|
|
|
|
|
}
|
|
|
|
|
}
|