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
14 changes: 7 additions & 7 deletions __tests__/lib/cli/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,39 +9,39 @@ describe( 'utils/cli/format', () => {
},
{
input: [ 'textnospaces' ],
expected: [ 'textnospaces' ],
expected: [ '"textnospaces"' ],
},
{
input: [ '{"json":"json with spaces"}' ],
expected: [ '{"json":"json with spaces"}' ],
expected: [ '"{\\"json\\":\\"json with spaces\\"}"' ],
},
{
input: [ '{ "json" : "json with spaces outside strings" }' ],
expected: [ '{ "json" : "json with spaces outside strings" }' ],
expected: [ '"{ \\"json\\" : \\"json with spaces outside strings\\" }"' ],
},
{
input: [
' { "json" : "json with spaces outside strings and outside the object" } ',
],
expected: [
' { "json" : "json with spaces outside strings and outside the object" } ',
'" { \\"json\\" : \\"json with spaces outside strings and outside the object\\" } "',
],
},
{
input: [ '{ "json" : "spaces-outside-strings-only" }' ],
expected: [ '{ "json" : "spaces-outside-strings-only" }' ],
expected: [ '"{ \\"json\\" : \\"spaces-outside-strings-only\\" }"' ],
},
{
input: [ '{"json":broken json with spaces}' ],
expected: [ '"{\\"json\\":broken json with spaces}"' ],
},
{
input: [ '--foo=bar1 "bar2" "bar3"' ],
expected: [ '--foo="bar1 \\"bar2\\" \\"bar3\\""' ],
expected: [ '"--foo=bar1 \\"bar2\\" \\"bar3\\""' ],
},
{
input: [ '--foo', 'bar1 "bar2" "bar3"' ],
expected: [ '--foo', '"bar1 \\"bar2\\" \\"bar3\\""' ],
expected: [ '"--foo"', '"bar1 \\"bar2\\" \\"bar3\\""' ],
},
] )( 'should requote args when needed - %o', ( { input, expected } ) => {
const result = requoteArgs( input );
Expand Down
25 changes: 1 addition & 24 deletions src/lib/cli/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,30 +140,7 @@ export function keyValue( values: Tuple[] ): string {
}

export function requoteArgs( args: string[] ): string[] {
return args.map( arg => {
if ( arg.includes( '--' ) && arg.includes( '=' ) && arg.includes( ' ' ) ) {
return arg.replace( /"/g, '\\"' ).replace( /^--([^=]*)=(.*)$/, '--$1="$2"' );
}

if ( arg.includes( ' ' ) && ! isJsonObject( arg ) ) {
return `"${ arg.replace( /"/g, '\\"' ) }"`;
}

return arg;
} );
}

export function isJsonObject( str: unknown ): boolean {
return typeof str === 'string' && str.trim().startsWith( '{' ) && isJson( str );
}

export function isJson( str: string ): boolean {
try {
JSON.parse( str );
return true;
} catch ( error ) {
return false;
}
return args.map( arg => `"${ arg.replace( /"/g, '\\"' ) }"` );
Comment thread
sjinks marked this conversation as resolved.
Dismissed
}

export function capitalize( str: unknown ): string {
Expand Down