Skip to content

Commit 5f2d03d

Browse files
committed
inital work
1 parent f60a6ab commit 5f2d03d

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

commands/upgrade.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
}

index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import { Command } from 'commander'
77
import { transform } from './commands/transform'
8+
import { upgrade } from './commands/upgrade'
89
import packageJson from './package.json'
910

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

26+
program
27+
.command('upgrade')
28+
.description('Upgrade your express server to the latest version.')
29+
.argument('[source]', 'Path to source files or directory to transform.')
30+
.action(upgrade)
31+
2532
program.parse(process.argv)

0 commit comments

Comments
 (0)