Here it is:
node -e "console.log(Object.keys(require('./package.json').peerDependencies).join(' '))" | xargs yarn add
How it works
Let's say we have peerDependencies
in our package.json
like this:
...
},
"peerDependencies": {
"react": "^17.0.2",
"react-dom": "^17.0.2"
}
}
We'll see one by one:
-
Load the
package.json
and get the peerDependencies.
Welcome to Node.js v15.14.0. > require('./package.json').peerDependencies { react: '^17.0.2', 'react-dom': '^17.0.2' }
-
Object.keys
function collects the keys of the object and put them together in the array.
Welcome to Node.js v15.14.0. > Object.keys({ react: '^17.0.2', 'react-dom': '^17.0.2' }) [ 'react', 'react-dom' ]
-
join
method joins the elements with the given string.
> [ 'react', 'react-dom' ].join(' ') 'react react-dom'
-
node -e
evaluates the given script.
$ node -e "console.log('react react-dom')" 'react react-dom'
-
Pass it to the
yarn add
withxargs
.
$ node -e "console.log('react react-dom')" | xargs yarn add yarn add v1.22.5 [1/4] Resolving packages... [2/4] Fetching packages... [########------------------
Done 🥳
Top comments (1)
thank you, great job 🥳