Skip to content

Commit

Permalink
feat(codemods): add codemod dry, print options (#1401)
Browse files Browse the repository at this point in the history
# Overview

- Safe Testing: The --dry option reduces risks by enabling developers to
preview changes without altering the actual code.
- Improved Visibility: The --print option makes it easier to understand
and verify the changes applied by the codemod.
- Update codemods usage docs for dry, print options

| dry | print |
| --- | --- |
| <img width="349" alt="image"
src="https://github.com/user-attachments/assets/2245fa5c-1c82-490c-b6dc-01703f492f80"
/>|<img width="396" alt="image"
src="https://github.com/user-attachments/assets/e58f731e-4c2e-4636-a703-1a20946e03c0"
/>|

## PR Checklist

- [x] I did below actions if need

1. I read the [Contributing
Guide](https://github.com/toss/suspensive/blob/main/CONTRIBUTING.md)
2. I added documents and tests.

---------

Co-authored-by: Jonghyeon Ko <[email protected]>
  • Loading branch information
gwansikk and manudeli authored Dec 22, 2024
1 parent cde15ff commit 9d72053
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/angry-cars-tie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@suspensive/codemods": minor
---

feat(codemods): add codemod dry, print options
5 changes: 5 additions & 0 deletions docs/suspensive.org/src/pages/en/docs/codemods/usage.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,8 @@ Adjust `<transform>` and `<path>` to match the environment where you want to app

- `<transform>`: name of transform
- `<path>`: files or directory to transform

You can use the following options.

- `--dry`: Output the transformation result without modifying the file.
- `--print`: Output the transformation result to the console.
5 changes: 5 additions & 0 deletions docs/suspensive.org/src/pages/ko/docs/codemods/usage.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,8 @@ Codemod를 적용하고자 하는 환경에서 맞춰서 `<transform>`과 `<path

- `<transform>`: 실행할 Codemods의 이름
- `<path>`: 변환할 디렉터리

다음 옵션을 사용할 수 있습니다.

- `--dry`: 변환 결과를 출력하지만 파일을 수정하지 않습니다.
- `--print`: 변환 결과를 콘솔에 출력합니다.
6 changes: 4 additions & 2 deletions packages/codemods/src/bin/codemods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ const program = new Command(packageJson.name)
program
.description(packageJson.description)
.version(packageJson.version, '-v, --version', 'Output the current version of @suspensive/codemods.')
.argument('[codemod]', 'Codemod slug to run.')
.argument('[codemod]', 'Codemod slug to run. See "https://suspensive.org/docs/codemods/motivation"')
.argument('[path]', 'Path to source directory.')
.usage('[codemod] [path]')
.helpOption('-h, --help', 'Display this help message.')
.action((codemod, path) => transformRunner(codemod, path))
.option('-d, --dry', 'Dry run (no changes are made to files)')
.option('-p, --print', 'Print transformed files to stdout, useful for development')
.action(transformRunner)

program.parse(process.argv)
12 changes: 10 additions & 2 deletions packages/codemods/src/bin/transformRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ function onCancel() {
export const jscodeshiftExecutable = require.resolve('.bin/jscodeshift')

Check warning on line 16 in packages/codemods/src/bin/transformRunner.ts

View workflow job for this annotation

GitHub Actions / Check quality (ci:eslint)

Unknown word: "jscodeshift"

Check warning on line 16 in packages/codemods/src/bin/transformRunner.ts

View workflow job for this annotation

GitHub Actions / Check quality (ci:eslint)

Unknown word: "jscodeshift"
export const transformerDirectory = join(__dirname, '../', '../', 'dist', 'transforms')

export async function transformRunner(transform?: string, path?: string) {
export async function transformRunner(transform?: string, path?: string, options?: { dry?: boolean; print?: boolean }) {
let transformer: string = transform ?? ''
let directory: string = path ?? ''

if (transform && !TRANSFORMER_INQUIRER_CHOICES.find((x) => x.title === transform)) {
console.error('Invalid transform choice, pick one of:')
console.error(TRANSFORMER_INQUIRER_CHOICES.map((x) => '- ' + x.title).join('\n'))
console.error(TRANSFORMER_INQUIRER_CHOICES.map((x) => `- ${+x.title}`).join('\n'))
process.exit(1)
}

Expand Down Expand Up @@ -62,9 +62,17 @@ export async function transformRunner(transform?: string, path?: string) {

const args: Array<string> = []

if (options?.dry) {
args.push('--dry')
}
if (options?.print) {
args.push('--print')
}
args.push('--no-babel')

args.push('--ignore-pattern=**/node_modules/**')
args.push('--ignore-pattern=**/.next/**')

args.push('--extensions=tsx,ts,jsx,js')

args.push('--transform', join(transformerDirectory, `${transformer}.cjs`))
Expand Down

0 comments on commit 9d72053

Please sign in to comment.