2

I just wann write a very basic script, which

  • inits npm in a new project folder
  • Installs nodemon
  • creates an index.js file
  • adds a start command into the package.json for nodemon
  • writes "console.log" into the index.js file

I seem to be close, but adjusting the package.json does not work. Somehow the search variable is two time in ...

I want to achive:

"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon my_file.js"

I achive:

"test": "echo \"Error: no test specified\" "test": "echo \"Error: no test specified\" && exit 1""test": "echo \"Error: no test specified\" && exit 1" exit 1","start": "nodemon my_file.js"

Full code

#!/bin/bash -e
set -e
clear

# Init npm
npm init -y && npm i --save-dev nodemon && touch index.js

wait

# find test command
constToFind='\"test\": \"echo \\\"Error: no test specified\\\" && exit 1\"'

# add nodemon command (with comma, line break and tab)
replacement=',\n\t\"start\": \"nodemon my_file.js\"'
combineReplacement="${constToFind}${replacement}"

# Add start command to package.json
sed -i "s/$constToFind/$combineReplacement/g" package.json

# Create entry javascript file with console.clear on tops
echo "console.clear();" >> index.js
Ehrlich_Bachman
  • 772
  • 1
  • 10
  • 23
  • Do you _have to_ use sed? Why not use a different tool that cna parse json, like `jq`? Are you aware what `&` character means in `sed`s `s` command replacement part? Maybe does https://stackoverflow.com/questions/407523/escape-a-string-for-a-sed-replace-pattern solves your issue? – KamilCuk Jul 22 '21 at 14:55
  • Not sure, I just used it yesterday, to adjust a .env file in another script and there it worked nice. Right now I just look for a simple tool, to adjust text in all kind of files, since I do not need to do that often. But I keep that in mind, for the next time, if I need to work on a json. Actually I am not aware, it just worked the last time, so it got my blue print. But I gonna check it out. – Thanks. – Ehrlich_Bachman Jul 22 '21 at 15:20

1 Answers1

1

Instead of substituting you can append with the sed a command:

$ addon='\"start\": \"nodemon my_file.js\"'
$ sed "/$constToFind/a\
$addon
" package.json
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon my_file.js"

(if you care your package.json file use the -i only after you checked that it really works as expected).

And if your sed version supports the alternate syntax this is a bit simpler:

$ addon='\"start\": \"nodemon my_file.js\"'
$ sed "/$constToFind/a$addon" package.json
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon my_file.js"
Renaud Pacalet
  • 25,260
  • 3
  • 34
  • 51
  • Awesome. That is way better then what I was trying. Weird is, that you previous/unedited answer works well (# add nodemon command addon=' ,"start\": \"nodemon my_file.js\"' # Add start command to package.json sed -ir "/$constToFind/a\ $addon " package.json). But the updated, with and without -i or -ir or -r does not. Anyway, it works. Thanks for that. – Ehrlich_Bachman Jul 22 '21 at 15:16
  • Ah! What version of `sed` do you use? – Renaud Pacalet Jul 22 '21 at 15:21
  • (sed --version results in: sed (GNU sed) 4.7 – Ehrlich_Bachman Jul 22 '21 at 15:23
  • Mine is 4.8. It could be that. Or not (I don't have 4.7 installed here). Anyway, I restored the working answer and added the simpler one in case it is supported. – Renaud Pacalet Jul 22 '21 at 15:25