4

I have this node script that parse a .YAML and output a field named version

node node/getAssetsVersion.js
=> "2.1.2"

I'm trying to get that stdout into a varible and use it in a NPM Script

This is what I'm trying to do in my package.json:

"scripts": {
   "build": "cross-env VERSION=\"$(node node/getAssetsVersion.js)\" \"node-sass --include-path scss src/main.scss dist/$VERSION/main.css\""
}

Thanks!

0

1 Answer 1

2

Instead of this:

VERSION=\"$(node node/getAssetsVersion.js)\" 

you may need to use:

VERSION=\"$(node node/getAssetsVersion.js | cut -d'\"' -f2)\"

if the output of your program is this as you wrote in the question:

=> "2.1.2"

If it's just this:

"2.1.2"

then the above will still work but you can use a simpler command:

VERSION=$(node node/getAssetsVersion.js)

with no quotes.

But in the later part the $VERSION will likely not get substituted as you expect.

Since you tagged you question with bash I would recommend writing a Bash script:

#!/bin/bash
VERSION=$(node node/getAssetsVersion.js | cut -d'\"' -f2)
node-sass --include-path scss src/main.scss dist/$VERSION/main.css

or:

#!/bin/bash
VERSION=$(node node/getAssetsVersion.js)
node-sass --include-path scss src/main.scss dist/$VERSION/main.css

depending on what is the output of getAssetsVersion.js and put this in package.json:

"scripts": {
   "build": "bash your-bash-script-name"
}

I would avoid any quotes that are escaped more than once.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.