Skip to content

Commit

Permalink
CLI: Throw an error when running upgrade command in incorrect cwd
Browse files Browse the repository at this point in the history
  • Loading branch information
yannbf committed Mar 20, 2024
1 parent b389754 commit 9b39b42
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
16 changes: 16 additions & 0 deletions code/lib/cli/src/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});
6 changes: 6 additions & 0 deletions code/lib/cli/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
5 changes: 5 additions & 0 deletions code/lib/cli/src/upgrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
Expand Down Expand Up @@ -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 });
}
Expand Down
14 changes: 14 additions & 0 deletions code/lib/core-events/src/errors/server-errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit 9b39b42

Please sign in to comment.