Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/cli/__tests__/args/multiple-command-config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
}
Expand All @@ -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 () => {
Expand Down
76 changes: 40 additions & 36 deletions src/cli/args.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -108,26 +108,41 @@ type ConfigByGenerator = {
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 })),
);
}
});
): E.Either<Error, readonly UserDefinedConfig[]> {
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(
extractMultiCommandConfig(extractFromProcessFn, 'markdown', generatorConfig),
E.map((cliArgs) => {
console.log('markdown', cliArgs);
return cliArgs;
}),
E.map((cliArgs) => ({ ...configOnlyMarkdownDefaults, ...generatorConfig, ...cliArgs })),
);
case 'openapi':
return pipe(
extractMultiCommandConfig(extractFromProcessFn, 'openapi', generatorConfig),
E.map((cliArgs) => ({ ...configOnlyOpenApiDefaults, ...generatorConfig, ...cliArgs })),
);
case 'changelog':
return pipe(
extractMultiCommandConfig(extractFromProcessFn, 'changelog', generatorConfig),
E.map((cliArgs) => {
console.log('changelog', cliArgs);
return cliArgs;
}),
E.map((cliArgs) => ({ ...configOnlyChangelogDefaults, ...generatorConfig, ...cliArgs })),
);
}
});

return E.sequenceArray(configs);
}
Expand Down Expand Up @@ -192,7 +207,7 @@ function extractYargsDemandingCommand(extractFromProcessFn: ExtractArgsFromProce
.parseSync(extractFromProcessFn());
}

function validateMultiCommandConfig(
function extractMultiCommandConfig(
extractFromProcessFn: ExtractArgsFromProcess,
command: Generators,
config: UserDefinedConfig,
Expand All @@ -209,25 +224,14 @@ function validateMultiCommandConfig(
}

const options = getOptions(command);
console.log('config', config);
return E.tryCatch(() => {
yargs
return yargs(extractFromProcessFn())
.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}`);
})
.parse(extractFromProcessFn());
.parseSync();
}, E.toError);
}
Loading