# Publishes the MathType integrations front-end packages to npm
#
# Jobs:
# - Publish
#
name: Publish

# Controls when the action will run
on:
  # Ideally we would want this to run on every PR, but GitHub Actions doesn't offer a way to set default values for the
  # input arguments of the execution.
  # pull_request:
  workflow_dispatch:
    inputs:
      publish_npm:
        description: |
          Whether to publish the frontend packages to npm.
          Type `publish_npm` to do publish to npm, any other value otherwise.
          WARNING: This action cannot be undone.
        required: false
        default: ""
      versions:
        description: |
          Space-separated list of versions to publish, e.g.: "ckeditor5=7.27.0 devkit=1.6.1 viewer=1.1.0"
          Packages not mentioned in the list will stay at their current version and will not be published.
          Packages will be published in the order they are given.
        required: false
        default: ""

# Array of jobs to run in this workflow
jobs:
  publish:
    runs-on: ubuntu-latest

    # By default all npm publishing actions in this pipeline will not be executed.
    # They will only be executed when the publish_npm flag is set to "publish_npm".
    if: github.event.inputs.publish_npm == 'publish_npm'

    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Set up a private npm registry and publish
        run: |

          # Add the token to publish to the npm registry
          npm set "//registry.npmjs.org/:_authToken" ${{ secrets.NPM_TOKEN }}

          # Call the script that interacts with the yarn CLI
          ./scripts/publish.mjs ${{ github.event.inputs.versions }}

  publish-wordpress:
    runs-on: ubuntu-latest

    # If we want to publish the WordPress plugin, which is not needed on npm.
    if: contains(github.event.inputs, 'wordpress')

    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v6
        with:
          node-version: ${{ vars.NODE_VERSION }}

      - name: Checkout
        uses: actions/checkout@v4

      # Install dependencies and create compiled files
      - name: Build the JavaScript
        run: |
          yarn install
          nx build wordpress
      - name: Create Icons folder inside the build one
        run: |
          cp -a packages/wordpress/src/icons/ packages/wordpress/build/icons/

      # Create a zip file with the files needed to integrate
      # Mathtype for WordPress
      - name: Zip integration
        uses: vimtor/action-zip@v1
        with:
          files: build/ VERSION
          dest: "mathtype-tinymce-wordpress_${{ github.event.inputs.version }}.zip"
      - uses: vimtor/action-zip@v1
        with:
          files: wordpress-wiris-plugin/tiny_mce_wiris.php
          dest: "wordpress-wiris_${{ github.event.inputs.version }}.zip"

      # Release WordPress plugin
      - name: Release on GitHub
        uses: marvinpinto/action-automatic-releases@latest
        with:
          repo_token: ${{ secrets.GITHUB_TOKEN }}
          automatic_release_tag: "latest"
          prerelease: true
          title: "CI"
          files: |
            *.zip
