Skip to content

Commit

Permalink
fix(codemod): Pass codemod options to jscodeshift script (#2137)
Browse files Browse the repository at this point in the history
Fixes: #2136

The codemod was ignoring valid JSCodeshift options that are helpful to optimize the script. This patch allows `--ignore-pattern`, `--ignore-config`, and `--verbose` to be passed through. I also set the codemod to ignore `node_modules` directories by default and updated the docs.

[category:Codemods]
  • Loading branch information
alanbsmith committed Apr 7, 2023
1 parent 5e956b0 commit edbde73
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 8 deletions.
46 changes: 41 additions & 5 deletions modules/codemod/README.md
@@ -1,6 +1,7 @@
# Canvas Kit Codemods

This package is designed to help you migrate between major versions of Canvas Kit and adopt API changes.
This package is designed to help you migrate between major versions of Canvas Kit and adopt API
changes.

## Usage

Expand All @@ -13,13 +14,18 @@ Commands (transforms):
canvas-kit-codemod v5 [path] Canvas Kit v4 > v5 migration transform

Options:
-v, --version Show version number [boolean]
-h, --help Show help [boolean]
--ignore-config, Ignore files if they match patterns sourced from a config file [string]
--ignore-pattern, Ignore files that match a provided glob expression [string] [default: **/node_modules/**]
--verbose, Show more information about the transform process [number] [choices: 0, 1, 2] [default: 0]
-v, --version Show version number [boolean]
-h, --help Show help [boolean]
```
> Note: These codemods only work on .js, .jsx, .ts, and .tsx extensions. You may need to make some manual changes in other file types (.json, .mdx, .md, etc.).
> Note: These codemods only work on .js, .jsx, .ts, and .tsx extensions. You may need to make some
> manual changes in other file types (.json, .mdx, .md, etc.).
> Note: You may need to run your linter after executing the codemod, as it's resulting formatting (spacing, quotes, etc.) may not match your project's styling.
> Note: You may need to run your linter after executing the codemod, as it's resulting formatting
> (spacing, quotes, etc.) may not match your project's styling.
## Installation
Expand All @@ -29,3 +35,33 @@ Alternatively, you can install the codemod package and run it without npx.
> yarn add @workday/canvas-kit-codemod
> canvas-kit-codemod <transform> [path]
```
## Options
### Ignore Config
If you'd like to provide a configuration for files to ignore instead of a glob, use
`--ignore-config`.
```sh
> canvas-kit-codemod <transform> [path] --ignore-config=.gitignore
```
### Ignore Pattern
If you'd like to provide a glob to ignore files, use `--ignore-pattern`. By default, this is set to
ignore all `node_modules` directories.
```sh
> canvas-kit-codemod <transform> [path] --ignore-pattern=**/dist/**
```
### Verbose
If you'd like to have more verbose logging to know which files are being parsed, use `--verbose`. By
default this is set to `0`.
```sh
# See all files being parsed
> canvas-kit-codemod <transform> [path] --verbose=2
```
26 changes: 23 additions & 3 deletions modules/codemod/index.js
Expand Up @@ -4,9 +4,27 @@
const {spawn} = require('child_process');
const chalk = require('chalk');

const {_: commands, path} = require('yargs')
const {_: commands, path, ignoreConfig, ignorePattern, verbose } = require('yargs')
.scriptName('canvas-kit-codemod')
.usage(chalk.bold.blueBright('$0 <transform> [path]'))
.options({
'ignore-config': {
type: 'string',
describe: 'Ignore files if they match patterns sourced from a configuration file (e.g. a .gitignore)',
default: '',
},
'ignore-pattern': {
type: 'string',
describe: 'Ignore files that match a provided glob expression (e.g. **/dist/**)',
default: '**/node_modules/**',
},
verbose: {
type: 'number',
describe: 'Show more information about the transform process',
default: 0,
choices: [0, 1, 2]
},
})
.command('v5 [path]', chalk.gray('Canvas Kit v4 > v5 migration transform'), yargs => {
yargs.positional('path', {
type: 'string',
Expand Down Expand Up @@ -60,10 +78,12 @@ const {_: commands, path} = require('yargs')
.help().argv;

const transform = commands[0];
console.log(transform, path);
// Only add an ignore-config if one is provided
const ignoreConfigArg = ignoreConfig ? `--ignore-config=${ignoreConfig}` : '';
console.log(ignorePattern)

console.log(chalk.blueBright(`\nApplying ${transform} transform to '${path}'\n`));
const args = `-t ${__dirname}/dist/es6/${transform} ${path} --parser tsx --extensions js,jsx,ts,tsx`.split(
const args = `-t ${__dirname}/dist/es6/${transform} ${path} --parser tsx --extensions js,jsx,ts,tsx ${ignoreConfigArg} --ignore-pattern=${ignorePattern} --verbose=${verbose}`.split(
' '
);

Expand Down

0 comments on commit edbde73

Please sign in to comment.