Skip to content

Commit

Permalink
Fix compodoc generation for angular sandboxes
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinpalkovic committed Jan 12, 2023
1 parent 9b014bb commit 090cf83
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 31 deletions.
64 changes: 47 additions & 17 deletions code/addons/docs/angular/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ To learn more about Storybook Docs, read the [general documentation](../README.m
- [Installation](#installation)
- [DocsPage](#docspage)
- [Props tables](#props-tables)
- [Automatic Compodoc setup](#automatic-compodoc-setup)
- [Manual Compodoc setup](#manual-compodoc-setup)
- [MDX](#mdx)
- [IFrame height](#iframe-height)
- [Inline Stories](#inline-stories)
Expand Down Expand Up @@ -42,35 +44,63 @@ When you [install docs](#installation) you should get basic [DocsPage](../docs/d

Getting [Props tables](../docs/props-tables.md) for your components requires a few more steps. Docs for Angular relies on [Compodoc](https://compodoc.app/), the excellent API documentation tool. It supports `inputs`, `outputs`, `properties`, `methods`, `view/content child/children` as first class prop types.

To get this, you'll first need to install Compodoc:
### Automatic Compodoc setup

During `sb init`, you will be asked, whether you want to setup Compodoc for your project. Just answer the question with Yes. Compodoc is then ready to use!

## Manual Compodoc setup

You'll need to register Compodoc's `documentation.json` file in `.storybook/preview.ts`:

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

setCompodocJson(docJson);
```

Finally, to set up compodoc, you'll first need to install Compodoc:

```sh
yarn add -D @compodoc/compodoc
```

Then you'll need to configure Compodoc to generate a `documentation.json` file. Adding the following snippet to your `package.json` creates a metadata file `./documentation.json` each time you run storybook:
Then you'll need to configure Compodoc to generate a `documentation.json` file. Adding the following snippet to your `projects.<project>.architect.<storybook|build-storybook>` in the `angular.json` creates a metadata file `./documentation.json` each time you run storybook:

```json
```jsonc
// angular.json
{
...
"scripts": {
"docs:json": "compodoc -p ./tsconfig.json -e json -d .",
"storybook": "npm run docs:json && start-storybook -p 6006 -s src/assets",
...
},
"projects": {
"your-project": {
"architect": {
"storybook": {
...,
"compodoc": true,
"compodocArgs": [
"-e",
"json",
"-d",
"." // the root folder of your project
],
},
"build-storybook": {
...,
"compodoc": true,
"compodocArgs": [
"-e",
"json",
"-d",
"." // the root folder of your project
],
}
}
}
}
}
```

Unfortunately, it's not currently possible to update this dynamically as you edit your components, but [there's an open issue](https://github.com/storybookjs/storybook/issues/8672) to support this with improvements to Compodoc.

Next, add the following to `.storybook/preview.ts` to load the Compodoc-generated file:

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

Finally, be sure to fill in the `component` field in your story metadata:

```ts
Expand Down
43 changes: 35 additions & 8 deletions docs/essentials/auto-generated-controls/angular.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ export default {

Storybook uses this to auto-generate the `ArgTypes` for your component using [Compodoc](https://compodoc.app/). It supports `inputs`, `outputs`, `properties`, `methods`, `view/content child/children` as first class prop types.

## Automatic Compodoc setup

During `sb init`, you will be asked, whether you want to setup Compodoc for your project. Just answer the question with Yes. Compodoc is then ready to use!

## Manual Compodoc setup

You'll need to register Compodoc's `documentation.json` file in `.storybook/preview.ts`:

```js
Expand All @@ -26,16 +32,37 @@ Finally, to set up compodoc, you'll first need to install Compodoc:
yarn add -D @compodoc/compodoc
```

Then you'll need to configure Compodoc to generate a `documentation.json` file. Adding the following snippet to your `package.json` creates a metadata file `./documentation.json` each time you run storybook:
Then you'll need to configure Compodoc to generate a `documentation.json` file. Adding the following snippet to your `projects.<project>.architect.<storybook|build-storybook>` in the `angular.json` creates a metadata file `./documentation.json` each time you run storybook:

```json
```jsonc
// angular.json
{
...
"scripts": {
"docs:json": "compodoc -p ./tsconfig.json -e json -d .",
"storybook": "npm run docs:json && start-storybook -p 6006 -s src/assets",
...
},
"projects": {
"your-project": {
"architect": {
"storybook": {
...,
"compodoc": true,
"compodocArgs": [
"-e",
"json",
"-d",
"." // the root folder of your project
],
},
"build-storybook": {
...,
"compodoc": true,
"compodocArgs": [
"-e",
"json",
"-d",
"." // the root folder of your project
],
}
}
}
}
}
```

Expand Down
48 changes: 46 additions & 2 deletions scripts/tasks/sandbox-parts.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
// This file requires many imports from `../code`, which requires both an install and bootstrap of
// the repo to work properly. So we load it async in the task runner *after* those steps.

/* eslint-disable no-restricted-syntax, no-await-in-loop */
import { copy, ensureSymlink, ensureDir, existsSync, pathExists } from 'fs-extra';
/* eslint-disable no-restricted-syntax, no-await-in-loop, no-param-reassign */
import {
copy,
ensureSymlink,
ensureDir,
existsSync,
pathExists,
readJson,
writeJson,
} from 'fs-extra';
import { join, resolve, sep } from 'path';

import type { Task } from '../task';
Expand Down Expand Up @@ -125,6 +133,13 @@ export const install: Task['run'] = async ({ sandboxDir, template }, { link, dry
prefix:
'NODE_OPTIONS="--preserve-symlinks --preserve-symlinks-main" STORYBOOK_TELEMETRY_URL="http://localhost:6007/event-log"',
});

switch (template.expected.framework) {
case '@storybook/angular':
await prepareAngularSandbox(cwd);
break;
default:
}
};

// Ensure that sandboxes can refer to story files defined in `code/`.
Expand Down Expand Up @@ -428,3 +443,32 @@ export const addStories: Task['run'] = async (

await writeConfig(mainConfig);
};

/**
* Sets compodoc option in angular.json projects to false. We have to generate compodoc
* manually to avoid symlink issues related to the template-stories folder.
* In a second step a docs:json script is placed into the package.json to generate the
* Compodoc documentation.json, which respects symlinks
* */
async function prepareAngularSandbox(cwd: string) {
const angularJson = await readJson(join(cwd, 'angular.json'));

Object.keys(angularJson.projects).forEach((projectName: string) => {
angularJson.projects[projectName].architect.storybook.options.compodoc = false;
angularJson.projects[projectName].architect['build-storybook'].options.compodoc = false;
});

await writeJson(join(cwd, 'angular.json'), angularJson, { spaces: 2 });

const packageJsonPath = join(cwd, 'package.json');
const packageJson = await readJson(packageJsonPath);

packageJson.scripts = {
...packageJson.scripts,
'docs:json': 'DIR=$PWD; cd ../../scripts; yarn ts-node combine-compodoc $DIR',
storybook: `yarn docs:json && ${packageJson.scripts.storybook}`,
'build-storybook': `yarn docs:json && ${packageJson.scripts['build-storybook']}`,
};

await writeJson(packageJsonPath, packageJson, { spaces: 2 });
}
4 changes: 0 additions & 4 deletions scripts/utils/package-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ export async function updatePackageScripts({ cwd, prefix }: { cwd: string; prefi
storybook: `${prefix} ${packageJson.scripts.storybook}`,
'build-storybook': `${prefix} ${packageJson.scripts['build-storybook']}`,
}),
// See comment in combine-compodoc as to why this is necessary
...(packageJson.scripts['docs:json'] && {
'docs:json': 'DIR=$PWD; cd ../../scripts; yarn ts-node combine-compodoc $DIR',
}),
};
await writeJSON(packageJsonPath, packageJson, { spaces: 2 });
}

0 comments on commit 090cf83

Please sign in to comment.