Skip to content

Commit

Permalink
fix(storybook): do not duplicate cacheable operations #27866 (#27951)
Browse files Browse the repository at this point in the history
<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

<!-- If this is a particularly complex change or feature addition, you
can request a dedicated Nx release for this pull request branch. Mention
someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they
will confirm if the PR warrants its own release for testing purposes,
and generate it for you if appropriate. -->

## Current Behavior
<!-- This is the behavior we have today -->
`build-storybook` is being added to cacheable operations multiple times
when the `@nx/storybook:init` generator is being invoked.


## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->
Do not add `build-storybook` multiple times

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #27866
  • Loading branch information
Coly010 authored Sep 17, 2024
1 parent 67ecff1 commit 79443ff
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
28 changes: 28 additions & 0 deletions packages/storybook/src/generators/init/init.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import {
type NxJsonConfiguration,
type Tree,
ProjectGraph,
updateNxJson,
readNxJson,
} from '@nx/devkit';
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
import { initGenerator } from './init';
Expand Down Expand Up @@ -56,4 +58,30 @@ describe('@nx/storybook:init', () => {
});
expect(tree.read('.gitignore', 'utf-8')).toMatchSnapshot();
});

it('should not duplicate cacheable operations in nx.json', async () => {
// ARRANGE
const nxJson = readNxJson(tree);
nxJson.tasksRunnerOptions ??= {};
nxJson.tasksRunnerOptions.default ??= {};
nxJson.tasksRunnerOptions.default.options ??= {};
nxJson.tasksRunnerOptions.default.options.cacheableOperations = [
'build-storybook',
];
updateNxJson(tree, nxJson);

// ACT
await initGenerator(tree, {
addPlugin: false,
});

// ASSERT
const updatedNxJson = readNxJson(tree);
expect(updatedNxJson.tasksRunnerOptions.default.options.cacheableOperations)
.toMatchInlineSnapshot(`
[
"build-storybook",
]
`);
});
});
2 changes: 1 addition & 1 deletion packages/storybook/src/generators/init/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ function addCacheableOperation(tree: Tree) {
const cacheableOperations: string[] | null =
nxJson.tasksRunnerOptions?.default?.options?.cacheableOperations;

if (cacheableOperations && cacheableOperations.includes('build-storybook')) {
if (cacheableOperations && !cacheableOperations.includes('build-storybook')) {
nxJson.tasksRunnerOptions.default.options.cacheableOperations.push(
'build-storybook'
);
Expand Down

0 comments on commit 79443ff

Please sign in to comment.