Skip to content

Commit 57ba77a

Browse files
committed
wip split example menu json
1 parent af9cf07 commit 57ba77a

File tree

12 files changed

+109
-192
lines changed

12 files changed

+109
-192
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
coverage/
99
dist/
1010
docs/app/componentMenu.json
11-
docs/app/exampleMenu.json
1211
docs/app/componentInfo
12+
docs/app/exampleMenus
1313
docs/build/
1414
dll/
1515
node_modules/

.prettierignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ coverage/
1010
dist/
1111
dll/
1212
docs/app/componentMenu.json
13-
docs/app/exampleMenu.json
13+
docs/app/exampleMenus
1414
docs/build/
1515

1616
package.json

docs/app/Components/ComponentDoc/ComponentSidebar/ComponentSidebar.js

+9-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import PropTypes from 'prop-types'
33
import React, { Component } from 'react'
44
import { Accordion, Menu, Sticky } from 'semantic-ui-react'
55

6-
import exampleMenu from 'docs/app/exampleMenu'
76
import ComponentSidebarSection from './ComponentSidebarSection'
87

98
const sidebarStyle = {
@@ -24,14 +23,18 @@ class ComponentSidebar extends Component {
2423

2524
state = {}
2625

27-
constructor(props) {
28-
super(props)
29-
30-
this.state = { sections: exampleMenu[props.displayName] }
26+
componentDidMount() {
27+
this.fetchSections()
3128
}
3229

3330
componentWillReceiveProps(nextProps) {
34-
this.setState({ sections: exampleMenu[nextProps.displayName] })
31+
this.fetchSections(nextProps)
32+
}
33+
34+
fetchSections = ({ displayName } = this.props) => {
35+
import(`docs/app/exampleMenus/${displayName}.examples.json`).then((sections) => {
36+
this.setState({ sections })
37+
})
3538
}
3639

3740
render() {

gulp/plugins/gulp-component-menu.js

+10-12
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
1+
import Vinyl from 'vinyl'
12
import gutil from 'gulp-util'
2-
import path from 'path'
33
import through from 'through2'
44

55
import getComponentInfo from './util/getComponentInfo'
66

7-
export default (filename) => {
8-
const defaultFilename = 'componentMenu.json'
7+
const pluginName = 'gulp-component-menu'
8+
9+
export default () => {
910
const result = []
10-
const pluginName = 'gulp-component-menu'
11-
let finalFile
12-
let latestFile
1311

1412
function bufferContents(file, enc, cb) {
15-
latestFile = file
16-
1713
if (file.isNull()) {
1814
cb(null, file)
1915
return
@@ -44,10 +40,12 @@ export default (filename) => {
4440
}
4541

4642
function endStream(cb) {
47-
finalFile = latestFile.clone({ contents: false })
48-
finalFile.path = path.join(latestFile.base, filename || defaultFilename)
49-
finalFile.contents = new Buffer(JSON.stringify(result, null, 2))
50-
this.push(finalFile)
43+
const file = new Vinyl({
44+
path: './componentMenu.json',
45+
contents: Buffer.from(JSON.stringify(result, null, 2)),
46+
})
47+
48+
this.push(file)
5149
cb()
5250
}
5351

gulp/plugins/gulp-example-menu.js

+44-37
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,30 @@
1+
import Vinyl from 'vinyl'
12
import gutil from 'gulp-util'
23
import _ from 'lodash'
34
import path from 'path'
45
import through from 'through2'
56

6-
import config from '../../config'
7-
import { parseDocExample, parseDocSection } from './util'
7+
import { parseDocSection } from './util'
88

9-
const examplesPath = path.join(config.paths.docsSrc(), 'Examples', path.sep)
9+
const SECTION_ORDER = {
10+
Types: 1,
11+
States: 2,
12+
Content: 3,
13+
Variations: 4,
14+
Groups: 5,
15+
DEFAULT_ORDER: 6,
16+
Usage: 9,
17+
}
1018

11-
const normalizeResult = result =>
12-
JSON.stringify(
13-
_.mapValues(result, sections =>
14-
_.map(_.sortBy(sections, 'position'), ({ examples, sectionName }) => ({
15-
examples,
16-
sectionName,
17-
})),
18-
),
19-
null,
20-
2,
21-
)
19+
const getSectionOrder = sectionName =>
20+
_.find(SECTION_ORDER, (val, key) => _.includes(sectionName, key)) || SECTION_ORDER.DEFAULT_ORDER
2221

23-
export default (filename) => {
24-
const defaultFilename = 'exampleMenu.json'
25-
const result = {}
26-
const pluginName = 'gulp-example-menu'
27-
let finalFile
28-
let latestFile
22+
const pluginName = 'gulp-example-menu'
2923

30-
function bufferContents(file, enc, cb) {
31-
latestFile = file
24+
export default () => {
25+
const exampleFilesByDisplayName = {}
3226

27+
function bufferContents(file, enc, cb) {
3328
if (file.isNull()) {
3429
cb(null, file)
3530
return
@@ -41,32 +36,44 @@ export default (filename) => {
4136
}
4237

4338
try {
44-
const relativePath = file.path.replace(examplesPath, '')
45-
const [, component, section] = _.split(relativePath, path.sep)
39+
// eslint-disable-next-line no-unused-vars
40+
const [type, displayName, sectionName, exampleName] = _.split(file.path, path.sep).slice(-4)
41+
const { examples } = parseDocSection(file.contents)
4642

47-
if (section === 'index.js') {
48-
result[component] = parseDocExample(file.contents)
49-
cb()
50-
return
51-
}
43+
_.merge(exampleFilesByDisplayName, {
44+
[displayName]: {
45+
[sectionName]: {
46+
order: getSectionOrder(sectionName),
47+
sectionName,
48+
examples,
49+
},
50+
},
51+
})
5252

53-
result[component][section] = {
54-
...result[component][section],
55-
...parseDocSection(file.contents),
56-
}
5753
cb()
5854
} catch (err) {
5955
const pluginError = new gutil.PluginError(pluginName, err)
6056
pluginError.message += `\nFile: ${file.path}.`
6157
this.emit('error', pluginError)
58+
// eslint-disable-next-line no-console
59+
console.log(err)
6260
}
6361
}
6462

6563
function endStream(cb) {
66-
finalFile = latestFile.clone({ contents: false })
67-
finalFile.path = path.join(latestFile.base, filename || defaultFilename)
68-
finalFile.contents = new Buffer(normalizeResult(result))
69-
this.push(finalFile)
64+
_.forEach(exampleFilesByDisplayName, (contents, displayName) => {
65+
const sortedContents = _.sortBy(contents, ['order', 'sectionName']).map(
66+
({ sectionName, examples }) => ({ sectionName, examples }),
67+
)
68+
69+
const file = new Vinyl({
70+
path: `./${displayName}.examples.json`,
71+
contents: Buffer.from(JSON.stringify(sortedContents, null, 2)),
72+
})
73+
74+
this.push(file)
75+
})
76+
7077
cb()
7178
}
7279

gulp/plugins/gulp-react-docgen.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import Vinyl from 'vinyl'
12
import gutil from 'gulp-util'
23
import through from 'through2'
34

@@ -20,9 +21,10 @@ export default () =>
2021
try {
2122
const contents = getComponentInfo(file.path)
2223

23-
const infoFile = file.clone({ content: false })
24-
infoFile.path = file.path.replace(/js$/, 'info.json')
25-
infoFile.contents = Buffer.from(JSON.stringify(contents, null, 2))
24+
const infoFile = new Vinyl({
25+
path: `./${file.basename.replace(/js$/, 'info.json')}`,
26+
contents: Buffer.from(JSON.stringify(contents, null, 2)),
27+
})
2628

2729
cb(null, infoFile)
2830
} catch (err) {

gulp/plugins/util/getComponentInfo.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import path from 'path'
33
import { defaultHandlers, parse } from 'react-docgen'
44
import fs from 'fs'
55

6-
import { parseDefaultValue, parseDocBlock, parserCustomHandler, parseType } from './'
6+
import { parseDefaultValue, parseDocblock, parserCustomHandler, parseType } from './'
77

88
const getComponentInfo = (filepath) => {
99
const absPath = path.resolve(process.cwd(), filepath)
@@ -62,7 +62,7 @@ const getComponentInfo = (filepath) => {
6262
).toLowerCase()
6363

6464
// replace the component.description string with a parsed docblock object
65-
info.docblock = parseDocBlock(info.description)
65+
info.docblock = parseDocblock(info.description)
6666
delete info.description
6767

6868
// file and path info
@@ -74,7 +74,7 @@ const getComponentInfo = (filepath) => {
7474

7575
// replace prop `description` strings with a parsed docblock object and updated `type`
7676
_.each(info.props, (propDef, propName) => {
77-
const { description, tags } = parseDocBlock(propDef.description)
77+
const { description, tags } = parseDocblock(propDef.description)
7878
const { name, value } = parseType(propName, propDef)
7979

8080
info.props[propName] = {

gulp/plugins/util/index.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
export getComponentInfo from './getComponentInfo'
22
export parseDefaultValue from './parseDefaultValue'
3-
export parseDocBlock from './parseDocBlock'
4-
export parseDocExample from './parseDocExample'
3+
export parseDocblock from './parseDocblock'
54
export parseDocSection from './parseDocSection'
65
export parserCustomHandler from './parserCustomHandler'
76
export parseType from './parseType'

gulp/plugins/util/parseDocExample.js

-41
This file was deleted.

0 commit comments

Comments
 (0)