stages:
  - build-web
  - build-desktop

variables:
  npm_config_cache: "$CI_PROJECT_DIR/.npm"
  ELECTRON_CACHE: "$CI_PROJECT_DIR/.cache/electron"

cache:
  key: ${CI_COMMIT_REF_SLUG}
  paths:
    - pwa/node_modules/
    - pwa/.npm/
    - pwa/.cache/

# Build the web application first
build:web:
  stage: build-web
  image: node:20-alpine
  script:
    - cd pwa
    - npm install
    - npm run build
  artifacts:
    paths:
      - pwa/dist/
    expire_in: 1 hour
  rules:
    - if: '$CI_COMMIT_TAG'
  tags:
    - docker

# Build Linux AppImage and .deb
build:linux:
  stage: build-desktop
  image: electronuserland/builder:wine
  dependencies:
    - build:web
  script:
    - cd pwa
    # Clean install to fix optional dependencies issue
    - rm -rf node_modules package-lock.json
    - npm install
    - npm run electron:build:linux
  artifacts:
    paths:
      - pwa/dist-electron/*.AppImage
      - pwa/dist-electron/*.deb
    expire_in: 1 week
  rules:
    - if: '$CI_COMMIT_TAG'
  tags:
    - docker

# Build Windows executable
build:windows:
  stage: build-desktop
  image: electronuserland/builder:wine
  dependencies:
    - build:web
  script:
    - cd pwa
    # Clean install to fix optional dependencies issue
    - rm -rf node_modules package-lock.json
    - npm install
    - npm run electron:build:win
  artifacts:
    paths:
      - pwa/dist-electron/*.exe
      - pwa/dist-electron/*.msi
    expire_in: 1 week
  rules:
    - if: '$CI_COMMIT_TAG'
  tags:
    - docker
