Skip to content

Commit

Permalink
Merge pull request #26295 from storybookjs/valentin/add-addons-automi…
Browse files Browse the repository at this point in the history
…gration

Automigration: Add @storybook/addons automigration
  • Loading branch information
valentinpalkovic committed Mar 5, 2024
2 parents 4df8934 + 9f0bc7d commit 02eec68
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 1 deletion.
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,
});
};

describe('check function', () => {
it('should return { usesAddonsAPI: true } if @storybook/addons is installed', async () => {
await expect(
checkAddonsAPI({
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

0 comments on commit 02eec68

Please sign in to comment.