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

CLI: Fix version detection compatibility with pre SB7.5 #26055

Closed
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
10 changes: 9 additions & 1 deletion code/lib/cli/src/upgrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
import { automigrate } from './automigrate/index';
import { autoblock } from './autoblock/index';
import { PreCheckFailure } from './automigrate/types';
import { legacy_getStorybookVersion } from './utils/legacy_storybookVersionDetection';

type Package = {
package: string;
Expand All @@ -43,7 +44,14 @@ export const getStorybookVersion = (line: string) => {
const getInstalledStorybookVersion = async (packageManager: JsPackageManager) => {
const installations = await packageManager.findInstallations(['storybook', '@storybook/cli']);
if (!installations) {
return;
/**
* In older versions of Storybook, the user may not have a direct dependency on the CLI package.
* In this case, we need to use the legacy method to determine the version.
* @see legacy_getStorybookVersion
* @deprecated
* @remove in SB9 (or when we drop support for upgrading from SB6 & SB7)
*/
return legacy_getStorybookVersion();
Comment on lines +47 to +54
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So where before we returned undefined which would cause an "no version detected"-error... we now have this fallback that should operate exactly how it was in SB6.

}
const cliVersion = installations.dependencies['@storybook/cli']?.[0].version;
if (cliVersion) {
Expand Down
59 changes: 59 additions & 0 deletions code/lib/cli/src/utils/legacy_storybookVersionDetection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { sync as spawnSync } from 'cross-spawn';
import semver from 'semver';

const legacy_versionRegex = /(@storybook\/[^@]+)@(\S+)/;

const legacy_excludeList = [
'@storybook/linter-config',
'@storybook/design-system',
'@storybook/ember-cli-storybook',
'@storybook/semver',
'@storybook/eslint-config-storybook',
'@storybook/bench',
'@storybook/addon-bench',
'@storybook/addon-console',
'@storybook/csf',
'@storybook/storybook-deployer',
'@storybook/addon-postcss',
'@storybook/react-docgen-typescript-plugin',
'@storybook/babel-plugin-require-context-hook',
'@storybook/builder-vite',
'@storybook/mdx1-csf',
'@storybook/mdx2-csf',
'@storybook/expect',
'@storybook/jest',
'@storybook/test-runner',
'@storybook/testing-library',
];
const legacy_isCorePackage = (pkg: string) =>
pkg.startsWith('@storybook/') &&
!pkg.startsWith('@storybook/preset-') &&
!legacy_excludeList.includes(pkg);

/**
* @deprecated This function has been deprecated. This is solely to support upgrading from SB6 (and possible SB7)
* I took this code from https://github.com/storybookjs/storybook/blob/v6.5.16/lib/cli/src/upgrade.ts#L29-L109
*/
export const legacy_getStorybookVersion = (): string | undefined => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's wait for the adjustments of findInstallations I did here: https://github.com/storybookjs/storybook/pull/26067/files#diff-afe49a7cb8e5ea8e3d02751da8f553064c9a6ab67a0a819ac8048a98bf43f506R133 and then reuse packageManager.findInstallations here.

Then the support for multiple package managers is guaranteed.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I personally don't feel like we should spend a lot of time refining this as it's purely to temporarily support an upgrade from SB6 to SBX

const lines = spawnSync('npm', ['ls'], { stdio: 'pipe' }).output.toString().split('\n');
const result = lines
.map((line) => {
if (line.startsWith('npm ')) return null;
const match = legacy_versionRegex.exec(line);
if (!match || !semver.clean(match[2])) return null;
return {
package: match[1],
version: match[2],
};
})
.filter(Boolean)
.filter((pkg) => legacy_isCorePackage(pkg!.package))
.sort((a, b) => semver.rcompare(a!.version, b!.version));

if (result.length === 0) {
return undefined;
}

return result[0]?.version;
};
Loading