Skip to content

Instantly share code, notes, and snippets.

@markmur
Last active June 6, 2023 12:11
Show Gist options
  • Save markmur/702df041ea315082caa153f91b391c1e to your computer and use it in GitHub Desktop.
Save markmur/702df041ea315082caa153f91b391c1e to your computer and use it in GitHub Desktop.
Add script to package.json via command line
# Reusable bash function you can add to your ~/.zshrc or ~/.bashrc file
#
# Usage: pkg-script start "node index.js"
#
function pkg-script () {
echo $(jq --arg key "${1}" --arg val "${2}" '.scripts[$key]=$val' package.json) | jq . | > package.json
}
# Add "test": "jest" to scripts object of node package
echo $(jq '.scripts.test="jest"' package.json) | jq . | > package.json
# Update package name
echo $(jq '.name="new-name"' package.json) | jq . | > package.json
# NOTE
# If you get this error: "pkg-script:1: file exists: package.json"
# run the following:
#
setopt clobber
@antoni
Copy link

antoni commented Mar 3, 2021

Just jq '.scripts.test="jest"' package.json | jq . | > package.json will do

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment