From c7525b4c3421efb38e810368ed921def49dab3df Mon Sep 17 00:00:00 2001 From: Katerina Skroumpelou Date: Fri, 10 Mar 2023 13:53:32 +0200 Subject: [PATCH] fix(storybook): better detection of failed migrations (#15588) --- .../helper-functions.spec.ts.snap | 6 +++ .../migrate-7/calling-storybook-cli.ts | 16 ++------ .../storybook-migration-summary.md__tmpl__ | 6 +++ .../generators/migrate-7/helper-functions.ts | 38 +++++++++++++++++-- .../src/generators/migrate-7/migrate-7.ts | 4 +- 5 files changed, 52 insertions(+), 18 deletions(-) diff --git a/packages/storybook/src/generators/migrate-7/__snapshots__/helper-functions.spec.ts.snap b/packages/storybook/src/generators/migrate-7/__snapshots__/helper-functions.spec.ts.snap index a6154bb29aedf..37ce1808aacdf 100644 --- a/packages/storybook/src/generators/migrate-7/__snapshots__/helper-functions.spec.ts.snap +++ b/packages/storybook/src/generators/migrate-7/__snapshots__/helper-functions.spec.ts.snap @@ -169,6 +169,8 @@ Please make sure to check the results yourself and make sure that everything is +### Some migrations failed + The following commands failed and your Storybook configuration for these projects was not migrated to the latest version 7: @@ -182,6 +184,10 @@ You can run these commands again, manually, and follow the instructions in the output of these commands to migrate your Storybook configuration to the latest version 7. +Also, we may have missed something. Please make sure to check the logs of the Storybook CLI commands that were run, and look for +the \`❌ Failed trying to evaluate\` message or \`❌ The migration failed to update\` message. This will indicate if a command was +unsuccessful, and will help you run the migration again, manually. + ## Final adjustments After the Storybook automigration scripts have run, some additional adjustments were made to your diff --git a/packages/storybook/src/generators/migrate-7/calling-storybook-cli.ts b/packages/storybook/src/generators/migrate-7/calling-storybook-cli.ts index ddb3dfaeab296..108b62b474df9 100644 --- a/packages/storybook/src/generators/migrate-7/calling-storybook-cli.ts +++ b/packages/storybook/src/generators/migrate-7/calling-storybook-cli.ts @@ -79,24 +79,14 @@ export function callAutomigrate( color: 'green', }); - const result = execSync( + execSync( `${commandToRun} ${schema.autoAcceptAllPrompts ? '--yes' : ''}`, { - stdio: [0, 1, 2], + stdio: 'inherit', } ); - const outputResult = result?.toString(); - - if ( - outputResult?.includes( - `The migration failed to update your ${storybookProjectInfo.configDir}` - ) - ) { - resultOfMigration.failedProjects[projectName] = commandToRun; - } else { - resultOfMigration.successfulProjects[projectName] = commandToRun; - } + resultOfMigration.successfulProjects[projectName] = commandToRun; } catch (e) { output.error({ title: 'Migration failed', diff --git a/packages/storybook/src/generators/migrate-7/files/storybook-migration-summary.md__tmpl__ b/packages/storybook/src/generators/migrate-7/files/storybook-migration-summary.md__tmpl__ index 192e7abbf0c11..efedcd82fd764 100644 --- a/packages/storybook/src/generators/migrate-7/files/storybook-migration-summary.md__tmpl__ +++ b/packages/storybook/src/generators/migrate-7/files/storybook-migration-summary.md__tmpl__ @@ -30,6 +30,8 @@ Please make sure to check the results yourself and make sure that everything is <% } %> <% if ( hasFailedProjects ) { %> +### Some migrations failed + The following commands failed and your Storybook configuration for these projects was not migrated to the latest version 7: @@ -41,6 +43,10 @@ You can run these commands again, manually, and follow the instructions in the output of these commands to migrate your Storybook configuration to the latest version 7. <% } %> +Also, we may have missed something. Please make sure to check the logs of the Storybook CLI commands that were run, and look for +the `❌ Failed trying to evaluate` message or `❌ The migration failed to update` message. This will indicate if a command was +unsuccessful, and will help you run the migration again, manually. + ## Final adjustments After the Storybook automigration scripts have run, some additional adjustments were made to your diff --git a/packages/storybook/src/generators/migrate-7/helper-functions.ts b/packages/storybook/src/generators/migrate-7/helper-functions.ts index 98d77851bb404..c01f3730865a8 100644 --- a/packages/storybook/src/generators/migrate-7/helper-functions.ts +++ b/packages/storybook/src/generators/migrate-7/helper-functions.ts @@ -7,11 +7,15 @@ import { readProjectConfiguration, Tree, updateProjectConfiguration, + workspaceRoot, } from '@nrwl/devkit'; import { forEachExecutorOptions } from '@nrwl/workspace/src/utilities/executor-options-utils'; import { tsquery } from '@phenomnomnominal/tsquery'; import ts = require('typescript'); import * as fs from 'fs'; +import { fileExists } from 'nx/src/utils/fileutils'; +import { readFileSync } from 'fs'; +import { join } from 'path'; export function onlyShowGuide(storybookProjects: { [key: string]: { @@ -530,10 +534,37 @@ export function handleMigrationResult( successfulProjects: {}; failedProjects: {}; }, - allStorybookProjectsLength: number -) { + allStorybookProjects: { + [key: string]: { + configDir: string; + uiFramework: string; + viteConfigFilePath?: string; + }; + } +): { successfulProjects: {}; failedProjects: {} } { + if ( + fileExists(join(workspaceRoot, 'migration-storybook.log')) && + Object.keys(migrateResult.successfulProjects)?.length + ) { + const sbLogFile = readFileSync( + join(workspaceRoot, 'migration-storybook.log'), + 'utf-8' + ); + Object.keys(migrateResult.successfulProjects).forEach((projectName) => { + if ( + sbLogFile.includes( + `The migration failed to update your ${allStorybookProjects[projectName].configDir}` + ) + ) { + migrateResult.failedProjects[projectName] = + migrateResult.successfulProjects[projectName]; + delete migrateResult.successfulProjects[projectName]; + } + }); + } + if ( - allStorybookProjectsLength === + Object.keys(allStorybookProjects)?.length === Object.keys(migrateResult.successfulProjects)?.length || Object.keys(migrateResult.failedProjects)?.length === 0 ) { @@ -578,6 +609,7 @@ export function handleMigrationResult( }); } } + return migrateResult; } export function checkStorybookInstalled(packageJson): boolean { diff --git a/packages/storybook/src/generators/migrate-7/migrate-7.ts b/packages/storybook/src/generators/migrate-7/migrate-7.ts index 5773638897777..4518858c13a3c 100644 --- a/packages/storybook/src/generators/migrate-7/migrate-7.ts +++ b/packages/storybook/src/generators/migrate-7/migrate-7.ts @@ -68,9 +68,9 @@ export async function migrate7Generator(tree: Tree, schema: Schema) { migrateResult = callAutomigrate(allStorybookProjects, schema); - handleMigrationResult( + migrateResult = handleMigrationResult( migrateResult, - Object.keys(allStorybookProjects).length + allStorybookProjects ); } }