2019-10-19 01:51:38 +00:00
|
|
|
use super::{Context, Module, RootModuleConfig, SegmentConfig};
|
|
|
|
|
|
|
|
use crate::configs::nodejs::NodejsConfig;
|
2019-12-02 22:42:55 +00:00
|
|
|
use crate::utils;
|
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:
|
2020-06-21 08:33:58 +00:00
|
|
|
/// - Current directory contains a `.js`, `.mjs` or `.cjs` file
|
|
|
|
/// - Current directory contains a `.ts` file
|
2020-04-03 19:02:28 +00:00
|
|
|
/// - Current directory contains a `package.json` or `.node-version` 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
|
2019-09-14 14:23:53 +00:00
|
|
|
.try_begin_scan()?
|
2020-04-03 19:02:28 +00:00
|
|
|
.set_files(&["package.json", ".node-version"])
|
2020-06-21 08:33:58 +00:00
|
|
|
.set_extensions(&["js", "mjs", "cjs", "ts"])
|
2019-05-12 17:37:23 +00:00
|
|
|
.set_folders(&["node_modules"])
|
2019-09-14 14:23:53 +00:00
|
|
|
.is_match();
|
2019-05-12 17:37:23 +00:00
|
|
|
|
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-12-02 22:42:55 +00:00
|
|
|
let node_version = utils::exec_cmd("node", &["--version"])?.stdout;
|
2019-10-19 01:51:38 +00:00
|
|
|
|
2019-12-02 22:42:55 +00:00
|
|
|
let mut module = context.new_module("nodejs");
|
|
|
|
let config: NodejsConfig = NodejsConfig::try_load(module.config);
|
2019-04-10 13:22:11 +00:00
|
|
|
|
2019-12-02 22:42:55 +00:00
|
|
|
module.set_style(config.style);
|
2019-04-23 18:51:08 +00:00
|
|
|
|
2019-12-02 22:42:55 +00:00
|
|
|
let formatted_version = node_version.trim();
|
|
|
|
module.create_segment("symbol", &config.symbol);
|
|
|
|
module.create_segment("version", &SegmentConfig::new(formatted_version));
|
2019-04-10 13:22:11 +00:00
|
|
|
|
2019-12-02 22:42:55 +00:00
|
|
|
Some(module)
|
2019-04-23 18:51:08 +00:00
|
|
|
}
|
2020-01-26 22:37:18 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use crate::modules::utils::test::render_module;
|
|
|
|
use ansi_term::Color;
|
|
|
|
use std::fs::{self, File};
|
|
|
|
use std::io;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn folder_without_node_files() -> io::Result<()> {
|
|
|
|
let dir = tempfile::tempdir()?;
|
2020-04-28 08:53:30 +00:00
|
|
|
let actual = render_module("nodejs", dir.path(), None);
|
2020-01-26 22:37:18 +00:00
|
|
|
let expected = None;
|
|
|
|
assert_eq!(expected, actual);
|
2020-03-15 17:12:25 +00:00
|
|
|
dir.close()
|
2020-01-26 22:37:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn folder_with_package_json() -> io::Result<()> {
|
|
|
|
let dir = tempfile::tempdir()?;
|
|
|
|
File::create(dir.path().join("package.json"))?.sync_all()?;
|
2020-04-03 19:02:28 +00:00
|
|
|
|
2020-04-28 08:53:30 +00:00
|
|
|
let actual = render_module("nodejs", dir.path(), None);
|
2020-04-03 19:02:28 +00:00
|
|
|
let expected = Some(format!("via {} ", Color::Green.bold().paint("⬢ v12.0.0")));
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
dir.close()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn folder_with_node_version() -> io::Result<()> {
|
|
|
|
let dir = tempfile::tempdir()?;
|
|
|
|
File::create(dir.path().join(".node-version"))?.sync_all()?;
|
2020-01-26 22:37:18 +00:00
|
|
|
|
2020-04-28 08:53:30 +00:00
|
|
|
let actual = render_module("nodejs", dir.path(), None);
|
2020-01-26 22:37:18 +00:00
|
|
|
let expected = Some(format!("via {} ", Color::Green.bold().paint("⬢ v12.0.0")));
|
|
|
|
assert_eq!(expected, actual);
|
2020-03-15 17:12:25 +00:00
|
|
|
dir.close()
|
2020-01-26 22:37:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn folder_with_js_file() -> io::Result<()> {
|
|
|
|
let dir = tempfile::tempdir()?;
|
|
|
|
File::create(dir.path().join("index.js"))?.sync_all()?;
|
|
|
|
|
2020-04-28 08:53:30 +00:00
|
|
|
let actual = render_module("nodejs", dir.path(), None);
|
2020-01-26 22:37:18 +00:00
|
|
|
let expected = Some(format!("via {} ", Color::Green.bold().paint("⬢ v12.0.0")));
|
|
|
|
assert_eq!(expected, actual);
|
2020-03-15 17:12:25 +00:00
|
|
|
dir.close()
|
2020-01-26 22:37:18 +00:00
|
|
|
}
|
|
|
|
|
2020-06-21 08:33:58 +00:00
|
|
|
#[test]
|
|
|
|
fn folder_with_mjs_file() -> io::Result<()> {
|
|
|
|
let dir = tempfile::tempdir()?;
|
|
|
|
File::create(dir.path().join("index.mjs"))?.sync_all()?;
|
|
|
|
|
|
|
|
let actual = render_module("nodejs", dir.path(), None);
|
|
|
|
let expected = Some(format!("via {} ", Color::Green.bold().paint("⬢ v12.0.0")));
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
dir.close()
|
|
|
|
}
|
|
|
|
|
2020-06-21 15:59:00 +00:00
|
|
|
#[test]
|
2020-06-21 08:33:58 +00:00
|
|
|
fn folder_with_cjs_file() -> io::Result<()> {
|
|
|
|
let dir = tempfile::tempdir()?;
|
|
|
|
File::create(dir.path().join("index.cjs"))?.sync_all()?;
|
|
|
|
|
|
|
|
let actual = render_module("nodejs", dir.path(), None);
|
|
|
|
let expected = Some(format!("via {} ", Color::Green.bold().paint("⬢ v12.0.0")));
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
dir.close()
|
|
|
|
}
|
|
|
|
|
2020-06-21 15:59:00 +00:00
|
|
|
#[test]
|
2020-06-21 08:33:58 +00:00
|
|
|
fn folder_with_ts_file() -> io::Result<()> {
|
|
|
|
let dir = tempfile::tempdir()?;
|
|
|
|
File::create(dir.path().join("index.ts"))?.sync_all()?;
|
|
|
|
|
|
|
|
let actual = render_module("nodejs", dir.path(), None);
|
|
|
|
let expected = Some(format!("via {} ", Color::Green.bold().paint("⬢ v12.0.0")));
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
dir.close()
|
|
|
|
}
|
|
|
|
|
2020-01-26 22:37:18 +00:00
|
|
|
#[test]
|
|
|
|
fn folder_with_node_modules() -> io::Result<()> {
|
|
|
|
let dir = tempfile::tempdir()?;
|
|
|
|
let node_modules = dir.path().join("node_modules");
|
|
|
|
fs::create_dir_all(&node_modules)?;
|
|
|
|
|
2020-04-28 08:53:30 +00:00
|
|
|
let actual = render_module("nodejs", dir.path(), None);
|
2020-01-26 22:37:18 +00:00
|
|
|
let expected = Some(format!("via {} ", Color::Green.bold().paint("⬢ v12.0.0")));
|
|
|
|
assert_eq!(expected, actual);
|
2020-03-15 17:12:25 +00:00
|
|
|
dir.close()
|
2020-01-26 22:37:18 +00:00
|
|
|
}
|
|
|
|
}
|