Skip to content

Commit e9a1262

Browse files
authored
[FRNT-142] generate snapshot pages (#16)
1 parent d605a66 commit e9a1262

File tree

25 files changed

+332
-124
lines changed

25 files changed

+332
-124
lines changed

example/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"license": "MIT",
1212
"scripts": {
1313
"start": "gatsby develop",
14-
"build": "gatsby build"
14+
"build": "gatsby build",
15+
"serve": "gatsby serve"
1516
},
1617
"dependencies": {
1718
"@types/react": "^17.0.1",
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
### Props
2+
3+
| Name | Type | Default | Description |
4+
| ------- | ------------------------ | -------- | ---------------------------------------------------------- | ---------- | ----------------------- |
5+
| `text` | `React.ReactNode` | | Text appeared on the button |
6+
| `left` | `React.ReactNode` | `null` | Component to show on the left side of the text (ex.: Icon) |
7+
| `right` | `React.ReactNode` | `null` | Component on the right side of the text |
8+
| `type` | `'button' | 'submit' | 'reset'` | `'button'` | HTML type of the button |
9+
| `...` | `HTMLButtonElementProps` | `{}` | Other props is inherited from `HTMLButtonElement` |
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
### Props
2+
3+
| Name | Type | Default | Description |
4+
| ------- | ------------------------ | -------- | ---------------------------------------------------------- | ---------- | ----------------------- |
5+
| `text` | `React.ReactNode` | | Text appeared on the button |
6+
| `left` | `React.ReactNode` | `null` | Component to show on the left side of the text (ex.: Icon) |
7+
| `right` | `React.ReactNode` | `null` | Component on the right side of the text |
8+
| `type` | `'button' | 'submit' | 'reset'` | `'button'` | HTML type of the button |
9+
| `...` | `HTMLButtonElementProps` | `{}` | Other props is inherited from `HTMLButtonElement` |

example/src/calendar/date-selector/usage.mdx

Lines changed: 0 additions & 15 deletions
This file was deleted.

example/src/calendar/time-selector/usage.mdx

Lines changed: 0 additions & 15 deletions
This file was deleted.

example/src/woly/atoms/button/__screenshot-test__/config.js

Whitespace-only changes.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import React from 'react';
2+
3+
const ButtonStateMap = () => <button>Test button</button>;
4+
5+
export default ButtonStateMap;

example/src/woly/atoms/button/map.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import React from 'react';
2+
3+
const ButtonMap = () => (
4+
<>
5+
<button>Button variant 1</button>
6+
<button>Button variant 2</button>
7+
</>
8+
);
9+
10+
export default ButtonMap;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
### Спецификация

example/src/woly/atoms/button/usage.mdx

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
---
2-
name: button
3-
category: atoms
4-
package: 'woly'
5-
---
6-
71
import * as button from './index';
82
import { New } from './new';
93
import { ButtonImage } from './example';
Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
---
2-
name: chips
3-
category: atoms
4-
package: 'woly'
5-
---
6-
71
import { ChipImage } from './example';
82

93
Hello! It is chips
@@ -12,10 +6,10 @@ Hello! It is chips
126

137
### Props
148

15-
| Name | Type | Default | Description |
16-
| ------- | ------------------------------- | ---------- | ---------------------------------------------------------- |
17-
| `text` | `React.ReactNode` | | Text appeared on the button |
18-
| `left` | `React.ReactNode` | `null` | Component to show on the left side of the text (ex.: Icon) |
19-
| `right` | `React.ReactNode` | `null` | Component on the right side of the text |
20-
| `type` | `'button' | 'submit' | 'reset'` | `'button'` | HTML type of the button |
21-
| `...` | `HTMLButtonElementProps` | `{}` | Other props is inherited from `HTMLButtonElement` |
9+
| Name | Type | Default | Description |
10+
| ------- | ------------------------ | -------- | ---------------------------------------------------------- | ---------- | ----------------------- |
11+
| `text` | `React.ReactNode` | | Text appeared on the button |
12+
| `left` | `React.ReactNode` | `null` | Component to show on the left side of the text (ex.: Icon) |
13+
| `right` | `React.ReactNode` | `null` | Component on the right side of the text |
14+
| `type` | `'button' | 'submit' | 'reset'` | `'button'` | HTML type of the button |
15+
| `...` | `HTMLButtonElementProps` | `{}` | Other props is inherited from `HTMLButtonElement` |
Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
1-
---
2-
name: input
3-
category: atoms
4-
package: 'woly'
5-
---
6-
71
### Props
82

9-
| Name | Type | Default | Description |
10-
| ------- | ------------------------------- | ---------- | ---------------------------------------------------------- |
11-
| `text` | `React.ReactNode` | | Text appeared on the button |
12-
| `left` | `React.ReactNode` | `null` | Component to show on the left side of the text (ex.: Icon) |
13-
| `right` | `React.ReactNode` | `null` | Component on the right side of the text |
14-
| `type` | `'button' | 'submit' | 'reset'` | `'button'` | HTML type of the button |
15-
| `...` | `HTMLButtonElementProps` | `{}` | Other props is inherited from `HTMLButtonElement` |
3+
| Name | Type | Default | Description |
4+
| ------- | ------------------------ | -------- | ---------------------------------------------------------- | ---------- | ----------------------- |
5+
| `text` | `React.ReactNode` | | Text appeared on the button |
6+
| `left` | `React.ReactNode` | `null` | Component to show on the left side of the text (ex.: Icon) |
7+
| `right` | `React.ReactNode` | `null` | Component on the right side of the text |
8+
| `type` | `'button' | 'submit' | 'reset'` | `'button'` | HTML type of the button |
9+
| `...` | `HTMLButtonElementProps` | `{}` | Other props is inherited from `HTMLButtonElement` |

example/src/woly/molecules/input-password/__screenshot-test__/config.js

Whitespace-only changes.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import React from 'react';
2+
3+
const InpurPasswordStateMap = () => <input type="password" />;
4+
5+
export default InpurPasswordStateMap;

example/src/woly/molecules/input-password/usage.mdx

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
---
2-
name: input-password
3-
category: molecules
4-
package: 'woly'
5-
---
6-
71
import { InputPassword } from './example';
82

93
Hello! It is InputPassword

gatsby-theme-woly/gatsby-config.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ module.exports = (themeOptions) => {
3030
path: themeOptions.components,
3131
},
3232
},
33+
{
34+
resolve: `gatsby-plugin-page-creator`,
35+
options: {
36+
path: themeOptions.components,
37+
ignore: ['**/!(__screenshot-test__)/*', '**/*.(js|ts|jsx)'],
38+
},
39+
},
3340
],
3441
};
3542
};

gatsby-theme-woly/gatsby-node.js

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
const pathsPath = require.resolve('./src/paths.js');
88
const { paths } = require(pathsPath);
9+
const fs = require('fs');
910

1011
try {
1112
require.resolve(`babel-plugin-extract-react-types`);
@@ -32,12 +33,6 @@ async function createUsagePages({ actions, graphql, reporter }) {
3233
usages: allMdx {
3334
nodes {
3435
id
35-
frontmatter {
36-
category
37-
name
38-
package
39-
}
40-
tableOfContents
4136
fileAbsolutePath
4237
}
4338
}
@@ -51,13 +46,61 @@ async function createUsagePages({ actions, graphql, reporter }) {
5146

5247
const component = require.resolve('./src/templates/usage.js');
5348

54-
result.data.usages.nodes.forEach(({ frontmatter, id }) => {
49+
result.data.usages.nodes.forEach(({ id, fileAbsolutePath }) => {
50+
const [_, name, category, packageName] = fileAbsolutePath
51+
.split('/')
52+
.reverse();
53+
5554
actions.createPage({
56-
path: paths.componentUsage(frontmatter),
55+
id,
56+
path: paths.componentPage({ package: packageName, category, name }),
5757
component,
5858
context: {
5959
pageID: id,
60+
name,
61+
category,
62+
package: packageName,
6063
},
6164
});
6265
});
6366
}
67+
68+
exports.onPostBuild = async (gatsby) => {
69+
await findScreenshotTestingConfigs(gatsby);
70+
};
71+
72+
async function findScreenshotTestingConfigs({ graphql, reporter }) {
73+
const result = await graphql(`
74+
{
75+
configs: allFile(filter: { name: { eq: "config" } }) {
76+
nodes {
77+
relativePath
78+
}
79+
}
80+
}
81+
`);
82+
83+
if (result.errors) {
84+
reporter.panicOnBuild(`Error while running GraphQL query`);
85+
throw result.errors;
86+
}
87+
88+
const configPaths = result.data.configs.nodes.map(({ relativePath }) => {
89+
const [name, category, packageName] = relativePath
90+
.replace('/__screenshot-test__/config.js', '')
91+
.split('/')
92+
.reverse();
93+
94+
return {
95+
path: relativePath,
96+
name,
97+
category,
98+
package: packageName,
99+
};
100+
});
101+
102+
fs.writeFileSync(
103+
`./public/screenshot-test-configs.json`,
104+
JSON.stringify(configPaths, null, 2),
105+
);
106+
}

gatsby-theme-woly/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"babel-preset-gatsby": "^1.1.0",
3535
"change-case": "^4.1.2",
3636
"gatsby-plugin-mdx": "^1.10.0",
37+
"gatsby-plugin-page-creator": "^3.7.1",
3738
"gatsby-plugin-react-helmet": "^3.10.0",
3839
"gatsby-plugin-sharp": "^3.1.2",
3940
"gatsby-plugin-sitemap": "^2.12.0",

gatsby-theme-woly/src/components/components-menu.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ const Group = ({ group, activeMenu }) => (
3535
<MenuItems>
3636
{Object.keys(group.components).map((key) => (
3737
<GroupItems
38+
key={key}
3839
name={key}
3940
groupItems={group.components[key]}
4041
activeMenu={activeMenu}

gatsby-theme-woly/src/components/layout.js

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@ import styled from 'styled-components';
55

66
import { ComponentsMenu } from './components-menu';
77
import { paths } from '../paths';
8-
import { isHiddenCategory } from '../lib/guards';
98

109
export const Layout = ({ children }) => {
1110
const data = useStaticQuery(graphql`
1211
{
13-
mdx: allMdx(
14-
sort: { fields: [frontmatter___category, frontmatter___name] }
12+
pages: allSitePage(
13+
sort: { fields: [context___category, context___name] }
14+
filter: { component: { regex: "/usage.js$/g" } }
1515
) {
1616
components: nodes {
1717
id
18-
meta: frontmatter {
18+
meta: context {
1919
category
2020
name
2121
package
@@ -47,25 +47,30 @@ export const Layout = ({ children }) => {
4747

4848
function createMapping(data) {
4949
const packages = {};
50-
for (const component of data.mdx.components) {
51-
if (!packages[component.meta.package]) {
52-
packages[component.meta.package] = {};
50+
51+
for (const component of data.pages.components) {
52+
const {
53+
package: packageName,
54+
name: componentName,
55+
category,
56+
} = component.meta;
57+
58+
if (!packages[packageName]) {
59+
packages[packageName] = {};
5360
}
5461

55-
if (!packages[component.meta.package][component.meta.category]) {
56-
packages[component.meta.package][component.meta.category] = [];
62+
if (!packages[packageName][category]) {
63+
packages[packageName][category] = [];
5764
}
5865

5966
const prefix = data.pathPrefix || '';
6067

61-
if (!isHiddenCategory(component.meta.category)) {
62-
packages[component.meta.package][component.meta.category].push({
63-
...component.meta,
64-
path: prefix + paths.componentUsage(component.meta),
65-
id: component.id,
66-
title: camelCase(component.meta.name),
67-
});
68-
}
68+
packages[packageName][category].push({
69+
...component.meta,
70+
path: prefix + paths.componentPage(component.meta),
71+
id: component.id,
72+
title: camelCase(componentName),
73+
});
6974
}
7075

7176
return Object.keys(packages).reduce((list, name) => {

gatsby-theme-woly/src/lib/constants.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

gatsby-theme-woly/src/lib/guards.js

Lines changed: 0 additions & 15 deletions
This file was deleted.

gatsby-theme-woly/src/paths.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const paths = {
2-
componentUsage: ({ package: p, name }) => `/package/${p}/component/${name}`,
2+
componentPage: ({ package: p, category, name }) =>
3+
`/${p}/${category}/${name}`,
34
};
45
module.exports = { paths };

0 commit comments

Comments
 (0)