Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinpalkovic committed Feb 20, 2024
1 parent dcb5d33 commit f85be76
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 68 deletions.
5 changes: 4 additions & 1 deletion code/lib/cli/src/autoblock/block-stories-mdx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import { glob } from 'glob';
export const blocker = createBlocker({
id: 'storiesMdxUsage',
async check() {
const files = await glob('**/*.stories.mdx', { cwd: process.cwd() });
const files = await glob('**/*.stories.mdx', {
cwd: process.cwd(),
ignore: ['node_modules/**'],
});
if (files.length === 0) {
return false;
}
Expand Down
50 changes: 9 additions & 41 deletions code/lib/cli/src/automigrate/fixes/prompt-remove-react.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,12 @@ const check = async ({
main: mainConfig,
storybookVersion = '8.0.0',
}: {
packageManagerContent: Pick<
Partial<Awaited<ReturnType<JsPackageManager['retrievePackageJson']>>>,
'dependencies' | 'devDependencies' | 'peerDependencies'
>;
packageManagerContent: Partial<Awaited<ReturnType<JsPackageManager['getAllDependencies']>>>;
main: Partial<StorybookConfig> & Record<string, unknown>;
storybookVersion?: string;
}) => {
const packageManager = {
retrievePackageJson: async () => packageManagerContent,
getAllDependencies: async () => packageManagerContent,
} as JsPackageManager;

return removeReactDependency.check({
Expand All @@ -31,21 +28,6 @@ const check = async ({
vi.mock('glob', () => ({ glob: vi.fn(() => []) }));

describe('early exits', () => {
it('cancel if storybookVersion < 8', async () => {
await expect(
check({
packageManagerContent: {
dependencies: { react: '16.0.0' },
},
main: {
stories: [],
framework: '@storybook/vue-vite',
},
storybookVersion: '7.0.0',
})
).resolves.toBeFalsy();
});

it('cancel if no react deps', async () => {
await expect(
check({
Expand All @@ -61,9 +43,7 @@ describe('early exits', () => {
it('cancel if react renderer', async () => {
await expect(
check({
packageManagerContent: {
dependencies: { react: '16.0.0' },
},
packageManagerContent: { react: '16.0.0' },
main: {
stories: [],
framework: '@storybook/react-vite',
Expand All @@ -73,9 +53,7 @@ describe('early exits', () => {

await expect(
check({
packageManagerContent: {
dependencies: { react: '16.0.0' },
},
packageManagerContent: { react: '16.0.0' },
main: {
stories: [],
framework: '@storybook/nextjs',
Expand All @@ -85,9 +63,7 @@ describe('early exits', () => {

await expect(
check({
packageManagerContent: {
dependencies: { react: '16.0.0' },
},
packageManagerContent: { react: '16.0.0' },
main: {
stories: [],
framework: { name: '@storybook/react-webpack5' },
Expand All @@ -101,9 +77,7 @@ describe('prompts', () => {
it('simple', async () => {
await expect(
check({
packageManagerContent: {
dependencies: { react: '16.0.0' },
},
packageManagerContent: { react: '16.0.0' },
main: {
stories: ['*.stories.ts'],
addons: [],
Expand All @@ -115,9 +89,7 @@ describe('prompts', () => {
it('detects addon docs', async () => {
await expect(
check({
packageManagerContent: {
dependencies: { react: '16.0.0' },
},
packageManagerContent: { react: '16.0.0' },
main: {
stories: ['*.stories.ts'],
addons: ['@storybook/addon-docs'],
Expand All @@ -129,9 +101,7 @@ describe('prompts', () => {
it('detects addon essentials', async () => {
await expect(
check({
packageManagerContent: {
dependencies: { react: '16.0.0' },
},
packageManagerContent: { react: '16.0.0' },
main: {
stories: ['*.stories.ts'],
addons: ['@storybook/addon-docs', '@storybook/addon-essentials'],
Expand All @@ -145,9 +115,7 @@ describe('prompts', () => {
glob.mockImplementationOnce(() => ['*.stories.mdx']);
await expect(
check({
packageManagerContent: {
dependencies: { react: '16.0.0' },
},
packageManagerContent: { react: '16.0.0' },
main: {
stories: ['*.stories.ts'],
addons: ['@storybook/addon-docs', '@storybook/addon-essentials'],
Expand Down
16 changes: 4 additions & 12 deletions code/lib/cli/src/automigrate/fixes/prompt-remove-react.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import dedent from 'ts-dedent';
import semver from 'semver';
import { getFrameworkPackageName } from '../helpers/mainConfigFile';
import type { Fix } from '../types';

Expand All @@ -10,27 +9,20 @@ export const removeReactDependency: Fix<{}> = {
id: 'remove-react-dependency',
promptType: 'manual',

versionRange: ['>=7 <8.0.0-alpha.4', `>=${minimumStorybookVersion}`],
versionRange: [`^7 || <${minimumStorybookVersion}`, `>=${minimumStorybookVersion}`],

async check({ packageManager, mainConfig, storybookVersion }) {
// when the user is using the react renderer, we should not prompt them to remove react
const frameworkPackageName = getFrameworkPackageName(mainConfig);

if (frameworkPackageName?.includes('react') || frameworkPackageName?.includes('nextjs')) {
return null;
}

// if the user has no dependency on react, we can skip this fix
const packageJson = await packageManager.retrievePackageJson();
if (
!packageJson?.dependencies?.['react'] &&
!packageJson?.peerDependencies?.['react'] &&
!packageJson?.devDependencies?.['react']
) {
return null;
}
const { react } = await packageManager.getAllDependencies();

// do not prompt to remove react for older versions of storybook
if (!semver.gte(storybookVersion, minimumStorybookVersion)) {
if (!react) {
return null;
}

Expand Down
5 changes: 4 additions & 1 deletion code/lib/cli/src/automigrate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
type JsPackageManager,
getCoercedStorybookVersion,
getStorybookInfo,
rendererPackages,
} from '@storybook/core-common';

import type {
Expand Down Expand Up @@ -156,8 +157,10 @@ export const automigrate = async ({

if (!hideMigrationSummary) {
const installationMetadata = await packageManager.findInstallations([
'@storybook/*',
'@storybook/cli',
'storybook',
...Object.keys(rendererPackages),
'@storybook/*',
]);

logger.info();
Expand Down
10 changes: 8 additions & 2 deletions code/lib/cli/src/doctor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import tempy from 'tempy';
import dedent from 'ts-dedent';
import { join } from 'path';

import { JsPackageManagerFactory } from '@storybook/core-common';
import {
JsPackageManagerFactory,
frameworkPackages,
rendererPackages,
} from '@storybook/core-common';
import type { PackageManagerName } from '@storybook/core-common';
import { getStorybookData } from '../automigrate/helpers/mainConfigFile';
import { cleanLog } from '../automigrate/helpers/cleanLog';
Expand Down Expand Up @@ -95,8 +99,10 @@ export const doctor = async ({
}

const installationMetadata = await packageManager.findInstallations([
'@storybook/*',
'@storybook/cli',
'storybook',
...Object.keys(rendererPackages),
...Object.keys(frameworkPackages),
]);

const allDependencies = (await packageManager.getAllDependencies()) as Record<string, string>;
Expand Down
13 changes: 7 additions & 6 deletions code/lib/cli/src/upgrade.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { describe, it, expect, vi } from 'vitest';
import { rendererPackages } from '@storybook/core-common';
import {
UpgradeStorybookToLowerVersionError,
UpgradeStorybookToSameVersionError,
} from '@storybook/core-events/server-errors';
import { doUpgrade, getStorybookVersion } from './upgrade';

import type * as sbcc from '@storybook/core-common';

const findInstallationsMock = vi.fn<string[], Promise<sbcc.InstallationMetadata | undefined>>();

vi.mock('@storybook/telemetry');
Expand Down Expand Up @@ -68,8 +67,9 @@ describe('Upgrade errors', () => {
await expect(doUpgrade({} as any)).rejects.toThrowError(UpgradeStorybookToLowerVersionError);
expect(findInstallationsMock).toHaveBeenCalledWith([
'@storybook/cli',
'@storybook/core',
'@storybook/core-common',
'storybook',
...Object.keys(rendererPackages),
'@storybook/*',
]);
});
it('should throw an error when upgrading to the same version number', async () => {
Expand All @@ -89,8 +89,9 @@ describe('Upgrade errors', () => {
await expect(doUpgrade({} as any)).rejects.toThrowError(UpgradeStorybookToSameVersionError);
expect(findInstallationsMock).toHaveBeenCalledWith([
'@storybook/cli',
'@storybook/core',
'@storybook/core-common',
'storybook',
...Object.keys(rendererPackages),
'@storybook/*',
]);
});
});
9 changes: 4 additions & 5 deletions code/lib/cli/src/upgrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
getStorybookInfo,
loadMainConfig,
JsPackageManagerFactory,
rendererPackages,
} from '@storybook/core-common';
import { automigrate } from './automigrate/index';
import { autoblock } from './autoblock/index';
Expand All @@ -41,12 +42,10 @@ export const getStorybookVersion = (line: string) => {

const getInstalledStorybookVersion = async (packageManager: JsPackageManager) => {
const installations = await packageManager.findInstallations([
// Storybook 3.0.0+ | the installation wasn't required though in Storybook < 7.0.0
'@storybook/cli',
// Storybook 3.3.3 - 6.5.16
'@storybook/core',
// Storybook 6.2+
'@storybook/core-common',
'storybook',
...Object.keys(rendererPackages),
'@storybook/*',
]);
if (!installations) {
return;
Expand Down

0 comments on commit f85be76

Please sign in to comment.