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: Improve error message when fetching CLI version #28289

Merged
merged 1 commit into from
Jun 24, 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
31 changes: 18 additions & 13 deletions code/lib/core-common/src/js-package-manager/NPMProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { sync as findUpSync } from 'find-up';
import { existsSync, readFileSync } from 'fs';
import path from 'path';
import { logger } from '@storybook/node-logger';
import { FindPackageVersionsError } from '@storybook/core-events/server-errors';

import { JsPackageManager } from './JsPackageManager';
import type { PackageJson } from './PackageJson';
import type { InstallationMetadata, PackageMetadata } from './types';
Expand Down Expand Up @@ -225,23 +227,26 @@ export class NPMProxy extends JsPackageManager {
fetchAllVersions: T
): Promise<T extends true ? string[] : string> {
const args = [fetchAllVersions ? 'versions' : 'version', '--json'];

const commandResult = await this.executeCommand({
command: 'npm',
args: ['info', packageName, ...args],
});

try {
const commandResult = await this.executeCommand({
command: 'npm',
args: ['info', packageName, ...args],
});

const parsedOutput = JSON.parse(commandResult);

if (parsedOutput.error) {
// FIXME: improve error handling
throw new Error(parsedOutput.error.summary);
} else {
return parsedOutput;
if (parsedOutput.error?.summary) {
// this will be handled in the catch block below
throw parsedOutput.error.summary;
}
} catch (e) {
throw new Error(`Unable to find versions of ${packageName} using npm`);

return parsedOutput;
} catch (error) {
throw new FindPackageVersionsError({
error,
packageManager: 'NPM',
packageName,
});
}
}

Expand Down
30 changes: 18 additions & 12 deletions code/lib/core-common/src/js-package-manager/PNPMProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import dedent from 'ts-dedent';
import { sync as findUpSync } from 'find-up';
import path from 'path';
import fs from 'fs';
import { FindPackageVersionsError } from '@storybook/core-events/server-errors';

import { JsPackageManager } from './JsPackageManager';
import type { PackageJson } from './PackageJson';
import type { InstallationMetadata, PackageMetadata } from './types';
Expand Down Expand Up @@ -222,22 +224,26 @@ export class PNPMProxy extends JsPackageManager {
): Promise<T extends true ? string[] : string> {
const args = [fetchAllVersions ? 'versions' : 'version', '--json'];

const commandResult = await this.executeCommand({
command: 'pnpm',
args: ['info', packageName, ...args],
});

try {
const commandResult = await this.executeCommand({
command: 'pnpm',
args: ['info', packageName, ...args],
});

const parsedOutput = JSON.parse(commandResult);

if (parsedOutput.error) {
// FIXME: improve error handling
throw new Error(parsedOutput.error.summary);
} else {
return parsedOutput;
if (parsedOutput.error?.summary) {
// this will be handled in the catch block below
throw parsedOutput.error.summary;
}
} catch (e) {
throw new Error(`Unable to find versions of ${packageName} using pnpm`);

return parsedOutput;
} catch (error) {
throw new FindPackageVersionsError({
error,
packageManager: 'PNPM',
packageName,
});
}
}

Expand Down
24 changes: 15 additions & 9 deletions code/lib/core-common/src/js-package-manager/Yarn1Proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import dedent from 'ts-dedent';
import { sync as findUpSync } from 'find-up';
import { existsSync, readFileSync } from 'fs';
import path from 'path';
import { FindPackageVersionsError } from '@storybook/core-events/server-errors';

import { createLogStream } from '../utils/cli';
import { JsPackageManager } from './JsPackageManager';
import type { PackageJson } from './PackageJson';
Expand Down Expand Up @@ -162,20 +164,24 @@ export class Yarn1Proxy extends JsPackageManager {
fetchAllVersions: T
): Promise<T extends true ? string[] : string> {
const args = [fetchAllVersions ? 'versions' : 'version', '--json'];

const commandResult = await this.executeCommand({
command: 'yarn',
args: ['info', packageName, ...args],
});

try {
const commandResult = await this.executeCommand({
command: 'yarn',
args: ['info', packageName, ...args],
});

const parsedOutput = JSON.parse(commandResult);
if (parsedOutput.type === 'inspect') {
return parsedOutput.data;
}
throw new Error(`Unable to find versions of ${packageName} using yarn`);
} catch (e) {
throw new Error(`Unable to find versions of ${packageName} using yarn`);
// eslint-disable-next-line local-rules/no-uncategorized-errors
throw new Error(`Yarn did not provide an output with type 'inspect'.`);
} catch (error) {
throw new FindPackageVersionsError({
error,
packageManager: 'Yarn 1',
packageName,
});
}
}

Expand Down
21 changes: 13 additions & 8 deletions code/lib/core-common/src/js-package-manager/Yarn2Proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { existsSync, readFileSync } from 'fs';
import path from 'path';
import { PosixFS, VirtualFS, ZipOpenFS } from '@yarnpkg/fslib';
import { getLibzipSync } from '@yarnpkg/libzip';
import { FindPackageVersionsError } from '@storybook/core-events/server-errors';

import { createLogStream } from '../utils/cli';
import { JsPackageManager } from './JsPackageManager';
import type { PackageJson } from './PackageJson';
Expand Down Expand Up @@ -248,17 +250,20 @@ export class Yarn2Proxy extends JsPackageManager {
): Promise<T extends true ? string[] : string> {
const field = fetchAllVersions ? 'versions' : 'version';
const args = ['--fields', field, '--json'];

const commandResult = await this.executeCommand({
command: 'yarn',
args: ['npm', 'info', packageName, ...args],
});

try {
const commandResult = await this.executeCommand({
command: 'yarn',
args: ['npm', 'info', packageName, ...args],
});

const parsedOutput = JSON.parse(commandResult);
return parsedOutput[field];
} catch (e) {
throw new Error(`Unable to find versions of ${packageName} using yarn 2`);
} catch (error) {
throw new FindPackageVersionsError({
error,
packageManager: 'Yarn Berry',
packageName,
});
}
}

Expand Down
19 changes: 19 additions & 0 deletions code/lib/core-events/src/errors/server-errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -604,3 +604,22 @@ export class NoStatsForViteDevError extends StorybookError {
`;
}
}

export class FindPackageVersionsError extends StorybookError {
readonly category = Category.CLI;

readonly code = 1;

constructor(
public data: { error: Error | unknown; packageName: string; packageManager: string }
) {
super();
}

template() {
return dedent`
Unable to find versions of "${this.data.packageName}" using ${this.data.packageManager}
${this.data.error && `Reason: ${this.data.error}`}
`;
}
}
Loading