name: 'Build and Publish to NPM'

on:
  release:
    types: [created]

permissions:
  contents: read
  id-token: write

jobs:
  build-publish:
    environment: NPM_TOKEN_NIK
    runs-on: ubuntu-latest

    steps:
      - name: checking repository out
        uses: actions/checkout@v2
        with:
          ref: ${{ github.ref }}
          path: .

      - name: Use Node.js 22.14.0
        uses: actions/setup-node@v6
        with:
          node-version: 22.14.0
          registry-url: 'https://registry.npmjs.org'
      
      - name: Use npm 11 for trusted publishing
        run: npm install -g npm@11.15.0

      - name: installing npm dependencies
        run: npm install
      
      - name: build web app
        run: npm run build:webapp
      
      - name: Build phone app
        run: CI=false npm run build

      - name: move script tag before body
        run: node move-script-tag.js

      - name: inline index.html
        run: script --return --quiet --command "npx inliner build/index.html > index-inline.html"
      
      - name: remove build folder
        run: rm -rf build
      
      - name: create build folder
        run: mkdir build

      - name: copy index-inline.html to build/index.html
        run: cp index-inline.html build/index.html

      - name: publishing to NPM
        run: npm publish --access public
