Skip to content

Commit f62c881

Browse files
committed
fix(docs): Auto generate root index.ts
1 parent a3ab1be commit f62c881

File tree

6 files changed

+124
-88
lines changed

6 files changed

+124
-88
lines changed

README.md

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ This repo contains a set of opinionated react component groups used to standardi
99
- the component should bring a value value above and beyond existing PatternFly components
1010

1111
### To add a new component:
12-
1. create a folder in `src/` matching its name (for example `src/MyNewComponent`)
13-
2. to the new folder add a new `.tsx` file named after the component (for example `src/MyNewComponent/MyNewComponent.tsx`)
12+
1. create a folder in `src/` matching its name (for example `src/MyComponent`)
13+
2. to the new folder add a new `.tsx` file named after the component (for example `src/MyComponent/MyComponent.tsx`)
1414
3. to the same folder include also a `index.ts` which will export the component as a default and then all necessary interfaces
15-
4. don't forget to export your component using `export * from './ComponentName'` from the root `index` file located in `src/index.ts`
15+
4. if this file structure is not met, your component won't be exposed correctly
1616

1717
#### Example component:
1818
```
@@ -22,7 +22,7 @@ import { createUseStyles } from 'react-jss';
2222
2323
// do not forget to export your component's interface
2424
// always place component's interface above the component itself in the code
25-
export interface MyNewComponentProps {
25+
export interface MyComponentProps {
2626
text: String;
2727
}
2828
@@ -34,7 +34,7 @@ const useStyles = createUseStyles({
3434
})
3535
3636
// do not use named export of your component, just a default one
37-
const MyNewComponent: React.FunctionComponent<MyNewComponentProps> = () => {
37+
const MyComponent: React.FunctionComponent<MyComponentProps> = () => {
3838
const classes = useStyles();
3939
4040
return (
@@ -44,42 +44,38 @@ const MyNewComponent: React.FunctionComponent<MyNewComponentProps> = () => {
4444
);
4545
};
4646
47-
export default MyNewComponent;
47+
export default MyComponent;
4848
```
4949

5050
#### Index file example:
5151
```
52-
export { default } from './MyNewComponent';
53-
export * from './MyNewComponent';
52+
export { default } from './MyComponent';
53+
export * from './MyComponent';
5454
```
5555

5656
#### Component directory structure example:
5757
```
5858
src
59-
|- MyNewComponent
60-
| |- index.ts
61-
| |- MyNewComponent.tsx
62-
|- ...
63-
|- ...
64-
|- ...
65-
|- index.ts <<-- export * from './MyNewComponent';
59+
|- MyComponent
60+
|- index.ts
61+
|- MyComponent.tsx
6662
```
6763

6864
### Component's API rules:
6965
- prop names comply with PatternFly components naming standards (`variant`, `onClick`, `position`, etc.)
7066
- the API is maximally simplified and all props are provided with a description
7167
- it is build on the top of existing PatternFly types without prop omitting
72-
- it is well documented using the PatternFly documentation (`/packages/module/patternfly-docs/content/extensions/component-groups/examples/MyNewComponent/MyNewComponent.md`) with examples of all possible use cases (`packages/module/patternfly-docs/content/extensions/component-groups/examples/MyNewComponent/MyNewComponent[...]Example.tsx`)
68+
- it is well documented using the PatternFly documentation (`/packages/module/patternfly-docs/content/extensions/component-groups/examples/MyComponent/MyComponent.md`) with examples of all possible use cases (`packages/module/patternfly-docs/content/extensions/component-groups/examples/MyComponent/MyComponent[...]Example.tsx`)
7369
- do not unnecessarily use external libraries in your component - rather, delegate the necessary logic to the component's user using the component's API
7470

7571
#### Component API definition example:
7672
```
7773
// when possible, extend available PatternFly types
78-
export interface MyNewComponentProps extends ButtonProps {
74+
export interface MyComponentProps extends ButtonProps {
7975
customLabel: Boolean
8076
};
8177
82-
export const MyNewComponent: React.FunctionComponent<MyNewComponentProps> = ({ customLabel, ...props }) => ( ... );
78+
export const MyComponent: React.FunctionComponent<MyComponentProps> = ({ customLabel, ...props }) => ( ... );
8379
```
8480

8581

@@ -88,28 +84,28 @@ export const MyNewComponent: React.FunctionComponent<MyNewComponentProps> = ({ c
8884
---
8985
section: extensions
9086
subsection: Component groups
91-
id: MyNewComponent
92-
propComponents: ['MyNewComponent']
87+
id: MyComponent
88+
propComponents: ['MyComponent']
9389
---
9490
95-
import MyNewComponent from "@patternfly/react-component-groups/dist/dynamic/MyNewComponent";
91+
import MyComponent from "@patternfly/react-component-groups/dist/dynamic/MyComponent";
9692
9793
## Component usage
9894
99-
MyNewComponent has been created to demo contributing to this repository.
95+
MyComponent has been created to demo contributing to this repository.
10096
101-
### MyNewComponent component example label
97+
### MyComponent component example label
10298
103-
```js file="./MyNewComponentExample.tsx"```
99+
```js file="./MyComponentExample.tsx"```
104100
105101
```
106102

107-
#### Component usage file example: (`MyNewComponentExample.tsx`)
103+
#### Component usage file example: (`MyComponentExample.tsx`)
108104
```
109105
import React from 'react';
110106
111-
const MyNewComponentExample: React.FunctionComponent = () => (
112-
<MyNewComponent customLabel="My label">
107+
const MyComponentExample: React.FunctionComponent = () => (
108+
<MyComponent customLabel="My label">
113109
);
114110
115111
export default BatteryLowExample;

package-lock.json

Lines changed: 37 additions & 37 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -53,29 +53,6 @@
5353
"eslint-plugin-promise": "^6.0.0",
5454
"eslint-plugin-react": "^7.21.4",
5555
"eslint-plugin-react-hooks": "^4.6.0",
56-
"eslint-config-prettier": "8.6.0",
57-
"@typescript-eslint/eslint-plugin": "^5.49.0",
58-
"@typescript-eslint/parser": "^5.49.0",
59-
"prettier": "2.8.3",
60-
"jest":"^29.2.2",
61-
"babel-jest":"^29.2.2",
62-
"@babel/core": "^7.19.6",
63-
"@babel/preset-env": "^7.19.4",
64-
"@babel/preset-react":"^7.18.6",
65-
"@babel/preset-flow": "^7.18.6",
66-
"@babel/preset-typescript": "^7.18.6",
67-
"@testing-library/react":"^13.4.0",
68-
"@testing-library/user-event": "14.4.3",
69-
"@testing-library/jest-dom":"^5.14.1",
70-
"@testing-library/dom": "^8.0.7",
71-
"jest-environment-jsdom": "^29.5.0",
72-
"jest-canvas-mock": "^2.4.0",
73-
"ts-jest":"29.0.3",
74-
"serve": "^14.1.2",
75-
"rimraf": "^2.6.2",
76-
"whatwg-fetch": "^3.6.2",
77-
"babel-polyfill": "6.26.0",
78-
"identity-obj-proxy": "^3.0.0",
7956
"fs-extra": "^9.1.0",
8057
"glob": "^7.2.3",
8158
"identity-obj-proxy": "^3.0.0",

packages/module/generate-index.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
const fse = require('fs-extra');
2+
const glob = require('glob');
3+
const path = require('path');
4+
5+
const root = process.cwd();
6+
7+
const ENV_AGNOSTIC_ROOT = `${root}/src`
8+
9+
const sourceFiles = glob.sync(path.resolve(__dirname, './src/*/index.ts'))
10+
11+
async function generateIndex(files) {
12+
// ensure the dynamic root exists
13+
fse.ensureDirSync(path.resolve(ENV_AGNOSTIC_ROOT));
14+
15+
const destFile = `${ENV_AGNOSTIC_ROOT}/index.ts`;
16+
17+
const stream = fse.createWriteStream(destFile);
18+
stream.once('open', () => {
19+
stream.write('// this file is autogenerated by generate-index.js, modifying it manually will have no effect\n');
20+
21+
files.forEach(file => {
22+
const name = file.replace('/index.ts', '').split('/').pop();
23+
stream.write(`\nexport { default as ${name} } from './${name}';\n`);
24+
stream.write(`export * from './${name}';\n`);
25+
});
26+
stream.end();
27+
});
28+
29+
return Promise.resolve();
30+
}
31+
32+
async function run(files) {
33+
try {
34+
await generateIndex(files);
35+
36+
} catch (error) {
37+
// eslint-disable-next-line no-console
38+
console.error(error);
39+
process.exit(1);
40+
}
41+
}
42+
43+
run(sourceFiles);
44+

packages/module/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
"main": "dist/cjs/index.js",
66
"module": "dist/esm/index.js",
77
"scripts": {
8-
"build": "npm run build:js && npm run build:esm && npm run build:fed:packages",
8+
"build": "npm run build:index && npm run build:js && npm run build:esm && npm run build:fed:packages",
99
"build:watch": "npm run build:js && npm run build:esm -- --watch && npm run build:fed:packages -- --watch",
1010
"build:esm": "tsc --build --verbose ./tsconfig.json",
1111
"build:fed:packages": "node generate-fed-package-json.js",
1212
"build:js": "tsc -p tsconfig.cjs.json",
13+
"build:index": "node generate-index.js",
1314
"clean": "rimraf dist",
1415
"docs:develop": "pf-docs-framework start",
1516
"docs:build": "pf-docs-framework build all --output public",

packages/module/src/index.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,25 @@
1+
// this file is autogenerated by generate-index.js, modifying it manually will have no effect
2+
3+
export { default as Battery } from './Battery';
14
export * from './Battery';
5+
6+
export { default as DetailsPage } from './DetailsPage';
27
export * from './DetailsPage';
8+
9+
export { default as DetailsPageHeader } from './DetailsPageHeader';
310
export * from './DetailsPageHeader';
11+
12+
export { default as ErrorBoundary } from './ErrorBoundary';
413
export * from './ErrorBoundary';
14+
15+
export { default as ErrorStack } from './ErrorStack';
16+
export * from './ErrorStack';
17+
18+
export { default as ErrorState } from './ErrorState';
519
export * from './ErrorState';
20+
21+
export { default as HorizontalNav } from './HorizontalNav';
622
export * from './HorizontalNav';
23+
24+
export { default as NotAuthorized } from './NotAuthorized';
725
export * from './NotAuthorized';

0 commit comments

Comments
 (0)