Last active
August 16, 2021 04:59
-
-
Save ybiquitous/48f69d29194ed1aecf9b85097da51715 to your computer and use it in GitHub Desktop.
Add `repository.directory` field to all `package.json` files
This file contains hidden or 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
#!/usr/bin/env bash | |
set -eu -o pipefail | |
if [[ $# -ne 1 ]]; then | |
echo "Usage: $0 <repo_url>" | |
exit 1 | |
fi | |
url="$1" | |
files="$(git ls-files | grep package.json)" | |
for file in $files; do | |
echo "Updating ${file}..." | |
dir="$(dirname "${file}")" | |
# Unfortunately, `npm pkg set repository={}` does not work... | |
jq '.repository = {}' < "${file}" > "${file}.new" | |
mv "${file}.new" "${file}" | |
if [[ $dir == '.' ]]; then | |
npm pkg set repository.type="git" repository.url="$url" | |
else | |
(cd "$dir" && npm pkg set repository.type="git" repository.url="$url" repository.directory="$dir") | |
fi | |
done | |
echo "Linting updated files..." | |
cat <<JSON > .npmpackagejsonlintrc.json | |
{ | |
"rules": { | |
"require-repository": "error" | |
} | |
} | |
JSON | |
cat <<EOF > .npmpackagejsonlintignore | |
/packages/* | |
EOF | |
npx npm-package-json-lint --quiet . | |
cat <<JSON > .npmpackagejsonlintrc.json | |
{ | |
"rules": { | |
"require-repository": "error", | |
"require-repository-directory": "error" | |
} | |
} | |
JSON | |
cat <<EOF > .npmpackagejsonlintignore | |
/package.json | |
EOF | |
npx npm-package-json-lint --quiet . | |
rm .npmpackagejsonlintrc.json .npmpackagejsonlintignore | |
echo "Done." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Required: