Skip to content

Commit

Permalink
inital work
Browse files Browse the repository at this point in the history
  • Loading branch information
bjohansebas committed Dec 12, 2024
1 parent 60f69d6 commit 13a6219
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
48 changes: 48 additions & 0 deletions commands/upgrade.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { TRANSFORM_OPTIONS } from '../config'
import { join } from 'node:path'
import { getAllFiles } from '../utils/file'
import prompts from 'prompts'
import execa from 'execa'

const jscodeshiftExecutable = require.resolve('.bin/jscodeshift')


const transformerDirectory = join(__dirname, '../', 'transforms')
export function onCancel() {
process.exit(1)
}
export async function upgrade(source: string): Promise<void> {
let sourceSelected = source

if (!sourceSelected) {
const res = await prompts(
{
type: 'text',
name: 'path',
message: 'Which files or directories should the codemods be applied to?',
initial: '.',
},
{ onCancel },
)

sourceSelected = res.path
}

const files = await getAllFiles(sourceSelected)

const args: string[] = []

args.push('--no-babel')
args.push('--silent')
args.push('--ignore-pattern=**/node_modules/**')
args.push('--extensions=cts,mts,ts,js,mjs,cjs')
args.push( ...files.map((file) => file.toString()))


for (const { value } of TRANSFORM_OPTIONS) {
const transformerPath = require.resolve(`${transformerDirectory}/${value}.js`)
const jscodeshiftProcess = execa(jscodeshiftExecutable, [...args, '--transform', transformerPath])

jscodeshiftProcess.stderr?.pipe(process.stderr)
}
}
7 changes: 7 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import { Command } from 'commander'
import { transform } from './commands/transform'
import { upgrade } from './commands/upgrade'
import packageJson from './package.json'

const program = new Command(packageJson.name)
Expand All @@ -22,4 +23,10 @@ const program = new Command(packageJson.name)
// Why this option is necessary is explained here: https://github.com/tj/commander.js/pull/1427
.enablePositionalOptions()

program
.command('upgrade')
.description('Upgrade your express server to the latest version.')
.argument('[source]', 'Path to source files or directory to transform.')
.action(upgrade)

program.parse(process.argv)

0 comments on commit 13a6219

Please sign in to comment.