name: ~Lib / NPM Publish
on:
  workflow_call:
    inputs:
      access:
        required: false
        description: The package access ('restricted' or 'public')
        default: restricted
        type: string
      botEmail:
        required: false
        description: The email of the bot that will appear in the GitOps commit
        default: dev.bot@parcellab.com
        type: string
      botName:
        required: false
        description: The name of the bot that will appear in the GitOps commit
        default: parcellab-dev-bot
        type: string
      buildBeforePublish:
        required: false
        description: Build the package before publishing
        default: true
        type: string
      defaultBranch:
        required: false
        description: The default branch
        default: main
        type: string
      scope:
        required: false
        description: The npm organization (defaults to @parcellab)
        default: "@parcellab"
        type: string
      version:
        required: true
        description: The version to publish (without 'v')
        type: string
    secrets:
      githubAuthToken:
        required: false
      npmjsAuthToken:
        required: false
jobs:
  npm-publish:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout current git repository
        uses: actions/checkout@v3
        with:
          persist-credentials: false # otherwise, the token used is the GITHUB_TOKEN, instead of REPO_ACCESS_TOKEN
          fetch-depth: 0 # otherwise, there would be errors pushing refs to the destination repository
      - name: Install npm dependencies
        run: npm i
      - name: Use Node.js with Github Packages as registry url
        uses: actions/setup-node@v3
        with:
          registry-url: https://npm.pkg.github.com
          scope: ${{ inputs.scope }}
      - if: inputs.buildBeforePublishing
        name: Run build
        run: npm run build
      - name: Update package.json version to ${{ inputs.version }}
        uses: mikefarah/yq@v4.27.2
        with:
          cmd: |
            yq e '.version = "${{ inputs.version }}"' -i package.json -j
      - name: Commit new package.json version
        run: |
          git config --local user.email "${{ inputs.botEmail }}"
          git config --local user.name "${{ inputs.botName }}"
          git commit -m "chore: set version ${{ inputs.version }} [skip ci]" -a
      - name: Push changes to current git repository
        uses: ad-m/github-push-action@v0.6.0
        with:
          github_token: ${{ secrets.githubAuthToken }}
          branch: ${{ inputs.defaultBranch }}
      - name: Publish NPM package
        run: npm publish --access ${{ inputs.access }}
        env:
          NODE_AUTH_TOKEN: ${{ secrets.githubAuthToken }}
      - if: inputs.access == 'public'
        name: Use Node.js with NPMjs as registry url
        uses: actions/setup-node@v3
        with:
          registry-url: https://registry.npmjs.org
          scope: ${{ inputs.scope }}
      - if: inputs.access == 'public'
        name: Publish NPM package
        run: npm publish --access ${{ inputs.access }}
        env:
          NODE_AUTH_TOKEN: ${{ secrets.npmjsAuthToken }}
