Skip to content

Commit

Permalink
feat: show suggetions options
Browse files Browse the repository at this point in the history
  • Loading branch information
bjohansebas committed Dec 7, 2024
1 parent 5f2d03d commit d83aa6a
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 24 deletions.
85 changes: 62 additions & 23 deletions commands/upgrade.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import { TRANSFORM_OPTIONS } from '../config'
import { join } from 'node:path'
import { getAllFiles } from '../utils/file'
import { readFile } from 'node:fs/promises'
import { join, resolve } from 'node:path'
import type { Options } from 'jscodeshift'
import { run as jscodeshift } from 'jscodeshift/src/Runner'
import prompts from 'prompts'
import execa from 'execa'

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

import { coerce, compare } from 'semver'
import { TRANSFORM_OPTIONS } from '../config'
import { onCancel } from '../utils/share'

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

export async function upgrade(source: string | undefined) {
let sourceSelected = source

if (!sourceSelected) {
Expand All @@ -28,21 +26,62 @@ export async function upgrade(source: string): Promise<void> {
sourceSelected = res.path
}

const files = await getAllFiles(sourceSelected)
try {
const packageJsonPath = resolve(sourceSelected || '', 'package.json')
const packageJson = JSON.parse(await readFile(packageJsonPath, 'utf8'))

const args: string[] = []
const codemods = suggestCodemods(packageJson)

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()))
const { codemodsSelected } = await prompts(
{
type: 'multiselect',
name: 'codemodsSelected',
message: `The following 'codemods' are recommended for your upgrade. Select the ones to apply.`,
choices: codemods.map(({ description, value, version }) => {
return {
title: `(v${version}) ${value}`,
description,
value,
selected: true,
}
}),
},
{ onCancel },
)

const args: Options = {
dry: false,
babel: false,
ignorePattern: '**/node_modules/**',
extensions: 'cts,mts,ts,js,mjs,cjs',
}
const results: object[] = []

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)
for (const codemod of codemodsSelected) {
const transformerPath = require.resolve(`${transformerDirectory}/${codemod}.js`)
const jscodeshiftProcess = await jscodeshift(transformerPath, [sourceSelected || ''], args)

results.push(jscodeshiftProcess)
}

return results
} catch (err) {
console.log(err)
}
}

function suggestCodemods(packageJson) {
const { dependencies } = packageJson

if (dependencies?.express == null) {
return []
}

const expressVersion = coerce(dependencies.express)?.version ?? '4.0.0'

const codemodsSuggested = TRANSFORM_OPTIONS.filter((a) => {
return compare(a.version, expressVersion) > 0
})

return codemodsSuggested
}
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,16 @@
"fast-glob": "^3.3.2",
"jscodeshift": "^17.1.1",
"picocolors": "^1.1.1",
"prompts": "^2.4.2"
"prompts": "^2.4.2",
"semver": "^7.6.3"
},
"devDependencies": {
"@biomejs/biome": "1.9.4",
"@types/jest": "29.5.14",
"@types/jscodeshift": "^0.12.0",
"@types/node": "^22.8.1",
"@types/prompts": "2.4.9",
"@types/semver": "^7.5.8",
"jest": "29.7.0",
"ts-jest": "29.2.5",
"typescript": "5.6.3"
Expand Down
3 changes: 3 additions & 0 deletions utils/share.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function onCancel() {
process.exit(1)
}

0 comments on commit d83aa6a

Please sign in to comment.