@@ -6,45 +6,56 @@ const glob = require('glob');
6
6
const del = require ( 'del' ) ;
7
7
const vinylPaths = require ( 'vinyl-paths' ) ;
8
8
const docjs = require ( 'documentation' ) ;
9
+ const REGEX_FILE_NAME = / ( ( \w ) + ( - ) * ( \w ) ) + (? = .j s ) / g;
9
10
11
+ function createDocForFile ( filename ) {
12
+ const splittedName = filename . split ( '/' ) ;
13
+ const filenameWithoutExt = filename . match ( REGEX_FILE_NAME ) [ 0 ] ;
10
14
15
+ return docjs . build ( [ filename ] , { shallow : true } )
16
+ . then ( result => docjs . formats . md ( result , { } ) )
17
+ . then ( output => ( {
18
+ file : filenameWithoutExt ,
19
+ // get the parent folder of file
20
+ folder : filenameWithoutExt !== 'index' ? splittedName [ splittedName . length - 2 ] : 'index' ,
21
+ markdown : output
22
+ } ) ) ;
23
+ }
11
24
12
25
// deletes all files in the output path
13
26
gulp . task ( 'clean-docs' , ( ) =>
14
27
gulp . src ( [ `${ paths . docsOutput } ` ] )
15
28
. pipe ( vinylPaths ( del ) )
16
29
) ;
17
30
18
- gulp . task ( 'build-jsdoc-to-md' , ( ) => {
19
- const files = glob . sync ( paths . source ) ; // glob allows pattern matching for filenames
20
- const regexFileName = / ( ( \w ) + ( \- ) * ( \w ) ) + (? = .j s ) / g;
21
- const createdMdDocs = [ ] ;
22
- files
23
- . filter ( filename => {
24
- const name = filename . match ( regexFileName ) [ 0 ] ;
25
- return name !== 'imports' ;
26
- } )
27
- . map ( filename => {
28
- const splittedName = filename . split ( '/' ) ;
29
- const filenameWithoutExt = filename . match ( regexFileName ) [ 0 ] ;
30
- const jsDocObj = docjs . buildSync ( [ filename ] , { shallow : true } ) ;
31
- return docjs . formats . md ( jsDocObj , { } , ( err , output ) => {
32
- createdMdDocs . push ( {
33
- file : filenameWithoutExt ,
34
- folder : filenameWithoutExt !== 'index' ? splittedName [ splittedName . length - 2 ] : '' , // get the parent folder of file
35
- markdown : output
36
- } ) ;
37
- } ) ;
38
- } ) ;
39
- const writeMdFiles = createdMdDocs . map ( mdDoc => {
40
- const folder = `${ paths . docsOutput } ${ mdDoc . folder } ` ;
41
- const filePath = mdDoc . file !== 'index' ? `${ paths . docsOutput } ${ mdDoc . folder } /${ mdDoc . file } .md` : `${ paths . docsOutput } ${ mdDoc . file } .md` ;
42
- if ( ! fs . existsSync ( folder ) ) {
43
- fs . mkdirSync ( folder ) ;
44
- }
45
- return fs . writeFile ( filePath , mdDoc . markdown ) ;
46
- } ) ;
47
- return Promise . all ( writeMdFiles ) ;
31
+ gulp . task ( 'build-jsdoc-to-md' , done => {
32
+ Promise . resolve ( glob . sync ( paths . source ) )
33
+ // Filter file names to match tag name pattern
34
+ . then ( fileNames => fileNames . filter ( fileName => {
35
+ const name = fileName . match ( REGEX_FILE_NAME ) [ 0 ] ;
36
+ return name !== 'imports' ;
37
+ } ) )
38
+ // Generate markdown for each file
39
+ . then ( fileNames =>
40
+ Promise . all ( fileNames . map ( createDocForFile ) )
41
+ )
42
+ // Group the markdown docs by there folder
43
+ . then ( docs => docs . reduce ( ( groups , doc ) => {
44
+ groups [ doc . folder ] = groups [ doc . folder ] || [ ] ;
45
+ groups [ doc . folder ] . push ( doc ) ;
46
+ return groups ;
47
+ } , { } ) )
48
+ // Create markdown files for each folder
49
+ . then ( groups => Object . keys ( groups ) . forEach ( folderName => {
50
+ const group = groups [ folderName ] ;
51
+ const fileName = `${ paths . apiDoc } /${ folderName } .md` ;
52
+ const content = `# ${ folderName } \n${ group . map ( doc => doc . markdown ) . join ( '\n' ) } ` ;
53
+ if ( ! fs . existsSync ( paths . apiDoc ) ) {
54
+ fs . mkdirSync ( paths . apiDoc ) ;
55
+ }
56
+ fs . writeFileSync ( fileName , content ) ;
57
+ } ) )
58
+ . then ( done ) ;
48
59
} ) ;
49
60
50
61
gulp . task ( 'generate-docs' , done => runSequence ( 'clean-docs' , 'build-jsdoc-to-md' , done ) ) ;
0 commit comments