Last active
June 6, 2023 12:11
-
-
Save markmur/702df041ea315082caa153f91b391c1e to your computer and use it in GitHub Desktop.
Add script to package.json via command line
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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
Just
jq '.scripts.test="jest"' package.json | jq . | > package.json
will do