From 6e5d4c77d41f19d7f7c776262f8275f7aa5170a2 Mon Sep 17 00:00:00 2001 From: Alexander Wang Date: Fri, 21 Mar 2025 10:04:48 -0600 Subject: [PATCH] change env var to flag --- .github/workflows/daily.yml | 2 +- d2js/js/ci/build.sh | 54 +++++++++++++++++++++++++++---------- 2 files changed, 41 insertions(+), 15 deletions(-) diff --git a/.github/workflows/daily.yml b/.github/workflows/daily.yml index 1e259799c..0738b8b75 100644 --- a/.github/workflows/daily.yml +++ b/.github/workflows/daily.yml @@ -51,7 +51,7 @@ jobs: - name: Publish nightly version to NPM if: steps.check_changes.outputs.has_changes == 'true' - run: COLOR=1 PUBLISH=1 ./make.sh js + run: COLOR=1 ./make.sh --version=nightly js env: NPM_TOKEN: ${{ secrets.NPM_TOKEN }} GITHUB_TOKEN: ${{ secrets._GITHUB_TOKEN }} diff --git a/d2js/js/ci/build.sh b/d2js/js/ci/build.sh index 3d78c0433..7d8771bfd 100755 --- a/d2js/js/ci/build.sh +++ b/d2js/js/ci/build.sh @@ -18,26 +18,52 @@ fi cd d2js/js sh_c bun build.js -if [ "${PUBLISH:-0}" = "1" ]; then - echo "Publishing nightly version to NPM..." - - DATE_TAG=$(date +'%Y%m%d') - COMMIT_SHORT=$(git rev-parse --short HEAD) - CURRENT_VERSION=$(node -p "require('./package.json').version") - NIGHTLY_VERSION="${CURRENT_VERSION}-nightly.${DATE_TAG}.${COMMIT_SHORT}" - +if [ -n "$VERSION" ]; then cp package.json package.json.bak trap 'rm -f .npmrc; mv package.json.bak package.json' EXIT - echo "Updating package version to ${NIGHTLY_VERSION}" - npm version "${NIGHTLY_VERSION}" --no-git-tag-version + if [ "$VERSION" = "nightly" ]; then + echo "Publishing nightly version to npm..." - echo "Publishing to npm with tag 'nightly'..." + DATE_TAG=$(date +'%Y%m%d') + COMMIT_SHORT=$(git rev-parse --short HEAD) + CURRENT_VERSION=$(node -p "require('./package.json').version") + PUBLISH_VERSION="${CURRENT_VERSION}-nightly.${DATE_TAG}.${COMMIT_SHORT}" + NPM_TAG="nightly" + + echo "Updating package version to ${PUBLISH_VERSION}" + else + echo "Publishing official version ${VERSION} to npm..." + PUBLISH_VERSION="$VERSION" + NPM_TAG="latest" + + echo "Setting package version to ${PUBLISH_VERSION}" + fi + + # Update package.json with the new version + npm version "${PUBLISH_VERSION}" --no-git-tag-version + + echo "Publishing to npm with tag '${NPM_TAG}'..." if [ -n "${NPM_TOKEN-}" ]; then + # Create .npmrc file with auth token echo "//registry.npmjs.org/:_authToken=${NPM_TOKEN}" > .npmrc - trap 'rm -f .npmrc' EXIT - if npm publish --tag nightly; then - echo "Successfully published @terrastruct/d2@${NIGHTLY_VERSION} to npm with tag 'nightly'" + + if npm publish --tag "$NPM_TAG"; then + echo "Successfully published @terrastruct/d2@${PUBLISH_VERSION} to npm with tag '${NPM_TAG}'" + + # For official releases, bump the patch version + if [ "$VERSION" != "nightly" ]; then + # Restore original package.json first + mv package.json.bak package.json + + echo "Bumping version to ${VERSION}" + npm version "${VERSION}" --no-git-tag-version + git add package.json + git commit -m "Bump version to ${VERSION} [skip ci]" + + # Cancel the trap since we manually restored and don't want it to execute on exit + trap - EXIT + fi else echoerr "Failed to publish package to npm" exit 1