Skip to content

Commit

Permalink
wip(okam-cli): judge package.json instead of script/build.js while up…
Browse files Browse the repository at this point in the history
…dating project
  • Loading branch information
xhong0 committed Jan 1, 2019
1 parent 4e17767 commit 8fbdd92
Showing 1 changed file with 58 additions and 34 deletions.
92 changes: 58 additions & 34 deletions packages/okam-cli/bin/okam-upgrade
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ const ora = require('ora');
const inquirer = require('inquirer');
const fs = require('fs-extra');
const getLatestVersion = require('latest-version')
const PROJECT_CONFIG = 'scripts/build.js';
const projectConfPath = path.join(process.cwd(), PROJECT_CONFIG);
const pkgPath = path.join(process.cwd(), 'package.json');

// 项目pakageJson;
Expand Down Expand Up @@ -52,38 +50,24 @@ function upgradeSelf(cb) {
/**
* 获取 最新 version
*
* @param {string} name 包名
* @param {Object} okamPkgs {depType, name}
* @return {string} version 带当前 pkg 中的前缀
*/
async function getCurrentLatestVersion(name) {
let devDeps = projectPackageJson.devDependencies || {};
let deps = projectPackageJson.dependencies || {};

let pkgKey = '';
if (devDeps.hasOwnProperty(name)) {
pkgKey = 'devDependencies';
}

if (deps.hasOwnProperty(name)) {
pkgKey = 'dependencies';
}

if (!pkgKey) {
return;
}

let oldVersion = projectPackageJson[pkgKey][name];
async function getCurrentLatestVersion(depInfo) {
let {depType, pkgName} = depInfo;
let oldVersion = projectPackageJson[depType][pkgName];
let version = oldVersion;
let prefix = /^[0-9*]$/.test(oldVersion[0]) ? '' : oldVersion[0];
try {
version = await getLatestVersion(name);
version = await getLatestVersion(pkgName);
version = `${prefix}${version}`;
}
catch (e) {
console.log(chalk.grey(`encounter error while getting latest version of ${pkgName}`));
}

projectPackageJson[pkgKey][name] = `${version}`;
console.log(chalk.cyan(`${name}: ${oldVersion} -> ${version}`));
projectPackageJson[depType][pkgName] = `${version}`;
console.log(chalk.cyan(`${pkgName}: ${oldVersion} -> ${version}`));

if (oldVersion !== version) {
changeFlag = true;
Expand All @@ -95,11 +79,11 @@ async function getCurrentLatestVersion(name) {
/**
* 更新 package.json 的配置
*
* @return {[type]} [description]
* @param {Array} okamPkgs [{depType, name}]
*/
async function updatePkgConfig() {
await Promise.all(PACKAGE_NAMES.map(pkg => {
return getCurrentLatestVersion(pkg);
async function updatePkgConfig(okamPkgs) {
await Promise.all(okamPkgs.map(pkgInfo => {
return getCurrentLatestVersion(pkgInfo);
}));
}

Expand Down Expand Up @@ -144,25 +128,65 @@ function upgradePkg() {
console.log(data);
})
}

/**
* 获取需要更新的 okam 相关包
*
* @return {Array} arr
*/
function getOkamPkg() {
let devDeps = projectPackageJson.devDependencies || {};
let deps = projectPackageJson.dependencies || {};
let result = [];

PACKAGE_NAMES.forEach(pkg => {

if (devDeps.hasOwnProperty(pkg)) {
result.push({
depType: 'devDependencies',
pkgName: pkg
});
}

if (deps.hasOwnProperty(pkg)) {
result.push({
depType: 'dependencies',
pkgName: pkg
});
}
});

return result;
}

async function upgradeProject() {
if (!fs.existsSync(projectConfPath)) {
if (!fs.existsSync(pkgPath)) {
console.log(chalk.red(`
The project configuration file [${PROJECT_CONFIG}] could not be found.
Please make sure that the current directory is the root of the Okam project!`
Can't find [package.json] file.
Please make sure that the current directory have package.json file!`
));
process.exit(1);
}

console.log('Update okam to the latest versions in the project.');

try {
projectPackageJson = require(pkgPath)
await updatePkgConfig();
projectPackageJson = require(pkgPath);

let okamPkgs = getOkamPkg();

if (!okamPkgs.length) {
console.log(chalk.yellow('No packages of okam in the package.json!'));
process.exit(1);
}

await updatePkgConfig(okamPkgs);

if (!changeFlag) {
console.log(chalk.green('It\'s the latest version.'));
console.log(chalk.green('No need to update version.'));
process.exit(1);
}

comfireUpdate();
}
catch (err) {
Expand Down

0 comments on commit 8fbdd92

Please sign in to comment.