mirror of
https://github.com/storybookjs/storybook.git
synced 2025-04-01 05:05:25 +08:00
add automigration warning users, add test for automigration
This commit is contained in:
parent
a3dbdc7673
commit
181ad75a23
@ -8,9 +8,11 @@ import { builderVite } from './builder-vite';
|
||||
import { npm7 } from './npm7';
|
||||
import { sbScripts } from './sb-scripts';
|
||||
import { newFrameworks } from './new-frameworks';
|
||||
import { removedGlobalClientAPIs } from './remove-global-client-apis';
|
||||
import { Fix } from '../types';
|
||||
|
||||
export * from '../types';
|
||||
|
||||
export const fixes: Fix[] = [
|
||||
cra5,
|
||||
webpack5,
|
||||
@ -22,4 +24,5 @@ export const fixes: Fix[] = [
|
||||
npm7,
|
||||
sbScripts,
|
||||
newFrameworks,
|
||||
removedGlobalClientAPIs,
|
||||
];
|
||||
|
@ -0,0 +1,58 @@
|
||||
/// <reference types="@types/jest" />;
|
||||
|
||||
/* eslint-disable no-underscore-dangle */
|
||||
import path from 'path';
|
||||
import { JsPackageManager } from '../../js-package-manager';
|
||||
import { RemovedAPIs, removedGlobalClientAPIs as migration } from './remove-global-client-apis';
|
||||
|
||||
// eslint-disable-next-line global-require, jest/no-mocks-import
|
||||
jest.mock('fs-extra', () => require('../../../../../__mocks__/fs-extra'));
|
||||
|
||||
const check = async ({ packageJson = {}, contents }) => {
|
||||
if (contents) {
|
||||
// eslint-disable-next-line global-require
|
||||
require('fs-extra').__setMockFiles({
|
||||
[path.join('.storybook', 'preview.js')]: contents,
|
||||
});
|
||||
}
|
||||
const packageManager = {
|
||||
retrievePackageJson: () => ({ dependencies: {}, devDependencies: {}, ...packageJson }),
|
||||
} as JsPackageManager;
|
||||
return migration.check({ packageManager });
|
||||
};
|
||||
|
||||
describe('removedGlobalClientAPIs fix', () => {
|
||||
it('file does not exist', async () => {
|
||||
const contents = false;
|
||||
await expect(check({ contents })).resolves.toBeNull();
|
||||
});
|
||||
it('uses no removed APIs', async () => {
|
||||
const contents = `
|
||||
export const parameters = {};
|
||||
`;
|
||||
await expect(check({ contents })).resolves.toBeNull();
|
||||
});
|
||||
it('uses 1 removed API', async () => {
|
||||
const contents = `
|
||||
import { addParameters } from '@storybook/react';
|
||||
addParameters({});
|
||||
`;
|
||||
await expect(check({ contents })).resolves.toEqual(
|
||||
expect.objectContaining({
|
||||
usedAPIs: [RemovedAPIs.addParameters],
|
||||
})
|
||||
);
|
||||
});
|
||||
it('uses >1 removed APIs', async () => {
|
||||
const contents = `
|
||||
import { addParameters, addDecorator } from '@storybook/react';
|
||||
addParameters({});
|
||||
addDecorator((storyFn) => storyFn());
|
||||
`;
|
||||
await expect(check({ contents })).resolves.toEqual(
|
||||
expect.objectContaining({
|
||||
usedAPIs: expect.arrayContaining([RemovedAPIs.addParameters, RemovedAPIs.addDecorator]),
|
||||
})
|
||||
);
|
||||
});
|
||||
});
|
@ -0,0 +1,65 @@
|
||||
import chalk from 'chalk';
|
||||
import dedent from 'ts-dedent';
|
||||
import { getStorybookInfo } from '@storybook/core-common';
|
||||
import { readFile } from 'fs-extra';
|
||||
import { Fix } from '../types';
|
||||
|
||||
export enum RemovedAPIs {
|
||||
addDecorator = 'addDecorator',
|
||||
addParameters = 'addParameters',
|
||||
addLoader = 'addLoader',
|
||||
getStorybook = 'getStorybook',
|
||||
setAddon = 'setAddon',
|
||||
clearDecorators = 'clearDecorators',
|
||||
}
|
||||
|
||||
interface GlobalClientAPIOptions {
|
||||
usedAPIs: RemovedAPIs[];
|
||||
previewPath: string;
|
||||
}
|
||||
|
||||
export const removedGlobalClientAPIs: Fix<GlobalClientAPIOptions> = {
|
||||
id: 'removedglobalclientapis',
|
||||
|
||||
async check({ packageManager }) {
|
||||
const packageJson = packageManager.retrievePackageJson();
|
||||
|
||||
const { previewConfig } = getStorybookInfo(packageJson);
|
||||
|
||||
if (previewConfig) {
|
||||
const contents = await readFile(previewConfig, 'utf8');
|
||||
|
||||
const usedAPIs = Object.values(RemovedAPIs).reduce((acc, item) => {
|
||||
if (contents.includes(item)) {
|
||||
acc.push(item);
|
||||
}
|
||||
return acc;
|
||||
}, [] as RemovedAPIs[]);
|
||||
|
||||
if (usedAPIs.length) {
|
||||
return {
|
||||
usedAPIs,
|
||||
previewPath: previewConfig,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
},
|
||||
prompt({ usedAPIs, previewPath }) {
|
||||
return dedent`
|
||||
The following APIs (used in "${chalk.yellow(previewPath)}") have been removed from Storybook:
|
||||
${usedAPIs.map(chalk.cyan).join(', ')}
|
||||
|
||||
You'll need to update "${chalk.yellow(previewPath)}" manually.
|
||||
|
||||
Please see the migration guide for more information:
|
||||
${chalk.yellow(
|
||||
'https://github.com/storybookjs/storybook/blob/next/MIGRATION.md#removed-global-client-apis'
|
||||
)}
|
||||
`;
|
||||
},
|
||||
async run() {
|
||||
console.log('Skipping automatic fix for removed global client APIs');
|
||||
},
|
||||
};
|
Loading…
x
Reference in New Issue
Block a user