Skip to content

Commit 85e1633

Browse files
SuperJollycpselvis
authored andcommitted
fix: stop update when full update error (#74)
* fix: stop update when full update error * fix: stop update when full update error
1 parent fc565be commit 85e1633

File tree

4 files changed

+42
-10
lines changed

4 files changed

+42
-10
lines changed

lib/core/upgrade.js

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,16 @@
22

33
const pathFn = require('path');
44
const fs = require('hexo-fs');
5+
const chalk = require('chalk');
56
const semver = require('semver');
6-
const spawn = require('cross-spawn');
77
const 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');
915
const pkg = require('../../package.json');
1016

1117
class 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
}

lib/feflow.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
'use strict';
22

3-
const Feflow = require('./core');
4-
const pkg = require('../package.json');
5-
const figlet = require('figlet');
6-
const chalk = require('chalk');
73
const minimist = require('minimist');
4+
const figlet = require('figlet');
85
const semver = require('semver');
6+
const chalk = require('chalk');
7+
const Feflow = require('./core');
8+
const pkg = require('../package.json');
99

1010
/**
1111
* Entrance file, parse user input and call a command.

lib/utils/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ exports.parseYaml = require('./yaml').parseYaml;
55
exports.safeDump = require('./yaml').safeDump;
66
exports.pkgJson = require('./pkgJson');
77
exports.formatDate = require('./date').formatDate;
8+
exports.isUndef = require('./util').isUndef;

lib/utils/util.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function isUndef(target) {
2+
return target === null || target === undefined;
3+
}
4+
5+
function isDef(target) {
6+
return target !== undefined && target !== null
7+
}
8+
9+
exports.isUndef = isUndef;
10+
exports.isDef = isDef;

0 commit comments

Comments
 (0)