Skip to content

Commit

Permalink
feat: plugin increment update policy.
Browse files Browse the repository at this point in the history
  • Loading branch information
cpselvis committed Nov 9, 2017
1 parent 529568d commit 7a3039d
Showing 1 changed file with 103 additions and 10 deletions.
113 changes: 103 additions & 10 deletions lib/core/upgrade.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
'use strict';

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

Expand All @@ -12,30 +15,30 @@ class Upgrade {
this.log = ctx.log;
}

check() {
checkCore() {
const self = this;
const { config, log } = this.ctx;
const registry = config && config.registry;

log.debug('正在检查更新...');
log.debug('正在检查cli core更新...');
return pkgJson('feflow-cli', 'latest', registry).then(json => {
const version = pkg.version;
const configs = json.configs;
const ltsVersion = configs && configs.version;
const ltsVersion = json.version;
const compatibleVersion = configs && configs.compatibleVersion;
log.debug(`本地版本: ${version}, 兼容版本 ${compatibleVersion}`);

// If local installed felfow version not compatible with remote, it will be force update to latest version.
if (!semver.satisfies(version, compatibleVersion)) {
log.info(`您当前使用的feflow版本 ${version}, feflow要求使用的版本为 ${compatibleVersion}, 将会使用全量更新策略`);

const loading = new Loading('正在全量安装feflow-cli,请稍等');
return self.execNpmCommand('install', 'feflow-cli').then(function (result) {
const loading = new Loading('正在全量更新cli core,请稍等');
return self.execNpmCommand('install', 'feflow-cli', true).then(function (result) {
if (!result.code) {
loading.success();
log.info(`已经自动升级到feflow的最新版本${ltsVersion}`);
} else {
const err = `feflow-cli安装失败,失败码为${result.code},错误日志为${result.data}`;
const err = `cli core全量更新失败,失败码为${result.code},错误日志为${result.data}`;
loading.fail(err);
log.error(err);
}
Expand All @@ -47,12 +50,102 @@ class Upgrade {
});
}

execNpmCommand(cmd, modules, where) {
checkPlugin() {
const self = this;
const { config, log, baseDir } = this.ctx;
const registry = config && config.registry;

log.debug('正在检查cli plugin更新...');

const table = new Table();
return this.getInstalledPlugins().map((plugin) => {
return this.getLocal(plugin).then((version) => {
return pkgJson(plugin, 'latest', registry).then((json) => {
const configs = json.configs;
const ltsVersion = json.version;
const compatibleVersion = configs && configs.compatibleVersion;
if (compatibleVersion && !semver.satisfies(version, compatibleVersion)) {
table.cell('Name', plugin);
table.cell('Version', version === ltsVersion ? version : version + ' -> ' + ltsVersion);
table.cell('Tag', 'latest');
table.cell('Update', version === ltsVersion ? 'N' : 'Y');
table.newRow();

return plugin;
}
});
});
}).filter((plugin) => {
return !!plugin;
}).then((needUpdatePlugins) => {
if (needUpdatePlugins.length) {
log.info('检测到您本地的部分插件需要更新才能继续使用,将采取 cli plugin 增量更新策略');

console.log(table.toString());

const loading = new Loading('正在增量更新cli plugin,请稍等');
return self.execNpmCommand('install', needUpdatePlugins, false, baseDir).then(function (result) {
if (!result.code) {
loading.success();
log.info(`cli plugin 增量更新完成`);
} else {
const err = `cli plugin 增量更新失败,失败码为${result.code},错误日志为${result.data}`;
loading.fail(err);
log.error(err);
}
});
}
});
}

getInstalledPlugins() {
const ctx = this.ctx;
const packagePath = pathFn.join(ctx.baseDir, 'package.json');
const pluginDir = ctx.pluginDir;

// Make sure package.json exists
return fs.exists(packagePath).then(function (exist) {
if (!exist) return [];

// Read package.json and find dependencies
return fs.readFile(packagePath).then(function (content) {
const json = JSON.parse(content);
const deps = json.dependencies || json.devDependencies || {};

return Object.keys(deps);
});
}).filter(function (name) {
// Ignore plugins whose name is not started with "feflow-plugin-"
if (!/^feflow-plugin-|^@[^/]+\/feflow-plugin-|^generator-|^@[^/]+\/generator-/.test(name)) return false;

// Make sure the plugin exists
const path = pathFn.join(pluginDir, name);
return fs.exists(path);
});
}

getLocal(name) {
const pluginDir = this.ctx.pluginDir;
const path = pathFn.join(pluginDir, name, 'package.json');
return fs.exists(path).then(function (exist) {
if (!exist) return;

return fs.readFile(path).then(function (content) {
const pkg = JSON.parse(content);
return pkg.version;
});
});
}

execNpmCommand(cmd, modules, isGlobal, where) {
const {registry, proxy} = this.ctx.config;
const log = this.ctx.log;

return new Promise((resolve, reject) => {
let args = [cmd].concat(modules).concat('--color=always').concat('--save').concat('-g');
let args = [cmd].concat(modules).concat('--color=always').concat('--save');
if (isGlobal) {
args = args.concat('-g');
}
if (registry) {
args = args.concat(`--registry=${registry}`);
}
Expand Down Expand Up @@ -86,8 +179,8 @@ class Upgrade {
module.exports = function (ctx) {
const upgrade = new Upgrade(ctx);


return Promise.all([
upgrade.check()
upgrade.checkCore(),
upgrade.checkPlugin()
]);
};

0 comments on commit 7a3039d

Please sign in to comment.