Skip to content

Commit c4b9419

Browse files
authored
Run specific command in multi config (#192)
* When creating a multi-config configuration, a single command can still be specified * When creating a multi-config configuration, a single command can still be specified
1 parent 8cc95e7 commit c4b9419

File tree

2 files changed

+51
-38
lines changed

2 files changed

+51
-38
lines changed

src/cli/__tests__/args/multiple-command-config.spec.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99

1010
describe('when extracting arguments', () => {
1111
describe('and a configuration is provided for multiple commands', () => {
12-
it('errors when a command was still passed through the cli', async () => {
12+
it('if a subcommand was specified through the cli, it only extract the specified subcommand', async () => {
1313
function getFromProcess() {
1414
return ['markdown'];
1515
}
@@ -20,13 +20,22 @@ describe('when extracting arguments', () => {
2020
markdown: {
2121
sourceDir: 'force-app',
2222
},
23+
openapi: {
24+
sourceDir: 'force-app',
25+
},
2326
},
2427
});
2528
}
2629

2730
const result = await extractArgs(getFromProcess, extractConfig);
2831

29-
expect(E.isLeft(result)).toBeTruthy();
32+
assertEither(result, (configs) => {
33+
expect(configs).toHaveLength(1);
34+
expect(configs[0].targetGenerator).toEqual('markdown');
35+
36+
const markdownConfig = configs[0] as UserDefinedMarkdownConfig;
37+
expect(markdownConfig.sourceDir).toEqual('force-app');
38+
});
3039
});
3140

3241
it('extracts multiple configurations', async () => {

src/cli/args.ts

Lines changed: 40 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { cosmiconfig } from 'cosmiconfig';
2-
import * as yargs from 'yargs';
2+
import yargs = require('yargs');
33
import * as E from 'fp-ts/Either';
44
import {
55
Generators,
@@ -108,26 +108,41 @@ type ConfigByGenerator = {
108108
function 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

Comments
 (0)