11import { cosmiconfig } from 'cosmiconfig' ;
2- import * as yargs from 'yargs' ;
2+ import yargs = require ( 'yargs' ) ;
33import * as E from 'fp-ts/Either' ;
44import {
55 Generators ,
@@ -108,26 +108,41 @@ type ConfigByGenerator = {
108108function extractArgsForCommandsProvidedInConfig (
109109 extractFromProcessFn : ExtractArgsFromProcess ,
110110 config : ConfigByGenerator ,
111- ) {
112- const configs = Object . entries ( config ) . map ( ( [ generator , generatorConfig ] ) => {
113- switch ( generator as Generators ) {
114- case 'markdown' :
115- return pipe (
116- validateMultiCommandConfig ( extractFromProcessFn , 'markdown' , generatorConfig ) ,
117- E . map ( ( ) => ( { ...configOnlyMarkdownDefaults , ...generatorConfig } ) ) ,
118- ) ;
119- case 'openapi' :
120- return pipe (
121- validateMultiCommandConfig ( extractFromProcessFn , 'openapi' , generatorConfig ) ,
122- E . map ( ( ) => ( { ...configOnlyOpenApiDefaults , ...generatorConfig } ) ) ,
123- ) ;
124- case 'changelog' :
125- return pipe (
126- validateMultiCommandConfig ( extractFromProcessFn , 'changelog' , generatorConfig ) ,
127- E . map ( ( ) => ( { ...configOnlyChangelogDefaults , ...generatorConfig } ) ) ,
128- ) ;
129- }
130- } ) ;
111+ ) : E . Either < Error , readonly UserDefinedConfig [ ] > {
112+ const providedThroughCli = yargs . parseSync ( extractFromProcessFn ( ) ) ;
113+ const hasACommandBeenProvided = providedThroughCli . _ . length > 0 ;
114+
115+ const configs = Object . entries ( config )
116+ // If no specific command was provided through the CLI, we will generate all the commands.
117+ // Otherwise, we only want to generate the command that was provided.
118+ . filter ( ( [ generator ] ) => ( hasACommandBeenProvided ? providedThroughCli . _ [ 0 ] === generator : true ) )
119+ . map ( ( [ generator , generatorConfig ] ) => {
120+ switch ( generator as Generators ) {
121+ case 'markdown' :
122+ return pipe (
123+ extractMultiCommandConfig ( extractFromProcessFn , 'markdown' , generatorConfig ) ,
124+ E . map ( ( cliArgs ) => {
125+ console . log ( 'markdown' , cliArgs ) ;
126+ return cliArgs ;
127+ } ) ,
128+ E . map ( ( cliArgs ) => ( { ...configOnlyMarkdownDefaults , ...generatorConfig , ...cliArgs } ) ) ,
129+ ) ;
130+ case 'openapi' :
131+ return pipe (
132+ extractMultiCommandConfig ( extractFromProcessFn , 'openapi' , generatorConfig ) ,
133+ E . map ( ( cliArgs ) => ( { ...configOnlyOpenApiDefaults , ...generatorConfig , ...cliArgs } ) ) ,
134+ ) ;
135+ case 'changelog' :
136+ return pipe (
137+ extractMultiCommandConfig ( extractFromProcessFn , 'changelog' , generatorConfig ) ,
138+ E . map ( ( cliArgs ) => {
139+ console . log ( 'changelog' , cliArgs ) ;
140+ return cliArgs ;
141+ } ) ,
142+ E . map ( ( cliArgs ) => ( { ...configOnlyChangelogDefaults , ...generatorConfig , ...cliArgs } ) ) ,
143+ ) ;
144+ }
145+ } ) ;
131146
132147 return E . sequenceArray ( configs ) ;
133148}
@@ -192,7 +207,7 @@ function extractYargsDemandingCommand(extractFromProcessFn: ExtractArgsFromProce
192207 . parseSync ( extractFromProcessFn ( ) ) ;
193208}
194209
195- function validateMultiCommandConfig (
210+ function extractMultiCommandConfig (
196211 extractFromProcessFn : ExtractArgsFromProcess ,
197212 command : Generators ,
198213 config : UserDefinedConfig ,
@@ -209,25 +224,14 @@ function validateMultiCommandConfig(
209224 }
210225
211226 const options = getOptions ( command ) ;
227+ console . log ( 'config' , config ) ;
212228 return E . tryCatch ( ( ) => {
213- yargs
229+ return yargs ( extractFromProcessFn ( ) )
214230 . config ( config )
215231 . options ( options )
216- . check ( ( argv ) => {
217- // we should not be receiving a command here
218- // since this is a multi-command config
219- if ( argv . _ . length > 0 ) {
220- throw new Error (
221- `Unexpected command "${ argv . _ [ 0 ] } ".
222- The command name should be provided in the configuration when using the current configuration format.` ,
223- ) ;
224- } else {
225- return true ;
226- }
227- } )
228232 . fail ( ( msg ) => {
229233 throw new Error ( `Invalid configuration for command "${ command } ": ${ msg } ` ) ;
230234 } )
231- . parse ( extractFromProcessFn ( ) ) ;
235+ . parseSync ( ) ;
232236 } , E . toError ) ;
233237}
0 commit comments