From b988b75485a252b2e6422bb2e8a5ea31b36ebc74 Mon Sep 17 00:00:00 2001 From: Sebastian Beltran Date: Wed, 11 Dec 2024 21:18:14 -0500 Subject: [PATCH] show results --- commands/upgrade.ts | 41 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/commands/upgrade.ts b/commands/upgrade.ts index edeb4f4..d21d394 100644 --- a/commands/upgrade.ts +++ b/commands/upgrade.ts @@ -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) { @@ -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', @@ -43,17 +49,42 @@ 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) + } } } @@ -61,7 +92,9 @@ 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'