Skip to content

Instantly share code, notes, and snippets.

@oirodolfo
Forked from markmur/.zshrc
Created February 10, 2022 16:43
Show Gist options
  • Save oirodolfo/cec1d4ecfb729ad95c788d53df8dd7f1 to your computer and use it in GitHub Desktop.
Save oirodolfo/cec1d4ecfb729ad95c788d53df8dd7f1 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment