Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLI: Add @storybook/addons automigration #26295

Merged
merged 1 commit into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion code/lib/cli/src/automigrate/fixes/addon-postcss.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const addonPostCSS: Fix<AddonPostcssRunOptions> = {
return dedent`
${chalk.bold(
'Attention'
)}: We've detected that you're using the following package which are incompatible with Storybook 8 and beyond:
)}: We've detected that you're using the following package which is incompatible with Storybook 8 and beyond:

- ${chalk.cyan(`@storybook/addon-postcss`)}

Expand Down
44 changes: 44 additions & 0 deletions code/lib/cli/src/automigrate/fixes/addons-api.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { addonsAPI } from './addons-api';
import type { StorybookConfig } from '@storybook/types';
import type { JsPackageManager } from '@storybook/core-common';
import { expect, describe, it } from 'vitest';

const checkAddonsAPI = async ({
packageManager,
mainConfig = {},
storybookVersion = '7.0.0',
}: {
packageManager?: Partial<JsPackageManager>;
mainConfig?: Partial<StorybookConfig>;
storybookVersion?: string;
}) => {
return addonsAPI.check({
packageManager: packageManager as any,
storybookVersion,
mainConfig: mainConfig as any,
valentinpalkovic marked this conversation as resolved.
Show resolved Hide resolved
});
};

describe('check function', () => {
it('should return { usesAddonsAPI: true } if @storybook/addons is installed', async () => {
await expect(
valentinpalkovic marked this conversation as resolved.
Show resolved Hide resolved
checkAddonsAPI({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be neccesary if you await this, and then remove the resolves

packageManager: {
getAllDependencies: async () => ({
'@storybook/addons': '6.0.0',
}),
},
})
).resolves.toEqual({ usesAddonsAPI: true });
});

it('should return null if @storybook/addons is not installed', async () => {
await expect(
checkAddonsAPI({
packageManager: {
getAllDependencies: async () => ({}),
},
})
).resolves.toBeNull();
});
});
45 changes: 45 additions & 0 deletions code/lib/cli/src/automigrate/fixes/addons-api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import chalk from 'chalk';
import { dedent } from 'ts-dedent';
import type { Fix } from '../types';

interface AddonsAPIRunOptions {
usesAddonsAPI: boolean;
}

export const addonsAPI: Fix<AddonsAPIRunOptions> = {
id: 'addons-api',

versionRange: ['<8', '>=8'],

promptType: 'notification',

async check({ packageManager }) {
const allDependencies = await packageManager.getAllDependencies();
const usesAddonsAPI = !!allDependencies['@storybook/addons'];

if (!usesAddonsAPI) {
return null;
}

return { usesAddonsAPI: true };
},

prompt() {
return dedent`
${chalk.bold(
'Attention'
)}: We've detected that you're using the following package which is removed in Storybook 8 and beyond:

- ${chalk.cyan(`@storybook/addons`)}

This package has been deprecated and replaced with ${chalk.cyan(
`@storybook/preview-api`
)} and ${chalk.cyan(`@storybook/manager-api`)}.

You can find more information about the new addons API in the migration guide:
${chalk.yellow(
'https://github.com/storybookjs/storybook/blob/next/MIGRATION.md#new-addons-api'
)}
`;
},
};
2 changes: 2 additions & 0 deletions code/lib/cli/src/automigrate/fixes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ import { storyshotsMigration } from './storyshots-migration';
import { removeArgtypesRegex } from './remove-argtypes-regex';
import { webpack5CompilerSetup } from './webpack5-compiler-setup';
import { removeJestTestingLibrary } from './remove-jest-testing-library';
import { addonsAPI } from './addons-api';
import { mdx1to3 } from './mdx-1-to-3';
import { addonPostCSS } from './addon-postcss';

export * from '../types';

export const allFixes: Fix[] = [
addonsAPI,
newFrameworks,
cra5,
webpack5,
Expand Down