jsonbashshellautomation

I am trying to write a bash script to update package.json dependency version


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?


Solution

  • (grep -o '"page-objects": "[^"]' package.json | grep -o '[^"]$') worked for me. thanks for the help