Skip to content

Commit

Permalink
Merge pull request #21508 from storybookjs/shilman/21287-cli-upgrade
Browse files Browse the repository at this point in the history
CLI: Upgrade non-core storybook packages better
  • Loading branch information
shilman authored Mar 9, 2023
2 parents 74eb850 + f9b5840 commit 41008a7
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 33 deletions.
35 changes: 31 additions & 4 deletions code/lib/cli/src/upgrade.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { addExtraFlags, getStorybookVersion, isCorePackage } from './upgrade';
import { addExtraFlags, getStorybookVersion, isCorePackage, NON_CANARY_REGEX } from './upgrade';

describe.each([
['│ │ │ ├── @babel/[email protected] deduped', null],
Expand All @@ -24,9 +24,9 @@ describe.each([
describe.each([
['@storybook/react', true],
['@storybook/node-logger', true],
['@storybook/addon-info', true],
['@storybook/something-random', true],
['@storybook/preset-create-react-app', false],
['@storybook/addon-info', false],
['@storybook/something-random', false],
['@storybook/preset-create-react-app', true],
['@storybook/linter-config', false],
['@storybook/design-system', false],
])('isCorePackage', (input, output) => {
Expand Down Expand Up @@ -70,3 +70,30 @@ describe('extra flags', () => {
).toEqual([]);
});
});

// https://github.com/sindresorhus/semver-regex/blob/main/test.js
describe('filter out canary releases', () => {
it.each([
['6.0.0'],
['6.0.0-alpha.0'],
['6.0.0-foobar.0'],
['1.2.3-4'],
['^1.2.3-4'],
['~1.2.3-4'],
['>1.2.3-4'],
['>=1.2.3-4'],
])('should match %s', (version) => {
expect(NON_CANARY_REGEX.test(version)).toEqual(true);
});
it.each([
['yahooo'],
['6.0.0-canary.0.0'],
['0.7.6--canary.18.9b6563c.0'],
['0.7.6--next.18.0'],
['0.7.1-next.18.0'],
['~0.7.1-next.18.0'],
['^0.7.1-next.18.0'],
])('should not match %s', (version) => {
expect(NON_CANARY_REGEX.test(version)).toEqual(false);
});
});
39 changes: 10 additions & 29 deletions code/lib/cli/src/upgrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type { PackageJsonWithMaybeDeps, PackageManagerName } from './js-package-
import { getPackageDetails, JsPackageManagerFactory, useNpmWarning } from './js-package-manager';
import { commandLog } from './helpers';
import { automigrate } from './automigrate';
import coreVersions from './versions';

type Package = {
package: string;
Expand All @@ -25,32 +26,8 @@ export const getStorybookVersion = (line: string) => {
};
};

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

const deprecatedPackages = [
{
Expand Down Expand Up @@ -147,6 +124,8 @@ export interface UpgradeOptions {
configDir?: string;
}

export const NON_CANARY_REGEX = /^(\^|~|>|>=)?([0-9]+)\.([0-9]+)\.([0-9]+)(-([a-z]+\.)?[0-9]+)?$/;

export const doUpgrade = async ({
tag,
prerelease,
Expand Down Expand Up @@ -175,17 +154,19 @@ export const doUpgrade = async ({
);
}

let flags = [];
let target = 'latest';
if (prerelease) {
// '@next' is storybook's convention for the latest prerelease tag.
// This used to be 'greatest', but that was not reliable and could pick canaries, etc.
// and random releases of other packages with storybook in their name.
target = '@next';
// However it doesn't upgrade other storybook-related packages like
// storybook-addon-designs. 'greatest' does, but we filter out canaries
// to try to eliminate the junk
target = 'greatest';
flags.push(`--filterVersion "${NON_CANARY_REGEX.toString()}"`);
} else if (tag) {
target = `@${tag}`;
}

let flags = [];
if (!dryRun) flags.push('--upgrade');
flags.push('--target');
flags.push(target);
Expand Down

0 comments on commit 41008a7

Please sign in to comment.