Having hard time replacing only the occurrence of the version property value in my package.json file
what i tried
CURRENT_VERSION=$(node -p "require('./package.json').version")
sed -i '0,/$CURRENT_VERSION/{s//1.0.2/}' package.json
-- file doesn't change --
my package.json file
{
"version": "1.0.0",
"dependencies": {
"demo-dep": "1.0.0"
}
}
>Solution :
Parsing json files with sed is too prone to errors, try to avoid that and use specialized tools for the job.
npm itself can be used to set versions hassle-free, see https://docs.npmjs.com/cli/v8/commands/npm-version
In your case running npm version 1.0.2 should work
If you still want to use sed – something like this should work
sed -i "0,/$CURRENT_VERSION/ s/$CURRENT_VERSION/1.0.2/"
But if npm is not an option – i suggest you to use jq
tmp=$(mktemp); jq '.version = "1.0.2"' package.json > "$tmp" && mv "$tmp" package.json
it will certainly be less error-prone