Skip to content
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

fix(tools): setup/update storybook only if there stories do exist #23413

Merged
merged 1 commit into from
Jun 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions tools/generators/migrate-converged-pkg/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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',
});
Expand Down
23 changes: 15 additions & 8 deletions tools/generators/migrate-converged-pkg/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
writeJson,
updateProjectConfiguration,
serializeJson,
offsetFromRoot,
} from '@nrwl/devkit';
import * as path from 'path';
import * as os from 'os';
Expand Down Expand Up @@ -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') {
Expand Down Expand Up @@ -668,6 +677,8 @@ function setupStorybook(tree: Tree, options: NormalizedSchema) {
});
}

removeTsIgnorePragmas();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should happen always no matter if init or remove of SB setup was initiated


function removeTsIgnorePragmas() {
const stories: string[] = [];
visitNotIgnoredFiles(tree, options.paths.sourceRoot, treePath => {
Expand Down Expand Up @@ -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 => {
Expand All @@ -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;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

simplified logic - this is now possible as no v9 component exists in react-examples. Also because create-component will create story files we can guarantee that this will be properly executed -> sb will be setup

const shouldDelete = !shouldInit;

if (shouldInit) {
if (hasStories) {
return 'init';
}

Expand Down