From 8f51d9ef40b6861e568801c854299047b7674bcf Mon Sep 17 00:00:00 2001 From: vagusX Date: Tue, 7 Jan 2020 15:06:21 +0800 Subject: [PATCH] fix: too long args --- .eslintignore | 2 ++ .gitignore | 2 +- bin/cli.js | 27 ++++++++++++++----- bin/codemod.ignore | 6 +++++ transforms/utils/icon.js | 1 + transforms/v3-Icon-to-v4-Icon.js | 12 +++------ transforms/v3-Modal-method-with-icon-to-v4.js | 13 +++------ ...-component-with-string-icon-props-to-v4.js | 19 ++++++------- 8 files changed, 44 insertions(+), 38 deletions(-) create mode 100644 .eslintignore create mode 100644 bin/codemod.ignore diff --git a/.eslintignore b/.eslintignore new file mode 100644 index 0000000..91ef80c --- /dev/null +++ b/.eslintignore @@ -0,0 +1,2 @@ +__testfixtures__/ +__tests__/ \ No newline at end of file diff --git a/.gitignore b/.gitignore index 5ee30a7..ecfdd4a 100644 --- a/.gitignore +++ b/.gitignore @@ -30,4 +30,4 @@ package-lock.json coverage/ .doc -cli_fixtures/ \ No newline at end of file +cli_fixtures/ diff --git a/bin/cli.js b/bin/cli.js index 1122e0c..e55e7c3 100644 --- a/bin/cli.js +++ b/bin/cli.js @@ -5,6 +5,7 @@ const chalk = require('chalk'); const execa = require('execa'); const globby = require('globby'); const updateCheck = require('update-check'); +const os = require('os'); const jscodeshiftBin = require.resolve('.bin/jscodeshift'); const pkg = require('../package.json'); @@ -15,6 +16,7 @@ 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'); +const ignoreConfig = path.join(__dirname, './codemod.ignore'); const transformers = [ // TODO: 考虑大多数项目并没有直接使用新版本的 `@antd-design/icons` @@ -68,7 +70,7 @@ function getRunnerArgs( const args = ['--verbose=2', '--ignore-pattern=**/node_modules/**']; // limit usage for cpus - const cpus = Math.max(2, Math.ceil(require('os').cpus().length / 3)); + const cpus = Math.max(2, Math.ceil(os.cpus().length / 3)); args.push('--cpus', cpus); args.push('--parser', parser); @@ -82,6 +84,8 @@ function getRunnerArgs( args.push('--transform', transformerPath); + args.push('--ignore-config', ignoreConfig); + if (styleOption) { args.push('--importStyles'); } @@ -99,27 +103,31 @@ async function run(filePath, args) { const jsPaths = paths.filter(path => /.jsx?$/.test(path)); const tsPaths = paths.filter(path => /.tsx?$/.test(path)); + // eslint-disable-next-line no-restricted-syntax for (const transformer of transformers) { if (jsPaths.length) { console.log( chalk.bgYellow.bold('JS/JSX files to convert'), jsPaths.length, ); - await transform(transformer, 'babylon', jsPaths, injectStyle); + // eslint-disable-next-line no-await-in-loop + await transform(transformer, 'babylon', filePath, injectStyle); } if (tsPaths.length) { - console.log(chalk.bgBlue.bold('TS/TSX files to convert'), jsPaths.length); - await transform(transformer, 'tsx', tsPaths, injectStyle); + console.log(chalk.bgBlue.bold('TS/TSX files to convert'), tsPaths.length); + // eslint-disable-next-line no-await-in-loop + await transform(transformer, 'tsx', filePath, injectStyle); } } } -async function transform(transformer, parser, paths, styleOption) { +async function transform(transformer, parser, globPath, styleOption) { console.log(chalk.bgGreen.bold('Transform'), transformer); const transformerPath = path.join(transformersDir, `${transformer}.js`); - const args = getRunnerArgs(transformerPath, parser, styleOption).concat( - paths, + + const args = [globPath].concat( + getRunnerArgs(transformerPath, parser, styleOption), ); try { if (process.env.NODE_ENV === 'local') { @@ -131,6 +139,11 @@ async function transform(transformer, parser, paths, styleOption) { }); } catch (err) { console.error(err); + if (process.env.NODE_ENV === 'local') { + const errorLogFile = path.join(__dirname, './error.log'); + fs.appendFileSync(errorLogFile, err); + fs.appendFileSync(errorLogFile, '\n'); + } } } diff --git a/bin/codemod.ignore b/bin/codemod.ignore new file mode 100644 index 0000000..69c8c44 --- /dev/null +++ b/bin/codemod.ignore @@ -0,0 +1,6 @@ +node_modules +*.css +*.json +*.less +*.sass +*.scss diff --git a/transforms/utils/icon.js b/transforms/utils/icon.js index 799352e..4498590 100644 --- a/transforms/utils/icon.js +++ b/transforms/utils/icon.js @@ -21,6 +21,7 @@ function getV4IconComponentName(type, theme) { theme ? `with ${theme}` : '' } cannot found, please check it at https://ant.design/components/icon`, ); + return ''; } function createIconJSXElement(j, iconLocalName, attrs = []) { diff --git a/transforms/v3-Icon-to-v4-Icon.js b/transforms/v3-Icon-to-v4-Icon.js index c3f01b5..af51062 100644 --- a/transforms/v3-Icon-to-v4-Icon.js +++ b/transforms/v3-Icon-to-v4-Icon.js @@ -1,4 +1,4 @@ -const summary = require('./utils/summary'); +const { addIconRelatedMsg } = require('./utils/summary'); const { printOptions } = require('./utils/config'); const { getV4IconComponentName } = require('./utils/icon'); const { @@ -72,7 +72,7 @@ module.exports = (file, api, options) => { // add @ant-design/icons imports addModuleDefaultImport(j, root, { moduleName: '@ant-design/icons', - localName: localName, + localName, before, }); return true; @@ -106,13 +106,7 @@ module.exports = (file, api, options) => { if (!v4IconComponentName) { const location = jsxElement.loc.start; - const message = - 'Contains an invalid icon, please check it at https://ant.design/components/icon'; - summary.appendLine( - `${file.path} - ${location.line}:${location.column}`, - j(jsxElement).toSource(), - message, - ); + addIconRelatedMsg(file, location, j(jsxElement).toSource()); return false; } diff --git a/transforms/v3-Modal-method-with-icon-to-v4.js b/transforms/v3-Modal-method-with-icon-to-v4.js index 38d85ed..35f3483 100644 --- a/transforms/v3-Modal-method-with-icon-to-v4.js +++ b/transforms/v3-Modal-method-with-icon-to-v4.js @@ -114,17 +114,10 @@ module.exports = (file, api, options) => { ); iconProperty.value = jsxElement; return; - } else { - // FIXME: use parent jsxElement - const location = nodePath.node.loc.start; - const message = - 'Contains an invalid icon, please check it at https://ant.design/components/icon'; - summary.appendLine( - `${file.path} - ${location.line}:${location.column}`, - j(nodePath).toSource(), - message, - ); } + // FIXME: use parent jsxElement + const location = nodePath.node.loc.start; + addIconRelatedMsg(file, location, j(nodePath).toSource()); } const jsxElement = importLegacyIcon(j, iconProperty, antdPkgName); diff --git a/transforms/v3-component-with-string-icon-props-to-v4.js b/transforms/v3-component-with-string-icon-props-to-v4.js index f19b36d..41063c2 100644 --- a/transforms/v3-component-with-string-icon-props-to-v4.js +++ b/transforms/v3-component-with-string-icon-props-to-v4.js @@ -40,16 +40,15 @@ module.exports = (file, api, options) => { name: 'icon', }, }) - .filter(nodePath => { - return ( + .filter( + nodePath => nodePath.node.type === 'StringLiteral' || - nodePath.node.type !== 'JSXExpressionContainer' - ); - }) - .forEach(path => { + nodePath.node.type !== 'JSXExpressionContainer', + ) + .forEach(nodePath => { hasChanged = true; - const iconProperty = path.value; + const iconProperty = nodePath.value; // v3-Icon-to-v4-Icon should handle with JSXElement if ( @@ -77,11 +76,9 @@ module.exports = (file, api, options) => { before: antdPkgName, }); return; - } else { - const location = path.node.loc.start; - - addIconRelatedMsg(file, location, j(nodePath).toSource()); } + const location = nodePath.node.loc.start; + addIconRelatedMsg(file, location, j(nodePath).toSource()); } // handle it with `@ant-design/compatible`