chrisguttandin / angular-prerender

A command line tool to prerender Angular Apps.
MIT License
125 stars 7 forks source link

Argument `--parameter-values` gives JSON parse error #102

Closed aristotelos closed 4 years ago

aristotelos commented 4 years ago

Running with the option --parameter-values as documented does not work, but gives a JSON parse exception:

PS C:\my-dir> npx angular-prerender --parameter-values '{":name":["amelia","oliver"]}'
Opties:
  --version           Toon versienummer                              [booleaans]
  --help              Toon help                                      [booleaans]
  --browser-target    specify the target inside your angular.json file which is
                      used to build the single page app
                                                   [string] [standaard: "build"]
  --config            specify the path to the angular.json file
                                                            [string] [standaard:
               "C:\mydir\angular.json"]
  --parameter-values  specify the parameter values which should be replaced with
                      the parameter in the routes     [string] [standaard: "{}"]
  --server-target     specify the target inside your angular.json file which is
                      used to build the server side code
                                                  [string] [standaard: "server"]
  --verbose, -v       set this flag if you prefer more detailed log messages
                                                  [booleaans] [standaard: false]

Unexpected token : in JSON at position 1

I tried other ways but did not yet succeed, except with an empty map: PS C:\my-dir> npx angular-prerender --parameter-values '{}'

chrisguttandin commented 4 years ago

Hi @aristotelos, thanks for bringing this to my attention. I can't reproduce the problem which is why I have the feeling that this could be an incompatibility between macOS and Windows.

Does it work if you use some other type of quotes?

aristotelos commented 4 years ago

Well, PowerShell has single quotes as its standard escape character, so this seems the best option. I also tried with double quotes like this, but it fails:

PS C:\mydir> npx angular-prerender --parameter-values "'{`":name`":[`"amelia`",`"oliver`"]}'"
Opties:
  --version           Toon versienummer                              [booleaans]
  --help              Toon help                                      [booleaans]
  --browser-target    specify the target inside your angular.json file which is
                      used to build the single page app
                                                   [string] [standaard: "build"]
                                                            [string] [standaard:
               "C:\mydir\angular.json"]
  --parameter-values  specify the parameter values which should be replaced with
                      the parameter in the routes     [string] [standaard: "{}"]
  --server-target     specify the target inside your angular.json file which is
                      used to build the server side code
                                                  [string] [standaard: "server"]
  --verbose, -v       set this flag if you prefer more detailed log messages

Unexpected token : in JSON at position 1

Even simple JSON like this fails:

PS C:\mydir> npx angular-prerender --parameter-values '{"foo":"bar"}'
Opties:
  --version           Toon versienummer                              [booleaans]
  --help              Toon help                                      [booleaans]
  --browser-target    specify the target inside your angular.json file which is
                      used to build the single page app
                                                   [string] [standaard: "build"]
  --config            specify the path to the angular.json file
                                                            [string] [standaard:
               "C:\mydir\angular.json"]
  --parameter-values  specify the parameter values which should be replaced with
                      the parameter in the routes     [string] [standaard: "{}"]
  --server-target     specify the target inside your angular.json file which is
                      used to build the server side code
                                                  [string] [standaard: "server"]
  --verbose, -v       set this flag if you prefer more detailed log messages
                                                  [booleaans] [standaard: false]

Unexpected token f in JSON at position 1
chrisguttandin commented 4 years ago

Thanks for checking again. I believe now that all quotes somehow get lost. I don't know why though.

If you execute JSON.parse('{:name:[amelia,oliver]}') the error is 'SyntaxError: Unexpected token : in JSON at position 1' and if you execute JSON.parse('{foo:bar}') the error is 'Unexpected token f in JSON at position 1'. That matches with the error messages that you got.

I improved the error messages in v4.3. It will not help with the problem but it will help to verify my assumption because it logs the values as part of the error that could not be parsed.

Can you please check again with the latest version? Many thanks in advance.

chrisguttandin commented 4 years ago

Just a quick follow up. I found this documentation for the AWS CLI. It explains how to escape JSON values for PowerShell.

https://docs.aws.amazon.com/cli/latest/userguide/cli-usage-parameters-quoting-strings.html

Does it work if you escape all quotes with a backslash and run: npx angular-prerender --parameter-values '{\":name\":[\"amelia\",\"oliver\"]}'?

aristotelos commented 4 years ago

No, it doesn't work unfortunately. I'll show a couple of commands that I tried so far with their output.

PS>npx angular-prerender --parameter-values '{\":name\":[\"amelia\",\"oliver\"]}'
Please specify valid parameter values. The given value "{:name:[amelia,oliver]}" is invalid.
PS>npx angular-prerender --parameter-values '{":name":["amelia","oliver"]}'
Please specify valid parameter values. The given value "{:name:[amelia,oliver]}" is invalid.
PS>npx angular-prerender --parameter-values "{`":name`":[`"amelia`",`"oliver`"]}"
Please specify valid parameter values. The given value "{:name:[amelia,oliver]}" is invalid.
PS>npx angular-prerender --parameter-values "{':name':['amelia','oliver']}"
Please specify valid parameter values. The given value "{':name':['amelia','oliver']}" is invalid.
PS>npx angular-prerender --parameter-values "{\`"name\`":[\`"amelia\`",\`"oliver\`"]}"
Please specify valid parameter values. The given value "{name:[amelia,oliver]}" is invalid.
PS>npx angular-prerender --parameter-values '{`\"name`\":[`\"amelia`\",`\"oliver`\"]}'
Please specify valid parameter values. The given value "{`name`:[`amelia`,`oliver`]}" is invalid.
PS>npx angular-prerender --parameter-values '{\`"name\`":[\`"amelia\`",\`"oliver\`"]}'
Please specify valid parameter values. The given value "{\`name\`:[\`amelia\`,\`oliver\`]}" is invalid.

I even tried using cmd instead, but also with no success. It seems the " characters are simply removed.

CMD>npx angular-prerender --parameter-values '{\"name\":[\"amelia\",\"oliver\"]}'
Please specify valid parameter values. The given value "{name:[amelia,oliver]}" is invalid.
chrisguttandin commented 4 years ago

It seems like a hopeless attempt but the AWS docs say the cmd prompt should be encapsulated in double quotes. Maybe that's a combination worth trying.

npx angular-prerender --parameter-values "{\":name\":[\"amelia\",\"oliver\"]}"

Does that work?

aristotelos commented 4 years ago

Tried that too, doesn't work:

CMD>npx angular-prerender --parameter-values "{\"name\":[\"amelia\",\"oliver\"]}"
Please specify valid parameter values. The given value "{name:[amelia,oliver]}" is invalid.

CMD>npx angular-prerender --parameter-values '{^"name^":[^"amelia^",^"oliver^"]}'
Please specify valid parameter values. The given value "{name:[amelia,oliver]}" is invalid.
chrisguttandin commented 4 years ago

To be honest I have no idea, what else you could try. But I was thinking about supporting files as well. Maybe that could be a solution which works for you as well. What would you think about something like this?

npx angular-prerender --parameter-values ./my-parameter-values.json

aristotelos commented 4 years ago

I have found a way! Using an escaped backslash and escaped double quote actually works, in both PowerShell and cmd:

PS>npx angular-prerender --parameter-values '{\\\":name\\\":[\\\"amelia\\\",\\\"oliver\\\"]}'

It may have to do with this issue.

aristotelos commented 4 years ago

The next question is: how do I escape a space inside a parameter value...

chrisguttandin commented 4 years ago

Wow thanks for the follow up. I will update the README once we resolved this issue.

Normally I would say don't escape whitespace at all, but I guess that doesn't work for you, right?

npx angular-prerender --parameter-values '{\\\":name\\\":[\\\"a m e l i a\\\",\\\"o l i v e r\\\"]}'

aristotelos commented 4 years ago

Well, that doesn't work, it ends up with:

PS>npx angular-prerender --parameter-values '{\\\":name\\\":[\\\"a m e l i a\\\",\\\"o l i v e r\\\"]}'
Please specify valid parameter values. The given value "{":name":["a" is invalid.

I again tried one, two or three or four slashes, but that doesn't work... Because it seems to be a PowerShell or general npx issue I will close it here.

aristotelos commented 4 years ago

I just found out what is a solution here: enclose the value in double quotes like this:

npx angular-prerender --parameter-values '\"{\\\":name\\\":[\\\"am e l i a\\\",\\\"o l i v e r\\\"]}\"'
chrisguttandin commented 4 years ago

Wow, I'm glad you found a solution. I added a small paragraph to the readme to mention that escaping might be an issue.