diff --git a/.github/workflows/daily.yml b/.github/workflows/daily.yml index 1a7075a1f..1e259799c 100644 --- a/.github/workflows/daily.yml +++ b/.github/workflows/daily.yml @@ -25,3 +25,34 @@ 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 + + - name: Check for changes + id: check_changes + run: | + if [ $(git rev-list --count --since="24 hours ago" HEAD) -gt 0 ]; then + echo "has_changes=true" >> $GITHUB_OUTPUT + echo "Found changes in the last 24 hours, proceeding to publish d2js nightly" + else + echo "has_changes=false" >> $GITHUB_OUTPUT + echo "No changes in the last 24 hours, skipping d2js nightly publish" + fi + + - uses: actions/setup-go@v4 + if: steps.check_changes.outputs.has_changes == 'true' + with: + go-version-file: ./go.mod + cache: true + + - name: Publish nightly version to NPM + if: steps.check_changes.outputs.has_changes == 'true' + 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..76f0132f3 100644 --- a/d2js/js/README.md +++ b/d2js/js/README.md @@ -29,6 +29,16 @@ pnpm add @terrastruct/d2 bun add @terrastruct/d2 ``` +### Nightly + +Use the `@nightly` tag to get the version that is built by daily CI on the master branch. + +For example, + +```bash +yarn add @terrastruct/d2@nightly +``` + ## Usage D2.js uses webworkers to call a WASM file. @@ -162,6 +172,16 @@ You can browse the examples by running the dev server: Visit `http://localhost:3000` to see the example page. +### Publishing + +TODO stable release publishing. + +Nightly builds are automated by CI 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..4775805e0 100644 --- a/d2js/js/package.json +++ b/d2js/js/package.json @@ -2,7 +2,7 @@ "name": "@terrastruct/d2", "author": "Terrastruct, Inc.", "description": "D2.js is a wrapper around the WASM build of D2, the modern text-to-diagram language.", - "version": "0.1.21", + "version": "0.1.22", "repository": { "type": "git", "url": "git+https://github.com/terrastruct/d2.git", @@ -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",