Skip to content

Commit

Permalink
Merge pull request #23956 from storybookjs/shaun/sb-add-version-speci…
Browse files Browse the repository at this point in the history
…fier

CLI: Install latest version of non-core addon
  • Loading branch information
yannbf committed Aug 29, 2023
2 parents aecfa17 + f4c9c7f commit c26e557
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 57 deletions.
5 changes: 3 additions & 2 deletions code/lib/cli/src/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
useNpmWarning,
type PackageManagerName,
} from './js-package-manager';
import { getStorybookVersion } from './utils';
import { getStorybookVersion, isCorePackage } from './utils';

const logger = console;

Expand Down Expand Up @@ -76,8 +76,9 @@ export async function add(

// add to package.json
const isStorybookAddon = addonName.startsWith('@storybook/');
const isAddonFromCore = isCorePackage(addonName);
const storybookVersion = await getStorybookVersion(packageManager);
const version = versionSpecifier || (isStorybookAddon ? storybookVersion : latestVersion);
const version = versionSpecifier || (isAddonFromCore ? storybookVersion : latestVersion);
const addonWithVersion = SemVer.valid(version)
? `${addonName}@^${version}`
: `${addonName}@${version}`;
Expand Down
25 changes: 1 addition & 24 deletions code/lib/cli/src/upgrade.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import {
addExtraFlags,
addNxPackagesToReject,
getStorybookVersion,
isCorePackage,
} from './upgrade';
import { addExtraFlags, addNxPackagesToReject, getStorybookVersion } from './upgrade';

describe.each([
['│ │ │ ├── @babel/code-frame@7.10.3 deduped', null],
Expand All @@ -26,24 +21,6 @@ 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/linter-config', false],
['@storybook/design-system', false],
['@storybook/addon-styling', false],
['@storybook/addon-styling-webpack', false],
['@nx/storybook', false],
['@nrwl/storybook', false],
])('isCorePackage', (input, output) => {
it(`${input}`, () => {
expect(isCorePackage(input)).toEqual(output);
});
});

describe('extra flags', () => {
const extraFlags = {
'react-scripts@<5': ['--foo'],
Expand Down
32 changes: 1 addition & 31 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 { isCorePackage } from './utils';

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

const excludeList = [
'@storybook/addon-bench',
'@storybook/addon-console',
'@storybook/addon-postcss',
'@storybook/addon-styling',
'@storybook/addon-styling-webpack',
'@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',
'@nrwl/storybook',
'@nx/storybook',
];
export const isCorePackage = (pkg: string) =>
pkg.startsWith('@storybook/') &&
!pkg.startsWith('@storybook/preset-') &&
!excludeList.includes(pkg);

const deprecatedPackages = [
{
minVersion: '6.0.0-alpha.0',
Expand Down
21 changes: 21 additions & 0 deletions code/lib/cli/src/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { isCorePackage } from './utils';

describe('UTILS', () => {
describe.each([
['@storybook/react', true],
['@storybook/node-logger', true],
['@storybook/addon-info', true],
['@storybook/something-random', true],
['@storybook/preset-create-react-app', false],
['@storybook/linter-config', false],
['@storybook/design-system', false],
['@storybook/addon-styling', false],
['@storybook/addon-styling-webpack', false],
['@nx/storybook', false],
['@nrwl/storybook', false],
])('isCorePackage', (input, output) => {
it(`It should return "${output}" when given "${input}"`, () => {
expect(isCorePackage(input)).toEqual(output);
});
});
});
31 changes: 31 additions & 0 deletions code/lib/cli/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,34 @@ export const createLogStream = async (
logStream.once('error', reject);
});
};

const PACKAGES_EXCLUDED_FROM_CORE = [
'@storybook/addon-bench',
'@storybook/addon-console',
'@storybook/addon-postcss',
'@storybook/addon-styling',
'@storybook/addon-styling-webpack',
'@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',
'@nrwl/storybook',
'@nx/storybook',
];
export const isCorePackage = (pkg: string) =>
pkg.startsWith('@storybook/') &&
!pkg.startsWith('@storybook/preset-') &&
!PACKAGES_EXCLUDED_FROM_CORE.includes(pkg);

0 comments on commit c26e557

Please sign in to comment.