From f85be7676a11a4fa86874844ceff399e125331a7 Mon Sep 17 00:00:00 2001 From: Valentin Palkovic Date: Tue, 20 Feb 2024 10:10:54 +0100 Subject: [PATCH] WIP --- .../cli/src/autoblock/block-stories-mdx.ts | 5 +- .../fixes/prompt-remove-react.test.ts | 50 ++++--------------- .../automigrate/fixes/prompt-remove-react.ts | 16 ++---- code/lib/cli/src/automigrate/index.ts | 5 +- code/lib/cli/src/doctor/index.ts | 10 +++- code/lib/cli/src/upgrade.test.ts | 13 ++--- code/lib/cli/src/upgrade.ts | 9 ++-- 7 files changed, 40 insertions(+), 68 deletions(-) diff --git a/code/lib/cli/src/autoblock/block-stories-mdx.ts b/code/lib/cli/src/autoblock/block-stories-mdx.ts index b868d913ecd0..99ab3e643827 100644 --- a/code/lib/cli/src/autoblock/block-stories-mdx.ts +++ b/code/lib/cli/src/autoblock/block-stories-mdx.ts @@ -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; } diff --git a/code/lib/cli/src/automigrate/fixes/prompt-remove-react.test.ts b/code/lib/cli/src/automigrate/fixes/prompt-remove-react.test.ts index 7d33c0fe90aa..baf9922b5f0f 100644 --- a/code/lib/cli/src/automigrate/fixes/prompt-remove-react.test.ts +++ b/code/lib/cli/src/automigrate/fixes/prompt-remove-react.test.ts @@ -9,15 +9,12 @@ const check = async ({ main: mainConfig, storybookVersion = '8.0.0', }: { - packageManagerContent: Pick< - Partial>>, - 'dependencies' | 'devDependencies' | 'peerDependencies' - >; + packageManagerContent: Partial>>; main: Partial & Record; storybookVersion?: string; }) => { const packageManager = { - retrievePackageJson: async () => packageManagerContent, + getAllDependencies: async () => packageManagerContent, } as JsPackageManager; return removeReactDependency.check({ @@ -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({ @@ -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', @@ -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', @@ -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' }, @@ -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: [], @@ -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'], @@ -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'], @@ -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'], diff --git a/code/lib/cli/src/automigrate/fixes/prompt-remove-react.ts b/code/lib/cli/src/automigrate/fixes/prompt-remove-react.ts index c95822da64dc..65463ec015ea 100644 --- a/code/lib/cli/src/automigrate/fixes/prompt-remove-react.ts +++ b/code/lib/cli/src/automigrate/fixes/prompt-remove-react.ts @@ -1,5 +1,4 @@ import dedent from 'ts-dedent'; -import semver from 'semver'; import { getFrameworkPackageName } from '../helpers/mainConfigFile'; import type { Fix } from '../types'; @@ -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; } diff --git a/code/lib/cli/src/automigrate/index.ts b/code/lib/cli/src/automigrate/index.ts index 92d21e2ec6c2..94dd0f1e2226 100644 --- a/code/lib/cli/src/automigrate/index.ts +++ b/code/lib/cli/src/automigrate/index.ts @@ -12,6 +12,7 @@ import { type JsPackageManager, getCoercedStorybookVersion, getStorybookInfo, + rendererPackages, } from '@storybook/core-common'; import type { @@ -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(); diff --git a/code/lib/cli/src/doctor/index.ts b/code/lib/cli/src/doctor/index.ts index 9aaeede8bc0f..3beb1423e3d8 100644 --- a/code/lib/cli/src/doctor/index.ts +++ b/code/lib/cli/src/doctor/index.ts @@ -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'; @@ -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; diff --git a/code/lib/cli/src/upgrade.test.ts b/code/lib/cli/src/upgrade.test.ts index 5a72e3130628..e67878dca2cc 100644 --- a/code/lib/cli/src/upgrade.test.ts +++ b/code/lib/cli/src/upgrade.test.ts @@ -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>(); vi.mock('@storybook/telemetry'); @@ -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 () => { @@ -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/*', ]); }); }); diff --git a/code/lib/cli/src/upgrade.ts b/code/lib/cli/src/upgrade.ts index 7958eacbb9b5..dc7645972955 100644 --- a/code/lib/cli/src/upgrade.ts +++ b/code/lib/cli/src/upgrade.ts @@ -18,6 +18,7 @@ import { getStorybookInfo, loadMainConfig, JsPackageManagerFactory, + rendererPackages, } from '@storybook/core-common'; import { automigrate } from './automigrate/index'; import { autoblock } from './autoblock/index'; @@ -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;