Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLI: Throw an error when running upgrade command in incorrect cwd #26585

Merged
merged 2 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'));
}
1 change: 1 addition & 0 deletions code/lib/cli/src/upgrade.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ vi.mock('@storybook/core-common', async (importOriginal) => {
JsPackageManagerFactory: {
getPackageManager: () => ({
findInstallations: findInstallationsMock,
getAllDependencies: async () => ({ storybook: '8.0.0' }),
}),
},
versions: Object.keys(originalModule.versions).reduce(
Expand Down
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