Skip to content
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
22 changes: 19 additions & 3 deletions code/addons/vitest/src/postinstall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { SupportedFramework } from 'storybook/internal/types';

import * as find from 'empathic/find';
import { dirname, relative, resolve } from 'pathe';
import { satisfies } from 'semver';
import { coerce, satisfies } from 'semver';
import { dedent } from 'ts-dedent';

import { type PostinstallOptions } from '../../../lib/cli-storybook/src/add';
Expand All @@ -50,17 +50,33 @@ export default async function postInstall(options: PostinstallOptions) {
{ last: getProjectRoot(), cwd: options.configDir }
);

const vitestVersionSpecifier = await packageManager.getInstalledVersion('vitest');
const allDeps = packageManager.getAllDependencies();

// Determine Vitest version/range from installed or declared dependency to avoid pulling
// incompatible majors by default.
let vitestVersionSpecifier = await packageManager.getInstalledVersion('vitest');
if (!vitestVersionSpecifier && allDeps['vitest']) {
vitestVersionSpecifier = allDeps['vitest'];
}

/**
* Coerce the version specifier to a version string
*
* This removed any version range specifiers like ^, ~, etc. which is needed to check with
* semver.satisfies.
*/
vitestVersionSpecifier = coerce(vitestVersionSpecifier)?.version ?? null;

logger.debug(`Vitest version specifier: ${vitestVersionSpecifier}`);
const isVitest3_2To4 = vitestVersionSpecifier
? satisfies(vitestVersionSpecifier, '>=3.2.0 <4.0.0')
: false;

const isVitest4OrNewer = vitestVersionSpecifier
? satisfies(vitestVersionSpecifier, '>=4.0.0')
: true;

const info = await getStorybookInfo(options.configDir);
const allDeps = packageManager.getAllDependencies();
// only install these dependencies if they are not already installed

const addonVitestService = new AddonVitestService(packageManager);
Expand Down
2 changes: 2 additions & 0 deletions code/core/src/common/js-package-manager/JsPackageManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,7 @@ export abstract class JsPackageManager {
logger.debug(`Getting installed version for ${packageName}...`);
const installations = await this.findInstallations([packageName]);
if (!installations) {
logger.debug(`No installations found for ${packageName}`);
// Cache the null result
JsPackageManager.installedVersionCache.set(cacheKey, null);
return null;
Expand All @@ -646,6 +647,7 @@ export abstract class JsPackageManager {

return coercedVersion;
} catch (e) {
logger.error(`Error getting installed version for ${packageName}: ${String(e)}`);
JsPackageManager.installedVersionCache.set(cacheKey, null);
return null;
}
Expand Down
8 changes: 3 additions & 5 deletions code/core/src/common/js-package-manager/NPMProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export class NPMProxy extends JsPackageManager {
const parsedOutput = JSON.parse(commandResult);

return this.mapDependencies(parsedOutput, pattern);
} catch (e) {
} catch {
// when --depth is higher than 0, npm can return a non-zero exit code
// in case the user's project has peer dependency issues. So we try again with no depth
try {
Expand All @@ -153,10 +153,8 @@ export class NPMProxy extends JsPackageManager {
const parsedOutput = JSON.parse(commandResult);

return this.mapDependencies(parsedOutput, pattern);
} catch (err) {
logger.debug(
`An issue occurred while trying to find dependencies metadata using npm: ${err}`
);
} catch (e) {
logger.debug(`Error finding installations for ${pattern.join(', ')}: ${String(e)}`);
return undefined;
}
}
Expand Down
7 changes: 5 additions & 2 deletions code/core/src/common/js-package-manager/PNPMProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { existsSync, readFileSync } from 'node:fs';
import { join } from 'node:path';
import { pathToFileURL } from 'node:url';

import { prompt } from 'storybook/internal/node-logger';
import { logger, prompt } from 'storybook/internal/node-logger';
import { FindPackageVersionsError } from 'storybook/internal/server-errors';

import * as find from 'empathic/find';
Expand Down Expand Up @@ -113,9 +113,11 @@ export class PNPMProxy extends JsPackageManager {

public async findInstallations(pattern: string[], { depth = 99 }: { depth?: number } = {}) {
try {
const args = ['list', pattern.map((p) => `"${p}"`).join(' '), '--json', `--depth=${depth}`];
const childProcess = await executeCommand({
command: 'pnpm',
args: ['list', pattern.map((p) => `"${p}"`).join(' '), '--json', `--depth=${depth}`],
shell: true,
args,
env: {
FORCE_COLOR: 'false',
},
Expand All @@ -126,6 +128,7 @@ export class PNPMProxy extends JsPackageManager {
const parsedOutput = JSON.parse(commandResult);
return this.mapDependencies(parsedOutput, pattern);
} catch (e) {
logger.debug(`Error finding installations for ${pattern.join(', ')}: ${String(e)}`);
return undefined;
}
}
Expand Down
4 changes: 3 additions & 1 deletion code/core/src/common/js-package-manager/Yarn1Proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { readFileSync } from 'node:fs';
import { join } from 'node:path';
import process from 'node:process';

import { prompt } from 'storybook/internal/node-logger';
import { logger, prompt } from 'storybook/internal/node-logger';
import { FindPackageVersionsError } from 'storybook/internal/server-errors';

import * as find from 'empathic/find';
Expand Down Expand Up @@ -115,6 +115,7 @@ export class Yarn1Proxy extends JsPackageManager {
const process = executeCommand({
command: 'yarn',
args: yarnArgs.concat(pattern),
shell: true,
env: {
FORCE_COLOR: 'false',
},
Expand All @@ -126,6 +127,7 @@ export class Yarn1Proxy extends JsPackageManager {
const parsedOutput = JSON.parse(commandResult);
return this.mapDependencies(parsedOutput, pattern);
} catch (e) {
logger.debug(`Error finding installations for ${pattern.join(', ')}: ${String(e)}`);
return undefined;
}
}
Expand Down
1 change: 1 addition & 0 deletions code/core/src/common/js-package-manager/Yarn2Proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ export class Yarn2Proxy extends JsPackageManager {

return this.mapDependencies(commandResult, pattern);
} catch (e) {
logger.debug(`Error finding installations for ${pattern.join(', ')}: ${String(e)}`);
return undefined;
}
}
Expand Down