Skip to content

Commit

Permalink
Merge pull request #26497 from storybookjs/norbert/improve-automigrat…
Browse files Browse the repository at this point in the history
…e-upgrader

CLI: Automigrate improve upgrade storybook related packages
  • Loading branch information
ndelangen authored Mar 15, 2024
2 parents 71ba298 + 164ea49 commit 704f964
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 28 deletions.
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
12 changes: 12 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,18 @@ 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;
}

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

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

0 comments on commit 704f964

Please sign in to comment.