Skip to content

Commit

Permalink
typos and a new script
Browse files Browse the repository at this point in the history
  • Loading branch information
kael-shipman committed Jan 11, 2024
1 parent 47ea965 commit 3eda0fb
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 1 deletion.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,16 @@ scripts that you compose into the higher-level ones. For example, you could writ
`./scripts/.internal/eslint.sh` that calls eslint with a bunch of parameters, then reference that from the higher level
`./scripts/lint.sh` and `./scripts/lint-fix.sh` scripts. (Easily reference the scripts dir using `DIR="$(dirname "$0)"`)

#### `pkgjson` Script

It's often really annoying to have to go through and edit a bunch of `package.json` files by hand. This top-level script
makes it easy to set values in all `package.json` files at once. You can also provide `--filter` and/or `--exclude`
arguments to target a subset of packages.

For example: `pnpm pkgjson --filter microservice --exclude my --merge set .files '["dist","README.md"]'`

See `pnpm pkgjson --help` for more details.


### Docker Infrastructure

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"docker:build:dev": "DOCKER_TARGET=dev SERVICE_NAME=monorepo ./scripts/docker-build.sh",
"docker:compose": "./scripts/docker-compose.sh",
"format": "pnpm -r format",
"pkgjson": "./scripts/.internal/pkgjson.sh",
"publish:all": "./scripts/.internal/publish-all.sh",
"test": "pnpm -r test",
"typecheck": "pnpm -r typecheck",
Expand Down
154 changes: 154 additions & 0 deletions scripts/.internal/pkgjson.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
#!/bin/bash
set -e

ROOT="$(dirname "$0")/../.."
CMD=
FILTER=".*"
EXCLUDE="$^"
JSON=
OPERATOR="|="
DRY_RUN=

function echo_usage() {
echo "Usage:"
echo " $0 -h|--help - Show this help message and exit"
echo " $0 ([options]) set [key] [value] - Set the given key to the given value in package.json. Keys may be specified in jq format."
echo " $0 ([options]) [del|delete] [key] - Delete the given key from package.json. Keys may be specified in jq format."
echo
echo "Options:"
echo " -f|--filter [regex] - Only include packages matching the given regex"
echo " -e|--exclude [regex] - Exclude any packages matching the given regex"
echo " -j|--json - (For set) Interpret the value as JSON"
echo " -m|--merge - (For set) Merge the value with the existing value (implies --json)"
echo " -d|--dry-run - Don't actually modify any files, just output the changes that would be made"
echo
}

function exit_with_error() {
>&2 echo_usage
>&2 echo
>&2 echo "E: $1"
exit 1
}

function process_set_args() {
SET_KEY="$1"
SET_VAL="$2"
if [ -z "$SET_KEY" ]; then exit_with_error "No key specified"; fi
if [ -z "$SET_VAL" ]; then exit_with_error "No value specified"; fi
if [ -n "$3" ]; then exit_with_error "Unknown argument: '$3'"; fi
}

function process_del_args() {
DEL_KEY="$1"
if [ -z "$DEL_KEY" ]; then exit_with_error "No key specified"; fi
if [ -n "$2" ]; then exit_with_error "Unknown argument: '$2'"; fi
}

function apply_cmd() {
local OUTPUT
for pkgjson in "$ROOT/apps"/*/package.json "$ROOT/libs"/*/package.json; do
if ! echo "$pkgjson" | grep -Eq "$FILTER" || echo "$pkgjson" | grep -Eq "$EXCLUDE"; then
continue
fi
if [ "$CMD" == "set" ]; then
if [ -n "$JSON" ]; then
OUTPUT="$(jq --argjson val "$SET_VAL" "$SET_KEY += \$val" "$pkgjson")"
else
OUTPUT="$(jq --arg val "$SET_VAL" "$SET_KEY |= \$val" "$pkgjson")"
fi
elif [ "$CMD" == "delete" ]; then
OUTPUT="$(jq "del($DEL_KEY)" "$pkgjson")"
else
exit_with_error "Unknown command: '$CMD'"
fi

if [ -n "$DRY_RUN" ]; then
echo ----------------------------------------------------------------------------------------
echo "$pkgjson:"
echo "$OUTPUT"
echo
else
echo "$OUTPUT" > "$pkgjson"
fi
done
}









while [ $# -gt 0 ]; do
case "$1" in
-h|--help)
echo_usage
exit
;;

-f|--filter)
FILTER="$2"
shift 2
;;

-e|--exclude)
EXCLUDE="$2"
shift 2
;;

-j|--json)
JSON="true"
shift
;;

-m|--merge)
JSON="true"
OPERATOR="+="
shift
;;

-d|--dry-run)
DRY_RUN="true"
shift
;;

set)
CMD="set"
shift
break
;;

del|delete)
CMD="delete"
shift
break
;;

*)
>&2 echo_usage
>&2 echo
>&2 echo "Unknown option: '$1'"
exit 1
;;
esac
done






if [ -z "$CMD" ]; then exit_with_error "No command specified"; fi

if [ "$CMD" == "set" ]; then
process_set_args "$@"
elif [ "$CMD" == "delete" ]; then
process_del_args "$@"
else
exit_with_error "Unknown command: '$CMD'"
fi

apply_cmd
2 changes: 1 addition & 1 deletion tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@
"paths": {}
},
"files": [],
"inclue": [],
"include": []
}

0 comments on commit 3eda0fb

Please sign in to comment.