Skip to content

Commit

Permalink
fix: stop update when full update error (#74)
Browse files Browse the repository at this point in the history
* fix: stop update when full update error

* fix: stop update when full update error
  • Loading branch information
SuperJolly authored and cpselvis committed Feb 20, 2019
1 parent fc565be commit 85e1633
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 10 deletions.
33 changes: 27 additions & 6 deletions lib/core/upgrade.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@

const pathFn = require('path');
const fs = require('hexo-fs');
const chalk = require('chalk');
const semver = require('semver');
const spawn = require('cross-spawn');
const Table = require('easy-table');
const { pkgJson, Loading } = require('../utils/index');
const spawn = require('cross-spawn');

const {
pkgJson,
Loading,
isUndef
} = require('../utils/index');
const pkg = require('../../package.json');

class Upgrade {
Expand Down Expand Up @@ -44,10 +50,14 @@ class Upgrade {
loading.success();
log.info(`已经自动升级到feflow的最新版本${ltsVersion}`);
} else {
const err = `cli core全量更新失败,失败码为${result.code},错误日志为${result.data}`;
loading.fail(err);
const err = `cli core全量更新失败,${isUndef(result.code) ? '' : '失败码为' + result.code},错误日志如下:\n${result.data}`;
loading.fail();
log.error(err);
}
}, function (error) {
const err = `cli core全量更新失败,${isUndef(error.code) ? '' : '失败码为' + error.code},错误日志如下:\n${error.data}`;
loading.fail();
log.error(err);
});
} else {
log.debug(`当前安装的版本 ${version} 和最新版本 ${ltsVersion} 兼容`);
Expand Down Expand Up @@ -162,9 +172,12 @@ class Upgrade {
execNpmCommand(cmd, modules, isGlobal, where) {
const {registry, proxy} = this.ctx.config;
const log = this.ctx.log;
const failedTimeout = 60 * 1000;

return new Promise((resolve, reject) => {
let args = [cmd].concat(modules).concat('--color=always').concat('--save');
let failedTimer;

if (isGlobal) {
args = args.concat('-g');
}
Expand All @@ -187,13 +200,21 @@ class Upgrade {
output += data;
}).pipe(process.stderr);

npm.on('close', (code) => {
if (!code) {
npm.on('close', (code, signal) => {
clearTimeout(failedTimer);
if(signal) {
output = chalk.red(` Command timeout! `);
}
if (code === 0) {
resolve({code: 0, data: output});
} else {
reject({code: code, data: output});
}
});

failedTimer = setTimeout(() => {
npm.kill();
}, failedTimeout);
});
}
}
Expand Down
8 changes: 4 additions & 4 deletions lib/feflow.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
'use strict';

const Feflow = require('./core');
const pkg = require('../package.json');
const figlet = require('figlet');
const chalk = require('chalk');
const minimist = require('minimist');
const figlet = require('figlet');
const semver = require('semver');
const chalk = require('chalk');
const Feflow = require('./core');
const pkg = require('../package.json');

/**
* Entrance file, parse user input and call a command.
Expand Down
1 change: 1 addition & 0 deletions lib/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ exports.parseYaml = require('./yaml').parseYaml;
exports.safeDump = require('./yaml').safeDump;
exports.pkgJson = require('./pkgJson');
exports.formatDate = require('./date').formatDate;
exports.isUndef = require('./util').isUndef;
10 changes: 10 additions & 0 deletions lib/utils/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
function isUndef(target) {
return target === null || target === undefined;
}

function isDef(target) {
return target !== undefined && target !== null
}

exports.isUndef = isUndef;
exports.isDef = isDef;

0 comments on commit 85e1633

Please sign in to comment.