1
- const fs = require ( 'fs-extra' ) ;
2
- const path = require ( 'path' ) ;
3
1
const Queue = require ( 'better-queue' ) ;
4
2
const of = require ( 'await-of' ) . default ;
5
3
const shortId = require ( 'shortid' ) ;
6
- const { markdownErrors } = require ( '../constants/errors' ) ;
7
4
8
5
class MarkdownService {
9
6
constructor (
10
7
powerShellService ,
11
8
logStoreService ,
12
9
logParseService ,
13
10
cmdletDependenciesService ,
11
+ fsService ,
14
12
config
15
13
) {
16
14
this . logStoreService = logStoreService ;
17
- this . powerShellService = powerShellService ;
15
+ this . pss = powerShellService ;
18
16
this . logParseService = logParseService ;
19
17
this . config = config ;
20
18
this . cds = cmdletDependenciesService ;
19
+ this . fsService = fsService ;
21
20
22
21
this . processQueue = this . processQueue . bind ( this ) ;
23
- this . copyMdInTempFolder = this . copyMdInTempFolder . bind ( this ) ;
24
- this . getLogFileContent = this . getLogFileContent . bind ( this ) ;
25
22
this . queueFinishHandler = this . queueFinishHandler . bind ( this ) ;
26
23
this . updateMd = this . updateMd . bind ( this ) ;
27
24
this . addMdFilesInQueue = this . addMdFilesInQueue . bind ( this ) ;
@@ -39,42 +36,7 @@ class MarkdownService {
39
36
}
40
37
41
38
async addMdFilesInQueue ( doc ) {
42
- const { ignoreFiles } = this . config . get ( 'platyPS' ) ;
43
- const ignoreAbsolutePathsArr = ignoreFiles . map ( ( f ) => path . resolve ( f ) ) ;
44
- const metaTagRegex = / (?< = a p p l i c a b l e : ) .+ / gmu;
45
-
46
- const isFileIgnore = ( fileName ) => {
47
- const absoluteFilePath = path . resolve ( fileName ) ;
48
-
49
- return ignoreAbsolutePathsArr . includes ( absoluteFilePath ) ;
50
- } ;
51
-
52
- const isContainTag = ( filePath ) => {
53
- if ( ! doc . metaTags . length ) {
54
- return true ;
55
- }
56
-
57
- const groups = fs
58
- . readFileSync ( filePath , 'utf8' )
59
- . toString ( )
60
- . match ( metaTagRegex ) ;
61
-
62
- if ( ! groups ) {
63
- return false ;
64
- }
65
-
66
- for ( const metaTag of doc . metaTags ) {
67
- if ( groups [ 0 ] . indexOf ( metaTag ) !== - 1 ) {
68
- return true ;
69
- }
70
- }
71
-
72
- return false ;
73
- } ;
74
-
75
- const mdFiles = ( await this . _getMdFiles ( doc . path ) ) . filter (
76
- ( fn ) => ! isFileIgnore ( fn ) && isContainTag ( fn )
77
- ) ;
39
+ const mdFiles = await this . fsService . getModuleFiles ( doc ) ;
78
40
79
41
mdFiles . forEach ( ( file ) => {
80
42
this . queue
@@ -84,70 +46,28 @@ class MarkdownService {
84
46
} ) ;
85
47
}
86
48
87
- async _getMdFiles ( path ) {
88
- const mdExt = '.md' ;
89
-
90
- const allFiles = await this . _getFolderFiles ( path ) ;
91
-
92
- return allFiles . filter ( ( file ) => file . endsWith ( mdExt ) ) ;
93
- }
94
-
95
- async _getFolderFiles ( folderPath ) {
96
- const files = await fs . readdir ( folderPath ) ;
97
-
98
- return await files . reduce ( async ( promiseResult , filePath ) => {
99
- const result = await promiseResult ;
100
- const absolute = path . resolve ( folderPath , filePath ) ;
101
-
102
- const fileStat = await fs . stat ( absolute ) ;
103
-
104
- if ( fileStat . isDirectory ( ) ) {
105
- const subDirFiles = await this . _getFolderFiles ( absolute ) ;
106
-
107
- return [ ...result , ...subDirFiles ] ;
108
- }
109
-
110
- return [ ...result , absolute ] ;
111
- } , [ ] ) ;
112
- }
113
-
114
49
async processQueue ( { file, doc } , cb ) {
115
50
let result , err ;
116
51
117
- const { name } = doc ;
118
-
119
- if ( ! this . installedDependencies . includes ( name ) ) {
120
- this . installedDependencies . push ( name ) ;
121
-
122
- await this . cds . installDependencies ( { cmdletName : name } ) ;
123
- }
124
-
125
- const getTempFolderName = ( ) => {
126
- let tempFolders = this . logStoreService . getAllTempFolders ( ) ;
127
-
128
- if ( ! tempFolders . has ( doc . name ) ) {
129
- const tempFolderPath = `${ doc . path } \\${ shortId ( ) } ` ;
52
+ const {
53
+ copyMdInTempFolder,
54
+ getTempFolderPath,
55
+ getFileContent
56
+ } = this . fsService ;
130
57
131
- this . logStoreService . addTempFolder ( tempFolderPath , doc . name ) ;
58
+ await this . _installDependenceIfNeeded ( doc ) ;
132
59
133
- tempFolders = this . logStoreService . getAllTempFolders ( ) ;
134
- }
60
+ const [ tempFolderPath ] = getTempFolderPath ( doc ) ;
135
61
136
- return tempFolders . get ( doc . name ) ;
137
- } ;
138
-
139
- const [ tempFolderPath ] = getTempFolderName ( ) ;
140
62
const logFilePath = `${ tempFolderPath } \\${ shortId ( ) } .log` ;
141
63
142
- [ result , err ] = await of ( this . copyMdInTempFolder ( file , tempFolderPath ) ) ;
64
+ [ result , err ] = await of ( copyMdInTempFolder ( file , tempFolderPath ) ) ;
143
65
144
66
if ( err ) {
145
67
return cb ( err , null ) ;
146
68
}
147
69
148
- [ result , err ] = await of (
149
- this . powerShellService . updateMarkdown ( result , logFilePath )
150
- ) ;
70
+ [ result , err ] = await of ( this . pss . updateMarkdown ( result , logFilePath ) ) ;
151
71
152
72
if ( err ) {
153
73
console . error ( err ) ;
@@ -159,7 +79,7 @@ class MarkdownService {
159
79
160
80
console . log ( result ) ; // print powershell command result
161
81
162
- [ result , err ] = await of ( this . getLogFileContent ( logFilePath ) ) ;
82
+ [ result , err ] = await of ( getFileContent ( logFilePath ) ) ;
163
83
164
84
console . log ( result ) ; // print update file log
165
85
@@ -182,58 +102,21 @@ class MarkdownService {
182
102
}
183
103
184
104
async queueEmptyHandler ( ) {
185
- this . powerShellService . dispose ( ) ;
105
+ this . pss . dispose ( ) ;
186
106
187
107
this . logStoreService . saveInFs ( ) ;
188
108
189
- const tempFolders = [
190
- ...this . logStoreService . getAllTempFolders ( ) . values ( )
191
- ] . map ( ( path ) => path [ 0 ] ) ;
192
-
193
- for ( const path of tempFolders ) {
194
- if ( fs . pathExists ( path ) ) {
195
- const [ , fsError ] = await of ( fs . remove ( path ) ) ;
196
-
197
- if ( fsError ) {
198
- throw new Error ( fsError ) ;
199
- }
200
- }
201
- }
109
+ await this . fsService . removeTempFolders ( ) ;
202
110
203
111
this . logParseService . parseAll ( ) ;
204
112
}
205
113
206
- async copyMdInTempFolder ( srcFilePath , tempFolderPath ) {
207
- let err ;
208
-
209
- const fileName = path . basename ( srcFilePath ) ;
210
- const distFilePath = `${ tempFolderPath } \\${ fileName } ` ;
211
-
212
- [ , err ] = await of ( fs . ensureDir ( tempFolderPath ) ) ;
213
-
214
- if ( err ) {
215
- throw new Error ( markdownErrors . CANT_CREATE_TEMP_FOLDER ) ;
216
- }
217
-
218
- [ , err ] = await of ( fs . copy ( srcFilePath , distFilePath ) ) ;
219
-
220
- if ( err ) {
221
- throw new Error ( markdownErrors . CANT_COPY_MD_FILE ) ;
222
- }
223
-
224
- return distFilePath ;
225
- }
226
-
227
- async getLogFileContent ( logFilePath ) {
228
- let err , result ;
229
-
230
- [ result , err ] = await of ( fs . ensureFile ( logFilePath ) ) ;
114
+ async _installDependenceIfNeeded ( { name } ) {
115
+ if ( ! this . installedDependencies . includes ( name ) ) {
116
+ this . installedDependencies . push ( name ) ;
231
117
232
- if ( result || err ) {
233
- throw new Error ( markdownErrors . CANT_OPEN_LOG_FILE ) ;
118
+ await this . cds . installDependencies ( { cmdletName : name } ) ;
234
119
}
235
-
236
- return ( await fs . readFile ( logFilePath ) ) . toString ( ) ;
237
120
}
238
121
}
239
122
0 commit comments