node.js - Find and merge all package.json files into one using jq in bash? -
how can find package.json files , merge them 1 file using jq within bash? following snippet far have gotten, appends files:
find ../../projects -maxdepth 4 -type f -name "package.json" \ -exec cat {} + | jq -s . > $(curdir)/node/.tmp/package.json i have tried using 'add' seems overwrite target each time without merging two.
my project structure initially looks this:
\ projects \ webapp \ package.json \ service \ package.json \ admin \ package.json \ solutions \ killersolution makefile \ node and should after make prenode (see below):
\ projects \ webapp \ package.json \ service \ package.json \ admin \ package.json \ solutions \ killersolution makefile \ node \ .tmp \ package.json <- created i using makefile kick off:
prenode: @find ./node -type d -name ".tmp" -exec rm -rf {} +; @mkdir -p ./node/.tmp @find ../../solutions -maxdepth 4 -type f -name "package.json" -exec cat {} + ... edit #1 : example input & output
let assume 3 package.json files found. dependencies , devdependencies different must combined:
found file #1 ...
{ "name":"project-a", "dependencies":{ "module-a":"1.2.3" } } found file #2 ...
{ "name":"project-b", "dependencies":{ "module-b":"2.3.4" } } found file #3 ...
{ "name":"project-c", "devdependencies":{ "gulp":"*" } } ... combined make following file:
{ "name":"project-c", "dependencies":{ "module-a":"1.2.3", "module-b":"2.3.4" }, "devdependencies":{ "gulp":"*" } } *note: name property, in final output file, irrelevant. key here merge of dependencies , devdependencies objects.
assuming following find command works (adjust if necessary)
find ../../projects -name package.json here solution uses jq * operator along reduce , -s option merge objects:
jq -s 'reduce .[] $d ({}; . *= $d)' $(find ../../projects -name package.json) if prefer can concatenate files , send them jq
find ../../projects -name package.json -exec cat {} \; | \ jq -m -s 'reduce .[] $d ({}; . *= $d)' as noted in reply comment if you're doing in makefile need take steps deal $ or put filter in file , use -f
Comments
Post a Comment