22
33const pathFn = require ( 'path' ) ;
44const fs = require ( 'hexo-fs' ) ;
5+ const chalk = require ( 'chalk' ) ;
56const semver = require ( 'semver' ) ;
6- const spawn = require ( 'cross-spawn' ) ;
77const Table = require ( 'easy-table' ) ;
8- const { pkgJson, Loading } = require ( '../utils/index' ) ;
8+ const spawn = require ( 'cross-spawn' ) ;
9+
10+ const {
11+ pkgJson,
12+ Loading,
13+ isUndef
14+ } = require ( '../utils/index' ) ;
915const pkg = require ( '../../package.json' ) ;
1016
1117class Upgrade {
@@ -44,10 +50,14 @@ class Upgrade {
4450 loading . success ( ) ;
4551 log . info ( `已经自动升级到feflow的最新版本${ ltsVersion } ` ) ;
4652 } else {
47- const err = `cli core全量更新失败,失败码为 ${ result . code } ,错误日志为 ${ result . data } ` ;
48- loading . fail ( err ) ;
53+ const err = `cli core全量更新失败,${ isUndef ( result . code ) ? '' : '失败码为' + result . code } ,错误日志如下:\n ${ result . data } ` ;
54+ loading . fail ( ) ;
4955 log . error ( err ) ;
5056 }
57+ } , function ( error ) {
58+ const err = `cli core全量更新失败,${ isUndef ( error . code ) ? '' : '失败码为' + error . code } ,错误日志如下:\n${ error . data } ` ;
59+ loading . fail ( ) ;
60+ log . error ( err ) ;
5161 } ) ;
5262 } else {
5363 log . debug ( `当前安装的版本 ${ version } 和最新版本 ${ ltsVersion } 兼容` ) ;
@@ -162,9 +172,12 @@ class Upgrade {
162172 execNpmCommand ( cmd , modules , isGlobal , where ) {
163173 const { registry, proxy} = this . ctx . config ;
164174 const log = this . ctx . log ;
175+ const failedTimeout = 60 * 1000 ;
165176
166177 return new Promise ( ( resolve , reject ) => {
167178 let args = [ cmd ] . concat ( modules ) . concat ( '--color=always' ) . concat ( '--save' ) ;
179+ let failedTimer ;
180+
168181 if ( isGlobal ) {
169182 args = args . concat ( '-g' ) ;
170183 }
@@ -187,13 +200,21 @@ class Upgrade {
187200 output += data ;
188201 } ) . pipe ( process . stderr ) ;
189202
190- npm . on ( 'close' , ( code ) => {
191- if ( ! code ) {
203+ npm . on ( 'close' , ( code , signal ) => {
204+ clearTimeout ( failedTimer ) ;
205+ if ( signal ) {
206+ output = chalk . red ( ` Command timeout! ` ) ;
207+ }
208+ if ( code === 0 ) {
192209 resolve ( { code : 0 , data : output } ) ;
193210 } else {
194211 reject ( { code : code , data : output } ) ;
195212 }
196213 } ) ;
214+
215+ failedTimer = setTimeout ( ( ) => {
216+ npm . kill ( ) ;
217+ } , failedTimeout ) ;
197218 } ) ;
198219 }
199220}
0 commit comments