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
34 changes: 34 additions & 0 deletions __tests__/lib/cli/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,40 @@ describe( 'utils/cli/command', () => {
} );

describe( 'subcommand dispatch', () => {
it( 'allows wp option-only invocations without separator', async () => {
const parentScript = path.join( process.cwd(), 'src/bin/vip.js' );
const childScript = path.join( process.cwd(), 'src/bin/vip-wp.js' );

const cmd = command( { requiredArgs: 0 } ).command( 'wp', 'Run WP-CLI commands.' );

await cmd.argv( [ process.execPath, parentScript, 'wp', '--help' ] );

expect( spawnSync ).toHaveBeenCalledWith( process.execPath, [ childScript, '--help' ], {
stdio: 'inherit',
env: process.env,
} );
expect( process.exit ).toHaveBeenCalledWith( 0 );
} );

it( 'allows wp option-only invocations with option values without separator', async () => {
const parentScript = path.join( process.cwd(), 'src/bin/vip.js' );
const childScript = path.join( process.cwd(), 'src/bin/vip-wp.js' );

const cmd = command( { requiredArgs: 0 } ).command( 'wp', 'Run WP-CLI commands.' );

await cmd.argv( [ process.execPath, parentScript, 'wp', '--path', '/tmp/wp' ] );

expect( spawnSync ).toHaveBeenCalledWith(
process.execPath,
[ childScript, '--path', '/tmp/wp' ],
{
stdio: 'inherit',
env: process.env,
}
);
expect( process.exit ).toHaveBeenCalledWith( 0 );
} );

it( 'dispatches when child options appear before -- and subcommand follows separator', async () => {
const parentScript = path.join( process.cwd(), 'src/bin/vip.js' );
const childScript = path.join( process.cwd(), 'src/bin/vip-wp.js' );
Expand Down
49 changes: 48 additions & 1 deletion src/lib/cli/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,52 @@
return null;
}

hasWpCommandPayload( subcommandArgs ) {

Check failure on line 397 in src/lib/cli/command.js

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this function to reduce its Cognitive Complexity from 20 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=Automattic_vip-cli&issues=AZ4W6HrUnopbnQ3DOBX4&open=AZ4W6HrUnopbnQ3DOBX4&pullRequest=2832
const wpFlagsWithoutValue = new Set( [ '--help', '-h', '--version', '-v', '--yes', '-y' ] );
Comment thread
sjinks marked this conversation as resolved.
let expectOptionValue = false;

for ( const arg of subcommandArgs ) {
if ( expectOptionValue ) {
if ( ! isOptionToken( arg ) ) {
expectOptionValue = false;
continue;
}

expectOptionValue = false;
}

if ( ! isOptionToken( arg ) ) {
return true;
}

if ( arg.startsWith( '--' ) ) {
if ( arg.includes( '=' ) || wpFlagsWithoutValue.has( arg ) ) {
continue;
}

expectOptionValue = true;
continue;
Comment thread
sjinks marked this conversation as resolved.
}

if ( /^-[A-Za-z0-9]$/.test( arg ) ) {
if ( wpFlagsWithoutValue.has( arg ) ) {
continue;
}

expectOptionValue = true;
continue;
}

if ( /^-[A-Za-z0-9]+=/.test( arg ) ) {
continue;
}

return true;
}

return false;
}

async executeSubcommand( argv, parsedAlias, subcommand ) {
const currentScript = argv[ 1 ];
const subcommandName = subcommand.name;
Expand All @@ -407,8 +453,9 @@
const hasSeparatorBeforeSubcommand = rawArgsBeforeSubcommand.includes( '--' );
const argsBeforeSubcommand = rawArgsBeforeSubcommand.filter( arg => arg !== '--' );
const subcommandArgs = parsedAlias.argv.slice( subcommand.index + 1 );
const hasWpCommandPayload = this.hasWpCommandPayload( subcommandArgs );
const hasSeparator = argv.includes( '--' );
if ( subcommandName === 'wp' && subcommandArgs.length && ! hasSeparator ) {
if ( subcommandName === 'wp' && hasWpCommandPayload && ! hasSeparator ) {
Comment thread
sjinks marked this conversation as resolved.
exit.withError(
'A double dash ("--") must separate the arguments of "vip" from those of "wp". Run "vip wp --help" for examples.'
);
Expand Down
Loading