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

Conversation

ndelangen
Copy link
Member

@ndelangen ndelangen commented Feb 16, 2024

Closes #25975

What I did

  • add a backup version detector ripped straight out of SB6

The problem was that some users on older versions don't have the SB CLI as a (direct) dependency. Because back in the good old days, we shipped a bin with every renderer

Checklist for Contributors

Testing

The changes in this PR are covered in the following automated tests:

  • stories
  • unit tests
  • integration tests
  • end-to-end tests

Manual testing

  • find/create a project using SB6.5 (check if it doesn't have the CLI) as a dependency) (check the issue attached)
  • use the canary release to upgrade

Documentation

  • Add or update documentation reflecting your changes
  • If you are deprecating/removing a feature, make sure to update
    MIGRATION.MD

Checklist for Maintainers

  • When this PR is ready for testing, make sure to add ci:normal, ci:merged or ci:daily GH label to it to run a specific set of sandboxes. The particular set of sandboxes can be found in code/lib/cli/src/sandbox-templates.ts

  • Make sure this PR contains one of the labels below:

    Available labels
    • bug: Internal changes that fixes incorrect behavior.
    • maintenance: User-facing maintenance tasks.
    • dependencies: Upgrading (sometimes downgrading) dependencies.
    • build: Internal-facing build tooling & test updates. Will not show up in release changelog.
    • cleanup: Minor cleanup style change. Will not show up in release changelog.
    • documentation: Documentation only changes. Will not show up in release changelog.
    • feature request: Introducing a new feature.
    • BREAKING CHANGE: Changes that break compatibility in some way with current major version.
    • other: Changes that don't fit in the above categories.

🦋 Canary release

This pull request has been released as version 0.0.0-pr-26055-sha-bcc55126. Try it out in a new sandbox by running npx [email protected] sandbox or in an existing project with npx [email protected] upgrade.

More information
Published version 0.0.0-pr-26055-sha-bcc55126
Triggered by @ndelangen
Repository storybookjs/storybook
Branch norbert/fix-version-detection-cli-upgrading-from-pre-SB-7-5
Commit bcc55126
Datetime Fri Feb 16 09:23:48 UTC 2024 (1708075428)
Workflow run 7928536891

To request a new release of this pull request, mention the @storybookjs/core team.

core team members can create a new canary release here or locally with gh workflow run --repo storybookjs/storybook canary-release-pr.yml --field pr=26055

Comment on lines 1 to 58
/* eslint-disable @typescript-eslint/naming-convention */
import { sync as spawnSync } from 'cross-spawn';
import semver from 'semver';
import { versionRegex } from '../upgrade';

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 => {
const lines = spawnSync('npm', ['ls'], { stdio: 'pipe' }).output.toString().split('\n');
const result = lines
.map((line) => {
if (line.startsWith('npm ')) return null;
const match = 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;
};
Copy link
Member Author

Choose a reason for hiding this comment

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

All ripped out of SB6's code.

You can review this for problems, but please review with the context of:

Code we need to support SB6, it's not eloquent, because we'll remove it as soon as we stop support upgrading from SB6

@ndelangen ndelangen added the patch:yes Bugfix & documentation PR that need to be picked to main branch label Feb 16, 2024
Comment on lines +47 to +54
/**
* 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();
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.

@ndelangen ndelangen marked this pull request as ready for review February 16, 2024 10:36
* @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

@valentinpalkovic
Copy link
Contributor

Superseded by #26067

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug ci:normal cli patch:yes Bugfix & documentation PR that need to be picked to main branch
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: Upgrade from Storybook 6 to Storybook 8 fails if storybook or @storybook/cli isn't installed
2 participants