From 2a25257c5e6230e43a1c0c8c60013e297de952a9 Mon Sep 17 00:00:00 2001 From: cesarParra Date: Sat, 5 Oct 2024 07:54:31 -0400 Subject: [PATCH 1/2] When creating a multi-config configuration, a single command can still be specified --- .../args/multiple-command-config.spec.ts | 13 +++- src/cli/args.ts | 59 +++++++++---------- 2 files changed, 38 insertions(+), 34 deletions(-) diff --git a/src/cli/__tests__/args/multiple-command-config.spec.ts b/src/cli/__tests__/args/multiple-command-config.spec.ts index db8efad6..e7afc977 100644 --- a/src/cli/__tests__/args/multiple-command-config.spec.ts +++ b/src/cli/__tests__/args/multiple-command-config.spec.ts @@ -9,7 +9,7 @@ import { describe('when extracting arguments', () => { describe('and a configuration is provided for multiple commands', () => { - it('errors when a command was still passed through the cli', async () => { + it('if a subcommand was specified through the cli, it only extract the specified subcommand', async () => { function getFromProcess() { return ['markdown']; } @@ -20,13 +20,22 @@ describe('when extracting arguments', () => { markdown: { sourceDir: 'force-app', }, + openapi: { + sourceDir: 'force-app', + }, }, }); } const result = await extractArgs(getFromProcess, extractConfig); - expect(E.isLeft(result)).toBeTruthy(); + assertEither(result, (configs) => { + expect(configs).toHaveLength(1); + expect(configs[0].targetGenerator).toEqual('markdown'); + + const markdownConfig = configs[0] as UserDefinedMarkdownConfig; + expect(markdownConfig.sourceDir).toEqual('force-app'); + }); }); it('extracts multiple configurations', async () => { diff --git a/src/cli/args.ts b/src/cli/args.ts index 21119ce9..8bb1c6f2 100644 --- a/src/cli/args.ts +++ b/src/cli/args.ts @@ -109,25 +109,32 @@ function extractArgsForCommandsProvidedInConfig( extractFromProcessFn: ExtractArgsFromProcess, config: ConfigByGenerator, ) { - const configs = Object.entries(config).map(([generator, generatorConfig]) => { - switch (generator as Generators) { - case 'markdown': - return pipe( - validateMultiCommandConfig(extractFromProcessFn, 'markdown', generatorConfig), - E.map(() => ({ ...configOnlyMarkdownDefaults, ...generatorConfig })), - ); - case 'openapi': - return pipe( - validateMultiCommandConfig(extractFromProcessFn, 'openapi', generatorConfig), - E.map(() => ({ ...configOnlyOpenApiDefaults, ...generatorConfig })), - ); - case 'changelog': - return pipe( - validateMultiCommandConfig(extractFromProcessFn, 'changelog', generatorConfig), - E.map(() => ({ ...configOnlyChangelogDefaults, ...generatorConfig })), - ); - } - }); + const providedThroughCli = yargs.parseSync(extractFromProcessFn()); + const hasACommandBeenProvided = providedThroughCli._.length > 0; + + const configs = Object.entries(config) + // If no specific command was provided through the CLI, we will generate all the commands. + // Otherwise, we only want to generate the command that was provided. + .filter(([generator]) => (hasACommandBeenProvided ? providedThroughCli._[0] === generator : true)) + .map(([generator, generatorConfig]) => { + switch (generator as Generators) { + case 'markdown': + return pipe( + validateMultiCommandConfig(extractFromProcessFn, 'markdown', generatorConfig), + E.map(() => ({ ...configOnlyMarkdownDefaults, ...generatorConfig })), + ); + case 'openapi': + return pipe( + validateMultiCommandConfig(extractFromProcessFn, 'openapi', generatorConfig), + E.map(() => ({ ...configOnlyOpenApiDefaults, ...generatorConfig })), + ); + case 'changelog': + return pipe( + validateMultiCommandConfig(extractFromProcessFn, 'changelog', generatorConfig), + E.map(() => ({ ...configOnlyChangelogDefaults, ...generatorConfig })), + ); + } + }); return E.sequenceArray(configs); } @@ -210,21 +217,9 @@ function validateMultiCommandConfig( const options = getOptions(command); return E.tryCatch(() => { - yargs + return yargs .config(config) .options(options) - .check((argv) => { - // we should not be receiving a command here - // since this is a multi-command config - if (argv._.length > 0) { - throw new Error( - `Unexpected command "${argv._[0]}". - The command name should be provided in the configuration when using the current configuration format.`, - ); - } else { - return true; - } - }) .fail((msg) => { throw new Error(`Invalid configuration for command "${command}": ${msg}`); }) From de7b563d0fcc9c3ac001850029de2c33c4d02d37 Mon Sep 17 00:00:00 2001 From: cesarParra Date: Wed, 9 Oct 2024 21:09:49 -0400 Subject: [PATCH 2/2] When creating a multi-config configuration, a single command can still be specified --- src/cli/args.ts | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/src/cli/args.ts b/src/cli/args.ts index 8bb1c6f2..52e38f74 100644 --- a/src/cli/args.ts +++ b/src/cli/args.ts @@ -1,5 +1,5 @@ import { cosmiconfig } from 'cosmiconfig'; -import * as yargs from 'yargs'; +import yargs = require('yargs'); import * as E from 'fp-ts/Either'; import { Generators, @@ -108,7 +108,7 @@ type ConfigByGenerator = { function extractArgsForCommandsProvidedInConfig( extractFromProcessFn: ExtractArgsFromProcess, config: ConfigByGenerator, -) { +): E.Either { const providedThroughCli = yargs.parseSync(extractFromProcessFn()); const hasACommandBeenProvided = providedThroughCli._.length > 0; @@ -120,18 +120,26 @@ function extractArgsForCommandsProvidedInConfig( switch (generator as Generators) { case 'markdown': return pipe( - validateMultiCommandConfig(extractFromProcessFn, 'markdown', generatorConfig), - E.map(() => ({ ...configOnlyMarkdownDefaults, ...generatorConfig })), + extractMultiCommandConfig(extractFromProcessFn, 'markdown', generatorConfig), + E.map((cliArgs) => { + console.log('markdown', cliArgs); + return cliArgs; + }), + E.map((cliArgs) => ({ ...configOnlyMarkdownDefaults, ...generatorConfig, ...cliArgs })), ); case 'openapi': return pipe( - validateMultiCommandConfig(extractFromProcessFn, 'openapi', generatorConfig), - E.map(() => ({ ...configOnlyOpenApiDefaults, ...generatorConfig })), + extractMultiCommandConfig(extractFromProcessFn, 'openapi', generatorConfig), + E.map((cliArgs) => ({ ...configOnlyOpenApiDefaults, ...generatorConfig, ...cliArgs })), ); case 'changelog': return pipe( - validateMultiCommandConfig(extractFromProcessFn, 'changelog', generatorConfig), - E.map(() => ({ ...configOnlyChangelogDefaults, ...generatorConfig })), + extractMultiCommandConfig(extractFromProcessFn, 'changelog', generatorConfig), + E.map((cliArgs) => { + console.log('changelog', cliArgs); + return cliArgs; + }), + E.map((cliArgs) => ({ ...configOnlyChangelogDefaults, ...generatorConfig, ...cliArgs })), ); } }); @@ -199,7 +207,7 @@ function extractYargsDemandingCommand(extractFromProcessFn: ExtractArgsFromProce .parseSync(extractFromProcessFn()); } -function validateMultiCommandConfig( +function extractMultiCommandConfig( extractFromProcessFn: ExtractArgsFromProcess, command: Generators, config: UserDefinedConfig, @@ -216,13 +224,14 @@ function validateMultiCommandConfig( } const options = getOptions(command); + console.log('config', config); return E.tryCatch(() => { - return yargs + return yargs(extractFromProcessFn()) .config(config) .options(options) .fail((msg) => { throw new Error(`Invalid configuration for command "${command}": ${msg}`); }) - .parse(extractFromProcessFn()); + .parseSync(); }, E.toError); }