Mike
Mike

Reputation: 12829

Nested json argument in node child process bash script

I am trying to use node to execute a bash command that takes a nested json object as an argument. I am finding that nested json does not work.

For example trying to convert this bash command. Notice awscloudformation is a nested object.

#!/bin/bash
set -e
IFS='|'

AWSCLOUDFORMATIONCONFIG="{\
\"configLevel\":\"project\",\
\"useProfile\":true,\
\"profileName\":\"testing\"\
}"
PROVIDERS="{\
\"awscloudformation\":$AWSCLOUDFORMATIONCONFIG\
}"

amplify init \
--providers $PROVIDERS \

Here is the node script that does not work as expected. Providers seems to be ignored and is not overriding the defaults like the above bash script.

const arguments = [
  '--providers',
  `{"awscloudformation":{"configLevel":"project","useProfile":true,"profileName":"testing"}}`,
];
const opts = { stdio: 'inherit', shell: true };
require('child_process').spawn('amplify init', getArgs(), opts);

Would exec be better suited here? It seems I would need to include a variable similar to how $AWSCLOUDFORMATIONCONFIG is being used in the bash script.

It is interesting to note that other arguments that are not nested seem to work fine. For example this amplify argument works fine:

  '--amplify',
  '{"projectName":"checking987","envName":"dev","defaultEditor":"code"}',

Upvotes: 0

Views: 208

Answers (1)

Philippe
Philippe

Reputation: 26707

It's better to use shell: false here :

const arguments = [
  '--providers',
  `{"awscloudformation":{"configLevel":"project","useProfile":true,"profileName":"testing"}}`,
];
const opts = { stdio: 'inherit', shell: false };
require('child_process').spawn("amplify", ["init", ...arguments], opts);

In shell mode, double quotes are removed.

Adapted shell mode version which works :

const arguments = [
  '--providers',
  `{\\"awscloudformation\\":{\\"configLevel\\":\\"project\\",\\"useProfile\\":true,\\"profileName\\":\\"testing\\"}}`,
];
const opts = { stdio: 'inherit', shell: true };
require('child_process').spawn("amplify init", arguments, opts);

Upvotes: 1

Related Questions