From 157b35b5a05f9b750a8eeca377666a791e779a4e Mon Sep 17 00:00:00 2001 From: Katerina Skroumpelou Date: Wed, 19 Apr 2023 17:33:10 +0300 Subject: [PATCH] fix(storybook): do not override existing v7 on init (#16405) --- .../storybook/src/generators/init/init.ts | 20 ++++++++++++++++--- packages/storybook/src/utils/utilities.ts | 12 +++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/packages/storybook/src/generators/init/init.ts b/packages/storybook/src/generators/init/init.ts index c83257b738591..6444369f434b0 100644 --- a/packages/storybook/src/generators/init/init.ts +++ b/packages/storybook/src/generators/init/init.ts @@ -30,6 +30,11 @@ import { webpack5Version, } from '../../utils/versions'; import { Schema } from './schema'; +import { + getInstalledStorybookVersion, + storybookMajorVersion, +} from '../../utils/utilities'; +import { gte } from 'semver'; function checkDependenciesInstalled(host: Tree, schema: Schema) { const packageJson = readJson(host, 'package.json'); @@ -43,6 +48,15 @@ function checkDependenciesInstalled(host: Tree, schema: Schema) { devDependencies['@nx/storybook'] = nxVersion; if (schema.storybook7Configuration) { + let storybook7VersionToInstall = storybook7Version; + if ( + storybookMajorVersion() === 7 && + getInstalledStorybookVersion() && + gte(getInstalledStorybookVersion(), '7.0.0') + ) { + storybook7VersionToInstall = getInstalledStorybookVersion(); + } + // Needed for Storybook 7 // https://github.com/storybookjs/storybook/blob/next/MIGRATION.md#react-peer-dependencies-required if ( @@ -61,10 +75,10 @@ function checkDependenciesInstalled(host: Tree, schema: Schema) { if (schema.uiFramework === '@storybook/react-native') { devDependencies['@storybook/react-native'] = storybookReactNativeVersion; } else { - devDependencies[schema.uiFramework] = storybook7Version; + devDependencies[schema.uiFramework] = storybook7VersionToInstall; } - devDependencies['@storybook/core-server'] = storybook7Version; - devDependencies['@storybook/addon-essentials'] = storybook7Version; + devDependencies['@storybook/core-server'] = storybook7VersionToInstall; + devDependencies['@storybook/addon-essentials'] = storybook7VersionToInstall; if (schema.uiFramework === '@storybook/angular') { if ( diff --git a/packages/storybook/src/utils/utilities.ts b/packages/storybook/src/utils/utilities.ts index 0e21876c79beb..74c0537d44d9f 100644 --- a/packages/storybook/src/utils/utilities.ts +++ b/packages/storybook/src/utils/utilities.ts @@ -63,6 +63,18 @@ export function storybookMajorVersion(): number | undefined { } } +export function getInstalledStorybookVersion(): string | undefined { + try { + const storybookPackageVersion = require(join( + '@storybook/core-server', + 'package.json' + )).version; + return storybookPackageVersion; + } catch { + return undefined; + } +} + export function safeFileDelete(tree: Tree, path: string): boolean { if (tree.exists(path)) { tree.delete(path);