Skip to content

Commit

Permalink
Merge pull request #21409 from storybookjs/valentin/angular-codemod
Browse files Browse the repository at this point in the history
Add Angular Builder Codemods
  • Loading branch information
valentinpalkovic authored Mar 6, 2023
2 parents a5c949a + c777f8b commit 541c0d7
Show file tree
Hide file tree
Showing 12 changed files with 637 additions and 21 deletions.
176 changes: 162 additions & 14 deletions code/frameworks/angular/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,16 @@

- [Storybook for Angular](#storybook-for-angular)
- [Getting Started](#getting-started)
- [Setup Compodoc](#setup-compodoc)
- [Support for multi-project workspace](#support-for-multi-project-workspace)
- [Setup Storybook for your Angular projects](#setup-storybook-for-your-angular-projects)
- [Run Storybook](#run-storybook)
- [Setup Compodoc](#setup-compodoc)
- [Automatic setup](#automatic-setup)
- [Manual setup](#manual-setup)
- [FAQ](#faq)
- [How do I migrate to a Angular Storybook builder?](#how-do-i-migrate-to-a-angular-storybook-builder)
- [Do you have only one Angular project in your workspace?](#do-you-have-only-one-angular-project-in-your-workspace)
- [Adjust your `package.json`](#adjust-your-packagejson)
- [I have multiple projects in my Angular workspace](#i-have-multiple-projects-in-my-angular-workspace)

Storybook for Angular is a UI development environment for your Angular components.
With it, you can visualize different states of your UI components and develop them interactively.
Expand All @@ -21,15 +28,9 @@ cd my-angular-app
npx storybook init
```

### Setup Compodoc
## Setup Storybook for your Angular projects

When installing, you will be given the option to set up Compodoc, which is a tool for creating documentation for Angular projects.

You can include JSDoc comments above components, directives, and other parts of your Angular code to include documentation for those elements. Compodoc uses these comments to generate documentation for your application. In Storybook, it is useful to add explanatory comments above @Inputs and @Outputs, since these are the main elements that Storybook displays in its user interface. The @Inputs and @Outputs are the elements that you can interact with in Storybook, such as controls.

## Support for multi-project workspace

Storybook supports Angular multi-project workspace. You can setup Storybook for each project in the workspace. When running `npx storybook init` you will be asked for which project Storybook should be set up. Essentially, during initialization, the `angular.json` will be edited to add the Storybook configuration for the selected project. The configuration looks approximately like this:
Storybook supports Angular multi-project workspace. You can setup Storybook for each project in the workspace. When running `npx storybook init` you will be asked for which project Storybook should be set up. Essentially, during initialization, the `.storybook` folder will be created and the `angular.json` will be edited to add the Storybook configuration for the selected project. The configuration looks approximately like this:

```json
// angular.json
Expand All @@ -44,19 +45,24 @@ Storybook supports Angular multi-project workspace. You can setup Storybook for
"storybook": {
"builder": "@storybook/angular:start-storybook",
"options": {
// the path to the storybook config directory
"configDir": ".storybook",
// the build target of your project
"browserTarget": "your-project:build",
"compodoc": false,
// the port you want to start Storybook on
"port": 6006
// further options are available and can be found in
// https://github.com/storybookjs/storybook/tree/next/code/frameworks/angular/src/builders/start-storybook/schema.json
}
},
"build-storybook": {
"builder": "@storybook/angular:build-storybook",
"options": {
"configDir": ".storybook",
"browserTarget": "your-project:build",
"compodoc": false,
"outputDir": "dist/storybook/your-project"
// further options are available and can be found in
// https://github.com/storybookjs/storybook/tree/next/code/frameworks/angular/src/builders/build-storybook/schema.json
}
}
}
Expand All @@ -70,19 +76,161 @@ Storybook supports Angular multi-project workspace. You can setup Storybook for
To run Storybook for a particular project, please run:

```sh
ng run your-project:storybook
ng run <your-project>:storybook
```

To build Storybook, run:

```sh
ng run your-project:build-storybook
ng run <your-project>:build-storybook
```

You will find the output in `dist/storybook/your-project`.

For more information visit: [storybook.js.org](https://storybook.js.org)

## Setup Compodoc

You can include JSDoc comments above components, directives, and other parts of your Angular code to include documentation for those elements. Compodoc uses these comments to generate documentation for your application. In Storybook, it is useful to add explanatory comments above @Inputs and @Outputs, since these are the main elements that Storybook displays in its user interface. The @Inputs and @Outputs are the elements that you can interact with in Storybook, such as controls.

### Automatic setup

When installing Storybook via `sb init`, you will be given the option to set up Compodoc automatically.

### Manual setup

If you have already installed Storybook, you can set up Compodoc manually.

Install the following dependencies:

```sh
npm i -D @compodoc/compodoc
```

Add the following option to your to the Storybook Builder:

```json
{
...
"projects": {
...
"your-project": {
...
"architect": {
...
"storybook": {
"builder": "@storybook/angular:start-storybook",
"options": {
...
"compodoc": true,
"compodocArgs": [
"-e",
"json",
"-d",
// Where to store the generated documentation. It's usually the root of your Angular project. It's not necessarily the root of your Angular Workspace!
"."
],
}
},
"build-storybook": {
"builder": "@storybook/angular:build-storybook",
"options": {
...
"compodoc": true,
"compodocArgs": [
"-e",
"json",
"-d",
"."
],
}
}
}
}
}
}
```

Go to your `.storybook/preview.js` and add the following:

```js
import { setCompodocJson } from '@storybook/addon-docs/angular';
import docJson from '../documentation.json';
setCompodocJson(docJson);

const preview: Preview = {
...
};

export default preview;
```

## FAQ

### How do I migrate to a Angular Storybook builder?

The Storybook [Angular builder](https://angular.io/guide/glossary#builder) is a new way to run Storybook in an Angular workspace. It is a drop-in replacement for running `storybook dev` and `storybook build` directly.

#### Do you have only one Angular project in your workspace?

In this case go to your `angular.json` and add `storybook` and `build-storybook` entries in `architect` section of your project like shown above.

##### Adjust your `package.json`

Go to your `package.json` and adjust your script section. Usually, it will look like this:

```json
{
"scripts": {
"storybook": "start-storybook -p 6006", // or `storybook dev -p 6006`
"build-storybook": "build-storybook" // or `storybook build`
}
}
```

Now, you can run Storybook with `ng run <your-project>:storybook` and build it with `ng run <your-project>:build-storybook`. Adjust the scripts in your `package.json` accordingly.

```json
{
"scripts": {
"storybook": "ng run <project-name>:storybook", // or `storybook dev -p 6006`
"build-storybook": "ng run <project-name>:build-storybook" // or `storybook build`
}
}
```

Also remove the compodoc part in your script section if you have set it up previously.
It is now built-in in `@storybook/angular` and you don't have to call it explicitly:

```json
{
"scripts": {
"docs:json": "compodoc -p tsconfig.json -e json -d ./documentation",
"storybook": "npm run docs:json && start-storybook -p 6006",
"build-storybook": "npm run docs:json && build-storybook"
}
}
```

Change it to:

```json
{
"scripts": {
"storybook": "ng run <project-name>:storybook",
"build-storybook": "ng run <project-name>:build-storybook"
}
}
```

#### I have multiple projects in my Angular workspace

In this case you have to adjust your `angular.json` and `package.json` as described above for each project in which you want to use Storybook. Please note, that each project should have a dedicated `.storybook` folder, which should be placed in the root of the project.

You can run `npx sb init` sequentially for each project to setup Storybook for each of them to automatically create the `.storybook` folder and create the necessary configuration in your `angular.json`.

You can then use [Storybook composition](https://storybook.js.org/docs/angular/sharing/storybook-composition) to composite multiple Storybooks into one.

---

Storybook also comes with a lot of [addons](https://storybook.js.org/addons) and a great API to customize as you wish.
Expand Down
4 changes: 2 additions & 2 deletions code/jest.init.base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ const localStorageMock = {
setItem: jest.fn().mockName('setItem'),
clear: jest.fn().mockName('clear'),
};
// @ts-expect-error (Converted from ts-ignore)
global.localStorage = localStorageMock;

Object.defineProperty(global, 'localStorage', { value: localStorageMock, writable: true });

/* Fail tests on PropType warnings
This allows us to throw an error in tests environments when there are prop-type warnings.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`is not Nx project angular builders Angular < 14.0.0 should throw an Error 1`] = `
"❌ Your project uses Angular < 14.0.0. Storybook 7.0 for Angular requires Angular 14.0.0 or higher.
Please upgrade your Angular version to at least version 14.0.0 to use Storybook 7.0 in your project."
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import type { StorybookConfig } from '@storybook/types';
import type { PackageJson } from '../../js-package-manager';
import { makePackageManager, mockStorybookData } from '../helpers/testing-helpers';
import { angularBuildersMultiproject } from './angular-builders-multiproject';
import * as helpers from '../../helpers';
import * as angularHelpers from '../../generators/ANGULAR/helpers';

const checkAngularBuilders = async ({
packageJson,
main: mainConfig = {},
storybookVersion = '7.0.0',
}: {
packageJson: PackageJson;
main?: Partial<StorybookConfig> & Record<string, unknown>;
storybookVersion?: string;
}) => {
mockStorybookData({ mainConfig, storybookVersion });

// mock file system (look at eslint plugin test)

return angularBuildersMultiproject.check({
packageManager: makePackageManager(packageJson),
});
};

jest.mock('../../helpers', () => ({
...jest.requireActual('../../helpers'),
isNxProject: jest.fn(),
}));

jest.mock('../../generators/ANGULAR/helpers', () => ({
...jest.requireActual('../../generators/ANGULAR/helpers'),
AngularJSON: jest.fn(),
}));

describe('is Nx project', () => {
beforeEach(() => {
(helpers.isNxProject as any as jest.SpyInstance).mockReturnValue(true);
});

it('should return null', async () => {
const packageJson = {
dependencies: { '@storybook/angular': '^7.0.0-alpha.0' },
};

await expect(checkAngularBuilders({ packageJson })).resolves.toBeNull();
});
});

describe('is not Nx project', () => {
beforeEach(() => {
(helpers.isNxProject as any as jest.SpyInstance).mockReturnValue(false);
});

describe('angular builders', () => {
afterEach(jest.restoreAllMocks);

describe('Angular not found', () => {
const packageJson = {
dependencies: { '@storybook/angular': '^7.0.0-alpha.0' },
};

it('should return null', async () => {
await expect(checkAngularBuilders({ packageJson })).resolves.toBeNull();
});
});

describe('Angular < 14.0.0', () => {
const packageJson = {
dependencies: { '@storybook/angular': '^7.0.0-alpha.0', '@angular/core': '^12.0.0' },
};

it('should return null', async () => {
await expect(checkAngularBuilders({ packageJson })).resolves.toBeNull();
});
});

describe('Angular >= 14.0.0', () => {
const packageJson = {
dependencies: { '@storybook/angular': '^7.0.0-alpha.0', '@angular/core': '^15.0.0' },
};

describe('has one Storybook builder defined', () => {
beforeEach(() => {
// Mock AngularJSON.constructor
(angularHelpers.AngularJSON as jest.Mock).mockImplementation(() => ({
hasStorybookBuilder: true,
}));
});

it('should return null', async () => {
await expect(checkAngularBuilders({ packageJson })).resolves.toBeNull();
});
});

describe('has one project', () => {
beforeEach(() => {
// Mock AngularJSON.constructor
(angularHelpers.AngularJSON as jest.Mock).mockImplementation(() => ({
hasStorybookBuilder: false,
projects: {
project1: { root: 'project1', architect: {} },
},
rootProject: 'project1',
}));
});

it('should return null', async () => {
await expect(checkAngularBuilders({ packageJson })).resolves.toBeNull();
});
});

describe('has multiple projects without root project defined', () => {
beforeEach(() => {
// Mock AngularJSON.constructor
(angularHelpers.AngularJSON as jest.Mock).mockImplementation(() => ({
hasStorybookBuilder: false,
projects: {
project1: { root: 'project1', architect: {} },
project2: { root: 'project2', architect: {} },
},
rootProject: null,
}));
});

it('should return an empty object', async () => {
await expect(checkAngularBuilders({ packageJson })).resolves.toMatchObject({});
});
});
});
});
});
Loading

0 comments on commit 541c0d7

Please sign in to comment.