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+ }
0 commit comments