Skip to content

Commit

Permalink
Merge pull request #13876 from storybookjs/tech/improve-cli
Browse files Browse the repository at this point in the history
CLI: Don't try to add packages that are already installed
  • Loading branch information
shilman authored Feb 15, 2021
2 parents 501e9fc + b035493 commit 37a6d3a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
13 changes: 10 additions & 3 deletions lib/cli/src/generators/baseGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { NpmOptions } from '../NpmOptions';
import { StoryFormat, SupportedLanguage, SupportedFrameworks } from '../project_types';
import { getBabelDependencies, copyComponents } from '../helpers';
import { configure } from './configure';
import { JsPackageManager } from '../js-package-manager';
import { getPackageDetails, JsPackageManager } from '../js-package-manager';

export type GeneratorOptions = {
language: SupportedLanguage;
Expand Down Expand Up @@ -64,21 +64,28 @@ export async function baseGenerator(
const yarn2Dependencies =
packageManager.type === 'yarn2' ? ['@storybook/addon-docs', '@mdx-js/react'] : [];

const packageJson = packageManager.retrievePackageJson();
const installedDependencies = new Set(Object.keys(packageJson.dependencies));

const packages = [
`@storybook/${framework}`,
...addonPackages,
...extraPackages,
...extraAddons,
...yarn2Dependencies,
].filter(Boolean);
]
.filter(Boolean)
.filter(
(packageToInstall) => !installedDependencies.has(getPackageDetails(packageToInstall)[0])
);

const versionedPackages = await packageManager.getVersionedPackages(...packages);

configure(framework, [...addons, ...extraAddons]);
if (addComponents) {
copyComponents(framework, language);
}

const packageJson = packageManager.retrievePackageJson();
const babelDependencies = addBabel ? await getBabelDependencies(packageManager, packageJson) : [];
packageManager.addDependencies({ ...npmOptions, packageJson }, [
...versionedPackages,
Expand Down
8 changes: 7 additions & 1 deletion lib/cli/src/js-package-manager/JsPackageManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@ import storybookPackagesVersions from '../versions.json';

const logger = console;

function getPackageDetails(pkg: string): [string, string?] {
/**
* Extract package name and version from input
*
* @param pkg A string like `@storybook/cli`, `react` or `react@^16`
* @return A tuple of 2 elements: [packageName, packageVersion]
*/
export function getPackageDetails(pkg: string): [string, string?] {
const idx = pkg.lastIndexOf('@');
// If the only `@` is the first character, it is a scoped package
// If it isn't in the string, it will be -1
Expand Down

0 comments on commit 37a6d3a

Please sign in to comment.