1
0
mirror of https://github.com/Llewellynvdm/starship.git synced 2024-11-16 10:05:13 +00:00
starship/src/modules/nodejs.rs
Thomas O'Donnell edc62f4518 refactor: Refactor modules to use the exec_cmd util (#676)
Have refactored the golang, java, nodejs, python, ruby and username
modules to use the new `exec_cmd` util.
2019-12-02 17:42:55 -05:00

37 lines
1.1 KiB
Rust

use super::{Context, Module, RootModuleConfig, SegmentConfig};
use crate::configs::nodejs::NodejsConfig;
use crate::utils;
/// 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;
}
let node_version = utils::exec_cmd("node", &["--version"])?.stdout;
let mut module = context.new_module("nodejs");
let config: NodejsConfig = NodejsConfig::try_load(module.config);
module.set_style(config.style);
let formatted_version = node_version.trim();
module.create_segment("symbol", &config.symbol);
module.create_segment("version", &SegmentConfig::new(formatted_version));
Some(module)
}