-
-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26377 from storybookjs/norbert/automigration-upgr…
…ading-storybook-related-deps CLI: Automigration for upgrading storybook related dependencies
- Loading branch information
Showing
6 changed files
with
296 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
code/lib/cli/src/automigrate/fixes/upgrade-storybook-related-dependencies.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import { describe, afterEach, it, expect, vi } from 'vitest'; | ||
import type { StorybookConfig } from '@storybook/types'; | ||
import type { JsPackageManager } from '@storybook/core-common'; | ||
import * as docsUtils from '../../doctor/getIncompatibleStorybookPackages'; | ||
|
||
import { upgradeStorybookRelatedDependencies } from './upgrade-storybook-related-dependencies'; | ||
|
||
vi.mock('../../doctor/getIncompatibleStorybookPackages'); | ||
|
||
const check = async ({ | ||
packageManager, | ||
main: mainConfig = {}, | ||
storybookVersion = '8.0.0', | ||
}: { | ||
packageManager: Partial<JsPackageManager>; | ||
main?: Partial<StorybookConfig> & Record<string, unknown>; | ||
storybookVersion?: string; | ||
}) => { | ||
return upgradeStorybookRelatedDependencies.check({ | ||
packageManager: packageManager as any, | ||
configDir: '', | ||
mainConfig: mainConfig as any, | ||
storybookVersion, | ||
}); | ||
}; | ||
|
||
describe('upgrade-storybook-related-dependencies fix', () => { | ||
afterEach(() => { | ||
vi.restoreAllMocks(); | ||
}); | ||
|
||
it('should detect storyshots registered in main.js', async () => { | ||
const analyzedPackages = [ | ||
{ | ||
packageName: '@chromatic-com/storybook', | ||
packageVersion: '1.2.9', | ||
availableUpgrade: '2.0.0', | ||
hasIncompatibleDependencies: false, | ||
}, | ||
{ | ||
packageName: '@storybook/jest', | ||
packageVersion: '0.2.3', | ||
availableUpgrade: '1.0.0', | ||
hasIncompatibleDependencies: false, | ||
}, | ||
{ | ||
packageName: '@storybook/preset-create-react-app', | ||
packageVersion: '3.2.0', | ||
availableUpgrade: '8.0.0', | ||
hasIncompatibleDependencies: true, | ||
}, | ||
{ | ||
packageName: 'storybook', | ||
packageVersion: '8.0.0', | ||
availableUpgrade: undefined, | ||
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', | ||
}), | ||
latestVersion: async (pkgName) => | ||
analyzedPackages.find((pkg) => pkg.packageName === pkgName)?.availableUpgrade || '', | ||
}, | ||
}) | ||
).resolves.toMatchInlineSnapshot(` | ||
{ | ||
"upgradable": [ | ||
{ | ||
"afterVersion": "2.0.0", | ||
"beforeVersion": "1.2.9", | ||
"packageName": "@chromatic-com/storybook", | ||
}, | ||
{ | ||
"afterVersion": "1.0.0", | ||
"beforeVersion": "0.2.3", | ||
"packageName": "@storybook/jest", | ||
}, | ||
{ | ||
"afterVersion": "8.0.0", | ||
"beforeVersion": "3.2.0", | ||
"packageName": "@storybook/preset-create-react-app", | ||
}, | ||
], | ||
} | ||
`); | ||
}); | ||
}); |
162 changes: 162 additions & 0 deletions
162
code/lib/cli/src/automigrate/fixes/upgrade-storybook-related-dependencies.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
import { dedent } from 'ts-dedent'; | ||
import { cyan, yellow } from 'chalk'; | ||
import { valid, coerce } from 'semver'; | ||
import type { JsPackageManager } from '@storybook/core-common'; | ||
import { isCorePackage } from '@storybook/core-common'; | ||
import type { Fix } from '../types'; | ||
import { getIncompatibleStorybookPackages } from '../../doctor/getIncompatibleStorybookPackages'; | ||
|
||
type PackageMetadata = { | ||
packageName: string; | ||
beforeVersion: string | null; | ||
afterVersion: string | null; | ||
}; | ||
|
||
interface Options { | ||
upgradable: PackageMetadata[]; | ||
} | ||
|
||
async function getLatestVersions( | ||
packageManager: JsPackageManager, | ||
packages: [string, string][] | ||
): Promise<PackageMetadata[]> { | ||
return Promise.all( | ||
packages.map(async ([packageName, beforeVersion]) => ({ | ||
packageName, | ||
beforeVersion: coerce(beforeVersion)?.toString() || 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! | ||
* | ||
* We communicate clearly that this migration is a helping hand, but not a complete solution. | ||
* The user should still manually check for other dependencies that might be incompatible. | ||
* | ||
* see: https://github.com/storybookjs/storybook/issues/25731#issuecomment-1977346398 | ||
*/ | ||
export const upgradeStorybookRelatedDependencies = { | ||
id: 'upgradeStorybookRelatedDependencies', | ||
versionRange: ['*.*.*', '*.*.*'], | ||
promptType: 'auto', | ||
promptDefaultValue: false, | ||
|
||
async check({ packageManager, storybookVersion }) { | ||
const analyzedPackages = await getIncompatibleStorybookPackages({ | ||
currentStorybookVersion: storybookVersion, | ||
packageManager, | ||
skipErrors: true, | ||
}); | ||
|
||
const allDependencies = (await packageManager.getAllDependencies()) as Record<string, string>; | ||
const storybookDependencies = Object.keys(allDependencies) | ||
.filter((dep) => dep.includes('storybook')) | ||
.filter((dep) => !isCorePackage(dep)); | ||
const incompatibleDependencies = analyzedPackages | ||
.filter((pkg) => pkg.hasIncompatibleDependencies) | ||
.map((pkg) => pkg.packageName); | ||
|
||
const uniquePackages = Array.from( | ||
new Set([...storybookDependencies, ...incompatibleDependencies]) | ||
).map((packageName) => [packageName, allDependencies[packageName]]) as [string, string][]; | ||
|
||
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); | ||
} | ||
); | ||
|
||
return upgradablePackages.length > 0 ? { upgradable: upgradablePackages } : null; | ||
}, | ||
|
||
prompt({ upgradable }) { | ||
return dedent` | ||
You're upgrading to the latest version of Storybook. We recommend upgrading the following packages: | ||
${upgradable | ||
.map(({ packageName, afterVersion, beforeVersion }) => { | ||
return `- ${cyan(packageName)}: ${cyan(beforeVersion)} => ${cyan(afterVersion)}`; | ||
}) | ||
.join('\n')} | ||
After upgrading, we will run the dedupe command, which could possibly have effects on dependencies that are not Storybook related. | ||
see: https://docs.npmjs.com/cli/commands/npm-dedupe | ||
Do you want to proceed (upgrade the detected packages)? | ||
`; | ||
}, | ||
|
||
async run({ result: { upgradable }, packageManager, dryRun }) { | ||
if (dryRun) { | ||
console.log(dedent` | ||
We would have upgrade the following: | ||
${upgradable | ||
.map( | ||
({ packageName, afterVersion, beforeVersion }) => | ||
`${packageName}: ${beforeVersion} => ${afterVersion}` | ||
) | ||
.join('\n')} | ||
`); | ||
return; | ||
} | ||
|
||
if (upgradable.length > 0) { | ||
const packageJson = await packageManager.readPackageJson(); | ||
|
||
upgradable.forEach((item) => { | ||
if (!item) { | ||
return; | ||
} | ||
|
||
const { packageName, afterVersion: version } = item; | ||
const prefixed = `^${version}`; | ||
|
||
if (packageJson.dependencies?.[packageName]) { | ||
packageJson.dependencies[packageName] = prefixed; | ||
} | ||
if (packageJson.devDependencies?.[packageName]) { | ||
packageJson.devDependencies[packageName] = prefixed; | ||
} | ||
if (packageJson.peerDependencies?.[packageName]) { | ||
packageJson.peerDependencies[packageName] = prefixed; | ||
} | ||
}); | ||
|
||
await packageManager.writePackageJson(packageJson); | ||
await packageManager.installDependencies(); | ||
|
||
await packageManager | ||
.executeCommand({ command: 'dedupe', args: [], stdio: 'ignore' }) | ||
.catch(() => {}); | ||
|
||
console.log(); | ||
console.log(dedent` | ||
We upgraded ${yellow(upgradable.length)} packages: | ||
${upgradable | ||
.map(({ packageName, afterVersion, beforeVersion }) => { | ||
return `- ${cyan(packageName)}: ${cyan(beforeVersion)} => ${cyan(afterVersion)}`; | ||
}) | ||
.join('\n')} | ||
`); | ||
} | ||
console.log(); | ||
}, | ||
} satisfies Fix<Options>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters