Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(storybook): do not override existing v7 on init #16405

Merged
merged 1 commit into from
Apr 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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