Skip to content

Commit

Permalink
fix: too long args (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
vagusX authored Jan 7, 2020
1 parent 2eecf7a commit 079e722
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 38 deletions.
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
__testfixtures__/
__tests__/
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ package-lock.json
coverage/
.doc

cli_fixtures/
cli_fixtures/
27 changes: 20 additions & 7 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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`
Expand Down Expand Up @@ -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);
Expand All @@ -82,6 +84,8 @@ function getRunnerArgs(

args.push('--transform', transformerPath);

args.push('--ignore-config', ignoreConfig);

if (styleOption) {
args.push('--importStyles');
}
Expand All @@ -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') {
Expand All @@ -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');
}
}
}

Expand Down
6 changes: 6 additions & 0 deletions bin/codemod.ignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
node_modules
*.css
*.json
*.less
*.sass
*.scss
1 change: 1 addition & 0 deletions transforms/utils/icon.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []) {
Expand Down
12 changes: 3 additions & 9 deletions transforms/v3-Icon-to-v4-Icon.js
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down
13 changes: 3 additions & 10 deletions transforms/v3-Modal-method-with-icon-to-v4.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
19 changes: 8 additions & 11 deletions transforms/v3-component-with-string-icon-props-to-v4.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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`
Expand Down

0 comments on commit 079e722

Please sign in to comment.