|
| 1 | +import { TRANSFORM_OPTIONS } from '../config' |
| 2 | +import { join } from 'node:path' |
| 3 | +import { getAllFiles } from '../utils/file' |
| 4 | +import prompts from 'prompts' |
| 5 | +import execa from 'execa' |
| 6 | + |
| 7 | +const jscodeshiftExecutable = require.resolve('.bin/jscodeshift') |
| 8 | + |
| 9 | + |
| 10 | +const transformerDirectory = join(__dirname, '../', 'transforms') |
| 11 | +export function onCancel() { |
| 12 | + process.exit(1) |
| 13 | +} |
| 14 | +export async function upgrade(source: string): Promise<void> { |
| 15 | + let sourceSelected = source |
| 16 | + |
| 17 | + if (!sourceSelected) { |
| 18 | + const res = await prompts( |
| 19 | + { |
| 20 | + type: 'text', |
| 21 | + name: 'path', |
| 22 | + message: 'Which files or directories should the codemods be applied to?', |
| 23 | + initial: '.', |
| 24 | + }, |
| 25 | + { onCancel }, |
| 26 | + ) |
| 27 | + |
| 28 | + sourceSelected = res.path |
| 29 | + } |
| 30 | + |
| 31 | + const files = await getAllFiles(sourceSelected) |
| 32 | + |
| 33 | + const args: string[] = [] |
| 34 | + |
| 35 | + args.push('--no-babel') |
| 36 | + args.push('--silent') |
| 37 | + args.push('--ignore-pattern=**/node_modules/**') |
| 38 | + args.push('--extensions=cts,mts,ts,js,mjs,cjs') |
| 39 | + args.push( ...files.map((file) => file.toString())) |
| 40 | + |
| 41 | + |
| 42 | + for (const { value } of TRANSFORM_OPTIONS) { |
| 43 | + const transformerPath = require.resolve(`${transformerDirectory}/${value}.js`) |
| 44 | + const jscodeshiftProcess = execa(jscodeshiftExecutable, [...args, '--transform', transformerPath]) |
| 45 | + |
| 46 | + jscodeshiftProcess.stderr?.pipe(process.stderr) |
| 47 | + } |
| 48 | +} |
0 commit comments