From 6fde64fd7764945124544324de701e49caefd883 Mon Sep 17 00:00:00 2001 From: Katerina Skroumpelou Date: Wed, 19 Apr 2023 14:33:52 +0300 Subject: [PATCH] fix(storybook): check storybook is installed and not on v7 already --- .../build-storybook/build-storybook.impl.ts | 4 ++-- .../src/executors/storybook/storybook.impl.ts | 4 ++-- .../src/migrations/update-16-0-0/update-sb-7.ts | 6 ++++++ packages/storybook/src/utils/utilities.ts | 12 ++---------- 4 files changed, 12 insertions(+), 14 deletions(-) diff --git a/packages/storybook/src/executors/build-storybook/build-storybook.impl.ts b/packages/storybook/src/executors/build-storybook/build-storybook.impl.ts index af817d7c5b7880..0b4ef483c90374 100644 --- a/packages/storybook/src/executors/build-storybook/build-storybook.impl.ts +++ b/packages/storybook/src/executors/build-storybook/build-storybook.impl.ts @@ -3,8 +3,8 @@ import * as build from '@storybook/core-server'; import { CLIOptions } from '@storybook/types'; // TODO(katerina): Remove when Storybook 7 import 'dotenv/config'; import { - isStorybookV7, storybookConfigExistsCheck, + storybookMajorVersion, } from '../../utils/utilities'; import { CommonNxStorybookConfig } from '../../utils/models'; import { @@ -18,7 +18,7 @@ export default async function buildStorybookExecutor( context: ExecutorContext ) { storybookConfigExistsCheck(options.configDir, context.projectName); - const storybook7 = isStorybookV7(); + const storybook7 = storybookMajorVersion() === 7; if (storybook7) { const buildOptions: CLIOptions = options; logger.info(`NX Storybook builder starting ...`); diff --git a/packages/storybook/src/executors/storybook/storybook.impl.ts b/packages/storybook/src/executors/storybook/storybook.impl.ts index c4ff7f89ce69a3..52e93aa1c02caf 100644 --- a/packages/storybook/src/executors/storybook/storybook.impl.ts +++ b/packages/storybook/src/executors/storybook/storybook.impl.ts @@ -2,8 +2,8 @@ import { ExecutorContext } from '@nx/devkit'; import * as build from '@storybook/core-server'; import 'dotenv/config'; import { - isStorybookV7, storybookConfigExistsCheck, + storybookMajorVersion, } from '../../utils/utilities'; import { getStorybookFrameworkPath, @@ -20,7 +20,7 @@ export default async function* storybookExecutor( success: boolean; info?: { port: number; baseUrl?: string }; }> { - const storybook7 = isStorybookV7(); + const storybook7 = storybookMajorVersion() === 7; storybookConfigExistsCheck(options.configDir, context.projectName); if (storybook7) { const buildOptions: CLIOptions = options; diff --git a/packages/storybook/src/migrations/update-16-0-0/update-sb-7.ts b/packages/storybook/src/migrations/update-16-0-0/update-sb-7.ts index 9dcdfa64696384..9ed8ec1be802a5 100644 --- a/packages/storybook/src/migrations/update-16-0-0/update-sb-7.ts +++ b/packages/storybook/src/migrations/update-16-0-0/update-sb-7.ts @@ -1,8 +1,14 @@ import { Tree } from '@nx/devkit'; import { output } from 'nx/src/utils/output'; import migrate7Generator from '../../generators/migrate-7/migrate-7'; +import { storybookMajorVersion } from '../../utils/utilities'; export default async function changeStorybookTargets(tree: Tree) { + const storybookVersion = storybookMajorVersion(); + if (!storybookVersion || storybookVersion === 7) { + return; + } + output.log({ title: 'Migrating Storybook to v7', bodyLines: [ diff --git a/packages/storybook/src/utils/utilities.ts b/packages/storybook/src/utils/utilities.ts index 0ed2dc5b125406..0e21876c79beb9 100644 --- a/packages/storybook/src/utils/utilities.ts +++ b/packages/storybook/src/utils/utilities.ts @@ -4,7 +4,7 @@ import { storybookVersion } from './versions'; import { statSync } from 'fs'; import { findNodes } from '@nx/js'; import ts = require('typescript'); -import { gte, lt, major } from 'semver'; +import { major } from 'semver'; import { join } from 'path'; export const Constants = { @@ -51,15 +51,7 @@ type Framework = { uiFramework: Constants['uiFrameworks'][keyof Constants['uiFrameworks']]; }; -export function isStorybookV7() { - const storybookPackageVersion = require(join( - '@storybook/core-server', - 'package.json' - )).version; - return gte(storybookPackageVersion, '7.0.0-alpha.0'); -} - -export function storybookMajorVersion() { +export function storybookMajorVersion(): number | undefined { try { const storybookPackageVersion = require(join( '@storybook/core-server',