77
88import { join } from 'path' ;
99import * as fs from 'fs' ;
10- import { Plugin } from '@oclif/config' ;
1110import {
1211 asString ,
1312 Dictionary ,
@@ -18,6 +17,7 @@ import {
1817 isArray ,
1918 JsonMap ,
2019} from '@salesforce/ts-types' ;
20+ import * as OclifCommand from '@oclif/command' ;
2121import * as chalk from 'chalk' ;
2222import { BaseDitamap } from './ditamap/base-ditamap' ;
2323import { CLIReference } from './ditamap/cli-reference' ;
@@ -30,6 +30,12 @@ import { copyStaticFile, events, punctuate } from './utils';
3030
3131const templatesDir = join ( __dirname , '..' , 'templates' ) ;
3232
33+ type CommandClass = OclifCommand . Command & {
34+ topic : string ;
35+ subtopic : string ;
36+ plugin : JsonMap & { name : string } ;
37+ } & JsonMap ;
38+
3339export class Docs {
3440 public constructor (
3541 private outputDir : string ,
@@ -38,7 +44,7 @@ export class Docs {
3844 private topicMeta : JsonMap
3945 ) { }
4046
41- public async build ( commands : JsonMap [ ] ) : Promise < void > {
47+ public async build ( commands : CommandClass [ ] ) : Promise < void > {
4248 // Create if doesn't exist
4349 await fs . promises . mkdir ( this . outputDir , { recursive : true } ) ;
4450
@@ -133,30 +139,28 @@ export class Docs {
133139 * @param commands - The entire set of command data.
134140 * @returns The commands grouped by topics/subtopic/commands.
135141 */
136- private groupTopicsAndSubtopics (
137- commands : JsonMap [ ]
138- ) : Dictionary < Dictionary < Dictionary < unknown > | Array < Dictionary < unknown > > > > {
139- const topLevelTopics : Dictionary < Dictionary < Dictionary | Dictionary [ ] > > = { } ;
142+ private groupTopicsAndSubtopics ( commands : CommandClass [ ] ) : Dictionary < Dictionary < CommandClass | CommandClass [ ] > > {
143+ const topLevelTopics : Dictionary < Dictionary < CommandClass | CommandClass [ ] > > = { } ;
140144
141145 for ( const command of commands ) {
142146 if ( command . hidden && ! this . hidden ) {
143147 continue ;
144148 }
145149 const commandParts = ensureString ( command . id ) . split ( ':' ) ;
146- if ( commandParts . length === 1 ) {
147- continue ; // Top level topic command. Just ignore for as it is usually help for the topic.
148- }
149-
150150 const topLevelTopic = commandParts [ 0 ] ;
151151
152- const plugin = command . plugin as unknown as Plugin ;
152+ const plugin = command . plugin ;
153+
153154 if ( this . plugins [ plugin . name ] ) {
154155 // Also include the namespace on the commands so we don't need to do the split at other times in the code.
155156 command . topic = topLevelTopic ;
156157
157158 const topics = topLevelTopics [ topLevelTopic ] || { } ;
158159
159- if ( commandParts . length === 2 ) {
160+ if ( commandParts . length === 1 ) {
161+ // This is a top-level topic that is also a command
162+ topics [ commandParts [ 0 ] ] = command ;
163+ } else if ( commandParts . length === 2 ) {
160164 // This is a command directly under the top-level topic
161165 topics [ commandParts [ 1 ] ] = command ;
162166 } else {
@@ -176,8 +180,8 @@ export class Docs {
176180
177181 const existingSubTopics = topics [ subtopic ] ;
178182 let subtopicCommands = [ ] ;
179- if ( isArray ( existingSubTopics ) ) {
180- subtopicCommands = existingSubTopics ;
183+ if ( existingSubTopics ) {
184+ subtopicCommands = isArray ( existingSubTopics ) ? existingSubTopics : [ existingSubTopics ] ;
181185 }
182186 ensureArray ( subtopicCommands ) ;
183187 subtopicCommands . push ( command ) ;
@@ -187,10 +191,11 @@ export class Docs {
187191 topLevelTopics [ topLevelTopic ] = topics ;
188192 }
189193 }
194+
190195 return topLevelTopics ;
191196 }
192197
193- private async populateTemplate ( commands : JsonMap [ ] ) : Promise < void > {
198+ private async populateTemplate ( commands : CommandClass [ ] ) : Promise < void > {
194199 const topicsAndSubtopics = this . groupTopicsAndSubtopics ( commands ) ;
195200
196201 await new CLIReference ( ) . write ( ) ;
@@ -205,6 +210,8 @@ export class Docs {
205210 topics . map ( ( topic ) => {
206211 events . emit ( 'topic' , { topic } ) ;
207212 const subtopics = ensure ( topicsAndSubtopics [ topic ] ) ;
213+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
214+ // @ts -ignore
208215 return this . populateTopic ( topic , subtopics ) ;
209216 } )
210217 ) ;
0 commit comments