I'm trying to write a script that can add the package.json dependencies from one project to another one programmatically.
This is probably trivial jq, although I did search around and couldn't find anything that does this exactly.
What I ultimately want to do is this:
yarn add $(get-my-deps.sh)
And so I'm writing get-my-deps right now.
- I know the path to my package.json file
- I know that
yarn add package-name@^1.23.45 package-name-2@^2.3.4
does what I want
I figure I want to do something like:
jq ".dependencies | keys" package.json
// ^ this is where I'm stuck
I don't want just a list of package names, I want to convert the hash of package-name/version-range data into a string with their names and ranges, like this:
babel@^7.3.12 react@^16.0.0 react-dom@^16.0.0 webpack@^4.0.0
I think I just don't understand how the... caret works in jq. I imagine it's the conceptual equivalent of regex backreferencing.
Here's the JS code:
module.exports = () => {
const package = require('./package.json')
// or, if you prefer
const FS = require('fs')
const package = FS.readFileSync('./package.json', 'utf8')
return Object.keys(package.dependencies)
// convert to array of strings: "{name}@{range}"
.reduce((modules, modName) => modules.concat(`${modName}@${package.dependencies[modName]}`), [])
// convert to single string for bash one-liner
.join(' ')
}
I'm sure this is a pretty trivial task for anyone who is fluent with jq, so I'm going to wait a couple days so that folks can take time to put together a good explanation.
Thanks for helping me get my head wrapped around jq.
Here's an example input:
{
"name": "chalk",
"version": "3.0.0",
"description": "Terminal string styling done right",
"license": "MIT",
"repository": "chalk/chalk",
"main": "source",
"engines": {
"node": ">=8"
},
"scripts": {
"test": "xo && nyc ava && tsd",
"bench": "matcha benchmark.js"
},
"dependencies": {
"ansi-styles": "^4.1.0",
"supports-color": "^7.1.0"
},
"devDependencies": {
"ava": "^2.4.0",
"coveralls": "^3.0.7",
"execa": "^3.2.0",
"import-fresh": "^3.1.0",
"matcha": "^0.7.0",
"nyc": "^14.1.1",
"resolve-from": "^5.0.0",
"tsd": "^0.7.4",
"xo": "^0.25.3"
}
}
What must be produced:
ansi-styles@^4.1.0 supports-color@^7.1.0