Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get stdout from NPM Script into a variable

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!

like image 307
dariogz.com Avatar asked Oct 28 '25 12:10

dariogz.com


1 Answers

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.

like image 137
rsp Avatar answered Oct 31 '25 03:10

rsp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!