1
- const commentParser = require ( 'comment-parser' ) ;
2
-
3
- const parseFile = file => new Promise ( ( resolve , reject ) => {
4
- commentParser . file ( file , ( err , result ) => err ? reject ( err ) : resolve ( result ) ) ;
5
- } )
6
-
7
- parseFile ( './sample.js' ) . then ( c => {
8
- let all = Object . values ( c ) . reduce ( ( result , item ) => {
9
- if ( ! item . tags . some ( i => i . tag === 'socket.io-doc' ) ) return result ;
10
- let category = item . tags . find ( i => i . tag === 'tag' ) ;
11
- category = category ? category . name : 'uncategorized' ;
12
-
13
- let transformed = item . tags . reduce ( ( r , t ) => {
14
- ( t . tag === 'listen' || t . tag === 'emit' ) && ( r = { ...r , action :t . tag , event : t . name } ) ;
15
- t . tag === 'example' && ( r = { ...r , example : t . name . replace ( / \n | \r / g, "" ) + t . description . replace ( / \n | \r / g, "" ) } ) ;
16
- return r ;
17
- } , {
18
- category,
19
- description :item . description || null
20
- } ) ;
21
- result . push ( transformed ) ;
22
- return result ;
23
- } , [ ] ) ;
24
-
25
- console . log ( all ) ;
26
- } ) . catch ( e => console . error ( e ) ) ;
1
+ #!/usr/bin/env node
2
+ const { get_files, parse_file, createLinks, generate_html_template} = require ( './lib' ) ;
3
+ const fs = require ( 'fs' ) ;
4
+ const path = require ( 'path' ) ;
27
5
6
+ const socketioDocCli = require ( "commander" ) ;
7
+ const chalk = require ( "chalk" ) ;
8
+
9
+ socketioDocCli . option ( '-c, --config <string>' , 'Specify config path.' ) . action ( cmd => {
10
+ let { config} = cmd ;
11
+ console . log ( chalk . white ( "\n\tconfig file: " ) , chalk . gray ( config || 'config file not specified, using default.' , '\n' ) ) ;
12
+
13
+ // check config file flag or default config file existance.
14
+ if ( ! config && ! fs . existsSync ( path . resolve ( './socketio-doc.conf.json' ) ) ) {
15
+ return console . log ( chalk . red ( 'config file not found.\n' ) ) ;
16
+ }
17
+
18
+ if ( config && ! fs . existsSync ( path . resolve ( config ) ) ) {
19
+ return console . log ( chalk . red ( 'specified config file, not found.\n' ) ) ;
20
+ }
21
+
22
+ let { source, destination, version } = config ? require ( path . resolve ( `${ config } ` ) ) : require ( path . resolve ( './socketio-doc.conf.json' ) ) ;
23
+
24
+ destination = path . resolve ( destination ) ;
25
+
26
+ if ( ! source ) {
27
+ return console . log ( chalk . red ( 'source not determined.\n' ) ) ;
28
+ }
29
+
30
+ if ( ! destination ) {
31
+ return console . log ( chalk . red ( 'destination not determined.\n' ) ) ;
32
+ }
33
+
34
+ console . log ( chalk . green ( "Start creating documents...\n" ) ) ;
35
+
36
+ return get_files ( source )
37
+ . then ( async files => {
38
+ let results = await Promise . all ( files . flat ( ) . map ( async file => {
39
+ let c = await parse_file ( file ) ;
40
+ return Object . values ( c ) . reduce ( ( result , item ) => {
41
+ if ( ! item . tags . some ( i => i . tag === 'socket.io-doc' ) ) return result ;
42
+ let tag = item . tags . find ( i => i . tag === 'tag' ) ;
43
+ tag = tag ? tag . name : 'uncategorized' ;
44
+
45
+ let transformed = item . tags . reduce ( ( r , t ) => {
46
+ ( t . tag === 'listen' || t . tag === 'emit' ) && ( r = { ...r , action :t . tag , event : t . name } ) ;
47
+ t . tag === 'example' && ( r = { ...r , example : t . name . replace ( / \n | \r / g, "" ) + t . description . replace ( / \n | \r / g, "" ) } ) ;
48
+ return r ;
49
+ } , {
50
+ tag,
51
+ description :item . description || null
52
+ } ) ;
53
+ result . push ( transformed ) ;
54
+ return result ;
55
+ } , [ ] ) ;
56
+ } ) ) ;
57
+
58
+ results = results . flat ( ) ;
59
+
60
+ let emitLinks = createLinks ( results . filter ( r => r . action === 'listen' ) ) ;
61
+
62
+ ! fs . existsSync ( `${ destination } ` ) && fs . mkdirSync ( `${ destination } ` ) ;
63
+ ! fs . existsSync ( `${ destination } /css` ) && fs . mkdirSync ( `${ destination } /css` ) ;
64
+
65
+ fs . createReadStream ( `${ __dirname } /templates/default/css/style.css` ) . pipe ( fs . createWriteStream ( `${ destination } /css/style.css` ) ) ;
66
+ fs . writeFileSync ( `${ destination } /index.html` , generate_html_template ( emitLinks , results , version ) ) ;
67
+ console . log ( chalk . green ( `\tBuild compelete : ${ destination } ` ) )
68
+ } )
69
+ . catch ( e => console . error ( chalk . red ( e ) ) ) ;
70
+ } ) ;
71
+
72
+ socketioDocCli . parse ( process . argv ) ;
0 commit comments