2022-03-26 09:42:19 +00:00
|
|
|
use super::{Context, Module, ModuleConfig};
|
2019-10-19 01:51:38 +00:00
|
|
|
|
|
|
|
use crate::configs::nodejs::NodejsConfig;
|
2021-04-08 22:37:54 +00:00
|
|
|
use crate::formatter::{StringFormatter, VersionFormatter};
|
2019-04-19 20:57:14 +00:00
|
|
|
|
2021-01-20 18:03:48 +00:00
|
|
|
use once_cell::sync::Lazy;
|
2020-12-05 11:25:48 +00:00
|
|
|
use regex::Regex;
|
|
|
|
use semver::Version;
|
|
|
|
use semver::VersionReq;
|
|
|
|
use serde_json as json;
|
2021-01-20 18:03:48 +00:00
|
|
|
use std::ops::Deref;
|
2020-12-05 11:25:48 +00:00
|
|
|
|
2019-07-19 20:18:52 +00:00
|
|
|
/// Creates a module with the current Node.js version
|
2019-07-02 20:12:53 +00:00
|
|
|
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
2021-02-21 12:22:40 +00:00
|
|
|
let mut module = context.new_module("nodejs");
|
|
|
|
let config = NodejsConfig::try_load(module.config);
|
2019-05-12 17:37:23 +00:00
|
|
|
let is_js_project = context
|
2019-09-14 14:23:53 +00:00
|
|
|
.try_begin_scan()?
|
2021-02-21 12:22:40 +00:00
|
|
|
.set_files(&config.detect_files)
|
|
|
|
.set_extensions(&config.detect_extensions)
|
|
|
|
.set_folders(&config.detect_folders)
|
2019-09-14 14:23:53 +00:00
|
|
|
.is_match();
|
2019-05-12 17:37:23 +00:00
|
|
|
|
2020-07-05 17:20:11 +00:00
|
|
|
let is_esy_project = context
|
|
|
|
.try_begin_scan()?
|
|
|
|
.set_folders(&["esy.lock"])
|
|
|
|
.is_match();
|
|
|
|
|
|
|
|
if !is_js_project || is_esy_project {
|
2019-04-13 03:06:48 +00:00
|
|
|
return None;
|
2019-04-11 23:31:30 +00:00
|
|
|
}
|
|
|
|
|
2021-02-11 20:34:47 +00:00
|
|
|
let nodejs_version = Lazy::new(|| {
|
|
|
|
context
|
|
|
|
.exec_cmd("node", &["--version"])
|
|
|
|
.map(|cmd| cmd.stdout)
|
|
|
|
});
|
2020-07-07 22:45:32 +00:00
|
|
|
let parsed = StringFormatter::new(config.format).and_then(|formatter| {
|
|
|
|
formatter
|
|
|
|
.map_meta(|var, _| match var {
|
|
|
|
"symbol" => Some(config.symbol),
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.map_style(|variable| match variable {
|
2020-12-05 11:25:48 +00:00
|
|
|
"style" => {
|
2022-04-25 14:18:01 +00:00
|
|
|
let engines_version = get_engines_version(context);
|
2021-01-20 18:03:48 +00:00
|
|
|
let in_engines_range =
|
2023-01-10 22:00:09 +00:00
|
|
|
check_engines_version(nodejs_version.as_deref(), engines_version);
|
2020-12-05 11:25:48 +00:00
|
|
|
if in_engines_range {
|
|
|
|
Some(Ok(config.style))
|
|
|
|
} else {
|
|
|
|
Some(Ok(config.not_capable_style))
|
|
|
|
}
|
|
|
|
}
|
2020-07-07 22:45:32 +00:00
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.map(|variable| match variable {
|
2021-08-25 15:41:10 +00:00
|
|
|
"version" => {
|
|
|
|
let version = nodejs_version
|
|
|
|
.deref()
|
|
|
|
.as_ref()?
|
|
|
|
.trim_start_matches('v')
|
|
|
|
.trim();
|
|
|
|
|
|
|
|
VersionFormatter::format_module_version(
|
|
|
|
module.get_name(),
|
|
|
|
version,
|
|
|
|
config.version_format,
|
|
|
|
)
|
|
|
|
.map(Ok)
|
|
|
|
}
|
2020-07-07 22:45:32 +00:00
|
|
|
_ => None,
|
|
|
|
})
|
2021-11-01 21:18:45 +00:00
|
|
|
.parse(None, Some(context))
|
2020-07-07 22:45:32 +00:00
|
|
|
});
|
2019-04-23 18:51:08 +00:00
|
|
|
|
2020-07-07 22:45:32 +00:00
|
|
|
module.set_segments(match parsed {
|
|
|
|
Ok(segments) => segments,
|
|
|
|
Err(error) => {
|
|
|
|
log::warn!("Error in module `nodejs`:\n{}", error);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
});
|
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
|
|
|
|
2022-04-25 14:18:01 +00:00
|
|
|
fn get_engines_version(context: &Context) -> Option<String> {
|
|
|
|
let json_str = context.read_file_from_pwd("package.json")?;
|
2020-12-05 11:25:48 +00:00
|
|
|
let package_json: json::Value = json::from_str(&json_str).ok()?;
|
|
|
|
let raw_version = package_json.get("engines")?.get("node")?.as_str()?;
|
|
|
|
Some(raw_version.to_string())
|
|
|
|
}
|
|
|
|
|
2023-01-10 22:00:09 +00:00
|
|
|
fn check_engines_version(nodejs_version: Option<&str>, engines_version: Option<String>) -> bool {
|
|
|
|
let (nodejs_version, engines_version) = match (nodejs_version, engines_version) {
|
|
|
|
(Some(nv), Some(ev)) => (nv, ev),
|
|
|
|
_ => return true,
|
|
|
|
};
|
|
|
|
let r = match VersionReq::parse(&engines_version) {
|
2020-12-05 11:25:48 +00:00
|
|
|
Ok(r) => r,
|
|
|
|
Err(_e) => return true,
|
|
|
|
};
|
|
|
|
let re = Regex::new(r"\d+\.\d+\.\d+").unwrap();
|
|
|
|
let version = re
|
|
|
|
.captures(nodejs_version)
|
|
|
|
.unwrap()
|
|
|
|
.get(0)
|
|
|
|
.unwrap()
|
|
|
|
.as_str();
|
|
|
|
let v = match Version::parse(version) {
|
|
|
|
Ok(v) => v,
|
|
|
|
Err(_e) => return true,
|
|
|
|
};
|
|
|
|
r.matches(&v)
|
|
|
|
}
|
|
|
|
|
2020-01-26 22:37:18 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2020-08-07 19:13:12 +00:00
|
|
|
use crate::test::ModuleRenderer;
|
2022-09-04 16:44:54 +00:00
|
|
|
use nu_ansi_term::Color;
|
2020-01-26 22:37:18 +00:00
|
|
|
use std::fs::{self, File};
|
|
|
|
use std::io;
|
2020-12-05 11:25:48 +00:00
|
|
|
use std::io::Write;
|
2020-01-26 22:37:18 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn folder_without_node_files() -> io::Result<()> {
|
|
|
|
let dir = tempfile::tempdir()?;
|
2020-08-07 19:13:12 +00:00
|
|
|
let actual = ModuleRenderer::new("nodejs").path(dir.path()).collect();
|
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-08-07 19:13:12 +00:00
|
|
|
let actual = ModuleRenderer::new("nodejs").path(dir.path()).collect();
|
2022-01-30 09:28:00 +00:00
|
|
|
let expected = Some(format!("via {}", Color::Green.bold().paint(" v12.0.0 ")));
|
2020-04-03 19:02:28 +00:00
|
|
|
assert_eq!(expected, actual);
|
|
|
|
dir.close()
|
|
|
|
}
|
|
|
|
|
2020-07-05 17:20:11 +00:00
|
|
|
#[test]
|
|
|
|
fn folder_with_package_json_and_esy_lock() -> io::Result<()> {
|
|
|
|
let dir = tempfile::tempdir()?;
|
|
|
|
File::create(dir.path().join("package.json"))?.sync_all()?;
|
|
|
|
let esy_lock = dir.path().join("esy.lock");
|
2022-11-05 11:40:46 +00:00
|
|
|
fs::create_dir_all(esy_lock)?;
|
2020-07-05 17:20:11 +00:00
|
|
|
|
2020-08-07 19:13:12 +00:00
|
|
|
let actual = ModuleRenderer::new("nodejs").path(dir.path()).collect();
|
2020-07-05 17:20:11 +00:00
|
|
|
let expected = None;
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
dir.close()
|
|
|
|
}
|
|
|
|
|
2020-04-03 19:02:28 +00:00
|
|
|
#[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-08-07 19:13:12 +00:00
|
|
|
let actual = ModuleRenderer::new("nodejs").path(dir.path()).collect();
|
2022-01-30 09:28:00 +00:00
|
|
|
let expected = Some(format!("via {}", Color::Green.bold().paint(" v12.0.0 ")));
|
2020-01-26 22:37:18 +00:00
|
|
|
assert_eq!(expected, actual);
|
2020-03-15 17:12:25 +00:00
|
|
|
dir.close()
|
2020-01-26 22:37:18 +00:00
|
|
|
}
|
|
|
|
|
2021-04-22 16:08:12 +00:00
|
|
|
#[test]
|
|
|
|
fn folder_with_nvmrc() -> io::Result<()> {
|
|
|
|
let dir = tempfile::tempdir()?;
|
|
|
|
File::create(dir.path().join(".nvmrc"))?.sync_all()?;
|
|
|
|
|
|
|
|
let actual = ModuleRenderer::new("nodejs").path(dir.path()).collect();
|
2022-01-30 09:28:00 +00:00
|
|
|
let expected = Some(format!("via {}", Color::Green.bold().paint(" v12.0.0 ")));
|
2021-04-22 16:08:12 +00:00
|
|
|
assert_eq!(expected, actual);
|
|
|
|
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-08-07 19:13:12 +00:00
|
|
|
let actual = ModuleRenderer::new("nodejs").path(dir.path()).collect();
|
2022-01-30 09:28:00 +00:00
|
|
|
let expected = Some(format!("via {}", Color::Green.bold().paint(" v12.0.0 ")));
|
2020-01-26 22:37:18 +00:00
|
|
|
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()?;
|
|
|
|
|
2020-08-07 19:13:12 +00:00
|
|
|
let actual = ModuleRenderer::new("nodejs").path(dir.path()).collect();
|
2022-01-30 09:28:00 +00:00
|
|
|
let expected = Some(format!("via {}", Color::Green.bold().paint(" v12.0.0 ")));
|
2020-06-21 08:33:58 +00:00
|
|
|
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()?;
|
|
|
|
|
2020-08-07 19:13:12 +00:00
|
|
|
let actual = ModuleRenderer::new("nodejs").path(dir.path()).collect();
|
2022-01-30 09:28:00 +00:00
|
|
|
let expected = Some(format!("via {}", Color::Green.bold().paint(" v12.0.0 ")));
|
2020-06-21 08:33:58 +00:00
|
|
|
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()?;
|
|
|
|
|
2020-08-07 19:13:12 +00:00
|
|
|
let actual = ModuleRenderer::new("nodejs").path(dir.path()).collect();
|
2022-01-30 09:28:00 +00:00
|
|
|
let expected = Some(format!("via {}", Color::Green.bold().paint(" v12.0.0 ")));
|
2020-06-21 08:33:58 +00:00
|
|
|
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");
|
2022-11-05 11:40:46 +00:00
|
|
|
fs::create_dir_all(node_modules)?;
|
2020-01-26 22:37:18 +00:00
|
|
|
|
2020-08-07 19:13:12 +00:00
|
|
|
let actual = ModuleRenderer::new("nodejs").path(dir.path()).collect();
|
2022-01-30 09:28:00 +00:00
|
|
|
let expected = Some(format!("via {}", Color::Green.bold().paint(" v12.0.0 ")));
|
2020-01-26 22:37:18 +00:00
|
|
|
assert_eq!(expected, actual);
|
2020-03-15 17:12:25 +00:00
|
|
|
dir.close()
|
2020-01-26 22:37:18 +00:00
|
|
|
}
|
2020-12-05 11:25:48 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn engines_node_version_match() -> io::Result<()> {
|
|
|
|
let dir = tempfile::tempdir()?;
|
|
|
|
let mut file = File::create(dir.path().join("package.json"))?;
|
|
|
|
file.write_all(
|
|
|
|
b"{
|
|
|
|
\"engines\":{
|
|
|
|
\"node\":\">=12.0.0\"
|
|
|
|
}
|
|
|
|
}",
|
|
|
|
)?;
|
|
|
|
file.sync_all()?;
|
|
|
|
|
|
|
|
let actual = ModuleRenderer::new("nodejs").path(dir.path()).collect();
|
2022-01-30 09:28:00 +00:00
|
|
|
let expected = Some(format!("via {}", Color::Green.bold().paint(" v12.0.0 ")));
|
2020-12-05 11:25:48 +00:00
|
|
|
assert_eq!(expected, actual);
|
|
|
|
dir.close()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn engines_node_version_not_match() -> io::Result<()> {
|
|
|
|
let dir = tempfile::tempdir()?;
|
|
|
|
let mut file = File::create(dir.path().join("package.json"))?;
|
|
|
|
file.write_all(
|
|
|
|
b"{
|
|
|
|
\"engines\":{
|
|
|
|
\"node\":\"<12.0.0\"
|
|
|
|
}
|
|
|
|
}",
|
|
|
|
)?;
|
|
|
|
file.sync_all()?;
|
|
|
|
|
|
|
|
let actual = ModuleRenderer::new("nodejs").path(dir.path()).collect();
|
2022-01-30 09:28:00 +00:00
|
|
|
let expected = Some(format!("via {}", Color::Red.bold().paint(" v12.0.0 ")));
|
2020-12-05 11:25:48 +00:00
|
|
|
assert_eq!(expected, actual);
|
|
|
|
dir.close()
|
|
|
|
}
|
2023-01-10 22:00:09 +00:00
|
|
|
#[test]
|
|
|
|
fn no_node_installed() -> io::Result<()> {
|
|
|
|
let dir = tempfile::tempdir()?;
|
|
|
|
File::create(dir.path().join("index.js"))?.sync_all()?;
|
|
|
|
let actual = ModuleRenderer::new("nodejs")
|
|
|
|
.path(dir.path())
|
|
|
|
.cmd("node --version", None)
|
|
|
|
.collect();
|
|
|
|
let expected = Some(format!("via {}", Color::Green.bold().paint(" ")));
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
dir.close()
|
|
|
|
}
|
2020-01-26 22:37:18 +00:00
|
|
|
}
|