1
- import chalk from 'chalk' ;
2
- import del from 'del' ;
3
1
import fs from 'fs/promises' ;
4
2
import jsdoc2md from 'jsdoc-to-markdown' ;
5
- import mm from 'micromatch' ;
6
3
import mkdirp from 'mkdirp' ;
7
4
import path from 'path' ;
8
5
import compileTemplates from 'vue-docgen-cli/lib/compileTemplates' ;
9
- import vueDocgen , { extractConfig } from 'vue-docgen-cli/lib/docgen' ;
6
+ import { extractConfig } from 'vue-docgen-cli/lib/docgen' ;
10
7
11
8
import { DirectoryFile } from './interfaces' ;
12
9
import { parseVuepressFileHeader } from './utils/comment-parser' ;
13
10
11
+ interface ParseReturn {
12
+ dest : string ;
13
+ filename : string ;
14
+ content : string ;
15
+ }
16
+
14
17
export const parseFile = async (
15
18
file : DirectoryFile ,
16
19
srcFolder : string ,
17
20
destFolder : string ,
18
21
configPath : string ,
19
22
partials : string | string [ ]
20
- ) => {
21
- if ( ! file . folder ) return ;
23
+ ) : Promise < ParseReturn | null > => {
24
+ if ( ! file . folder ) return null ;
22
25
23
26
const root = process . cwd ( ) ;
24
27
const folderInDest = path . join ( root , destFolder , file . folder . replace ( srcFolder , '' ) ) ;
25
28
const folderInSrc = path . join ( root , file . folder ) ;
26
29
27
- try {
28
- // render file
29
- const content = await jsdoc2md . render ( {
30
- files : [ `${ path . join ( folderInSrc , file . name + file . ext ) } ` ] ,
31
- configure : configPath ,
32
- partial : [
33
- path . resolve ( __filename , '../../template/header.hbs' ) ,
34
- path . resolve ( __filename , '../../template/main.hbs' ) ,
35
- ...partials
36
- ]
37
- } ) ;
38
-
39
- const header = parseVuepressFileHeader (
40
- await fs . readFile ( `${ path . join ( folderInSrc , file . name + file . ext ) } ` , 'utf-8' ) ,
41
- file
42
- ) ;
43
-
44
- await mkdirp ( folderInDest ) ;
45
- await fs . writeFile ( `${ path . join ( folderInDest , file . name ) } .md` , `${ header } ${ content } ` , 'utf-8' ) ;
46
-
47
- return `${ header } ${ content } ` ;
48
- } catch ( e ) { }
30
+ // render file
31
+ const content = await jsdoc2md . render ( {
32
+ files : [ `${ path . join ( folderInSrc , file . name + file . ext ) } ` ] ,
33
+ configure : configPath ,
34
+ partial : [
35
+ path . resolve ( __filename , '../../template/header.hbs' ) ,
36
+ path . resolve ( __filename , '../../template/main.hbs' ) ,
37
+ ...partials
38
+ ]
39
+ } ) ;
40
+
41
+ if ( ! content ) {
42
+ return null ;
43
+ }
44
+
45
+ let fileContent = parseVuepressFileHeader (
46
+ await fs . readFile ( `${ path . join ( folderInSrc , file . name + file . ext ) } ` , 'utf-8' ) ,
47
+ file
48
+ ) ;
49
+
50
+ fileContent += content ;
51
+
52
+ return {
53
+ dest : folderInDest ,
54
+ filename : file . name ,
55
+ content : fileContent
56
+ } ;
49
57
} ;
50
58
51
- export const parseVueFile = async ( file : DirectoryFile , srcFolder : string , destFolder : string ) => {
52
- if ( ! file . folder ) return ;
59
+ export const parseVueFile = async (
60
+ file : DirectoryFile ,
61
+ srcFolder : string ,
62
+ destFolder : string
63
+ ) : Promise < ParseReturn | null > => {
64
+ if ( ! file . folder ) return null ;
53
65
54
66
const root = process . cwd ( ) ;
55
67
const folderInDest = path . join ( root , destFolder , file . folder . replace ( srcFolder , '' ) ) ;
@@ -66,18 +78,31 @@ export const parseVueFile = async (file: DirectoryFile, srcFolder: string, destF
66
78
file . name + file . ext
67
79
) ;
68
80
81
+ await fs . unlink ( `${ path . join ( folderInDest , file . name ) } .md` ) ;
82
+
69
83
if ( ! data . content ) {
70
- return ;
84
+ return null ;
71
85
}
72
86
73
87
let fileContent = parseVuepressFileHeader (
74
88
await fs . readFile ( `${ path . join ( folderInSrc , file . name + file . ext ) } ` , 'utf-8' ) ,
75
89
file
76
90
) ;
91
+
77
92
fileContent += data . content ;
78
93
79
- await mkdirp ( folderInDest ) ;
80
- await fs . writeFile ( `${ path . join ( folderInDest , file . name ) } .md` , fileContent , 'utf-8' ) ;
94
+ return {
95
+ dest : folderInDest ,
96
+ filename : file . name ,
97
+ content : fileContent
98
+ } ;
99
+ } ;
81
100
82
- return fileContent ;
101
+ export const writeContentToFile = async ( file : Promise < ParseReturn | null > ) => {
102
+ const data = await file ;
103
+
104
+ if ( data ) {
105
+ await mkdirp ( data ?. dest ) ;
106
+ await fs . writeFile ( `${ path . join ( data . dest , data . filename ) } .md` , data . content , 'utf-8' ) ;
107
+ }
83
108
} ;
0 commit comments