From 1119de086edd6fb159adea73649f86bacebaf909 Mon Sep 17 00:00:00 2001 From: Martin Hochel Date: Wed, 1 Jun 2022 10:51:59 +0200 Subject: [PATCH] fix(tools): setup/update storybook only if there stories do exist --- .../migrate-converged-pkg/index.spec.ts | 10 ++++++-- .../generators/migrate-converged-pkg/index.ts | 23 ++++++++++++------- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/tools/generators/migrate-converged-pkg/index.spec.ts b/tools/generators/migrate-converged-pkg/index.spec.ts index 1584274ba322c8..f2d4b8b0eec239 100644 --- a/tools/generators/migrate-converged-pkg/index.spec.ts +++ b/tools/generators/migrate-converged-pkg/index.spec.ts @@ -506,6 +506,7 @@ describe('migrate-converged-pkg generator', () => { lib: `${projectConfig.root}/tsconfig.lib.json`, test: `${projectConfig.root}/tsconfig.spec.json`, }, + packageJson: `${projectConfig.root}/package.json`, }; if (config.createDummyStories) { @@ -544,6 +545,13 @@ describe('migrate-converged-pkg generator', () => { expect(tree.exists(projectStorybookConfigPath)).toBeTruthy(); + expect(readJson(tree, paths.packageJson).scripts).toEqual( + expect.objectContaining({ + start: 'yarn storybook', + storybook: 'node ../../scripts/storybook/runner', + }), + ); + expect(readJson(tree, paths.tsconfig.storybook)).toEqual({ extends: '../tsconfig.json', compilerOptions: { @@ -814,8 +822,6 @@ describe('migrate-converged-pkg generator', () => { 'code-style': 'just-scripts code-style', just: 'just-scripts', lint: 'just-scripts lint', - start: 'yarn storybook', - storybook: 'node ../../../scripts/storybook/runner', test: 'jest --passWithNoTests', 'type-check': 'tsc -b tsconfig.json', }); diff --git a/tools/generators/migrate-converged-pkg/index.ts b/tools/generators/migrate-converged-pkg/index.ts index 41e9d9fc560d5c..c96a66939c764e 100644 --- a/tools/generators/migrate-converged-pkg/index.ts +++ b/tools/generators/migrate-converged-pkg/index.ts @@ -12,6 +12,7 @@ import { writeJson, updateProjectConfiguration, serializeJson, + offsetFromRoot, } from '@nrwl/devkit'; import * as path from 'path'; import * as os from 'os'; @@ -633,7 +634,15 @@ function setupStorybook(tree: Tree, options: NormalizedSchema) { return json; }); - removeTsIgnorePragmas(); + updateJson(tree, options.paths.packageJson, (json: PackageJson) => { + const scripts = { + storybook: `node ${offsetFromRoot(options.projectConfig.root)}scripts/storybook/runner`, + start: 'yarn storybook', + }; + Object.assign(json.scripts, scripts); + + return json; + }); } if (sbAction === 'remove') { @@ -668,6 +677,8 @@ function setupStorybook(tree: Tree, options: NormalizedSchema) { }); } + removeTsIgnorePragmas(); + function removeTsIgnorePragmas() { const stories: string[] = []; visitNotIgnoredFiles(tree, options.paths.sourceRoot, treePath => { @@ -703,7 +714,6 @@ function setupStorybook(tree: Tree, options: NormalizedSchema) { } function shouldSetupStorybook(tree: Tree, options: NormalizedSchema) { - const hasStorybookConfig = tree.exists(options.paths.storybook.main); let hasStories = false; visitNotIgnoredFiles(tree, options.projectConfig.root, treePath => { @@ -713,13 +723,10 @@ function shouldSetupStorybook(tree: Tree, options: NormalizedSchema) { } }); - const tags = options.projectConfig.tags || []; - const hasTags = tags.includes('vNext') && tags.includes('platform:web'); - - const shouldInit = hasStories || hasTags; - const shouldDelete = !shouldInit && hasStorybookConfig; + const shouldInit = hasStories; + const shouldDelete = !shouldInit; - if (shouldInit) { + if (hasStories) { return 'init'; }