diff --git a/.github/workflows/daily.yml b/.github/workflows/daily.yml index 1a7075a1f..1275cfb66 100644 --- a/.github/workflows/daily.yml +++ b/.github/workflows/daily.yml @@ -25,3 +25,21 @@ jobs: with: name: d2chaos path: ./d2chaos/out + npm-nightly: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + with: + fetch-depth: 0 # Needed for git history and version tags + + - uses: actions/setup-go@v4 + with: + go-version-file: ./go.mod + cache: true + + - name: Publish nightly version to NPM + run: COLOR=1 PUBLISH=1 ./make.sh js + env: + NPM_TOKEN: ${{ secrets.NPM_TOKEN }} + GITHUB_TOKEN: ${{ secrets._GITHUB_TOKEN }} + DISCORD_WEBHOOK_URL: ${{ secrets.DISCORD_WEBHOOK_URL }} diff --git a/d2js/js/README.md b/d2js/js/README.md index 55cb9e159..4cf7cf016 100644 --- a/d2js/js/README.md +++ b/d2js/js/README.md @@ -162,6 +162,16 @@ You can browse the examples by running the dev server: Visit `http://localhost:3000` to see the example page. +### Publishing + +WIP. + +Nightly builds are automated by CI upon pull request merging by running: + +```bash +PUBLISH=1 ./make.sh build +``` + ## Contributing Contributions are welcome! diff --git a/d2js/js/ci/build.sh b/d2js/js/ci/build.sh index f081d8e4f..3d78c0433 100755 --- a/d2js/js/ci/build.sh +++ b/d2js/js/ci/build.sh @@ -17,3 +17,33 @@ 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}" + + 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 + + echo "Publishing to npm with tag 'nightly'..." + if [ -n "${NPM_TOKEN-}" ]; then + 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'" + else + echoerr "Failed to publish package to npm" + exit 1 + fi + else + echoerr "NPM_TOKEN environment variable is required for publishing to npm" + exit 1 + fi +fi diff --git a/d2js/js/package.json b/d2js/js/package.json index a876c24be..0c3f84c6c 100644 --- a/d2js/js/package.json +++ b/d2js/js/package.json @@ -41,7 +41,7 @@ "test:integration": "bun test test/integration", "test:all": "bun run test && bun run test:integration", "dev": "bun --watch dev-server.js", - "prepublishOnly": "./make.sh all" + "prepublishOnly": "PUBLISH=0 ./make.sh all" }, "keywords": [ "d2",