Skip to content

Commit

Permalink
CLI: add automigration summary
Browse files Browse the repository at this point in the history
  • Loading branch information
yannbf committed Dec 14, 2022
1 parent 46721c0 commit 8a53e38
Showing 1 changed file with 51 additions and 13 deletions.
64 changes: 51 additions & 13 deletions code/lib/cli/src/automigrate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { JsPackageManagerFactory, type PackageManagerName } from '../js-package-

import type { Fix } from './fixes';
import { fixes } from './fixes';
import dedent from 'ts-dedent';

const logger = console;

Expand Down Expand Up @@ -33,26 +34,27 @@ export const automigrate = async ({ fixId, dryRun, yes, useNpm, force }: FixOpti

logger.info('🔎 checking possible migrations..');
const fixResults = {} as Record<FixId, FixStatus>;
const fixSummary = { succeeded: [], failed: {} } as { succeeded: FixId[], failed: Record<FixId, string>};

for (let i = 0; i < filtered.length; i += 1) {
const f = fixes[i] as Fix;
let result;
let fixStatus;
let fixStatus = FixStatus.UNNECESSARY;

try {
result = await f.check({ packageManager });
} catch (e) {
} catch (error) {
logger.info(`⚠️ failed to check fix ${chalk.bold(f.id)}`);
fixStatus = FixStatus.CHECK_FAILED;
logger.info(`failed to check fix: ${f.id}`);
fixSummary.failed[f.id] = error.message;
}
if (!result) {
fixStatus = FixStatus.UNNECESSARY;
} else {
logger.info(`🔎 found a '${chalk.cyan(f.id)}' migration:`);
logger.info();

if (result) {
logger.info(`\n🔎 found a '${chalk.cyan(f.id)}' migration:`);
const message = f.prompt(result);

logger.info(
boxen(message, { borderStyle: 'round', padding: 1, borderColor: '#F1618C' } as any)
boxen(message, { borderStyle: 'round', padding: 1, borderColor: '#F1618C' })
);

let runAnswer: { fix: boolean };
Expand All @@ -75,8 +77,10 @@ export const automigrate = async ({ fixId, dryRun, yes, useNpm, force }: FixOpti
await f.run({ result, packageManager, dryRun });
logger.info(`✅ ran ${chalk.cyan(f.id)} migration`);
fixStatus = FixStatus.SUCCEEDED;
fixSummary.succeeded.push(f.id);
} catch (error) {
fixStatus = FixStatus.FAILED;
fixSummary.failed[f.id] = error.message;
logger.info(`❌ error when running ${chalk.cyan(f.id)} migration:`);
logger.info(error);
logger.info();
Expand All @@ -91,12 +95,46 @@ export const automigrate = async ({ fixId, dryRun, yes, useNpm, force }: FixOpti
}
}

fixResults[f.id] = fixStatus;
fixResults[f.id] = fixStatus
}

logger.info();
logger.info('✅ migration check successfully ran');
logger.info();
logger.info()
logger.info(getMigrationSummary(fixResults, fixSummary));
logger.info()

return fixResults;
};

function getMigrationSummary(fixResults: Record<string, FixStatus>, fixSummary: { succeeded: FixId[]; failed: Record<FixId, string>; }) {
const hasNoFixes = Object.values(fixResults).every((r) => r === FixStatus.UNNECESSARY);
const hasFailures = Object.values(fixResults).some((r) => r === FixStatus.FAILED || r === FixStatus.CHECK_FAILED);
let title = hasNoFixes ? 'No migrations were applicable to your project' : hasFailures ? 'Migration check ran with failures' : 'Migration check ran successfully';

let successfulFixesMessage = fixSummary.succeeded.length > 0
? `
${chalk.bold('Migrations that succeeded:')}\n\n ${fixSummary.succeeded.map(m => chalk.magenta(m)).join(', ')}
`
: '';

let failedFixesMessage = Object.keys(fixSummary.failed).length > 0
? `
${chalk.bold('Migrations that failed:')}\n ${Object.entries(fixSummary.failed).reduce((acc, [id, error]) => {
return acc + `\n${chalk.magenta(id)}:\n${error}`;
}, '')}
\n`
: '';

const divider = hasNoFixes ? '' : '\n─────────────────────────────────────────────────\n\n';

let summaryMessage = dedent`
${successfulFixesMessage}${failedFixesMessage}${divider}If you'd like to run the migrations again, you can do so by running '${chalk.cyan('npx storybook@next automigrate')}'
The automigrations try to migrate common patterns in your project, but might not contain everything needed to migrate to the latest version of Storybook.
Please check the changelog and migration guide for manual migrations and more information: ${chalk.yellow('https://storybook.js.org/migration-guides/7.0')}
And reach out on Discord if you need help: ${chalk.yellow('https://discord.gg/storybook')}
`;

return boxen(summaryMessage, { borderStyle: 'round', padding: 1, title, borderColor: hasFailures ? 'red' : 'green' });
}

0 comments on commit 8a53e38

Please sign in to comment.