From ad745325d60660d8ee918927979ab64d43afa680 Mon Sep 17 00:00:00 2001 From: vagusX Date: Fri, 28 Feb 2020 21:07:30 +0800 Subject: [PATCH] feat: additional dependency verion check (#68) * feat: additional dependency verion check * chore: update text * fix: warning for alias icons * feat: update text --- .eslintrc.js | 1 + bin/cli.js | 67 ++++++++++++++++++++++- bin/upgrade-list.json | 8 +++ package.json | 2 + transforms/utils/compatible-icon-utils.js | 9 ++- 5 files changed, 81 insertions(+), 6 deletions(-) create mode 100644 bin/upgrade-list.json diff --git a/.eslintrc.js b/.eslintrc.js index 97741c7..d9c42d1 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -10,5 +10,6 @@ module.exports = { 'react/sort-comp': 0, '@typescript-eslint/no-explicit-any': 0, 'no-return-await': 0, + 'consistent-return': 0, }, }; diff --git a/bin/cli.js b/bin/cli.js index 9061aff..bcb78ca 100644 --- a/bin/cli.js +++ b/bin/cli.js @@ -1,23 +1,33 @@ +/* eslint no-console: 0 */ + const path = require('path'); const fs = require('fs'); const os = require('os'); -const isGitClean = require('is-git-clean'); +const _ = require('lodash'); const chalk = require('chalk'); const execa = require('execa'); const globby = require('globby'); +const isGitClean = require('is-git-clean'); const updateCheck = require('update-check'); +const readPkgUp = require('read-pkg-up'); +const semverSatisfies = require('semver/functions/satisfies'); const jscodeshiftBin = require.resolve('.bin/jscodeshift'); -const pkg = require('../package.json'); + const summary = require('../transforms/utils/summary'); const marker = require('../transforms/utils/marker'); +const pkg = require('../package.json'); +const upgradeList = require('./upgrade-list'); +// jscodeshift codemod scripts dir const transformersDir = path.join(__dirname, '../transforms'); // override default babylon parser config to enable `decorator-legacy` // https://github.com/facebook/jscodeshift/blob/master/parser/babylon.js const babylonConfig = path.join(__dirname, './babylon.config.json'); + +// jscodeshift bin#--ignore-config const ignoreConfig = path.join(__dirname, './codemod.ignore'); const transformers = [ @@ -32,6 +42,14 @@ const transformers = [ 'v3-typings-to-compatible', ]; +const dependencyProperties = [ + 'dependencies', + 'devDependencies', + 'clientDependencies', + 'isomorphicDependencies', + 'buildDependencies', +]; + async function ensureGitClean() { let clean = false; try { @@ -153,11 +171,51 @@ function dependenciesAlert(needIcon, needCompatible) { console.log(dependencies.map(n => `* ${n}`).join('\n')); } +async function upgradeDetect(targetDir) { + const result = []; + const cwd = path.join(process.cwd(), targetDir); + const closetPkgJson = await readPkgUp({ cwd }); + if (!closetPkgJson) { + console.log( + chalk.yellow("Skipping because we didn't find package.json file"), + ); + return; + } + const { packageJson, path: pkgJsonPath } = closetPkgJson; + Object.keys(upgradeList).forEach(depName => { + dependencyProperties.forEach(property => { + const expectVersion = upgradeList[depName].version; + const versionRange = _.get(packageJson, `${property}.${depName}`); + if (versionRange && !semverSatisfies(expectVersion, versionRange)) { + result.push([depName, expectVersion, property]); + } + }); + }); + + if (!result.length) { + console.log(chalk.green('Checking passed')); + return; + } + + console.log( + chalk.yellow( + "It's recommended to upgrade these dependencies to ensure working well with antd4\n", + ), + ); + console.log(`> package.json file: ${pkgJsonPath} \n`); + const dependencies = result.map( + ([depName, expectVersion, dependencyProperty]) => + `${depName}^${expectVersion} in ${dependencyProperty}`, + ); + + console.log(dependencies.map(n => `* ${n}`).join('\n')); +} + /** * options * --force // force skip git checking (dangerously) * --cpus=1 // specify cpus cores to use - * --extraScripts=v4-Icon-Outlined,blabla // add extra codemod scripts to run + * --extraScripts=v4-Icon-Outlined, blabla // add extra codemod scripts to run */ async function bootstrap() { @@ -214,6 +272,9 @@ async function bootstrap() { const needCompatible = dependenciesMarkers['@ant-design/compatible']; dependenciesAlert(needIcon, needCompatible); + console.log('\n----------- additional dependencies alert -----------\n'); + await upgradeDetect(dir); + console.log( `\n----------- Thanks for using @ant-design/codemod ${pkg.version} -----------`, ); diff --git a/bin/upgrade-list.json b/bin/upgrade-list.json new file mode 100644 index 0000000..5615724 --- /dev/null +++ b/bin/upgrade-list.json @@ -0,0 +1,8 @@ +{ + "@ant-design/pro-layout": { + "version": "5.0.0" + }, + "@ant-design/icons": { + "version": "4.0.0" + } +} diff --git a/package.json b/package.json index 061d300..2abc1db 100644 --- a/package.json +++ b/package.json @@ -45,6 +45,8 @@ "jscodeshift": "^0.7.0", "lodash": "^4.17.15", "papaparse": "^5.1.0", + "read-pkg-up": "^7.0.1", + "semver": "^7.1.3", "update-check": "^1.5.3", "yargs-parser": "^16.1.0" }, diff --git a/transforms/utils/compatible-icon-utils.js b/transforms/utils/compatible-icon-utils.js index a3e2782..5b72869 100644 --- a/transforms/utils/compatible-icon-utils.js +++ b/transforms/utils/compatible-icon-utils.js @@ -53,9 +53,12 @@ function alias(type) { break; default: } - console.warn( - `Icon '${type}' was a typo and is now deprecated, please use '${newType}' instead.`, - ); + + if (type !== newType) { + console.warn( + `Icon '${type}' was a typo and is now deprecated, please use '${newType}' instead.`, + ); + } return newType; }