This is my package.json file
{
"name": "project-1",
"version": "1.0.0",
"description": "TestCafe project",
"main": "index.js",
"scripts": {
"ci:build": "npm ci",
"test": "testcafe",
"precise-commits": "precise-commits"
},
"repository": {
"type": "git",
"url": "http://github.com/dev/test/testcafe-test"
},
"author": "Tom",
"license": "ISC",
"devDependencies": {
"precise-commits": "^1.0.2",
"prettier": "^1.16.4",
"random-words": "^1.1.0",
"testcafe": "^1.8.0"
},
"dependencies": {
"page-objects": "1.6.169"
}
}
I need to update page-objects version from 1.6.169 to 1.6.170 and as and when we make changes to page objects version changes this is the bash file i have written
#!/bin/bash
PROJECT=${1?Error: No test project directory passed}
NEW_VERSION=${2?Error: Pass new version}
echo "Updating page object version for ${PROJECT} project"
cd ~/git/master/test/${PROJECT}
CURRENT_VERSION=$(node -p "require('./package.json').version") # here it gives me the
# version of json which is 1.0.0 but i need 1.6.169
sed -i '' "s/${CURRENT_VERSION}/${NEW_VERSION}/" package.json
echo $CURRENT_VERSION
#npm i
how can i get 1.6.69?
Sed is an efficient stream editor for filtering and transforming text. It has great ability to filter text in a pipeline unlike other editors. However, considering the content of your package.json, it is more preferable to use a lightweight and flexible command-line JSON processor. For instance, you can make use of jq with a temporary file like as shown below to get an in-place replace editing effect. It is like sed for JSON data - you can use it to slice and filter and map and transform structured data with the same ease that sed, awk, grep and friends let you play with text.
tmp=$(mktemp)
jq '.dependencies."page-objects"="1.6.170"' package.json > "$tmp" && mv "$tmp" package.json
If you want to avoid hard-coded version, pass the correct version via a jq argument:
version="1.6.172"
tmp=$(mktemp)
jq --arg version "$version" '.dependencies."page-objects"=$version' package.json > "$tmp" && mv "$tmp" package.json
jq is written in C and has no runtime dependencies, so it should be possible to build it for nearly any platform. Prebuilt binaries are available for Linux, OS X and Windows. You can check more details bout jq here if you're interested. If jq is not installed on your machine, you can easily grab it from here.
I had similar problem where i had to update version of package.json for our library that i maintain. What i done is something similar to what you have done
#!/bin/bash
NEW_VERSION=${1?Error: Pass new version}
CURRENT_VERSION=$(node -p "require('./package.json').version")
sed -i '' "s/${CURRENT_VERSION}/${NEW_VERSION}/" package.json
cat package.json
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With