|
1 |
| -import { writeFile } from 'node:fs/promises' |
2 |
| -import { bold } from 'picocolors' |
| 1 | +import { join } from 'node:path' |
| 2 | +import execa from 'execa' |
| 3 | +import { bold, green } from 'picocolors' |
3 | 4 | import prompts from 'prompts'
|
4 | 5 | import { TRANSFORM_OPTIONS } from '../config'
|
5 |
| -import { getAllFiles, getContent } from '../utils/file' |
| 6 | +import { getAllFiles } from '../utils/file' |
6 | 7 |
|
7 | 8 | export function onCancel() {
|
8 | 9 | process.exit(1)
|
9 | 10 | }
|
10 | 11 |
|
| 12 | +const jscodeshiftExecutable = require.resolve('.bin/jscodeshift') |
| 13 | +const transformerDirectory = join(__dirname, '../', 'transforms') |
| 14 | + |
11 | 15 | // biome-ignore lint/suspicious/noExplicitAny: 'Any' is used because options can be anything.
|
12 | 16 | export async function transform(codemodName: string, source: string, options: any): Promise<void> {
|
13 | 17 | let codemodSelected = codemodName
|
14 | 18 | let sourceSelected = source
|
15 | 19 |
|
16 |
| - const { dry } = options |
| 20 | + const { dry, print, verbose } = options |
17 | 21 |
|
18 | 22 | let existCodemod = TRANSFORM_OPTIONS.find(({ value }) => value === codemodSelected)
|
19 | 23 |
|
@@ -52,17 +56,37 @@ export async function transform(codemodName: string, source: string, options: an
|
52 | 56 | sourceSelected = res.path
|
53 | 57 | }
|
54 | 58 |
|
55 |
| - const files = await getAllFiles(sourceSelected) |
| 59 | + const transformerPath = join(transformerDirectory, `${codemodSelected}.js`) |
| 60 | + |
| 61 | + const args: string[] = [] |
56 | 62 |
|
57 |
| - for (const file of files) { |
58 |
| - const content = await getContent(file) |
| 63 | + if (dry) { |
| 64 | + args.push('--dry') |
| 65 | + } |
59 | 66 |
|
60 |
| - if (existCodemod) { |
61 |
| - const newContent = existCodemod.codemod({ path: file.toString(), source: content }, options) |
| 67 | + if (print) { |
| 68 | + args.push('--print') |
| 69 | + } |
62 | 70 |
|
63 |
| - if (!dry) { |
64 |
| - await writeFile(file.toString(), newContent) |
65 |
| - } |
66 |
| - } |
| 71 | + if (verbose) { |
| 72 | + args.push('--verbose=2') |
67 | 73 | }
|
| 74 | + |
| 75 | + args.push('--no-babel') |
| 76 | + args.push('--ignore-pattern=**/node_modules/**') |
| 77 | + args.push('--extensions=cts,mts,ts,js,mjs,cjs') |
| 78 | + |
| 79 | + const files = await getAllFiles(sourceSelected) |
| 80 | + |
| 81 | + args.push('--transform', transformerPath, ...files.map((file) => file.toString())) |
| 82 | + |
| 83 | + console.log(`Executing command: ${green('jscodeshift')} ${args.join(' ')}`) |
| 84 | + |
| 85 | + const jscodeshiftProcess = execa(jscodeshiftExecutable, args, { |
| 86 | + // include ANSI color codes |
| 87 | + env: process.stdout.isTTY ? { FORCE_COLOR: 'true' } : {}, |
| 88 | + }) |
| 89 | + |
| 90 | + jscodeshiftProcess.stdout?.pipe(process.stdout) |
| 91 | + jscodeshiftProcess.stderr?.pipe(process.stderr) |
68 | 92 | }
|
0 commit comments