1
1
import fs from 'fs/promises' ;
2
2
import jsdoc2md from 'jsdoc-to-markdown' ;
3
3
import mkdirp from 'mkdirp' ;
4
- import path from 'path' ;
4
+ import { join , resolve } from 'path' ;
5
5
import compileTemplates from 'vue-docgen-cli/lib/compileTemplates' ;
6
6
import { extractConfig } from 'vue-docgen-cli/lib/docgen' ;
7
7
@@ -10,9 +10,12 @@ import { DirectoryFile } from '../interfaces';
10
10
import { parseVuepressFileHeader } from './comment-parser' ;
11
11
12
12
interface ParseReturn {
13
+ success : boolean ;
13
14
dest : string ;
14
- filename : string ;
15
+ file : DirectoryFile ;
15
16
content : string ;
17
+ relativePathSrc : string ;
18
+ relativePathDest : string ;
16
19
}
17
20
18
21
export const parseFile = async (
@@ -25,34 +28,42 @@ export const parseFile = async (
25
28
if ( ! file . folder ) return null ;
26
29
27
30
const root = process . cwd ( ) ;
28
- const folderInDest = path . join ( root , destFolder , file . folder . replace ( srcFolder , '' ) ) ;
29
- const folderInSrc = path . join ( root , file . folder ) ;
30
-
31
- // render file
32
- const content = await jsdoc2md . render ( {
33
- files : [ `${ path . join ( folderInSrc , file . name + file . ext ) } ` ] ,
34
- configure : configPath ,
35
- partial : [
36
- path . resolve ( __filename , '../../template/header.hbs' ) ,
37
- path . resolve ( __filename , '../../template/main.hbs' ) ,
38
- ...partials
39
- ]
40
- } ) ;
41
-
42
- if ( ! content ) {
43
- return null ;
31
+ const relativePathDest = join ( destFolder , file . folder . replace ( srcFolder , '' ) ) ;
32
+ const folderInDest = join ( root , relativePathDest ) ;
33
+ const folderInSrc = join ( root , file . folder ) ;
34
+
35
+ let success = true ;
36
+ let fileContent = '' ;
37
+
38
+ // parse file
39
+ try {
40
+ const content = await jsdoc2md . render ( {
41
+ files : [ `${ join ( folderInSrc , file . name + file . ext ) } ` ] ,
42
+ configure : configPath ,
43
+ partial : [
44
+ resolve ( __filename , '../../template/header.hbs' ) ,
45
+ resolve ( __filename , '../../template/main.hbs' ) ,
46
+ ...partials
47
+ ]
48
+ } ) ;
49
+
50
+ if ( content ) {
51
+ fileContent = parseVuepressFileHeader (
52
+ await fs . readFile ( `${ join ( folderInSrc , file . name + file . ext ) } ` , 'utf-8' ) ,
53
+ file
54
+ ) ;
55
+ fileContent += content ;
56
+ }
57
+ } catch {
58
+ success = false ;
44
59
}
45
60
46
- let fileContent = parseVuepressFileHeader (
47
- await fs . readFile ( `${ path . join ( folderInSrc , file . name + file . ext ) } ` , 'utf-8' ) ,
48
- file
49
- ) ;
50
-
51
- fileContent += content ;
52
-
53
61
return {
62
+ success,
63
+ file,
64
+ relativePathDest,
65
+ relativePathSrc : file . folder ,
54
66
dest : folderInDest ,
55
- filename : file . name ,
56
67
content : fileContent
57
68
} ;
58
69
} ;
@@ -65,45 +76,72 @@ export const parseVueFile = async (
65
76
if ( ! file . folder ) return null ;
66
77
67
78
const root = process . cwd ( ) ;
68
- const folderInDest = path . join ( root , destFolder , file . folder . replace ( srcFolder , '' ) ) ;
69
- const folderInSrc = path . join ( root , file . folder ) ;
79
+ const relativePathDest = join ( destFolder , file . folder . replace ( srcFolder , '' ) ) ;
80
+ const folderInDest = join ( root , relativePathDest ) ;
81
+ const folderInSrc = join ( root , file . folder ) ;
70
82
const config = {
71
- ...extractConfig ( path . join ( root , file . folder ) ) ,
83
+ ...extractConfig ( join ( root , file . folder ) ) ,
72
84
components : file . name + file . ext ,
73
85
outDir : folderInDest
74
86
} ;
75
87
76
- const data = await compileTemplates (
77
- path . join ( config . componentsRoot , file . name + file . ext ) ,
78
- config ,
79
- file . name + file . ext
80
- ) ;
81
-
82
- await fs . unlink ( `${ path . join ( folderInDest , file . name ) } .md` ) ;
83
-
84
- if ( ! data . content ) {
85
- return null ;
88
+ let success = true ;
89
+ let fileContent = '' ;
90
+
91
+ try {
92
+ // parse file
93
+ const data = await compileTemplates (
94
+ join ( config . componentsRoot , file . name + file . ext ) ,
95
+ config ,
96
+ file . name + file . ext
97
+ ) ;
98
+
99
+ if ( data . content ) {
100
+ fileContent = parseVuepressFileHeader (
101
+ await fs . readFile ( `${ join ( folderInSrc , file . name + file . ext ) } ` , 'utf-8' ) ,
102
+ file
103
+ ) ;
104
+
105
+ fileContent += data . content ;
106
+ }
107
+ } catch {
108
+ success = false ;
86
109
}
87
110
88
- let fileContent = parseVuepressFileHeader (
89
- await fs . readFile ( `${ path . join ( folderInSrc , file . name + file . ext ) } ` , 'utf-8' ) ,
90
- file
91
- ) ;
92
-
93
- fileContent += data . content ;
94
-
95
111
return {
112
+ success,
113
+ file,
114
+ relativePathDest,
115
+ relativePathSrc : file . folder ,
96
116
dest : folderInDest ,
97
- filename : file . name ,
98
117
content : fileContent
99
118
} ;
100
119
} ;
101
120
102
121
export const writeContentToFile = async ( file : Promise < ParseReturn | null > ) => {
103
122
const data = await file ;
123
+ let type = data ?. success ? 'empty' : 'error' ;
104
124
105
- if ( data ) {
106
- await mkdirp ( data ?. dest ) ;
107
- await fs . writeFile ( `${ path . join ( data . dest , data . filename ) } .md` , data . content , 'utf-8' ) ;
125
+ try {
126
+ if ( data && data ?. content ) {
127
+ const path = `${ join ( data . dest , data . file . name ) } .md` ;
128
+
129
+ await mkdirp ( data ?. dest ) ;
130
+ await fs . writeFile ( path , data . content , 'utf-8' ) ;
131
+
132
+ type = 'success' ;
133
+ }
134
+
135
+ return {
136
+ ...data ,
137
+ type
138
+ } ;
139
+ } catch {
140
+ type = 'error' ;
108
141
}
142
+
143
+ return {
144
+ ...data ,
145
+ type
146
+ } ;
109
147
} ;
0 commit comments