Skip to content

Commit

Permalink
show results
Browse files Browse the repository at this point in the history
  • Loading branch information
bjohansebas committed Dec 12, 2024
1 parent f20ce1c commit 31a6d97
Showing 1 changed file with 37 additions and 4 deletions.
41 changes: 37 additions & 4 deletions commands/upgrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { onCancel, promptSource } from '../utils/share'

const transformerDirectory = join(__dirname, '../', 'transforms')

export async function upgrade(source: string | undefined) {
export async function upgrade(source?: string) {
const sourceSelected = source || (await promptSource('Which directory should the codemods be applied to?'))

if (!sourceSelected) {
Expand All @@ -23,6 +23,12 @@ export async function upgrade(source: string | undefined) {

const codemods = suggestCodemods(packageJson)

if (codemods.length === 0) {
console.info('> No codemods are suggested for this project. \n')

return
}

const { codemodsSelected } = await prompts(
{
type: 'multiselect',
Expand All @@ -43,25 +49,52 @@ export async function upgrade(source: string | undefined) {
const args: Options = {
dry: false,
babel: false,
silent: true,
ignorePattern: '**/node_modules/**',
extensions: 'cts,mts,ts,js,mjs,cjs',
}

const results = {
ok: 0,
skipped: 0,
failed: 0,
unmodified: 0,
}

for (const codemod of codemodsSelected) {
const transformerPath = require.resolve(`${transformerDirectory}/${codemod}.js`)

await jscodeshift(transformerPath, [resolve(sourceSelected)], args)
console.log(`> Applying codemod: ${codemod}`)
const { ok, skip, error, nochange } = await jscodeshift(transformerPath, [resolve(sourceSelected)], args)

results.ok += ok
results.skipped += skip
results.failed += error
results.unmodified += nochange
}

console.log(`\n> Summary of the upgrade`)
console.log(`> ${results.ok} codemods were applied successfully`)
console.log(`> ${results.skipped} codemods were skipped`)
console.log(`> ${results.failed} codemods failed`)
console.log(`> ${results.unmodified} codemods were skipped because they didn't change anything`)
} catch (err) {
console.log(err)
if (err.code === 'ENOENT') {
console.info('> No package.json found in the selected directory. \n')
process.exit(1)
} else {
console.error(err.message)
}
}
}

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

if (dependencies?.express == null) {
return []
console.info('> No express dependency found in package.json. \n')

process.exit(0)
}

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

0 comments on commit 31a6d97

Please sign in to comment.