Skip to content

Commit

Permalink
feat(codemods): add codemod dry, print options
Browse files Browse the repository at this point in the history
  • Loading branch information
gwansikk committed Dec 21, 2024
1 parent 9485f2e commit d1c3998
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
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/ko/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 d1c3998

Please sign in to comment.