Skip to content

Commit

Permalink
fix(storybook): do not override existing v7 on init (#16405)
Browse files Browse the repository at this point in the history
  • Loading branch information
mandarini authored Apr 19, 2023
1 parent 1b7587f commit 157b35b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
20 changes: 17 additions & 3 deletions packages/storybook/src/generators/init/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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 (
Expand All @@ -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 (
Expand Down
12 changes: 12 additions & 0 deletions packages/storybook/src/utils/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 157b35b

Please sign in to comment.