Skip to content
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

Fix error when parsing an invalid command with shell-quote #571

Merged
merged 1 commit into from
Sep 27, 2024
Merged
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
29 changes: 18 additions & 11 deletions src/hooks/use-is-valid-wp-cli-inline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,24 @@ const PLACEHOLDER_CHAR_BEGIN = [ '<', '[', '{', '(' ];
const UNSUPPORTED_COMMANDS = [ 'db', 'server', 'shell' ];

export function useIsValidWpCliInline( command: string ) {
const wpCliArgs = parse( command )
.map( ( arg ) => {
if ( typeof arg === 'string' || arg instanceof String ) {
return arg;
} else if ( 'op' in arg ) {
return arg.op;
} else {
return false;
}
} )
.filter( Boolean ) as string[];
let wpCliArgs: string[] = [];

try {
wpCliArgs = parse( command )
.map( ( arg ) => {
if ( typeof arg === 'string' || arg instanceof String ) {
return arg;
} else if ( 'op' in arg ) {
return arg.op;
} else {
return false;
}
} )
.filter( Boolean ) as string[];
} catch ( error ) {
return false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we know what command made it fail?

Copy link
Member Author

@sejas sejas Sep 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes!, it was when we tried to parse JS code as bash command. In my case:

command import { store, getContext } from '@wordpress/interactivity';

store( "myPlugin", {
    actions: {
        toggle: () => {
            const context = getContext();
            context.isOpen = !context.isOpen;
        },
    },
    callbacks: {
        logIsOpen: () => {
            const { isOpen } = getContext();
            console.log( `Is open: ${ isOpen }` );
        }
    },
});

and

import { store, getContext } from '@wordpress/interactivity';

store( "myPlugin", {
    actions: {
        toggle: () => {
            const context = getContext();
            context.isOpen = !context.isOpen;
        },
    },
    callbacks: {
        logIsOpen: () => {
            const { isOpen } = getContext();
            console.log( `Is open: ${ isOpen }` );
        }
    },
});

BTW, I'll change the initial prompt as a follow up. Because it matches store with wp-data instead of a woocommerce store.

}

const wpCommandCount = wpCliArgs.filter( ( arg ) => arg === 'wp' ).length;
const containsPath = wpCliArgs.some( ( arg ) => /path/i.test( arg ) || arg.startsWith( '/' ) );
const containsPlaceholderArgs = wpCliArgs.some( ( arg ) =>
Expand Down
Loading