Skip to content

Instantly share code, notes, and snippets.

@ybiquitous
Last active August 16, 2021 04:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ybiquitous/48f69d29194ed1aecf9b85097da51715 to your computer and use it in GitHub Desktop.
Save ybiquitous/48f69d29194ed1aecf9b85097da51715 to your computer and use it in GitHub Desktop.
Add `repository.directory` field to all `package.json` files
#!/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."
@ybiquitous
Copy link
Author

ybiquitous commented Aug 16, 2021

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