# This workflow will do a clean install of node dependencies, cache/restore them, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions

name: Semantic Release

#on:
#  push:
#    branches: [ master ]

on:
  workflow_dispatch

jobs:
  build:

    runs-on: ubuntu-latest

    strategy:
      matrix:
        node-version: [16.x]
        # See supported Node.js release schedule at https://nodejs.org/en/about/releases/

    steps:    
    - uses: actions/checkout@v2
      with:
        fetch-depth: 0
    - name: Use Node.js ${{ matrix.node-version }}
      uses: actions/setup-node@v2
      with:
        node-version: ${{ matrix.node-version }}
        cache: 'npm'
        
    - name: Cache node modules
      uses: actions/cache@v2
      env:
        cache-name: cache-node-modules
      with:
        # npm cache files are stored in `~/.npm` on Linux/macOS
        path: ~/.npm
        key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }}
        restore-keys: |
          ${{ runner.os }}-build-${{ env.cache-name }}-
          ${{ runner.os }}-build-
          ${{ runner.os }}-
        
    - run: npm ci
    - run: npm run build --if-present
    - run: zip build.zip $(find build)

    - name: Generate version
      id: version
      uses: paulhatch/semantic-version@v4.0.2
      with:
        # The prefix to use to identify tags
        tag_prefix: "v"
        # A string which, if present in a git commit, indicates that a change represents a
        # major (breaking) change, supports regular expressions wrapped with '/'
        major_pattern: "(MAJOR)"
        # Same as above except indicating a minor change, supports regular expressions wrapped with '/'
        minor_pattern: "(MINOR)"
        # Indicate whether short tags like 'v1' should be supported. If false only full
        # tags like 'v1.0.0' will be recognized.
        short_tags: false
        # A string to determine the format of the version output
        format: "${major}.${minor}.${patch}"
        # If this is set to true, *every* commit will be treated as a new version.
        bump_each_commit: false

    - name: Create Release
      id: create_release
      uses: softprops/action-gh-release@v0.1.13
      env:
        GITHUB_TOKEN: ${{ github.token }}
      with:
        tag_name: ${{ steps.version.outputs.version_tag }}
        name: ${{ steps.version.outputs.version }}
        draft: false
        prerelease: false
        fail_on_unmatched_files: true
        files: |
          build.zip
          README.md
          LICENSE
