Bsconfig workflow


#1

I’m trying to write a bash script that add an item to an array in my bsconfig.json file using jq. I can’t figure out the syntax for doing so.

This doesn’t work:

function addbsdep() {
  newDep="$@"   
  jq '.["bs-dependencies"] += ["$newDep"]' bsconfig.json | sponge  bsconfig.json
}

In terminal you would run addbsdep reason-react. The above adds $newDep rather than reason-react to the json file.

What is the correct syntax?

Update - Solution:

function addbsdep() {
	newDep="$@"	

	echo $newDep
	jq --arg v "$newDep" '.["bs-dependencies"] += [$v]' bsconfig.json | sponge bsconfig.json
}

source


#2

Because I can’t let stuff go…

To add a dependency to package.json and bsconfig.json run, for example, yarnb @glennsl/bs-json. This will add the dep to both. Of course, if you run it twice, it will add it to bsconfig twice.

function yarnb() {
	newDep="$@"	

	if yarn add $newDep; then
		jq --arg v "$newDep" '.["bs-dependencies"] += [$v]' bsconfig.json | sponge bsconfig.json;
		printf 'added ${newDep} to package.json & bsconfig.json\n'
	else
    	printf '\e[31madd ${newDep} to package.json failed\n\e[0m'
	fi

}

Script uses sponge and jq.