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

feat(#126): add tool to generate stories #283

Closed
wants to merge 2 commits into from
Closed
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
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
🚀 Start UI <small>[web]</small> is an opinionated frontend starter repository created & maintained by the [BearStudio Team](https://www.bearstudio.fr/team) and other contributors.
It represents our team's up-to-date stack that we use when creating web apps for our clients.

---

## Getting Started

```bash
Expand All @@ -17,6 +19,8 @@ cd myApp
yarn dev
```

---

## Technology

- 🟦 [TypeScript](https://www.typescriptlang.org/)
Expand All @@ -40,6 +44,8 @@ yarn dev

ℹ️ API calls are mapped on a [JHipster](https://www.jhipster.tech/) backend application.

---

## Features

- Reponsive layout / navigation.
Expand All @@ -55,13 +61,17 @@ yarn dev
- API Schema documentation via [Swagger UI React](https://github.com/swagger-api/swagger-ui).
- API Mocking with persisting state via [MirageJS](https://miragejs.com/).

---

## Installation

```bash
yarn install
yarn build
```

---

## Development

```bash
Expand Down Expand Up @@ -118,6 +128,20 @@ NEXT_PUBLIC_DEV_ENV_NAME=staging
NEXT_PUBLIC_DEV_ENV_COLOR_SCHEME=teal
```

---

## Generators

### Generate Storybook documentation for components

Using fuzzy finding, you can search for components and generate the stories for it.

```bash
yarn new:story
```

---

## API Documentation

API documentation is accessible by admins in the app with [Swagger-UI](https://www.npmjs.com/package/swagger-ui-react).
Expand All @@ -126,6 +150,8 @@ yarn docs:build
```
This will build the json documentation from the main file `/src/mocks/openapi/openapi.yaml`.

---

## Translations

### Setup the i18n Ally extension
Expand Down Expand Up @@ -216,6 +242,8 @@ t('account:resetPassword.actions.reset')

- Use the common workspace only for VERY generic translations. By default, use specific namespaces to allow easy update on large code base without unwanted side-effects.

---

## Production

### NodeJS (recommended)
Expand Down
12 changes: 12 additions & 0 deletions generators/story/docs.stories.tsx.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import React from 'react';
import { ComponentMeta } from '@storybook/react';

import { {{ properCase ( getName path ) }} } from '@/{{ getFolder path }}';

export default {
title: '{{ directoryCase ( getFolder path ) }}',
} as ComponentMeta<typeof {{ properCase ( getName path ) }}>;

export const Default = () => (
<{{ properCase ( getName path ) }} />
)
21 changes: 21 additions & 0 deletions generators/story/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module.exports = {
description: 'Component Story Generator',
prompts: [
{
type: 'fuzzypath',
message: 'Component name for story',
name: 'path',
rootPath: 'src',
itemType: 'file',
excludeFilter: (nodePath) =>
!nodePath.endsWith('.tsx') || nodePath.endsWith('.stories.tsx'),
},
],
actions: [
{
type: 'add',
path: 'src/{{getFolder path}}/docs.stories.tsx',
templateFile: 'generators/story/docs.stories.tsx.hbs',
},
],
};
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"static:serve": "yarn static:build && npx servor out app.html 5000",
"theme:generate-typing": "chakra-cli tokens ./src/theme/theme.ts",
"theme:generate-icons": "svgr --config-file src/components/Icons/svgr.config.js src/components/Icons/svg-sources",
"new:story": "plop story",
Copy link
Contributor

@DecampsRenan DecampsRenan Jan 5, 2023

Choose a reason for hiding this comment

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

I should have done something like this:

Suggested change
"new:story": "plop story",
"generate": "plop",

Here we create an alias of plop, so we can do the following commands without to define new scripts in package.json:

yarn generate component
yarn generate page

We could have use plop, but I personally don't like yarn plop component because it doesn't mean anything ^^

"docs:build": "swagger-cli bundle src/mocks/openapi/openapi.yaml -t json --outfile public/open-api.json"
},
"lint-staged": {
Expand Down Expand Up @@ -99,8 +100,10 @@
"eslint-plugin-react": "7.30.1",
"eslint-plugin-react-hooks": "4.6.0",
"husky": "8.0.1",
"inquirer-fuzzy-path": "2.3.0",
"lint-staged": "13.0.3",
"miragejs": "0.1.45",
"plop": "3.1.1",
"prettier": "2.7.1",
"react-is": "18.2.0",
"storybook-dark-mode": "1.1.0",
Expand Down
43 changes: 43 additions & 0 deletions plopfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/* eslint-disable @typescript-eslint/no-var-requires */
const inquirerFuzzyPath = require('inquirer-fuzzy-path');
const storyGenerator = require('./generators/story');

module.exports = function (plop) {
plop.setGenerator('story', storyGenerator);

//#region //*=========== Handlebars Helper ===========
/**
* Generate story component route
* @see https://stackoverflow.com/questions/41490076/capitalize-every-letter-after-and-characters
*/
plop.setHelper('directoryCase', function (title) {
return title.replace(/(^|\/|-)(\S)/g, (s) => s.toUpperCase());
});

/**
* Remove 'src', and file name from path
*/
plop.setHelper('getFolder', (path) => {
const split = path.split('/');
// remove filename
split.pop();
if (split[0] === 'src') split.splice(0, 1);
return split.join('/');
});

/**
* Remove file name from path and get the parent folder name
*/
plop.setHelper('getName', (path) => {
const split = path.split('/');
// remove filename
split.pop();
// get the parent folder name
return split.pop();
});
//#endregion //*======== Handlebars Helper ===========

//#region //*=========== Inquirer Prompt ===========
plop.setPrompt('fuzzypath', inquirerFuzzyPath);
//#endregion //*======== Inquirer Prompt ===========
};
10 changes: 8 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@
"incremental": false,
"typeRoots": ["node_modules/@types", "src/types"]
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
"exclude": ["node_modules"]
"include": [
"next-env.d.ts",
"**/*.ts",
"**/*.tsx",
"generators/story.js",
"plopfile.js"
Jessy-BAER marked this conversation as resolved.
Show resolved Hide resolved
],
"exclude": ["node_modules", "generators/**/*", "plopfile.js"]
}
Loading