Skip to content

Commit

Permalink
fix(plugins): npm/post optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
dalisoft committed Apr 26, 2024
1 parent cc7085e commit b09c380
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 35 deletions.
71 changes: 37 additions & 34 deletions plugins/npm-post.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ prepare() {
log_verbose "Git username [$GIT_USERNAME] and Git e-mail [$GIT_EMAIL] set"
fi

if [ -z "${GPG_NO_SIGN-}" ] && [ -n "${GPG_KEY_ID-}" ]; then
if [ -z "${GPG_NO_SIGN-}" ] && [ -n "${GPG_KEY-}" ] && [ -n "${GPG_KEY_ID-}" ]; then
git config --local commit.gpgsign true
git config --local user.signingkey "$GPG_KEY_ID"
git config --local tag.forceSignAnnotated true
Expand All @@ -24,7 +24,7 @@ prepare() {
log_verbose "Git GPG key import skipped"
fi

if [ -n "${GPG_PASSPHRASE}" ]; then
if [ -n "${GPG_PASSPHRASE-}" ]; then
echo "$GPG_PASSPHRASE" | gpg --quiet --batch --yes --pinentry-mode loopback --sign --local-user "${GPG_KEY_ID-}" --passphrase-fd 0 >/dev/null
log_verbose "Git GPG passphrase set"
fi
Expand All @@ -38,22 +38,20 @@ cleanup() {
log_verbose "Git username and Git e-mail unset"
fi

if [ -z "${GPG_NO_SIGN-}" ] && [ -n "${GPG_KEY_ID-}" ]; then
if [ -z "${GPG_NO_SIGN-}" ] && [ -n "${GPG_KEY-}" ] && [ -n "${GPG_KEY_ID-}" ]; then
git config --local --unset commit.gpgsign
git config --local --unset user.signingkey
git config --local --unset tag.forceSignAnnotated
git config --local --unset gpg.program
log_verbose "Git GPG sign and key ID [$GPG_KEY_ID] are unset"

if gpg --list-keys | grep -q "${GPG_KEY_ID-}"; then
if [ -n "${GPG_PASSPHRASE}" ]; then
if [ -n "${GPG_PASSPHRASE-}" ]; then
echo "$GPG_PASSPHRASE" | gpg --quiet --batch --yes --passphrase-fd 0 --delete-secret-and-public-key "$GPG_KEY_ID" >/dev/null
else
gpg --quiet --batch --yes --delete-secret-and-public-key "$GPG_KEY_ID"
fi
log_verbose "Git GPG key deleted"
else
log_verbose "Git GPG key delete skipped"
fi

log_verbose "Git GPG config cleanup"
Expand All @@ -66,41 +64,46 @@ release() {
# Committing a `npm` tag
log "Committing npm tag..."
log_verbose "Git hash: $CHECKOUT_SHA!"

# Don't load this plugin if
# - `--dry-run` used
# - `package.json` is missing
# - `package.json` is not changed on `Git` tracking
if ! $IS_DRY_RUN && [ -f package.json ] && [ -n "$(git diff --name-only package.json 2>/dev/null)" ]; then
prepare
git add package.json

if $IS_WORKSPACE; then
if [ -z "${GPG_NO_SIGN-}" ] && [ -n "${GPG_KEY_ID-}" ]; then
git commit --sign -m "chore(${PKG_NAME}): update \`package.json\` version to ${NEXT_RELEASE_VERSION}"
else
git commit --no-gpg-sign -m "chore(${PKG_NAME}): update \`package.json\` version to ${NEXT_RELEASE_VERSION}"
if [ -n "${NPM_TOKEN-}" ]; then

# Don't load this plugin if
# - `--dry-run` used
# - `package.json` is missing
# - `package.json` is not changed on `Git` tracking
if ! $IS_DRY_RUN; then
if [ ! -f package.json ] || [ -z "$(git diff --name-only package.json 2>/dev/null)" ]; then
log "Project does not have package.json or package.json not changed"
return 1
fi
else
if [ -z "${GPG_NO_SIGN-}" ] && [ -n "${GPG_KEY_ID-}" ]; then
git commit --sign -m "chore: update \`package.json\` version to ${NEXT_RELEASE_VERSION}"
prepare
git add package.json

if $IS_WORKSPACE; then
if [ -z "${GPG_NO_SIGN-}" ] && [ -n "${GPG_KEY-}" ] && [ -n "${GPG_KEY_ID-}" ]; then
git commit --sign -m "chore(${PKG_NAME}): update \`package.json\` version to ${NEXT_RELEASE_VERSION}"
else
git commit --no-gpg-sign -m "chore(${PKG_NAME}): update \`package.json\` version to ${NEXT_RELEASE_VERSION}"
fi
else
git commit --no-gpg-sign -m "chore: update \`package.json\` version to ${NEXT_RELEASE_VERSION}"
if [ -z "${GPG_NO_SIGN-}" ] && [ -n "${GPG_KEY-}" ] && [ -n "${GPG_KEY_ID-}" ]; then
git commit --sign -m "chore: update \`package.json\` version to ${NEXT_RELEASE_VERSION}"
else
git commit --no-gpg-sign -m "chore: update \`package.json\` version to ${NEXT_RELEASE_VERSION}"
fi
fi
fi

CHECKOUT_SHA=$(git rev-parse HEAD)
CHECKOUT_SHA=$(git rev-parse HEAD)
log "Committed npm [$NEXT_RELEASE_TAG] tag"

if [ -n "$GIT_REMOTE_ORIGIN" ]; then
git push
log_verbose "Pushed update to remote"
cleanup
else
log_verbose "No Git remote to push tag"
log "Skipped committing npm [$NEXT_RELEASE_TAG] tag in DRY-RUN mode."
fi
log "Committed npm [$NEXT_RELEASE_TAG] tag"

cleanup
else
log "Skipped committing npm [$NEXT_RELEASE_TAG] tag in DRY-RUN mode."
echo "
npm Token is not found
Please export npm Token so this plugin can be used
"
exit 1
fi
}
7 changes: 6 additions & 1 deletion plugins/npm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ release() {
# Don't load this plugin if
# - `--dry-run` used
# - `package.json` is missing
if ! $IS_DRY_RUN && [ -f package.json ]; then
if ! $IS_DRY_RUN; then
if [ ! -f package.json ]; then
log "Project does not have package.json"
return 1
fi

TEMP_FILE=$(mktemp)
printf "%s\n" "//registry.npmjs.org/:_authToken=$NPM_TOKEN" >>"$TEMP_FILE"

Expand Down

0 comments on commit b09c380

Please sign in to comment.