-
-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,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); | ||
}); | ||
}); |
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,110 @@ | ||
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 deprecatedPackages = [ | ||
{ | ||
minVersion: '6.0.0-alpha.0', | ||
url: 'https://github.com/storybookjs/storybook/blob/next/MIGRATION.md#60-deprecations', | ||
deprecations: [ | ||
'@storybook/addon-notes', | ||
'@storybook/addon-info', | ||
'@storybook/addon-contexts', | ||
'@storybook/addon-options', | ||
'@storybook/addon-centered', | ||
], | ||
}, | ||
]; | ||
|
||
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); | ||
} | ||
|
||
deprecatedPackages.forEach(({ minVersion, url, deprecations }) => { | ||
if (semver.gte(latestVersion, minVersion)) { | ||
const deprecated = storybookPackages.filter((pkg) => deprecations.includes(pkg.package)); | ||
if (deprecated.length > 0) { | ||
logger.warn(`Found ${deprecated.length} deprecated packages since ${minVersion}`); | ||
logger.warn(`See ${url}`); | ||
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(); | ||
}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.