11// 环境配置检查可以避免一些奇葩的报错
22
33const exec = require ( 'child_process' ) . exec ;
4+
5+ function getVersionNumber ( str , regex ) {
6+ const match = str . match ( regex ) ;
7+ return match ? match [ 1 ] : null ;
8+ }
9+
410module . exports = ( hexo ) => {
511 if ( ! hexo . checkEnvironment ) {
6- hexo . checkEnvironment = 1 ;
12+ hexo . checkEnvironment = 1 ;
713 hexo . log . info ( `Checking environment configuration...` ) ;
814
915 // Checking environment
1016 exec ( 'node -v' , ( err , stdout , stderr ) => {
1117 if ( err ) {
12- CheckError ( hexo , `node.js: ${ err } ` ) ;
18+ CheckError ( hexo , `node.js: ${ err } ` ) ;
1319 }
14- let nodeVersion = stdout . match ( / v ( \d * ) / ) [ 1 ] ;
15- if ( nodeVersion < 16 ) {
20+ const nodeVersion = getVersionNumber ( stdout , / v ( \d * ) / ) ;
21+ if ( nodeVersion < 16 ) {
1622 hexo . log . info ( `node.js 版本:${ stdout } ` ) ;
17- CheckError ( hexo , `node.js 版本过低,请升级至 v16.x 及以上版本!` ) ;
18- } else {
23+ CheckError ( hexo , `node.js 版本过低,请升级至 v16.x 及以上版本!` ) ;
24+ } else {
1925 exec ( 'hexo -v' , ( err , stdout , stderr ) => {
2026 if ( err ) {
21- CheckError ( hexo , `hexo-cli: ${ err } ` ) ;
27+ CheckError ( hexo , `hexo-cli: ${ err } ` ) ;
2228 }
23- let hexoVersion1 = stdout . match ( / h e x o : \s * ( \d * ) / ) [ 1 ] ;
24- let hexoVersion2 = stdout . match ( / h e x o : \s * \d * \. ( \d * ) / ) [ 1 ] ;
25- if ( hexoVersion1 < 5 || ( hexoVersion1 == 5 && hexoVersion2 < 4 ) ) {
29+ const hexoVersion1 = getVersionNumber ( stdout , / h e x o : \s * ( \d * ) / ) ;
30+ const hexoVersion2 = getVersionNumber ( stdout , / h e x o : \s * \d * \. ( \d * ) / ) ;
31+ if ( hexoVersion1 < 5 || ( hexoVersion1 === 5 && hexoVersion2 < 4 ) ) {
2632 hexo . log . info ( `hexo 版本:${ stdout } ` ) ;
27- CheckError ( hexo , `hexo 版本过低,请升级至 5.4 以上版本!` ) ;
28- } else {
29- let hexoClVersion1 = stdout . match ( / h e x o - c l i : \s * ( \d * ) / ) [ 1 ] ;
30- let hexoClVersion2 = stdout . match ( / h e x o - c l i : \s * \d * \. ( \d * ) / ) [ 1 ] ;
31- if ( hexoClVersion1 < 4 || ( hexoClVersion1 == 4 && hexoClVersion2 < 1 ) ) {
33+ CheckError ( hexo , `hexo 版本过低,请升级至 5.4 以上版本!` ) ;
34+ } else {
35+ const hexoClVersion1 = getVersionNumber ( stdout , / h e x o - c l i : \s * ( \d * ) / ) ;
36+ const hexoClVersion2 = getVersionNumber ( stdout , / h e x o - c l i : \s * \d * \. ( \d * ) / ) ;
37+ if ( hexoClVersion1 < 4 || ( hexoClVersion1 === 4 && hexoClVersion2 < 1 ) ) {
3238 hexo . log . info ( `hexo-cli 版本:${ stdout } ` ) ;
33- CheckError ( hexo , `hexo-cli 版本过低,请升级至 4.1 以上版本!` ) ;
34- } else {
39+ CheckError ( hexo , `hexo-cli 版本过低,请升级至 4.1 以上版本!` ) ;
40+ } else {
3541 // Checking configuration
3642 let checkConfiguration = require ( './check-configuration' ) ( hexo ) ;
37- if ( checkConfiguration !== true ) {
38- CheckConfError ( hexo , checkConfiguration ) ;
43+ if ( checkConfiguration !== true ) {
44+ CheckConfError ( hexo , checkConfiguration ) ;
3945 }
4046 }
4147 }
@@ -45,7 +51,7 @@ module.exports =(hexo) => {
4551 }
4652} ;
4753
48- function CheckError ( hexo , msg ) {
54+ function CheckError ( hexo , msg ) {
4955 hexo . log . error ( `
5056============================================================
5157环境配置检查失败!| Environment configuration check failed!
@@ -65,7 +71,7 @@ debug: env
6571 throw new Error ( '环境配置检查失败!| Environment configuration check failed!' ) ;
6672}
6773
68- function CheckConfError ( hexo , msg ) {
74+ function CheckConfError ( hexo , msg ) {
6975 hexo . log . error ( `
7076============================================================
7177配置文件检查失败!| Configuration check failed!
@@ -77,4 +83,4 @@ debug: env
7783关闭调试模式主题配置文件设置 debug: false
7884============================================================` ) ;
7985 throw new Error ( '配置文件检查失败!| Configuration check failed!' ) ;
80- }
86+ }
0 commit comments