-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(storybook): created a generator as well
- Loading branch information
Showing
21 changed files
with
311 additions
and
163 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
docs/generated/api-angular/generators/change-storybook-targets.md
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,28 @@ | ||
--- | ||
title: '@nrwl/angular:change-storybook-targets generator' | ||
description: 'Change Storybook targets to use native Storybook builders' | ||
--- | ||
|
||
# @nrwl/angular:change-storybook-targets | ||
|
||
Change Storybook targets to use native Storybook builders | ||
|
||
## Usage | ||
|
||
```bash | ||
nx generate change-storybook-targets ... | ||
``` | ||
|
||
By default, Nx will search for `change-storybook-targets` in the default collection provisioned in `workspace.json`. | ||
|
||
You can specify the collection explicitly as follows: | ||
|
||
```bash | ||
nx g @nrwl/angular:change-storybook-targets ... | ||
``` | ||
|
||
Show what will be generated without writing to disk: | ||
|
||
```bash | ||
nx g change-storybook-targets ... --dry-run | ||
``` |
28 changes: 28 additions & 0 deletions
28
docs/generated/api-storybook/generators/change-storybook-targets.md
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,28 @@ | ||
--- | ||
title: '@nrwl/storybook:change-storybook-targets generator' | ||
description: 'Change Storybook targets to use native Storybook builders' | ||
--- | ||
|
||
# @nrwl/storybook:change-storybook-targets | ||
|
||
Change Storybook targets to use native Storybook builders | ||
|
||
## Usage | ||
|
||
```bash | ||
nx generate change-storybook-targets ... | ||
``` | ||
|
||
By default, Nx will search for `change-storybook-targets` in the default collection provisioned in `workspace.json`. | ||
|
||
You can specify the collection explicitly as follows: | ||
|
||
```bash | ||
nx g @nrwl/storybook:change-storybook-targets ... | ||
``` | ||
|
||
Show what will be generated without writing to disk: | ||
|
||
```bash | ||
nx g change-storybook-targets ... --dry-run | ||
``` |
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
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
8 changes: 8 additions & 0 deletions
8
packages/angular/src/generators/change-storybook-targets/change-storybook-targets.ts
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,8 @@ | ||
import type { Tree } from '@nrwl/devkit'; | ||
import { changeStorybookTargetsGenerator } from '@nrwl/storybook'; | ||
|
||
export async function angularChangeStorybookTargestGenerator(tree: Tree) { | ||
await changeStorybookTargetsGenerator(tree); | ||
} | ||
|
||
export default angularChangeStorybookTargestGenerator; |
4 changes: 4 additions & 0 deletions
4
packages/angular/src/generators/change-storybook-targets/compat.ts
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,4 @@ | ||
import { convertNxGenerator } from '@nrwl/devkit'; | ||
import angularChangeStorybookTargestGenerator from './change-storybook-targets'; | ||
|
||
export default convertNxGenerator(angularChangeStorybookTargestGenerator); |
9 changes: 9 additions & 0 deletions
9
packages/angular/src/generators/change-storybook-targets/schema.json
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,9 @@ | ||
{ | ||
"$schema": "http://json-schema.org/schema", | ||
"$id": "NxAngularChangeStorybookTargetsGenerator", | ||
"type": "object", | ||
"cli": "nx", | ||
"properties": {}, | ||
"additionalProperties": false, | ||
"required": [] | ||
} |
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
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
File renamed without changes.
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
166 changes: 166 additions & 0 deletions
166
packages/storybook/src/generators/change-storybook-targets/change-storybook-targets.ts
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,166 @@ | ||
import { | ||
logger, | ||
Tree, | ||
formatFiles, | ||
updateProjectConfiguration, | ||
getProjects, | ||
TargetConfiguration, | ||
ProjectConfiguration, | ||
Target, | ||
convertNxGenerator, | ||
} from '@nrwl/devkit'; | ||
import { findStorybookAndBuildTargets } from '../../utils/utilities'; | ||
|
||
export async function changeStorybookTargetsGenerator(tree: Tree) { | ||
let changesMade = false; | ||
let changesMadeToAtLeastOne = false; | ||
const projects = getProjects(tree); | ||
[...projects.entries()].forEach(([projectName, projectConfiguration]) => { | ||
changesMade = false; | ||
const { storybookBuildTarget, storybookTarget, buildTarget } = | ||
findStorybookAndBuildTargets(projectConfiguration.targets); | ||
if ( | ||
projectName && | ||
storybookTarget && | ||
projectConfiguration?.targets?.[storybookTarget]?.options?.uiFramework === | ||
'@storybook/angular' | ||
) { | ||
projectConfiguration.targets[storybookTarget] = updateStorybookTarget( | ||
projectConfiguration, | ||
storybookTarget, | ||
projectName, | ||
buildTarget, | ||
storybookBuildTarget | ||
); | ||
changesMade = true; | ||
changesMadeToAtLeastOne = true; | ||
projectConfiguration.targets[storybookBuildTarget] = | ||
updateStorybookBuildTarget( | ||
projectConfiguration, | ||
projectName, | ||
buildTarget, | ||
storybookBuildTarget | ||
); | ||
} else { | ||
logger.warn(`Could not find a Storybook target for ${projectName}.`); | ||
} | ||
if (changesMade) { | ||
updateProjectConfiguration(tree, projectName, projectConfiguration); | ||
} | ||
}); | ||
|
||
if (changesMadeToAtLeastOne) { | ||
await formatFiles(tree); | ||
} | ||
} | ||
|
||
function updateStorybookTarget( | ||
projectConfiguration: ProjectConfiguration, | ||
storybookTarget: string, | ||
projectName: string, | ||
buildTarget: string, | ||
storybookBuildTarget: string | ||
): TargetConfiguration { | ||
const oldStorybookTargetConfig: TargetConfiguration = | ||
projectConfiguration?.targets?.[storybookTarget]; | ||
const newStorybookTargetConfig: TargetConfiguration = { | ||
executor: '@storybook/angular:start-storybook', | ||
options: { | ||
port: oldStorybookTargetConfig.options.port, | ||
configDir: oldStorybookTargetConfig.options.config?.configFolder, | ||
browserTarget: undefined, | ||
compodoc: false, | ||
}, | ||
configurations: oldStorybookTargetConfig.configurations, | ||
}; | ||
|
||
const { project, target } = parseTargetStringCustom( | ||
oldStorybookTargetConfig.options.projectBuildConfig | ||
); | ||
if (project && target) { | ||
newStorybookTargetConfig.options.browserTarget = | ||
oldStorybookTargetConfig.options.projectBuildConfig; | ||
} else { | ||
newStorybookTargetConfig.options.browserTarget = `${projectName}:${ | ||
buildTarget ? buildTarget : storybookBuildTarget | ||
}`; | ||
} | ||
|
||
const { | ||
uiFramework, | ||
outputPath, | ||
config, | ||
projectBuildConfig, | ||
...optionsToCopy | ||
} = oldStorybookTargetConfig.options; | ||
|
||
newStorybookTargetConfig.options = { | ||
...optionsToCopy, | ||
...newStorybookTargetConfig.options, | ||
}; | ||
|
||
return newStorybookTargetConfig; | ||
} | ||
|
||
function updateStorybookBuildTarget( | ||
projectConfiguration: ProjectConfiguration, | ||
projectName: string, | ||
buildTarget: string, | ||
storybookBuildTarget: string | ||
): TargetConfiguration { | ||
const oldStorybookBuildTargetConfig: TargetConfiguration = | ||
projectConfiguration?.targets?.[storybookBuildTarget]; | ||
const newStorybookBuildTargetConfig: TargetConfiguration = { | ||
executor: '@storybook/angular:build-storybook', | ||
outputs: oldStorybookBuildTargetConfig.outputs, | ||
options: { | ||
outputDir: oldStorybookBuildTargetConfig.options.outputPath, | ||
configDir: oldStorybookBuildTargetConfig.options.config?.configFolder, | ||
browserTarget: undefined, | ||
compodoc: false, | ||
}, | ||
configurations: oldStorybookBuildTargetConfig.configurations, | ||
}; | ||
|
||
const { project, target } = parseTargetStringCustom( | ||
oldStorybookBuildTargetConfig.options.projectBuildConfig | ||
); | ||
if (project && target) { | ||
newStorybookBuildTargetConfig.options.browserTarget = | ||
oldStorybookBuildTargetConfig.options.projectBuildConfig; | ||
} else { | ||
newStorybookBuildTargetConfig.options.browserTarget = `${projectName}:${ | ||
buildTarget ? buildTarget : storybookBuildTarget | ||
}`; | ||
} | ||
|
||
const { | ||
uiFramework, | ||
outputPath, | ||
config, | ||
projectBuildConfig, | ||
...optionsToCopy | ||
} = oldStorybookBuildTargetConfig.options; | ||
|
||
newStorybookBuildTargetConfig.options = { | ||
...optionsToCopy, | ||
...newStorybookBuildTargetConfig.options, | ||
}; | ||
|
||
return newStorybookBuildTargetConfig; | ||
} | ||
|
||
function parseTargetStringCustom(targetString: string): Target { | ||
const [project, target, configuration] = targetString.split(':'); | ||
|
||
return { | ||
project, | ||
target, | ||
configuration, | ||
}; | ||
} | ||
|
||
export default changeStorybookTargetsGenerator; | ||
export const changeStorybookTargetsSchematic = convertNxGenerator( | ||
changeStorybookTargetsGenerator | ||
); |
Oops, something went wrong.