Skip to content

Commit 6e43412

Browse files
authored
Use jscodeshift executable (#14)
* use jscodeshift executable * update execa * fix code style * remove unused code * remove unnecesary concat * remove export
1 parent 39f8b2e commit 6e43412

File tree

6 files changed

+58
-34
lines changed

6 files changed

+58
-34
lines changed

.gitignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,8 @@ yarn-error.log*
2121

2222
# Misc
2323
.DS_Store
24-
*.pem
24+
*.pem
25+
26+
*.d.ts
27+
*.js
28+
*.js.map

commands/transform.ts

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
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'
34
import prompts from 'prompts'
45
import { TRANSFORM_OPTIONS } from '../config'
5-
import { getAllFiles, getContent } from '../utils/file'
6+
import { getAllFiles } from '../utils/file'
67

78
export function onCancel() {
89
process.exit(1)
910
}
1011

12+
const jscodeshiftExecutable = require.resolve('.bin/jscodeshift')
13+
const transformerDirectory = join(__dirname, '../', 'transforms')
14+
1115
// biome-ignore lint/suspicious/noExplicitAny: 'Any' is used because options can be anything.
1216
export async function transform(codemodName: string, source: string, options: any): Promise<void> {
1317
let codemodSelected = codemodName
1418
let sourceSelected = source
1519

16-
const { dry } = options
20+
const { dry, print, verbose } = options
1721

1822
let existCodemod = TRANSFORM_OPTIONS.find(({ value }) => value === codemodSelected)
1923

@@ -52,17 +56,37 @@ export async function transform(codemodName: string, source: string, options: an
5256
sourceSelected = res.path
5357
}
5458

55-
const files = await getAllFiles(sourceSelected)
59+
const transformerPath = join(transformerDirectory, `${codemodSelected}.js`)
60+
61+
const args: string[] = []
5662

57-
for (const file of files) {
58-
const content = await getContent(file)
63+
if (dry) {
64+
args.push('--dry')
65+
}
5966

60-
if (existCodemod) {
61-
const newContent = existCodemod.codemod({ path: file.toString(), source: content }, options)
67+
if (print) {
68+
args.push('--print')
69+
}
6270

63-
if (!dry) {
64-
await writeFile(file.toString(), newContent)
65-
}
66-
}
71+
if (verbose) {
72+
args.push('--verbose=2')
6773
}
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)
6892
}

config.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
1-
import magicRedirect from './transforms/magic-redirect'
2-
import pluralizedMethods from './transforms/pluralized-methods'
3-
41
export const TRANSFORM_OPTIONS = [
52
{
63
description: 'Transform the deprecated magic string "back"',
74
value: 'magic-redirect',
85
version: '5.0.0',
9-
codemod: magicRedirect,
106
},
117
{
128
description: 'Transform the methods to their pluralized versions',
139
value: 'pluralized-methods',
1410
version: '5.0.0',
15-
codemod: pluralizedMethods,
1611
},
1712
//{ description: 'Transform the deprecated signatures in Express v4', value: 'signature-deprecated', version: '5.0.0' },
1813
]

index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#!/usr/bin/env node
22

3+
// Based on https://github.com/vercel/next.js/blob/26a2bab6fa0f0bdf9ff88f85d64342bb5a975658/packages/next-codemod/bin/next-codemod.ts
4+
// @expressjs/codemod optional-name-of-transform optional/path/ [...options]
5+
36
import { Command } from 'commander'
47
import { transform } from './commands/transform'
58
import packageJson from './package.json'
@@ -8,9 +11,11 @@ const program = new Command(packageJson.name)
811
.version(packageJson.version, '-v, --version', `Output the current version of ${packageJson.name}.`)
912
.description(packageJson.description)
1013
.argument('[codemod]', 'Codemod slug to run')
11-
.argument('[source]', 'Path to source files or directory to transform including glob patterns.')
14+
.argument('[source]', 'Path to source files or directory to transform.')
1215
.helpOption('-h, --help', 'Display this help message.')
1316
.option('-d, --dry', 'Dry run (no changes are made to files)')
17+
.option('-p, --print', 'Print transformed files to stdout')
18+
.option('--verbose', 'Show more information about the transform process')
1419
.usage('[codemod] [source] [options]')
1520
.action(transform)
1621
// Why this option is necessary is explained here: https://github.com/tj/commander.js/pull/1427

package.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,30 @@
66
"main": "index.js",
77
"author": "Sebastian Beltran <[email protected]>",
88
"license": "MIT",
9-
"bin": "./dist/index.js",
10-
"files": ["dist"],
9+
"bin": "./index.js",
10+
"files": ["transforms/*.js", "commands/*.js", "utils/*.js", "config.js", "index.js"],
1111
"scripts": {
12-
"dev": "ncc build ./index.ts -w -o dist/",
13-
"build": "ncc build ./index.ts -o ./dist/ --minify --no-cache --no-source-map-register",
12+
"dev": "tsc -d -w -p tsconfig.json",
13+
"build": "tsc -d -p tsconfig.json",
1414
"lint": "biome check",
1515
"lint:fix": "biome check --fix",
1616
"test": "jest",
1717
"test:ci": "jest --ci"
1818
},
1919
"dependencies": {
2020
"commander": "^12.1.0",
21+
"execa": "^5.1.1",
2122
"fast-glob": "^3.3.2",
22-
"jscodeshift": "^17.0.0",
23-
"picocolors": "1.1.1",
24-
"prompts": "2.4.2"
23+
"jscodeshift": "^17.1.1",
24+
"picocolors": "^1.1.1",
25+
"prompts": "^2.4.2"
2526
},
2627
"devDependencies": {
2728
"@biomejs/biome": "1.9.4",
2829
"@types/jest": "29.5.14",
2930
"@types/jscodeshift": "^0.12.0",
3031
"@types/node": "^22.8.1",
3132
"@types/prompts": "2.4.9",
32-
"@vercel/ncc": "0.38.2",
3333
"jest": "29.7.0",
3434
"ts-jest": "29.2.5",
3535
"typescript": "5.6.3"

utils/file.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { PathLike } from 'node:fs'
2-
import { readFile, stat } from 'node:fs/promises'
2+
import { stat } from 'node:fs/promises'
33
import { join, resolve } from 'node:path'
44
import { async as glob } from 'fast-glob'
55

@@ -14,7 +14,7 @@ export async function isDirectory(path: PathLike): Promise<boolean> {
1414

1515
export async function getAllFiles(path: PathLike, arrayOfFiles: PathLike[] = []): Promise<PathLike[]> {
1616
if (await isDirectory(path)) {
17-
const files = await glob('**/*.{js,ts}', {
17+
const files = await glob('**/*.{js,ts,cjs,mjs,cts,mts}', {
1818
cwd: path.toString(),
1919
dot: true,
2020
ignore: ['node_modules', 'dist', 'build'],
@@ -30,7 +30,3 @@ export async function getAllFiles(path: PathLike, arrayOfFiles: PathLike[] = [])
3030

3131
return arrayOfFiles
3232
}
33-
34-
export async function getContent(path: PathLike): Promise<string> {
35-
return readFile(path, 'utf-8')
36-
}

0 commit comments

Comments
 (0)