1717import fs from 'node:fs/promises' ;
1818import { AnyJson , ensureString } from '@salesforce/ts-types' ;
1919import chalk from 'chalk' ;
20- import { BaseDitamap } from './ditamap/base-ditamap.js' ;
21- import { CLIReference } from './ditamap/cli-reference.js' ;
22- import { Command } from './ditamap/command.js' ;
23- import { TopicCommands } from './ditamap/topic-commands.js' ;
24- import { TopicDitamap } from './ditamap/topic-ditamap.js' ;
2520import { CliMeta , events , punctuate , SfTopic , SfTopics , CommandClass } from './utils.js' ;
26- import { HelpReference } from './ditamap/help-reference .js' ;
21+ import { DitaGeneratorFactory , GeneratorFactory , MarkdownGeneratorFactory , OutputFormat } from './generator-factory .js' ;
2722
2823type TopicsByTopicsByTopLevel = Map < string , Map < string , CommandClass [ ] > > ;
2924
@@ -37,12 +32,17 @@ function emitNoTopicMetadataWarning(topic: string): void {
3732}
3833
3934export class Docs {
35+ private factory : GeneratorFactory ;
36+
4037 public constructor (
4138 private outputDir : string ,
39+ outputFormat : OutputFormat ,
4240 private hidden : boolean ,
4341 private topicMeta : SfTopics ,
4442 private cliMeta : CliMeta
45- ) { }
43+ ) {
44+ this . factory = outputFormat === 'markdown' ? new MarkdownGeneratorFactory ( outputDir ) : new DitaGeneratorFactory ( ) ;
45+ }
4646
4747 public async build ( commands : CommandClass [ ] ) : Promise < void > {
4848 // Create if doesn't exist
@@ -77,13 +77,6 @@ export class Docs {
7777
7878 for ( const [ subtopic , classes ] of subtopics . entries ( ) ) {
7979 try {
80- // const subTopicsMeta = topicMeta.subtopics;
81-
82- // if (!subTopicsMeta?.get(subtopic)) {
83- // emitNoTopicMetadataWarning(`${topic}:${subtopic}`);
84- // continue;
85- // }
86-
8780 subTopicNames . push ( subtopic ) ;
8881
8982 // Commands within the sub topic
@@ -110,8 +103,8 @@ export class Docs {
110103 // The topic ditamap with all of the subtopic links.
111104 events . emit ( 'subtopics' , topic , subTopicNames ) ;
112105
113- await new TopicCommands ( topic , topicMeta ) . write ( ) ;
114- await new TopicDitamap ( topic , commandIds ) . write ( ) ;
106+ await this . factory . createTopicCommands ( topic , topicMeta ) . write ( ) ;
107+ await this . factory . createTopicIndex ( topic , commandIds ) . write ( ) ;
115108 return subTopicNames ;
116109 }
117110
@@ -123,7 +116,6 @@ export class Docs {
123116 * @returns The commands grouped by topics/subtopic/commands.
124117 */
125118 private groupTopicsAndSubtopics ( commands : CommandClass [ ] ) : TopicsByTopicsByTopLevel {
126- // const topLevelTopics: Dictionary<Dictionary<CommandClass | CommandClass[]>> = {};
127119 const topLevelTopics = new Map < string , Map < string , CommandClass [ ] > > ( ) ;
128120
129121 for ( const command of commands ) {
@@ -135,17 +127,14 @@ export class Docs {
135127
136128 const plugin = command . plugin ;
137129 if ( plugin ) {
138- // Also include the namespace on the commands so we don't need to do the split at other times in the code.
139130 command . topic = topLevelTopic ;
140131
141132 const existingTopicsForTopLevel = topLevelTopics . get ( topLevelTopic ) ?? new Map < string , CommandClass [ ] > ( ) ;
142133
143134 if ( commandParts . length === 1 ) {
144- // This is a top-level topic that is also a command
145135 const existingTarget = existingTopicsForTopLevel . get ( commandParts [ 0 ] ) ?? [ ] ;
146136 existingTopicsForTopLevel . set ( commandParts [ 0 ] , [ ...existingTarget , command ] ) ;
147137 } else if ( commandParts . length === 2 ) {
148- // This is a command directly under the top-level topic
149138 const existingTarget = existingTopicsForTopLevel . get ( commandParts [ 1 ] ) ?? [ ] ;
150139 existingTopicsForTopLevel . set ( commandParts [ 1 ] , [ ...existingTarget , command ] ) ;
151140 } else {
@@ -177,11 +166,12 @@ export class Docs {
177166 private async populateTemplate ( commands : CommandClass [ ] ) : Promise < void > {
178167 const topicsAndSubtopics = this . groupTopicsAndSubtopics ( commands ) ;
179168
180- await new CLIReference ( ) . write ( ) ;
181- await new HelpReference ( ) . write ( ) ;
169+ await this . factory . createCliReference ( ) . write ( ) ;
170+
171+ const helpReference = this . factory . createHelpReference ( ) ;
172+ if ( helpReference ) await helpReference . write ( ) ;
182173
183- // Generate one base file with all top-level topics.
184- await new BaseDitamap ( Array . from ( topicsAndSubtopics . keys ( ) ) ) . write ( ) ;
174+ await this . factory . createRootIndex ( Array . from ( topicsAndSubtopics . keys ( ) ) ) . write ( ) ;
185175
186176 for ( const [ topic , subtopics ] of topicsAndSubtopics . entries ( ) ) {
187177 events . emit ( 'topic' , { topic } ) ;
@@ -240,8 +230,8 @@ export class Docs {
240230 return '' ;
241231 }
242232
243- const commandDitamap = new Command ( topic , subtopic , command , commandMeta ) ;
244- await commandDitamap . write ( ) ;
245- return commandDitamap . getFilename ( ) ;
233+ const commandGenerator = this . factory . createCommand ( topic , subtopic , command , commandMeta ) ;
234+ await commandGenerator . write ( ) ;
235+ return commandGenerator . getFilename ( ) ;
246236 }
247237}
0 commit comments