From 399ceba19216f53fbbd9d1f4d24788ea5b85d9cd Mon Sep 17 00:00:00 2001 From: Jason Jean Date: Thu, 17 Dec 2020 15:24:53 -0500 Subject: [PATCH] fix(storybook): update storybook to 6.1.11 --- packages/storybook/migrations.json | 5 ++ .../update-11-0-12/update-storybook.spec.ts | 51 ++++++++++++++++++ .../update-11-0-12/update-storybook.ts | 53 +++++++++++++++++++ packages/storybook/src/utils/versions.ts | 2 +- 4 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 packages/storybook/src/migrations/update-11-0-12/update-storybook.spec.ts create mode 100644 packages/storybook/src/migrations/update-11-0-12/update-storybook.ts diff --git a/packages/storybook/migrations.json b/packages/storybook/migrations.json index a5fd4cc203a71..c962ab1e15ba5 100644 --- a/packages/storybook/migrations.json +++ b/packages/storybook/migrations.json @@ -19,6 +19,11 @@ "version": "10.3.1-beta.1", "description": "Add missing storybook config to lint target", "factory": "./src/migrations/update-10-3-0/update-10-3-0" + }, + "update-11-0-12": { + "version": "11.0.12", + "description": "Update storybook if installed and above 6", + "factory": "./src/migrations/update-11-0-12/update-storybook" } }, "packageJsonUpdates": { diff --git a/packages/storybook/src/migrations/update-11-0-12/update-storybook.spec.ts b/packages/storybook/src/migrations/update-11-0-12/update-storybook.spec.ts new file mode 100644 index 0000000000000..ec1162dbb7f7d --- /dev/null +++ b/packages/storybook/src/migrations/update-11-0-12/update-storybook.spec.ts @@ -0,0 +1,51 @@ +import { Tree } from '@angular-devkit/schematics'; +import { readJsonInTree, readWorkspace } from '@nrwl/workspace'; +import { getFileContent } from '@nrwl/workspace/testing'; + +import { runMigration } from '../../utils/testing'; + +describe('Update 11-0-12', () => { + let tree: Tree; + + beforeEach(async () => { + tree = Tree.empty(); + }); + + it('should update storybook versions if storybook is already above 6', async () => { + tree.create( + 'package.json', + JSON.stringify({ + devDependencies: { + '@storybook/angular': '^6.0.0', + '@storybook/react': '^6.0.0', + '@storybook/addon-knobs': '^6.0.0', + }, + }) + ); + const result = await runMigration('update-11-0-12', {}, tree); + expect( + readJsonInTree(result, 'package.json').devDependencies[ + '@storybook/angular' + ] + ).toBe('^6.1.11'); + }); + + it('should not update storybook versions if storybook is below 6', async () => { + tree.create( + 'package.json', + JSON.stringify({ + devDependencies: { + '@storybook/angular': '^5.0.0', + '@storybook/react': '^5.0.0', + '@storybook/addon-knobs': '^5.0.0', + }, + }) + ); + const result = await runMigration('update-11-0-12', {}, tree); + expect( + readJsonInTree(result, 'package.json').devDependencies[ + '@storybook/angular' + ] + ).toBe('^5.0.0'); + }); +}); diff --git a/packages/storybook/src/migrations/update-11-0-12/update-storybook.ts b/packages/storybook/src/migrations/update-11-0-12/update-storybook.ts new file mode 100644 index 0000000000000..2435676f06ff4 --- /dev/null +++ b/packages/storybook/src/migrations/update-11-0-12/update-storybook.ts @@ -0,0 +1,53 @@ +import { chain, SchematicContext, Tree } from '@angular-devkit/schematics'; +import { + addInstallTask, + checkAndCleanWithSemver, + formatFiles, + updateJsonInTree, +} from '@nrwl/workspace'; + +import { gte } from 'semver'; +let needsInstall = false; + +const maybeUpdateVersion = updateJsonInTree('package.json', (json) => { + json.dependencies = json.dependencies || {}; + json.devDependencies = json.devDependencies || {}; + + const storybookPackages = [ + '@storybook/angular', + '@storybook/react', + '@storybook/addon-knobs', + ]; + storybookPackages.forEach((storybookPackageName) => { + if (json.dependencies[storybookPackageName]) { + const version = checkAndCleanWithSemver( + storybookPackageName, + json.dependencies[storybookPackageName] + ); + if (gte(version, '6.0.0')) { + json.dependencies[storybookPackageName] = '^6.1.11'; + needsInstall = true; + } + } + if (json.devDependencies[storybookPackageName]) { + const version = checkAndCleanWithSemver( + storybookPackageName, + json.devDependencies[storybookPackageName] + ); + if (gte(version, '6.0.0')) { + json.devDependencies[storybookPackageName] = '^6.1.11'; + needsInstall = true; + } + } + }); + + return json; +}); + +export default function (tree: Tree, context: SchematicContext) { + return chain([ + maybeUpdateVersion, + formatFiles(), + addInstallTask({ skipInstall: !needsInstall }), + ]); +} diff --git a/packages/storybook/src/utils/versions.ts b/packages/storybook/src/utils/versions.ts index fe1d55239b1d9..97f0edb9f6a3d 100644 --- a/packages/storybook/src/utils/versions.ts +++ b/packages/storybook/src/utils/versions.ts @@ -1,5 +1,5 @@ export const nxVersion = '*'; -export const storybookVersion = '^6.0.21'; +export const storybookVersion = '^6.1.11'; export const babelCoreVersion = '7.9.6'; export const babelLoaderVersion = '8.1.0'; export const babelPresetTypescriptVersion = '7.9.0';