Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 283
notfluffy Avatar asked May 25 '26 20:05

notfluffy


2 Answers

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.

like image 51
1218985 Avatar answered May 27 '26 10:05

1218985


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
like image 30
Zeljko Marinkovic Avatar answered May 27 '26 08:05

Zeljko Marinkovic