-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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: Add upgrade utility with version consistency check #11396
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { getStorybookVersion, isCorePackage } from './upgrade'; | ||
|
||
describe.each([ | ||
['│ │ │ ├── @babel/[email protected] deduped', null], | ||
[ | ||
'│ ├── @storybook/[email protected] extraneous', | ||
{ package: '@storybook/theming', version: '6.0.0-beta.37' }, | ||
], | ||
[ | ||
'├─┬ @storybook/[email protected]', | ||
{ package: '@storybook/preset-create-react-app', version: '3.1.2' }, | ||
], | ||
['│ ├─┬ @storybook/[email protected]', { package: '@storybook/node-logger', version: '5.3.19' }], | ||
[ | ||
'npm ERR! peer dep missing: @storybook/react@>=5.2, required by @storybook/[email protected]', | ||
null, | ||
], | ||
])('getStorybookVersion', (input, output) => { | ||
it(input, () => { | ||
expect(getStorybookVersion(input)).toEqual(output); | ||
}); | ||
}); | ||
|
||
describe.each([ | ||
['@storybook/react', true], | ||
['@storybook/node-logger', true], | ||
['@storybook/addon-info', true], | ||
['@storybook/something-random', true], | ||
['@storybook/preset-create-react-app', false], | ||
['@storybook/linter-config', false], | ||
['@storybook/design-system', false], | ||
])('isCorePackage', (input, output) => { | ||
it(input, () => { | ||
expect(isCorePackage(input)).toEqual(output); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import { sync as spawnSync } from 'cross-spawn'; | ||
import semver from '@storybook/semver'; | ||
import { logger } from '@storybook/node-logger'; | ||
import { JsPackageManagerFactory } from './js-package-manager'; | ||
import { commandLog } from './helpers'; | ||
|
||
type Package = { | ||
package: string; | ||
version: string; | ||
}; | ||
|
||
const versionRegex = /(@storybook\/[^@]+)@(\S+)/; | ||
export const getStorybookVersion = (line: string) => { | ||
if (line.startsWith('npm ')) return null; | ||
const match = versionRegex.exec(line); | ||
if (!match || !semver.clean(match[2])) return null; | ||
return { | ||
package: match[1], | ||
version: match[2], | ||
}; | ||
}; | ||
|
||
const excludeList = [ | ||
'@storybook/linter-config', | ||
'@storybook/design-system', | ||
'@storybook/ember-cli-storybook', | ||
'@storybook/semver', | ||
'@storybook/eslint-config-storybook', | ||
'@storybook/addon-console', | ||
'@storybook/csf', | ||
]; | ||
export const isCorePackage = (pkg: string) => | ||
pkg.startsWith('@storybook/') && | ||
!pkg.startsWith('@storybook/preset-') && | ||
!excludeList.includes(pkg); | ||
|
||
const deprecatedList = [ | ||
'@storybook/addon-notes', | ||
'@storybook/addon-info', | ||
'@storybook/addon-contexts', | ||
'@storybook/addon-options', | ||
'@storybook/addon-centered', | ||
]; | ||
export const isDeprecatedPackage = (pkg: string) => deprecatedList.includes(pkg); | ||
|
||
const formatPackage = (pkg: Package) => `${pkg.package}@${pkg.version}`; | ||
|
||
const warnPackages = (pkgs: Package[]) => | ||
pkgs.forEach((pkg) => logger.warn(`- ${formatPackage(pkg)}`)); | ||
|
||
export const checkVersionConsistency = () => { | ||
const lines = spawnSync('npm', ['ls'], { stdio: 'pipe' }).output.toString().split('\n'); | ||
const storybookPackages = lines | ||
.map(getStorybookVersion) | ||
.filter(Boolean) | ||
.filter((pkg) => isCorePackage(pkg.package)); | ||
if (!storybookPackages.length) { | ||
throw new Error('No storybook core packages found!'); | ||
} | ||
storybookPackages.sort((a, b) => semver.rcompare(a.version, b.version)); | ||
const latestVersion = storybookPackages[0].version; | ||
const outdated = storybookPackages.filter((pkg) => pkg.version !== latestVersion); | ||
if (outdated.length > 0) { | ||
logger.warn( | ||
`Found ${outdated.length} outdated packages (relative to '${formatPackage( | ||
storybookPackages[0] | ||
)}')` | ||
); | ||
logger.warn('Please make sure your packages are updated to ensure a consistent experience.'); | ||
warnPackages(outdated); | ||
} | ||
|
||
if (semver.gt(latestVersion, '5.4.0')) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would we have to update this magic constant every release? Should we not pull it from There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Made this more general, but maybe not in the way you had in mind |
||
const deprecated = storybookPackages.filter((pkg) => isDeprecatedPackage(pkg.package)); | ||
if (deprecated.length > 0) { | ||
logger.warn(`Found ${deprecated.length} deprecated packages`); | ||
logger.warn( | ||
'See https://github.com/storybookjs/storybook/blob/next/MIGRATION.md#60-deprecations' | ||
); | ||
warnPackages(deprecated); | ||
} | ||
} | ||
}; | ||
|
||
type Options = { prerelease: boolean; skipCheck: boolean; useNpm: boolean; dryRun: boolean }; | ||
export const upgrade = async ({ prerelease, skipCheck, useNpm, dryRun }: Options) => { | ||
const packageManager = JsPackageManagerFactory.getPackageManager(useNpm); | ||
|
||
commandLog(`Checking for latest versions of '@storybook/*' packages`); | ||
|
||
const flags = []; | ||
if (!dryRun) flags.push('--upgrade'); | ||
if (prerelease) flags.push('--newest'); | ||
const check = spawnSync('npx', ['npm-check-updates', '/storybook/', ...flags], { | ||
stdio: 'pipe', | ||
}).output.toString(); | ||
logger.info(check); | ||
|
||
if (!dryRun) { | ||
commandLog(`Installing upgrades`); | ||
packageManager.installDependencies(); | ||
} | ||
|
||
if (!skipCheck) checkVersionConsistency(); | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm confused what this is testing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the output of
npm ls
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see.