@@ -13,6 +13,19 @@ const TEMPLATE_DIR = path.resolve(__dirname);
13
13
const FILES_TO_IGNORE = [ 'index.js' , 'content/pipelines v2/_index.md' , 'content/pipelines v2/spec.md' ] ;
14
14
const baseDir = path . resolve ( TEMP_DIR , './content' ) ;
15
15
const ALLOW_BETA_COMMANDS = process . env . ALLOW_BETA_COMMANDS ;
16
+ const categoriesOrder = {
17
+ authentication : 30 ,
18
+ pipelines : 40 ,
19
+ builds : 50 ,
20
+ contexts : 70 ,
21
+ images : 80 ,
22
+ triggers : 90 ,
23
+ environments : 100 ,
24
+ compositions : 110 ,
25
+ 'predefined pipelines' : 120 ,
26
+
27
+ } ;
28
+
16
29
17
30
const hasSubCommands = async ( command ) => {
18
31
const files = await recursive ( path . resolve ( __dirname , '../lib/interface/cli/commands' ) ) ;
@@ -77,7 +90,26 @@ const copyTemplateToTmp = async () => {
77
90
* in case the file already exists in the base docs folder it will extend it
78
91
* possible extensions are: HEADER, DESCRIPTION, COMMANDS, ARGUMENTS, OPTIONS
79
92
*/
80
- const createCommandFile = async ( nestedCategory , docs ) => {
93
+ const getWeight = async ( command ) => {
94
+ const docs = command . prepareDocs ( ) ;
95
+ let weight = 0 ;
96
+ if ( docs . weight ) {
97
+ return docs . weight ;
98
+ }
99
+ else {
100
+ let parent = command . getParentCommand ( ) ;
101
+ while ( parent && ! parent . prepareDocs ( ) . weight ) {
102
+ parent = parent . getParentCommand ( ) ;
103
+ }
104
+ if ( parent ) {
105
+ weight = parent . prepareDocs ( ) . weight ;
106
+ }
107
+ return weight ;
108
+ }
109
+ } ;
110
+
111
+ const createCommandFile = async ( nestedCategory , command ) => {
112
+ const docs = command . prepareDocs ( ) ;
81
113
const dir = path . resolve ( baseDir , `${ ( nestedCategory || 'undefined' ) . toLowerCase ( ) } ` ) ;
82
114
83
115
const commandFilePath = path . resolve ( dir , `./${ docs . title } .md` ) ;
@@ -90,10 +122,11 @@ const createCommandFile = async (nestedCategory,docs) => {
90
122
91
123
// HEADER STRING
92
124
let headerString ;
125
+ const weight = await getWeight ( command ) ;
93
126
if ( docs . subCategory ) {
94
- headerString = `+++\ntitle = "${ docs . subCategory } "\n+++\n\n` ;
127
+ headerString = `+++\ntitle = "${ docs . subCategory } "\nweight = ${ weight || 100 } \ n+++\n\n` ;
95
128
} else {
96
- headerString = `${ docs . header } \n\n` ;
129
+ headerString = `+++\ntitle = " ${ docs . title } "\nweight = ${ weight || 100 } \n+++ \n\n` ;
97
130
}
98
131
99
132
if ( skeletonFileExists ) {
@@ -176,17 +209,10 @@ const createCategoryFile = async (content, category) => {
176
209
* updates the category main file with a specific command
177
210
* possible extensions are: HEADER, COMMANDS
178
211
*/
179
- const updateCategoryFileContent = async ( nestedCategory , command , existingContent ) => {
212
+ const updateCategoryFileArray = async ( nestedCategory , command , existingContent ) => {
180
213
const docs = command . prepareDocs ( ) ;
181
214
const { category } = docs ;
182
- let finalCategoryFileString = existingContent || "" ;
183
-
184
- if ( ! finalCategoryFileString ) {
185
- const indexFile = path . resolve ( baseDir , `./${ ( nestedCategory || 'undefined' ) . toLowerCase ( ) } /_index.md` ) ;
186
- if ( fs . existsSync ( indexFile ) ) {
187
- finalCategoryFileString = fs . readFileSync ( indexFile , 'utf8' ) ;
188
- }
189
- }
215
+ const finalCategoryArr = existingContent || { } ;
190
216
191
217
// HEADER string
192
218
const parent = command . parentCommand ;
@@ -197,13 +223,12 @@ const updateCategoryFileContent = async (nestedCategory,command, existingContent
197
223
title = parentdocs . subCategory ;
198
224
}
199
225
}
200
- const headerString = `+++\ntitle = " ${ title } "\nweight = 100\n+++\n\n` ;
201
- finalCategoryFileString = finalCategoryFileString . replace ( '{{HEADER}}' , headerString ) ;
202
- if ( ! finalCategoryFileString ) {
203
- finalCategoryFileString = headerString ;
204
- }
226
+ const indexFile = path . resolve ( baseDir , `./ ${ ( nestedCategory || 'undefined' ) . toLowerCase ( ) } /_index.md` ) ;
227
+ finalCategoryArr . indexPath = indexFile ;
228
+
229
+ const headerString = `+++\ntitle = " ${ title } "\nweight = ${ categoriesOrder [ title . toLowerCase ( ) ] || 100 } \n+++\n\n` ;
230
+ finalCategoryArr . header = headerString ;
205
231
206
- // COMMANDS string
207
232
let commandString = '' ;
208
233
const formattedTitle = docs . title . replace ( / \s + / g, '-' )
209
234
. toLowerCase ( ) ;
@@ -212,11 +237,40 @@ const updateCategoryFileContent = async (nestedCategory,command, existingContent
212
237
commandString += `${ docs . description } \n\n` ;
213
238
commandString += `${ docs . usage } \n\n` ;
214
239
215
- if ( finalCategoryFileString . indexOf ( '{{COMMANDS}}' ) !== - 1 ) {
216
- finalCategoryFileString = finalCategoryFileString . replace ( '{{COMMANDS}}' , `${ commandString } \n\n{{COMMANDS}}` ) ;
217
- } else {
218
- finalCategoryFileString += commandString ;
240
+ const newCmd = { } ;
241
+ finalCategoryArr . category = category ;
242
+ newCmd . weight = await getWeight ( command ) ;
243
+ newCmd . content = commandString ;
244
+ if ( ! finalCategoryArr . commands ) {
245
+ finalCategoryArr . commands = [ ] ;
246
+
247
+ }
248
+ finalCategoryArr . commands . push ( newCmd ) ;
249
+
250
+
251
+ return finalCategoryArr ;
252
+ } ;
253
+
254
+
255
+ /**
256
+ * updates the category main file with a specific command
257
+ * possible extensions are: HEADER, COMMANDS
258
+ */
259
+ const updateCategoryFileContent = async ( finalContent ) => {
260
+ let finalCategoryFileString = '' ;
261
+ const indexFile = finalContent . indexPath ;
262
+ if ( fs . existsSync ( indexFile ) ) {
263
+ finalCategoryFileString = fs . readFileSync ( indexFile , 'utf8' ) ;
219
264
}
265
+ finalCategoryFileString += finalContent . header ;
266
+ const commandArr = finalContent . commands ;
267
+ commandArr . sort ( ( a , b ) => {
268
+ return a . weight - b . weight ;
269
+ } ) ;
270
+
271
+ _ . forEach ( commandArr , ( command ) => {
272
+ finalCategoryFileString += command . content ;
273
+ } ) ;
220
274
221
275
return finalCategoryFileString ;
222
276
} ;
@@ -259,12 +313,18 @@ const createAutomatedDocs = async () => {
259
313
260
314
nestedCategories [ category ] = await getNestedCategories ( command ) ;
261
315
const nestedCategory = nestedCategories [ category ] ;
262
- categories [ category ] = await updateCategoryFileContent ( nestedCategory , command , categories [ category ] ) ;
316
+ categories [ category ] = await updateCategoryFileArray ( nestedCategory , command , categories [ category ] ) ;
263
317
await upsertCategoryFolder ( nestedCategory ) ;
264
- await createCommandFile ( nestedCategory , docs ) ;
318
+ await createCommandFile ( nestedCategory , command ) ;
319
+ }
320
+
321
+ let categoriesFinalContent = { } ;
322
+ for ( let command in categories ) {
323
+ let f = categories [ command ] ;
324
+ categoriesFinalContent [ f . category ] = await updateCategoryFileContent ( f ) ;
265
325
}
266
326
267
- _ . forEach ( categories , async ( content , category ) => {
327
+ _ . forEach ( categoriesFinalContent , async ( content , category ) => {
268
328
await createCategoryFile ( content , nestedCategories [ category ] ) ;
269
329
} ) ;
270
330
} ;
0 commit comments