1
+ import Vinyl from 'vinyl'
1
2
import gutil from 'gulp-util'
2
3
import _ from 'lodash'
3
4
import path from 'path'
4
5
import through from 'through2'
5
6
6
- import config from '../../config'
7
- import { parseDocExample , parseDocSection } from './util'
7
+ import { parseDocSection } from './util'
8
8
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
+ }
10
18
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
22
21
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'
29
23
30
- function bufferContents ( file , enc , cb ) {
31
- latestFile = file
24
+ export default ( ) => {
25
+ const exampleFilesByDisplayName = { }
32
26
27
+ function bufferContents ( file , enc , cb ) {
33
28
if ( file . isNull ( ) ) {
34
29
cb ( null , file )
35
30
return
@@ -41,32 +36,44 @@ export default (filename) => {
41
36
}
42
37
43
38
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 )
46
42
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
+ } )
52
52
53
- result [ component ] [ section ] = {
54
- ...result [ component ] [ section ] ,
55
- ...parseDocSection ( file . contents ) ,
56
- }
57
53
cb ( )
58
54
} catch ( err ) {
59
55
const pluginError = new gutil . PluginError ( pluginName , err )
60
56
pluginError . message += `\nFile: ${ file . path } .`
61
57
this . emit ( 'error' , pluginError )
58
+ // eslint-disable-next-line no-console
59
+ console . log ( err )
62
60
}
63
61
}
64
62
65
63
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
+
70
77
cb ( )
71
78
}
72
79
0 commit comments