@@ -41,6 +41,28 @@ const parseDirectoryFile = async (file: DirectoryFile, argv: CLIArguments) => {
41
41
}
42
42
} ;
43
43
44
+ const createReadmeFile = async ( argv : CLIArguments , deletedPaths ?: string [ ] ) => {
45
+ const { srcFolder, codeFolder, docsFolder, title, readme } = parseArguments ( argv ) ;
46
+
47
+ let readMeContent = `### Welcome to ${ title } ` ;
48
+ const readmePath = readme || `${ srcFolder } /README.md` ;
49
+
50
+ try {
51
+ readMeContent = await fs . readFile ( readmePath , 'utf-8' ) ;
52
+ if ( deletedPaths ?. some ( p => p . indexOf ( `${ codeFolder } /README.md` ) !== - 1 ) ) {
53
+ console . log (
54
+ `\n${ chalk . white . bgBlack ( ' README ' ) } ${ chalk . dim ( readmePath . replace ( 'README.md' , '' ) ) } ${ chalk . bold (
55
+ 'README.md'
56
+ ) } \u2192 ${ chalk . dim ( docsFolder ) } ${ chalk . bold ( '/README.md' ) } `
57
+ ) ;
58
+ }
59
+ } catch ( e ) {
60
+ console . log ( `\n${ chalk . white . bgBlack ( ' README ' ) } Add default README.md` ) ;
61
+ }
62
+
63
+ await fs . writeFile ( `${ docsFolder } /README.md` , readMeContent ) ;
64
+ } ;
65
+
44
66
const parseArguments = ( argv : CLIArguments ) => {
45
67
return {
46
68
exclude : ( argv . exclude || '' ) . split ( ',' ) . filter ( Boolean ) ,
@@ -59,7 +81,7 @@ const parseArguments = (argv: CLIArguments) => {
59
81
* @param {object } argv passed arguments
60
82
*/
61
83
export const generate = async ( argv : CLIArguments ) => {
62
- const { exclude, srcFolder, codeFolder, docsFolder, title, readme , rmPattern } = parseArguments ( argv ) ;
84
+ const { exclude, srcFolder, codeFolder, docsFolder, title, rmPattern } = parseArguments ( argv ) ;
63
85
64
86
const startTime = + new Date ( ) ;
65
87
@@ -133,27 +155,7 @@ export const generate = async (argv: CLIArguments) => {
133
155
}
134
156
135
157
// create README.md
136
- let readMeContent = `### Welcome to ${ title } ` ;
137
- const readmePath = readme || `${ srcFolder } /README.md` ;
138
-
139
- try {
140
- readMeContent = await fs . readFile ( readmePath , 'utf-8' ) ;
141
- if ( deletedPaths . some ( p => p . indexOf ( `${ codeFolder } /README.md` ) !== - 1 ) ) {
142
- console . log (
143
- `\n${ chalk . white . bgBlack ( ' README ' ) } ${ chalk . dim ( readmePath . replace ( 'README.md' , '' ) ) } ${ chalk . bold (
144
- 'README.md'
145
- ) } \u2192 ${ chalk . dim ( docsFolder ) } ${ chalk . bold ( '/README.md' ) } `
146
- ) ;
147
- }
148
- } catch ( e ) {
149
- console . log ( `\n${ chalk . white . bgBlack ( ' README ' ) } Add default README.md` ) ;
150
- }
151
-
152
- try {
153
- await fs . access ( `${ docsFolder } /README.md` ) ;
154
- } catch ( e ) {
155
- await fs . writeFile ( `${ docsFolder } /README.md` , readMeContent ) ;
156
- }
158
+ await createReadmeFile ( argv , deletedPaths ) ;
157
159
158
160
const resultTime = ( Math . abs ( startTime - + new Date ( ) ) / 1000 ) . toFixed ( 2 ) ;
159
161
@@ -167,7 +169,7 @@ const watchFiles = (argv: CLIArguments) => {
167
169
168
170
if ( argv . watch ) {
169
171
console . log ( '\n---\n\n👀 watching files...' ) ;
170
- const watcher = chokidar . watch ( srcFolder , {
172
+ const watcher = chokidar . watch ( [ srcFolder , argv . readme , ` ${ srcFolder } /README.md` ] . filter ( Boolean ) , {
171
173
ignored : / ( ^ | [ \/ \\ ] ) \. ./ ,
172
174
persistent : true
173
175
} ) ;
@@ -176,10 +178,16 @@ const watchFiles = (argv: CLIArguments) => {
176
178
const lsFolder = await listFolder ( srcFolder , exclude ) ;
177
179
const file = lsFolder . paths . find ( p => p . path === path ) ;
178
180
181
+ readline . clearLine ( process . stdout , 0 ) ;
182
+ readline . cursorTo ( process . stdout , 0 ) ;
183
+
184
+ if ( path === 'README.md' || path === `${ srcFolder } /README.md` ) {
185
+ await createReadmeFile ( argv ) ;
186
+ }
187
+
179
188
if ( file ) {
180
- readline . clearLine ( process . stdout , 0 ) ;
181
- readline . cursorTo ( process . stdout , 0 ) ;
182
189
process . stdout . write ( chalk . dim ( `update ${ file . name + file . ext } ` ) ) ;
190
+
183
191
const data = await parseDirectoryFile ( file , argv ) ;
184
192
if ( data ) {
185
193
await writeContentToFile ( data . content , data . dest ) ;
@@ -201,6 +209,7 @@ export default (argv: CLIArguments, ctx) => ({
201
209
name : 'vuepress-plugin-jsdoc' ,
202
210
ready : async ( ) => {
203
211
if ( ! ctx . isProd ) {
212
+ argv . watch = true ;
204
213
watchFiles ( argv ) ;
205
214
}
206
215
}
0 commit comments