@@ -13,6 +13,19 @@ const TEMPLATE_DIR = path.resolve(__dirname);
1313const FILES_TO_IGNORE = [ 'index.js' , 'content/pipelines v2/_index.md' , 'content/pipelines v2/spec.md' ] ;
1414const baseDir = path . resolve ( TEMP_DIR , './content' ) ;
1515const 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+
1629
1730const hasSubCommands = async ( command ) => {
1831 const files = await recursive ( path . resolve ( __dirname , '../lib/interface/cli/commands' ) ) ;
@@ -77,7 +90,26 @@ const copyTemplateToTmp = async () => {
7790 * in case the file already exists in the base docs folder it will extend it
7891 * possible extensions are: HEADER, DESCRIPTION, COMMANDS, ARGUMENTS, OPTIONS
7992 */
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 ( ) ;
81113 const dir = path . resolve ( baseDir , `${ ( nestedCategory || 'undefined' ) . toLowerCase ( ) } ` ) ;
82114
83115 const commandFilePath = path . resolve ( dir , `./${ docs . title } .md` ) ;
@@ -90,10 +122,11 @@ const createCommandFile = async (nestedCategory,docs) => {
90122
91123 // HEADER STRING
92124 let headerString ;
125+ const weight = await getWeight ( command ) ;
93126 if ( docs . subCategory ) {
94- headerString = `+++\ntitle = "${ docs . subCategory } "\n+++\n\n` ;
127+ headerString = `+++\ntitle = "${ docs . subCategory } "\nweight = ${ weight || 100 } \ n+++\n\n` ;
95128 } else {
96- headerString = `${ docs . header } \n\n` ;
129+ headerString = `+++\ntitle = " ${ docs . title } "\nweight = ${ weight || 100 } \n+++ \n\n` ;
97130 }
98131
99132 if ( skeletonFileExists ) {
@@ -176,17 +209,10 @@ const createCategoryFile = async (content, category) => {
176209 * updates the category main file with a specific command
177210 * possible extensions are: HEADER, COMMANDS
178211 */
179- const updateCategoryFileContent = async ( nestedCategory , command , existingContent ) => {
212+ const updateCategoryFileArray = async ( nestedCategory , command , existingContent ) => {
180213 const docs = command . prepareDocs ( ) ;
181214 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 || { } ;
190216
191217 // HEADER string
192218 const parent = command . parentCommand ;
@@ -197,13 +223,12 @@ const updateCategoryFileContent = async (nestedCategory,command, existingContent
197223 title = parentdocs . subCategory ;
198224 }
199225 }
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 ;
205231
206- // COMMANDS string
207232 let commandString = '' ;
208233 const formattedTitle = docs . title . replace ( / \s + / g, '-' )
209234 . toLowerCase ( ) ;
@@ -212,11 +237,40 @@ const updateCategoryFileContent = async (nestedCategory,command, existingContent
212237 commandString += `${ docs . description } \n\n` ;
213238 commandString += `${ docs . usage } \n\n` ;
214239
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' ) ;
219264 }
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+ } ) ;
220274
221275 return finalCategoryFileString ;
222276} ;
@@ -259,12 +313,18 @@ const createAutomatedDocs = async () => {
259313
260314 nestedCategories [ category ] = await getNestedCategories ( command ) ;
261315 const nestedCategory = nestedCategories [ category ] ;
262- categories [ category ] = await updateCategoryFileContent ( nestedCategory , command , categories [ category ] ) ;
316+ categories [ category ] = await updateCategoryFileArray ( nestedCategory , command , categories [ category ] ) ;
263317 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 ) ;
265325 }
266326
267- _ . forEach ( categories , async ( content , category ) => {
327+ _ . forEach ( categoriesFinalContent , async ( content , category ) => {
268328 await createCategoryFile ( content , nestedCategories [ category ] ) ;
269329 } ) ;
270330} ;
0 commit comments