Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

From my last question - npm version "scripts have access to the new version in package.json"

How do you output JS / JSON from an NPM script. After an npm version I am writing that out to a JS / JSON file to be picked up in Angular with an import like what I've written at the bottom of this post

I tried these variations of the following:

"version": "version=$(git tag | head -1); echo "{version:$version}" > client/src/app/config-from-npm.json",
  • single quote (') instead of double (") in all cases
  • echo '{version:... - error Unexpected token { in JSON
  • echo '%7Bversion:... - outputs '%7Bversion ...'
  • echo '%7Bversion:... - outputs '{version ...'

Angular import of version number

What I'm trying to achieve and how it will be used:

 export var config = {
     version: v1.0.0
 }

 ----

 import { config } from './config';

If I don't work out this '{' in JS / JSON problem I will just write the version number to file and read in. Nowhere near as neat!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
162 views
Welcome To Ask or Share your Answers For Others

1 Answer

Instead of forging a json string with shell commands, use tools that are json aware.

Here is an example using jq command line parser :

$ echo "v1.0.0" | jq -R '{"version":.}'
{
   "version": "v1.0.0"
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...