Skip to content

feat: Support required arguments in CLI commands #2658

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
49 changes: 49 additions & 0 deletions __e2e__/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,31 @@ export default {
};
`;

const USER_CONFIG_COMMAND_ARG_REQUIRED = `;
module.exports = {
commands: [
{
name: 'test-command-arg-required',
description: 'test command',
func: () => {
console.log('ok');
},
options: [
{
name: '--required-arg <string>',
description: 'test command arg',
required: true,
},
{
name: '--optional-arg <string>',
description: 'test command arg',
},
],
},
],
};
`;

test('should read user config from react-native.config.js', () => {
writeFiles(path.join(DIR, 'TestProject'), {
'react-native.config.js': USER_CONFIG,
Expand Down Expand Up @@ -275,3 +300,27 @@ test('should read config if using import/export in react-native.config.mjs with
const {stdout} = runCLI(path.join(DIR, 'TestProject'), ['test-command-esm']);
expect(stdout).toMatch('test-command-esm');
});

test('should fail if a required arg is missing, pass if optional missing', () => {
writeFiles(path.join(DIR, 'TestProject'), {
'react-native.config.cjs': USER_CONFIG_COMMAND_ARG_REQUIRED,
});

const {stderr} = runCLI(
path.join(DIR, 'TestProject'),
['test-command-arg-required', '--optional-arg', 'foo'],
{
expectedFailure: true,
},
);
expect(stderr).toMatch(
"required option '--required-arg <string>' not specified",
);

const {stdout} = runCLI(path.join(DIR, 'TestProject'), [
'test-command-arg-required',
'--required-arg',
'bar',
]);
expect(stdout).toMatch('ok');
});
5 changes: 5 additions & 0 deletions docs/plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type Command = {
| boolean
| number
| ((config: ConfigT) => string | boolean | number);
required?: boolean;
}>;
examples?: Array<{
desc: string;
Expand Down Expand Up @@ -102,6 +103,10 @@ Default value for the option when not provided. Can be either a primitive value

Useful when you want to use project settings as default value for your option.

##### `options.required`

If true and no default is specified, fail with a friendly error if the user doesn't supply this option.

#### `examples`

An array of example usage of the command to be printed to the user.
Expand Down
1 change: 1 addition & 0 deletions packages/cli-config/src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const command = t.object({
default: t
.alternatives()
.try(t.bool(), t.number(), t.string().allow(''), t.func()),
required: t.bool(),
})
.rename('command', 'name', {ignoreUndefined: true}),
),
Expand Down
1 change: 1 addition & 0 deletions packages/cli-types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export type CommandOption<T = (ctx: Config) => OptionValue> = {
description?: string;
parse?: (val: string) => any;
default?: OptionValue | T;
required?: boolean;
};

export type DetachedCommandFunction<Args = Object> = (
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ function attachCommand<C extends Command<boolean>>(
cmd.addHelpText('after', printExamples(command.examples));

for (const opt of command.options || []) {
cmd.option(
cmd[opt.required ? 'requiredOption' : 'option'](
opt.name,
opt.description ?? '',
opt.parse || ((val: any) => val),
Expand Down
Loading