From ce18fda72cddd0bfaa3f718b87c2af01fb3bdd22 Mon Sep 17 00:00:00 2001 From: Yann Braga Date: Wed, 20 Mar 2024 17:08:26 +0100 Subject: [PATCH] CLI: Throw an error when running upgrade command in incorrect cwd --- code/lib/cli/src/helpers.test.ts | 16 ++++++++++++++++ code/lib/cli/src/helpers.ts | 6 ++++++ code/lib/cli/src/upgrade.ts | 5 +++++ code/lib/core-events/src/errors/server-errors.ts | 14 ++++++++++++++ 4 files changed, 41 insertions(+) diff --git a/code/lib/cli/src/helpers.test.ts b/code/lib/cli/src/helpers.test.ts index 73d7efa548bd..35cdeb0c3150 100644 --- a/code/lib/cli/src/helpers.test.ts +++ b/code/lib/cli/src/helpers.test.ts @@ -210,4 +210,20 @@ describe('Helpers', () => { }).toThrowError(`Could not coerce ${invalidSemverString} into a semver.`); }); }); + + describe('hasStorybookDependencies', () => { + it(`should return true when any storybook dependency exists`, async () => { + const result = await helpers.hasStorybookDependencies({ + getAllDependencies: async () => ({ storybook: 'x.y.z' }), + } as unknown as JsPackageManager); + expect(result).toEqual(true); + }); + + it(`should return false when no storybook dependency exists`, async () => { + const result = await helpers.hasStorybookDependencies({ + getAllDependencies: async () => ({ axios: 'x.y.z' }), + } as unknown as JsPackageManager); + expect(result).toEqual(false); + }); + }); }); diff --git a/code/lib/cli/src/helpers.ts b/code/lib/cli/src/helpers.ts index ed273831eefb..ab3bf239592c 100644 --- a/code/lib/cli/src/helpers.ts +++ b/code/lib/cli/src/helpers.ts @@ -296,3 +296,9 @@ export function coerceSemver(version: string) { invariant(coercedSemver != null, `Could not coerce ${version} into a semver.`); return coercedSemver; } + +export async function hasStorybookDependencies(packageManager: JsPackageManager) { + const currentPackageDeps = await packageManager.getAllDependencies(); + + return Object.keys(currentPackageDeps).some((dep) => dep.includes('storybook')); +} diff --git a/code/lib/cli/src/upgrade.ts b/code/lib/cli/src/upgrade.ts index f0b7cc5ff612..4483bdb0f2e3 100644 --- a/code/lib/cli/src/upgrade.ts +++ b/code/lib/cli/src/upgrade.ts @@ -4,6 +4,7 @@ import semver, { eq, lt, prerelease } from 'semver'; import { logger } from '@storybook/node-logger'; import { withTelemetry } from '@storybook/core-server'; import { + UpgradeStorybookInWrongWorkingDirectory, UpgradeStorybookToLowerVersionError, UpgradeStorybookToSameVersionError, UpgradeStorybookUnknownCurrentVersionError, @@ -22,6 +23,7 @@ import { } from '@storybook/core-common'; import { automigrate } from './automigrate/index'; import { autoblock } from './autoblock/index'; +import { hasStorybookDependencies } from './helpers'; type Package = { package: string; @@ -134,6 +136,9 @@ export const doUpgrade = async ({ beforeVersion.startsWith('portal:') || beforeVersion.startsWith('workspace:'); + if (!(await hasStorybookDependencies(packageManager))) { + throw new UpgradeStorybookInWrongWorkingDirectory(); + } if (!isCanary && lt(currentVersion, beforeVersion)) { throw new UpgradeStorybookToLowerVersionError({ beforeVersion, currentVersion }); } diff --git a/code/lib/core-events/src/errors/server-errors.ts b/code/lib/core-events/src/errors/server-errors.ts index 85bf7cda02c4..efd3c929de0c 100644 --- a/code/lib/core-events/src/errors/server-errors.ts +++ b/code/lib/core-events/src/errors/server-errors.ts @@ -577,6 +577,20 @@ export class UpgradeStorybookUnknownCurrentVersionError extends StorybookError { } } +export class UpgradeStorybookInWrongWorkingDirectory extends StorybookError { + readonly category = Category.CLI_UPGRADE; + + readonly code = 6; + + template() { + return dedent` + You are running the upgrade command in a CWD that does not contain Storybook dependencies. + + Did you mean to run it in a different directory? Make sure the directory you run this command in contains a package.json with your Storybook dependencies. + `; + } +} + export class NoStatsForViteDevError extends StorybookError { readonly category = Category.BUILDER_VITE;