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: Automigrate improve upgrade storybook related packages #26497

Merged
merged 4 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,26 @@ describe('upgrade-storybook-related-dependencies fix', () => {
{
packageName: 'storybook',
packageVersion: '8.0.0',
availableUpgrade: undefined,
availableUpgrade: '8.0.0',
hasIncompatibleDependencies: true,
},
];
vi.mocked(docsUtils.getIncompatibleStorybookPackages).mockResolvedValue(analyzedPackages);
await expect(
check({
packageManager: {
getAllDependencies: async () => ({
'@chromatic-com/storybook': '1.2.9',
'@storybook/jest': '0.2.3',
'@storybook/preset-create-react-app': '3.2.0',
storybook: '8.0.0',
}),
getAllDependencies: async () =>
analyzedPackages.reduce(
(acc, { packageName, packageVersion }) => {
acc[packageName] = packageVersion;
return acc;
},
{} as Record<string, string>
),
latestVersion: async (pkgName) =>
analyzedPackages.find((pkg) => pkg.packageName === pkgName)?.availableUpgrade || '',
getInstalledVersion: async (pkgName) =>
analyzedPackages.find((pkg) => pkg.packageName === pkgName)?.packageVersion || null,
},
})
).resolves.toMatchInlineSnapshot(`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { dedent } from 'ts-dedent';
import { cyan, yellow } from 'chalk';
import { valid, coerce } from 'semver';
import { gt } from 'semver';
import type { JsPackageManager } from '@storybook/core-common';
import { isCorePackage } from '@storybook/core-common';
import type { Fix } from '../types';
Expand All @@ -21,24 +21,14 @@ async function getLatestVersions(
packages: [string, string][]
): Promise<PackageMetadata[]> {
return Promise.all(
packages.map(async ([packageName, beforeVersion]) => ({
packages.map(async ([packageName]) => ({
packageName,
beforeVersion: coerce(beforeVersion)?.toString() || null,
beforeVersion: await packageManager.getInstalledVersion(packageName).catch(() => null),
afterVersion: await packageManager.latestVersion(packageName).catch(() => null),
}))
);
}

function isPackageUpgradable(
afterVersion: string,
packageName: string,
allDependencies: Record<string, string>
) {
const installedVersion = coerce(allDependencies[packageName])?.toString();

return valid(afterVersion) && afterVersion !== installedVersion;
}

/**
* Is the user upgrading to the `latest` version of Storybook?
* Let's try to pull along some of the storybook related dependencies to `latest` as well!
Expand Down Expand Up @@ -75,15 +65,13 @@ export const upgradeStorybookRelatedDependencies = {

const packageVersions = await getLatestVersions(packageManager, uniquePackages);

const upgradablePackages = packageVersions.filter(
({ packageName, afterVersion, beforeVersion }) => {
if (beforeVersion === null || afterVersion === null) {
return false;
}

return isPackageUpgradable(afterVersion, packageName, allDependencies);
const upgradablePackages = packageVersions.filter(({ afterVersion, beforeVersion }) => {
if (beforeVersion === null || afterVersion === null) {
return false;
}
);

return gt(afterVersion, beforeVersion);
});

return upgradablePackages.length > 0 ? { upgradable: upgradablePackages } : null;
},
Expand Down
17 changes: 17 additions & 0 deletions code/lib/core-common/src/js-package-manager/JsPackageManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,23 @@ export abstract class JsPackageManager {
}
}

/**
* Returns the installed (within node_modules or pnp zip) version of a specified package
*/
public async getInstalledVersion(packageName: string): Promise<string | null> {
const installations = await this.findInstallations([packageName]);
if (!installations) {
return null;
}

console.log({
installations,
version: Object.entries(installations.dependencies)[0]?.[1]?.[0].version,
});
yannbf marked this conversation as resolved.
Show resolved Hide resolved

return Object.entries(installations.dependencies)[0]?.[1]?.[0].version || null;
}

public async executeCommand({
command,
args = [],
Expand Down
Loading