diff --git a/__tests__/lib/cli/format.js b/__tests__/lib/cli/format.js index 44c6538a3..5fd8d8d06 100644 --- a/__tests__/lib/cli/format.js +++ b/__tests__/lib/cli/format.js @@ -9,27 +9,27 @@ 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}' ], @@ -37,11 +37,11 @@ describe( 'utils/cli/format', () => { }, { 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 ); diff --git a/src/lib/cli/format.ts b/src/lib/cli/format.ts index 891d364b3..ab52dd335 100644 --- a/src/lib/cli/format.ts +++ b/src/lib/cli/format.ts @@ -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, '\\"' ) }"` ); } export function capitalize( str: unknown ): string {