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: Fix regex used in upgrade command #25284

Merged
merged 2 commits into from
Jan 3, 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
2 changes: 1 addition & 1 deletion code/lib/cli/src/upgrade.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ describe('addNxPackagesToReject', () => {
const flags = ['--reject', '/preset-create-react-app/', '--some-flag', 'hello'];
expect(addNxPackagesToReject(flags)).toMatchObject([
'--reject',
'/(preset-create-react-app|@nrwl/storybook|@nx/storybook)/',
'"/(preset-create-react-app|@nrwl/storybook|@nx/storybook)/"',
'--some-flag',
'hello',
]);
Expand Down
38 changes: 30 additions & 8 deletions code/lib/cli/src/upgrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import { telemetry, getStorybookCoreVersion } from '@storybook/telemetry';
import semver from 'semver';
import { logger } from '@storybook/node-logger';
import { withTelemetry } from '@storybook/core-server';
import {
ConflictingVersionTagsError,
UpgradeStorybookPackagesError,
} from '@storybook/core-events/server-errors';

import type { PackageJsonWithMaybeDeps, PackageManagerName } from './js-package-manager';
import { getPackageDetails, JsPackageManagerFactory } from './js-package-manager';
Expand Down Expand Up @@ -117,7 +121,7 @@ export const addNxPackagesToReject = (flags: string[]) => {
if (newFlags[index + 1].endsWith('/') && newFlags[index + 1].startsWith('/')) {
// Remove last and first slash so that I can add the parentheses
newFlags[index + 1] = newFlags[index + 1].substring(1, newFlags[index + 1].length - 1);
newFlags[index + 1] = `/(${newFlags[index + 1]}|@nrwl/storybook|@nx/storybook)/`;
newFlags[index + 1] = `"/(${newFlags[index + 1]}|@nrwl/storybook|@nx/storybook)/"`;
} else {
// Adding the two packages as comma-separated values
// If the existing rejects are in regex format, they will be ignored.
Expand Down Expand Up @@ -156,12 +160,10 @@ export const doUpgrade = async ({

const beforeVersion = await getStorybookCoreVersion();

commandLog(`Checking for latest versions of '@storybook/*' packages`);
commandLog(`Checking for latest versions of '@storybook/*' packages\n`);

if (tag && prerelease) {
throw new Error(
`Cannot set both --tag and --prerelease. Use --tag next to get the latest prereleae`
);
throw new ConflictingVersionTagsError();
}

let target = 'latest';
Expand All @@ -180,20 +182,40 @@ export const doUpgrade = async ({
flags.push(target);
flags = addExtraFlags(EXTRA_FLAGS, flags, await packageManager.retrievePackageJson());
flags = addNxPackagesToReject(flags);
const check = spawnSync('npx', ['npm-check-updates@latest', '/storybook/', ...flags], {

const command = 'npx';
const checkArgs = ['npm-check-updates@latest', '/storybook/', ...flags];
const check = spawnSync(command, checkArgs, {
stdio: 'pipe',
shell: true,
});

if (check.stderr && !check.stderr.toString().includes('npm notice')) {
throw new UpgradeStorybookPackagesError({
command,
args: checkArgs,
errorMessage: check.stderr.toString(),
});
}

logger.info(check.stdout.toString());
logger.info(check.stderr.toString());

const checkSb = spawnSync('npx', ['npm-check-updates@latest', 'sb', ...flags], {
const checkSbArgs = ['npm-check-updates@latest', 'sb', ...flags];
const checkSb = spawnSync(command, checkSbArgs, {
stdio: 'pipe',
shell: true,
});
logger.info(checkSb.stdout.toString());
logger.info(checkSb.stderr.toString());

if (checkSb.stderr && !checkSb.stderr.toString().includes('npm notice')) {
throw new UpgradeStorybookPackagesError({
command,
args: checkSbArgs,
errorMessage: checkSb.stderr.toString(),
});
}

if (!dryRun) {
commandLog(`Installing upgrades`);
await packageManager.installDependencies();
Expand Down
34 changes: 33 additions & 1 deletion code/lib/core-events/src/errors/server-errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ export class GenerateNewProjectOnInitError extends StorybookError {
super();
}

template(): string {
template() {
return dedent`
There was an error while using ${this.data.packageManager} to create a new ${
this.data.projectType
Expand All @@ -432,3 +432,35 @@ export class GenerateNewProjectOnInitError extends StorybookError {
`;
}
}

export class ConflictingVersionTagsError extends StorybookError {
readonly category = Category.CLI_UPGRADE;

readonly code = 1;

template() {
return 'Cannot set both --tag and --prerelease. Use --tag=next to get the latest prerelease.';
}
}

export class UpgradeStorybookPackagesError extends StorybookError {
readonly category = Category.CLI_UPGRADE;

readonly code = 2;

constructor(public data: { command: string; args: string[]; errorMessage: string }) {
super();
}

template() {
return dedent`
There was an error while trying to upgrade your Storybook dependencies.

Command:
${this.data.command} ${this.data.args.join(' ')}

Error:
${this.data.errorMessage}
`;
}
}