Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
172 views
in Technique[技术] by (71.8m points)

How to output JS/JSON in NPM Script

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

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

1 Reply

0 votes
by (71.8m points)

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"
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...