name: "Build And Deploy JS"
on:
  push:
    branches:
      - master

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3 # Check out the repository

      - name: Setup Node
        uses: actions/setup-node@v6
        with:
          node-version: '24'

      - name: Cache npm
        uses: actions/cache@v5
        with:
          path: ~/.npm
          key: ${{ runner.os }}-node-${{ hashFiles('**/package.json') }}

      - run: npm ci # Install dependencies

      - run: npm run build # Build the site

      # Store the dist folder
      - uses: actions/upload-artifact@v7
        with:
          name: app-build
          path: dist

  deploy:
    needs: [ build ]
    runs-on: ubuntu-latest
    permissions:
      contents: write
      id-token: write  # Required for OIDC
    steps:
      - name: Checkout code
        uses: actions/checkout@v6
        with:
          fetch-depth: 0

      - name: Release
        id: release
        uses: justincy/github-action-npm-release@2.0.2

        # Setup .npmrc file to publish to npm
      - uses: actions/setup-node@v6
        with:
          node-version: '24'
          registry-url: 'https://registry.npmjs.org'

        # Get the dist folder
      - name: Download build artifact
        uses: actions/download-artifact@v5
        with:
          name: app-build
          path: dist

      - run: npm publish --access public --provenance

