image: node:18

cache:
  paths:
    - node_modules/

before_script:
  - npm --color="always" install --force

build:
  stage: build
  script:
    - npx tsc
    # Uses CI_COMMIT_TAG
    - NODE_ENV=production npx rollup --config

    - node bin/nxapi.js util validate-discord-titles

    - export VERSION=`[ "$CI_COMMIT_BRANCH" != "main" ] || node resources/build/ci-main-version.js`
    - node resources/build/ci-package-json.js
    - PACKAGE=`npm --color="always" pack`
    - mv "$PACKAGE" nxapi.tgz
  artifacts:
    paths:
      - dist
      - nxapi.tgz

build-docker:
  stage: build
  before_script:
    - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" "$CI_REGISTRY"
    - |
      [ "$DH_REGISTRY_IMAGE" != "" ] && docker login -u "$DH_REGISTRY_USER" -p "$DH_REGISTRY_PASSWORD" "$DH_REGISTRY"
    - |
      [ "$GH_REGISTRY_IMAGE" != "" ] && docker login -u "$GH_REGISTRY_USER" -p "$GH_REGISTRY_PASSWORD" "$GH_REGISTRY"
  script:
    - node resources/build/ci-package-json.js

    - docker build --pull --no-cache --tag "$CI_REGISTRY_IMAGE:ref-$CI_COMMIT_REF_SLUG" .
    - |
      if [ "$CI_COMMIT_TAG" = "" ]; then
        docker push "$CI_REGISTRY_IMAGE:ref-$CI_COMMIT_REF_SLUG"
      fi

    - |
      if [ "$DH_REGISTRY_IMAGE" != "" ] && [ "$CI_COMMIT_TAG" = "" ]; then
        docker tag "$CI_REGISTRY_IMAGE:ref-$CI_COMMIT_REF_SLUG" "$DH_REGISTRY_IMAGE:ref-$CI_COMMIT_REF_SLUG"
        docker push "$DH_REGISTRY_IMAGE:ref-$CI_COMMIT_REF_SLUG"
      fi

    - |
      if [ "$GH_REGISTRY_IMAGE" != "" ] && [ "$CI_COMMIT_TAG" = "" ]; then
        docker tag "$CI_REGISTRY_IMAGE:ref-$CI_COMMIT_REF_SLUG" "$GH_REGISTRY_IMAGE:ref-$CI_COMMIT_REF_SLUG"
        docker push "$GH_REGISTRY_IMAGE:ref-$CI_COMMIT_REF_SLUG"
      fi

    - |
      if [ "$CI_COMMIT_TAG" != "" ]; then
        docker tag "$CI_REGISTRY_IMAGE:ref-$CI_COMMIT_REF_SLUG" "$CI_REGISTRY_IMAGE:$CI_COMMIT_TAG"
        docker push "$CI_REGISTRY_IMAGE:$CI_COMMIT_TAG"
        docker tag "$CI_REGISTRY_IMAGE:ref-$CI_COMMIT_REF_SLUG" "$CI_REGISTRY_IMAGE:latest"
        docker push "$CI_REGISTRY_IMAGE:latest"
      fi

    - |
      if [ "$CI_COMMIT_TAG" != "" ] && [ "$DH_REGISTRY_IMAGE" != "" ]; then
        docker tag "$CI_REGISTRY_IMAGE:ref-$CI_COMMIT_REF_SLUG" "$DH_REGISTRY_IMAGE:$CI_COMMIT_TAG"
        docker push "$DH_REGISTRY_IMAGE:$CI_COMMIT_TAG"
        docker tag "$CI_REGISTRY_IMAGE:ref-$CI_COMMIT_REF_SLUG" "$DH_REGISTRY_IMAGE:latest"
        docker push "$DH_REGISTRY_IMAGE:latest"
      fi

    - |
      if [ "$CI_COMMIT_TAG" != "" ] && [ "$GH_REGISTRY_IMAGE" != "" ]; then
        docker tag "$CI_REGISTRY_IMAGE:ref-$CI_COMMIT_REF_SLUG" "$GH_REGISTRY_IMAGE:$CI_COMMIT_TAG"
        docker push "$GH_REGISTRY_IMAGE:$CI_COMMIT_TAG"
        docker tag "$CI_REGISTRY_IMAGE:ref-$CI_COMMIT_REF_SLUG" "$GH_REGISTRY_IMAGE:latest"
        docker push "$GH_REGISTRY_IMAGE:latest"
      fi
  tags:
    - docker
  only:
    variables:
      - $BUILD_DOCKER_IMAGE == "true"
  cache:
    policy: pull

build-app:
  stage: build
  script:
    - node resources/build/ci-package-json.js

    - npx electron-builder build --macos zip:x64 zip:arm64 --linux deb snap appimage --publish never
    - mv dist/app/package app
  needs:
    - job: build
  artifacts:
    paths:
      - app
    exclude:
      - app/mac/**/*
      - app/mac-arm64/**/*
      - app/linux-unpacked/**/*
  only:
    variables:
      - $BUILD_APP == "true"
  cache:
    paths:
      - node_modules/
    policy: pull

build-windows:
  stage: build
  image: electronuserland/builder:wine
  script:
    - node resources/build/ci-package-json.js

    - npx electron-builder build --win nsis --publish never
    - mv dist/app/package app
  needs:
    - job: build
  artifacts:
    paths:
      - app
    exclude:
      - app/win-unpacked/**/*
  only:
    variables:
      - $BUILD_WINDOWS_APP == "true"
  cache:
    paths:
      - node_modules/
    policy: pull

publish-npm:
  stage: deploy
  script:
    - node resources/build/ci-package-json.js

    - echo "//registry.npmjs.org/:_authToken=${NPM_TOKEN}" > .npmrc
    - npm --color="always" publish
  needs:
    - build
  only:
    refs:
      - /^v.*$/
    variables:
      - $NPM_TOKEN
  except:
    - branches
  cache:
    paths:
      - node_modules/
    policy: pull

publish-gitlab:
  stage: deploy
  script:
    # Update the package.json and package-lock.json
    - node resources/build/ci-package-json.js gitlab

    - echo "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/npm/:_authToken=${CI_JOB_TOKEN}" | sed s/^https:// > .npmrc
    - npm --color="always" --registry=${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/npm/ publish
  needs:
    - build
  only:
    refs:
      - /^v.*$/
    variables:
      - $GITLAB_NPM_PUBLISH == "true"
      - $GITLAB_NPM_PACKAGE_NAME
  except:
    - branches
  cache:
    paths:
      - node_modules/
    policy: pull

publish-github:
  stage: deploy
  script:
    # Update the package.json and package-lock.json
    - node resources/build/ci-package-json.js github

    - echo "//npm.pkg.github.com/:_authToken=${GITHUB_NPM_TOKEN}" > .npmrc
    - npm --color="always" --registry=https://npm.pkg.github.com/ publish
  needs:
    - build
  only:
    refs:
      - /^v.*$/
    variables:
      - $GITHUB_REPOSITORY
      - $GITHUB_NPM_PACKAGE_NAME
      - $GITHUB_NPM_TOKEN
  except:
    - branches
  cache:
    paths:
      - node_modules/
    policy: pull

publish-next:
  stage: deploy
  script:
    - export VERSION=`[ "$CI_COMMIT_BRANCH" != "main" ] || node resources/build/ci-main-version.js`
    - node resources/build/ci-package-json.js

    - |
      if [ "$CI_COMMIT_BRANCH" = "main" -a "$VERSION" != "" ]; then
        if [ "$NPM_PUBLISH_NEXT" = "true" -a "$NPM_TOKEN" != "" ]; then
          echo "Publishing $VERSION to npm"
          # Prerelease version was already written to package.json
          echo "//registry.npmjs.org/:_authToken=${NPM_TOKEN}" > .npmrc
          npm --color="always" publish --tag next
        fi

        if [ "$GITLAB_NPM_PUBLISH_NEXT" = "true" -a "$GITLAB_NPM_PACKAGE_NAME" != "" ]; then
          echo "Publishing $VERSION to GitLab"
          node resources/build/ci-package-json.js gitlab

          echo "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/npm/:_authToken=${CI_JOB_TOKEN}" | sed s/^https:// > .npmrc
          npm --color="always" --registry=${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/npm/ publish --tag next
        fi

        if [ "$GITHUB_NPM_PUBLISH_NEXT" = "true" -a "$GITHUB_NPM_PACKAGE_NAME" != "" -a "$GITHUB_REPOSITORY" != "" -a "$GITHUB_NPM_TOKEN" != "" ]; then
          echo "Publishing $VERSION to GitHub"
          node resources/build/ci-package-json.js github

          echo "//npm.pkg.github.com/:_authToken=${GITHUB_NPM_TOKEN}" > .npmrc
          npm --color="always" --registry=https://npm.pkg.github.com/ publish --tag next
        fi
      fi
  needs:
    - build
  only:
    refs:
      - main
  cache:
    paths:
      - node_modules/
    policy: pull
