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