Skip to content

Commit

Permalink
[FRNT-142] generate snapshot pages (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
ainursharaev authored Jun 22, 2021
1 parent d605a66 commit e9a1262
Show file tree
Hide file tree
Showing 25 changed files with 332 additions and 124 deletions.
3 changes: 2 additions & 1 deletion example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"license": "MIT",
"scripts": {
"start": "gatsby develop",
"build": "gatsby build"
"build": "gatsby build",
"serve": "gatsby serve"
},
"dependencies": {
"@types/react": "^17.0.1",
Expand Down
9 changes: 9 additions & 0 deletions example/src/calendar/atoms/date-selector/usage.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
### Props

| Name | Type | Default | Description |
| ------- | ------------------------ | -------- | ---------------------------------------------------------- | ---------- | ----------------------- |
| `text` | `React.ReactNode` | | Text appeared on the button |
| `left` | `React.ReactNode` | `null` | Component to show on the left side of the text (ex.: Icon) |
| `right` | `React.ReactNode` | `null` | Component on the right side of the text |
| `type` | `'button' | 'submit' | 'reset'` | `'button'` | HTML type of the button |
| `...` | `HTMLButtonElementProps` | `{}` | Other props is inherited from `HTMLButtonElement` |
9 changes: 9 additions & 0 deletions example/src/calendar/atoms/time-selector/usage.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
### Props

| Name | Type | Default | Description |
| ------- | ------------------------ | -------- | ---------------------------------------------------------- | ---------- | ----------------------- |
| `text` | `React.ReactNode` | | Text appeared on the button |
| `left` | `React.ReactNode` | `null` | Component to show on the left side of the text (ex.: Icon) |
| `right` | `React.ReactNode` | `null` | Component on the right side of the text |
| `type` | `'button' | 'submit' | 'reset'` | `'button'` | HTML type of the button |
| `...` | `HTMLButtonElementProps` | `{}` | Other props is inherited from `HTMLButtonElement` |
15 changes: 0 additions & 15 deletions example/src/calendar/date-selector/usage.mdx

This file was deleted.

15 changes: 0 additions & 15 deletions example/src/calendar/time-selector/usage.mdx

This file was deleted.

Empty file.
5 changes: 5 additions & 0 deletions example/src/woly/atoms/button/__screenshot-test__/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import React from 'react';

const ButtonStateMap = () => <button>Test button</button>;

export default ButtonStateMap;
10 changes: 10 additions & 0 deletions example/src/woly/atoms/button/map.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import React from 'react';

const ButtonMap = () => (
<>
<button>Button variant 1</button>
<button>Button variant 2</button>
</>
);

export default ButtonMap;
1 change: 1 addition & 0 deletions example/src/woly/atoms/button/spec.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
### Спецификация
6 changes: 0 additions & 6 deletions example/src/woly/atoms/button/usage.mdx
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
---
name: button
category: atoms
package: 'woly'
---

import * as button from './index';
import { New } from './new';
import { ButtonImage } from './example';
Expand Down
20 changes: 7 additions & 13 deletions example/src/woly/atoms/chips/usage.mdx
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
---
name: chips
category: atoms
package: 'woly'
---

import { ChipImage } from './example';

Hello! It is chips
Expand All @@ -12,10 +6,10 @@ Hello! It is chips

### Props

| Name | Type | Default | Description |
| ------- | ------------------------------- | ---------- | ---------------------------------------------------------- |
| `text` | `React.ReactNode` | | Text appeared on the button |
| `left` | `React.ReactNode` | `null` | Component to show on the left side of the text (ex.: Icon) |
| `right` | `React.ReactNode` | `null` | Component on the right side of the text |
| `type` | `'button' | 'submit' | 'reset'` | `'button'` | HTML type of the button |
| `...` | `HTMLButtonElementProps` | `{}` | Other props is inherited from `HTMLButtonElement` |
| Name | Type | Default | Description |
| ------- | ------------------------ | -------- | ---------------------------------------------------------- | ---------- | ----------------------- |
| `text` | `React.ReactNode` | | Text appeared on the button |
| `left` | `React.ReactNode` | `null` | Component to show on the left side of the text (ex.: Icon) |
| `right` | `React.ReactNode` | `null` | Component on the right side of the text |
| `type` | `'button' | 'submit' | 'reset'` | `'button'` | HTML type of the button |
| `...` | `HTMLButtonElementProps` | `{}` | Other props is inherited from `HTMLButtonElement` |
20 changes: 7 additions & 13 deletions example/src/woly/atoms/input/usage.mdx
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
---
name: input
category: atoms
package: 'woly'
---

### Props

| Name | Type | Default | Description |
| ------- | ------------------------------- | ---------- | ---------------------------------------------------------- |
| `text` | `React.ReactNode` | | Text appeared on the button |
| `left` | `React.ReactNode` | `null` | Component to show on the left side of the text (ex.: Icon) |
| `right` | `React.ReactNode` | `null` | Component on the right side of the text |
| `type` | `'button' | 'submit' | 'reset'` | `'button'` | HTML type of the button |
| `...` | `HTMLButtonElementProps` | `{}` | Other props is inherited from `HTMLButtonElement` |
| Name | Type | Default | Description |
| ------- | ------------------------ | -------- | ---------------------------------------------------------- | ---------- | ----------------------- |
| `text` | `React.ReactNode` | | Text appeared on the button |
| `left` | `React.ReactNode` | `null` | Component to show on the left side of the text (ex.: Icon) |
| `right` | `React.ReactNode` | `null` | Component on the right side of the text |
| `type` | `'button' | 'submit' | 'reset'` | `'button'` | HTML type of the button |
| `...` | `HTMLButtonElementProps` | `{}` | Other props is inherited from `HTMLButtonElement` |
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import React from 'react';

const InpurPasswordStateMap = () => <input type="password" />;

export default InpurPasswordStateMap;
6 changes: 0 additions & 6 deletions example/src/woly/molecules/input-password/usage.mdx
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
---
name: input-password
category: molecules
package: 'woly'
---

import { InputPassword } from './example';

Hello! It is InputPassword
Expand Down
7 changes: 7 additions & 0 deletions gatsby-theme-woly/gatsby-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ module.exports = (themeOptions) => {
path: themeOptions.components,
},
},
{
resolve: `gatsby-plugin-page-creator`,
options: {
path: themeOptions.components,
ignore: ['**/!(__screenshot-test__)/*', '**/*.(js|ts|jsx)'],
},
},
],
};
};
59 changes: 51 additions & 8 deletions gatsby-theme-woly/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

const pathsPath = require.resolve('./src/paths.js');
const { paths } = require(pathsPath);
const fs = require('fs');

try {
require.resolve(`babel-plugin-extract-react-types`);
Expand All @@ -32,12 +33,6 @@ async function createUsagePages({ actions, graphql, reporter }) {
usages: allMdx {
nodes {
id
frontmatter {
category
name
package
}
tableOfContents
fileAbsolutePath
}
}
Expand All @@ -51,13 +46,61 @@ async function createUsagePages({ actions, graphql, reporter }) {

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

result.data.usages.nodes.forEach(({ frontmatter, id }) => {
result.data.usages.nodes.forEach(({ id, fileAbsolutePath }) => {
const [_, name, category, packageName] = fileAbsolutePath
.split('/')
.reverse();

actions.createPage({
path: paths.componentUsage(frontmatter),
id,
path: paths.componentPage({ package: packageName, category, name }),
component,
context: {
pageID: id,
name,
category,
package: packageName,
},
});
});
}

exports.onPostBuild = async (gatsby) => {
await findScreenshotTestingConfigs(gatsby);
};

async function findScreenshotTestingConfigs({ graphql, reporter }) {
const result = await graphql(`
{
configs: allFile(filter: { name: { eq: "config" } }) {
nodes {
relativePath
}
}
}
`);

if (result.errors) {
reporter.panicOnBuild(`Error while running GraphQL query`);
throw result.errors;
}

const configPaths = result.data.configs.nodes.map(({ relativePath }) => {
const [name, category, packageName] = relativePath
.replace('/__screenshot-test__/config.js', '')
.split('/')
.reverse();

return {
path: relativePath,
name,
category,
package: packageName,
};
});

fs.writeFileSync(
`./public/screenshot-test-configs.json`,
JSON.stringify(configPaths, null, 2),
);
}
1 change: 1 addition & 0 deletions gatsby-theme-woly/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"babel-preset-gatsby": "^1.1.0",
"change-case": "^4.1.2",
"gatsby-plugin-mdx": "^1.10.0",
"gatsby-plugin-page-creator": "^3.7.1",
"gatsby-plugin-react-helmet": "^3.10.0",
"gatsby-plugin-sharp": "^3.1.2",
"gatsby-plugin-sitemap": "^2.12.0",
Expand Down
1 change: 1 addition & 0 deletions gatsby-theme-woly/src/components/components-menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const Group = ({ group, activeMenu }) => (
<MenuItems>
{Object.keys(group.components).map((key) => (
<GroupItems
key={key}
name={key}
groupItems={group.components[key]}
activeMenu={activeMenu}
Expand Down
39 changes: 22 additions & 17 deletions gatsby-theme-woly/src/components/layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ import styled from 'styled-components';

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

export const Layout = ({ children }) => {
const data = useStaticQuery(graphql`
{
mdx: allMdx(
sort: { fields: [frontmatter___category, frontmatter___name] }
pages: allSitePage(
sort: { fields: [context___category, context___name] }
filter: { component: { regex: "/usage.js$/g" } }
) {
components: nodes {
id
meta: frontmatter {
meta: context {
category
name
package
Expand Down Expand Up @@ -47,25 +47,30 @@ export const Layout = ({ children }) => {

function createMapping(data) {
const packages = {};
for (const component of data.mdx.components) {
if (!packages[component.meta.package]) {
packages[component.meta.package] = {};

for (const component of data.pages.components) {
const {
package: packageName,
name: componentName,
category,
} = component.meta;

if (!packages[packageName]) {
packages[packageName] = {};
}

if (!packages[component.meta.package][component.meta.category]) {
packages[component.meta.package][component.meta.category] = [];
if (!packages[packageName][category]) {
packages[packageName][category] = [];
}

const prefix = data.pathPrefix || '';

if (!isHiddenCategory(component.meta.category)) {
packages[component.meta.package][component.meta.category].push({
...component.meta,
path: prefix + paths.componentUsage(component.meta),
id: component.id,
title: camelCase(component.meta.name),
});
}
packages[packageName][category].push({
...component.meta,
path: prefix + paths.componentPage(component.meta),
id: component.id,
title: camelCase(componentName),
});
}

return Object.keys(packages).reduce((list, name) => {
Expand Down
1 change: 0 additions & 1 deletion gatsby-theme-woly/src/lib/constants.js

This file was deleted.

15 changes: 0 additions & 15 deletions gatsby-theme-woly/src/lib/guards.js

This file was deleted.

3 changes: 2 additions & 1 deletion gatsby-theme-woly/src/paths.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const paths = {
componentUsage: ({ package: p, name }) => `/package/${p}/component/${name}`,
componentPage: ({ package: p, category, name }) =>
`/${p}/${category}/${name}`,
};
module.exports = { paths };
Loading

0 comments on commit e9a1262

Please sign in to comment.