1
1
const path = require ( 'path' )
2
- const fs = require ( 'fs' )
2
+ const fsp = require ( 'fs' ) . promises
3
3
const matterService = require ( '../utils/frontmatter-service' )
4
4
const workspacePath = path . resolve ( __dirname , '..' , '..' )
5
5
@@ -17,33 +17,28 @@ const rewriteMarkdownTitle = (filePath) => {
17
17
matter . set ( 'title' , title ) . save ( )
18
18
}
19
19
20
- const ergodicDirectory = ( dirPath ) => {
21
- fs . readdir ( dirPath , ( err , files ) => {
22
- if ( err ) {
23
- console . warn ( 'Directory reading failed !' )
24
- return
25
- }
26
-
27
- files . forEach ( ( file ) => {
28
- const filePath = path . join ( dirPath , file )
29
- fs . stat ( filePath , ( err , stats ) => {
30
- if ( err ) {
31
- console . warn ( 'File status reading failed !' )
32
- return
20
+ const ergodicDirectory = async ( dirPath ) => {
21
+ try {
22
+ const files = await fsp . readdir ( dirPath )
23
+ for ( let i = 0 ; i < files . length ; i ++ ) {
24
+ const file = files [ i ] ,
25
+ filePath = path . join ( dirPath , file )
26
+ const stats = await fsp . stat ( filePath )
27
+ if ( stats . isFile ( ) ) {
28
+ if ( filePath . split ( '.' ) . pop ( ) . toLowerCase ( ) === 'md' ) {
29
+ rewriteMarkdownTitle ( filePath )
33
30
}
34
-
35
- if ( stats . isFile ( ) ) {
36
- if ( filePath . split ( '.' ) . pop ( ) . toLowerCase ( ) === 'md' ) {
37
- rewriteMarkdownTitle ( filePath )
38
- }
39
- } else if ( stats . isDirectory ( ) ) {
40
- if ( articleDirs . includes ( filePath . split ( '/' ) . pop ( ) ) ) {
41
- ergodicDirectory ( filePath )
42
- }
31
+ } else if ( stats . isDirectory ( ) ) {
32
+ if ( articleDirs . includes ( filePath . split ( '/' ) . pop ( ) ) ) {
33
+ await ergodicDirectory ( filePath )
43
34
}
44
- } )
45
- } )
46
- } )
35
+ }
36
+ }
37
+ } catch ( err ) {
38
+ console . warn (
39
+ `vite-docs-cn: failed to rewrite frontmatter for titles.\n ${ err } !`
40
+ )
41
+ }
47
42
}
48
43
49
- ergodicDirectory ( workspacePath )
44
+ module . exports = ( ) => ergodicDirectory ( workspacePath )
0 commit comments