I am working on a command line that will install every required node for a nodejs project.
Currently you can do:
sudo gen-web-app express
and this will genereate everything you need to start developing for expressjs.
I'm working now on
sudo gen-web-app reactjs
Everything is working except I have to go manually in the package.json and add the startup script to the file. I know this is possible using SED In BASH, but I need a little help using sed.
This is the file:
{
"name": "test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"nodemon": "^1.11.0",
"path": "^0.12.7",
"react": "^15.6.1",
"webpack": "^3.1.0",
"webpack-dev-server": "^2.5.1"
}
}
I want to replace the following, but I would appreciate a little help :)
"test": "echo \"Error: no test specified\" && exit 1"
with
"start": "webpack-dev-server"
Download and install jq - A command line JSON syntax aware parser which lets you modify .json files. For your example in question.
tmp=$(mktemp)
jq '.scripts.start = "webpack-dev-server" | del(.scripts.test)' input.json > "$tmp" && mv "$tmp" input.json
will produce the final .json file as
{
"name": "test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "webpack-dev-server"
},
"author": "",
"license": "ISC",
"dependencies": {
"nodemon": "^1.11.0",
"path": "^0.12.7",
"react": "^15.6.1",
"webpack": "^3.1.0",
"webpack-dev-server": "^2.5.1"
}
}
You can play with jq in a free-playground developed at jqplay.org.
$ sed -ie 's/\"test\": \"echo \\\"Error: no test specified\\\" && exit 1\"/\"start\": \"webpack-dev-server\"/g' file
$ cat file
{
"name": "test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "webpack-dev-server"
},
"author": "",
"license": "ISC",
"dependencies": {
"nodemon": "^1.11.0",
"path": "^0.12.7",
"react": "^15.6.1",
"webpack": "^3.1.0",
"webpack-dev-server": "^2.5.1"
}
}
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