-
Couldn't load subscription status.
- Fork 430
Description
There is a problem with passing variables to ReLaXed with the shown example. I propose a small change on the wiki. We could also discuss better solutions. 😄
Note: Passing a JSON file works fine.
Steps
Tested on Windows and Linux.
1. Install ReLaXed.
2. Create `mydoc.pug` with the following content:
p Name - #{name}
3. Execute the current example from the wiki:
relaxed mydoc.pug --build-once --locals {"name": "Marc"}
2 Problems:
1st problem
We would get an error like this:
SyntaxError: Unexpected number in JSON at position 1
at JSON.parse (<anonymous>)
at parseJson (C:\Users\user\AppData\Roaming\npm\node_modules\relaxedjs\src\parseLocals.js:22:17)
at parseLocals (C:\Users\user\AppData\Roaming\npm\node_modules\relaxedjs\src\parseLocals.js:48:10)
at Object.<anonymous> (C:\Users\user\AppData\Roaming\npm\node_modules\relaxedjs\src\index.js:84:14)
at Module._compile (internal/modules/cjs/loader.js:1158:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)
at Module.load (internal/modules/cjs/loader.js:1002:32)
at Function.Module._load (internal/modules/cjs/loader.js:901:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)
at internal/main/run_main_module.js:18:47
ReLaXed error: Could not parse locals JSON, see error above.
Launching ReLaXed...
Processing \mydoc.pug...
... HTML generated in 0.1s
... Document loaded in 1.0s
... Network idled in 0.2s
... PDF written in 0.3s (27.2 KB)
... Done in 1.62s
Because of it, no data is passed to our file.
2nd problem
A file named Marc} is created instead of mydoc.pdf. This is a simple syntax fix, see below.
Fixes
First, simply add quotes around the JSON object to get the right output file name. Now the reason behind the JSON parsing error is how the data is given through Commander.js.
With our example, it looks like this: {name:Marc}
The latter is not a valid JSON object (missing quotes on keys) and would be of wrong type if the value was a string, which is the case here. The input style is also the case for other CLI tools like Caporal.js and Yargs.js since they are also subject to the Strong Quoting's principle.
The solution proposed is to simply to escape quotes. This is how the JSON object has to be passed:
"{\"name\":\"Marc\"}"
So on the wiki, the command should be:
relaxed mydoc.pug --build-once --locals "{\"name\":\"Marc\"}"
Trick
A neat trick to obtain the result is to stringify twice the JSON object before passing it as a parameter to ReLaXed:
JSON.stringify(JSON.stringify(data)))