Skip to content

Commit 8a5bd7a

Browse files
committed
feat: 增加对环境的判断
1 parent a5d3b09 commit 8a5bd7a

6 files changed

Lines changed: 105 additions & 2 deletions

File tree

index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55

66
export function validateConfig(configStr: string): void
77
export function validatePackage(appPath: string, nodeModulesPath: string, cliVersion: string): void
8+
export function validateEnv(): void

index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,8 @@ if (!nativeBinding) {
252252
throw new Error(`Failed to load native binding`)
253253
}
254254

255-
const { validateConfig, validatePackage } = nativeBinding
255+
const { validateConfig, validatePackage, validateEnv } = nativeBinding
256256

257257
module.exports.validateConfig = validateConfig
258258
module.exports.validatePackage = validatePackage
259+
module.exports.validateEnv = validateEnv

src/lib.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ mod validators;
44

55
use std::{ fs, error::Error, path::PathBuf, env };
66

7+
use validators::env::EnvValidator;
8+
79
use crate::validators::{ message::{ Message, self }, package::{ PackageValidator }, config::ConfigValidator, common::{ Validator } };
810

911
#[macro_use]
@@ -25,6 +27,14 @@ pub fn validate_package(app_path: String, node_modules_path: String, cli_version
2527
}
2628
}
2729

30+
#[napi]
31+
pub fn validate_env() {
32+
let result = validate_env_core();
33+
if let Err(e) = result {
34+
println!("{}", Message { kind: message::MessageKind::Error, content: e.to_string() });
35+
}
36+
}
37+
2838
fn validate_config_core(config_str: String) -> Result<(), Box<dyn Error>> {
2939
let tip = Message {
3040
kind: validators::message::MessageKind::Info,
@@ -84,3 +94,12 @@ fn validate_package_core(app_path: String, node_modules_path: String, cli_versio
8494
}
8595
Ok(())
8696
}
97+
98+
fn validate_env_core() -> Result<(), Box<dyn Error>> {
99+
let env_validator = EnvValidator::build();
100+
let messages = env_validator.validate();
101+
for message in messages {
102+
println!("{}", message);
103+
}
104+
Ok(())
105+
}

src/validators/common.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{error::Error, path::PathBuf, fs};
1+
use std::{error::Error, path::PathBuf, fs, cmp::Ordering};
22

33
use serde_json::{from_str, Value};
44

@@ -44,3 +44,21 @@ pub fn get_package_info(node_modules_path: &str, name: &str) -> Result<PackageIn
4444
}
4545
}
4646
}
47+
48+
pub fn compare_versions(a: &str, b: &str) -> Option<Ordering> {
49+
let parts1: Vec<u64> = a.split('.').filter_map(|s| s.parse().ok()).collect();
50+
let parts2: Vec<u64> = b.split('.').filter_map(|s| s.parse().ok()).collect();
51+
for (p1, p2) in parts1.iter().zip(parts2.iter()) {
52+
match p1.cmp(p2) {
53+
Ordering::Less => return Some(Ordering::Less),
54+
Ordering::Greater => return Some(Ordering::Greater),
55+
Ordering::Equal => continue,
56+
}
57+
}
58+
59+
match parts1.len().cmp(&parts2.len()) {
60+
Ordering::Less => Some(Ordering::Less),
61+
Ordering::Greater => Some(Ordering::Greater),
62+
Ordering::Equal => Some(Ordering::Equal),
63+
}
64+
}

src/validators/env.rs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
use std::{ process::Command, cmp::Ordering };
2+
3+
use super::{ common::{Validator, compare_versions}, message::{ Message, MessageKind } };
4+
5+
pub struct EnvValidator {
6+
7+
}
8+
9+
impl EnvValidator {
10+
pub fn build() -> Self {
11+
Self { }
12+
}
13+
}
14+
15+
impl Validator for EnvValidator {
16+
fn validate(&self) -> Vec<Message> {
17+
let mut messgaes = vec![];
18+
// 获取当前 node 版本
19+
let output = Command::new("node")
20+
.arg("--version")
21+
.output();
22+
let message = match output {
23+
Ok(output) => {
24+
if output.status.success() {
25+
let version = String::from_utf8_lossy(&output.stdout);
26+
if let Some(ordering) = compare_versions(version.as_ref().replace("v", "").replace("\n", "").as_str(), "14.0.0") {
27+
if ordering == Ordering::Greater || ordering == Ordering::Equal {
28+
Message {
29+
kind: MessageKind::Success,
30+
content: format!("安装的 Node 版本为 {}", version)
31+
}
32+
} else {
33+
Message {
34+
kind: MessageKind::Error,
35+
content: format!("安装的 Node 版本为 {},小于最低要求 Node 版本 14.0.0,请安装正确的 Node 版本,推荐使用 nvm(https://github.com/nvm-sh/nvm) 来管理 Node 版本", version)
36+
}
37+
}
38+
} else {
39+
Message {
40+
kind: MessageKind::Success,
41+
content: format!("安装的 Node 版本为 {}", version)
42+
}
43+
}
44+
} else {
45+
Message {
46+
kind: MessageKind::Error,
47+
content: format!("获取 Node 版本失败,请查看是否正确安装 Node")
48+
}
49+
}
50+
},
51+
Err(_) => {
52+
Message {
53+
kind: MessageKind::Error,
54+
content: format!("获取 Node 版本失败,请查看是否正确安装 Node")
55+
}
56+
}
57+
};
58+
59+
messgaes.push(message);
60+
61+
messgaes
62+
}
63+
}

src/validators/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ pub(crate) mod config;
22
pub(crate) mod message;
33
pub(crate) mod common;
44
pub(crate) mod package;
5+
pub(crate) mod env;

0 commit comments

Comments
 (0)