Skip to content

Commit

Permalink
fix(storybook): better detection of failed migrations (#15588)
Browse files Browse the repository at this point in the history
  • Loading branch information
mandarini committed Mar 10, 2023
1 parent 79f88dc commit c7525b4
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -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
Expand Down
38 changes: 35 additions & 3 deletions packages/storybook/src/generators/migrate-7/helper-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]: {
Expand Down Expand Up @@ -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
) {
Expand Down Expand Up @@ -578,6 +609,7 @@ export function handleMigrationResult(
});
}
}
return migrateResult;
}

export function checkStorybookInstalled(packageJson): boolean {
Expand Down
4 changes: 2 additions & 2 deletions packages/storybook/src/generators/migrate-7/migrate-7.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
}
Expand Down

0 comments on commit c7525b4

Please sign in to comment.