name: publish

on: [push]

jobs:
  publish:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    - uses: actions/setup-node@v1
      with:
        node-version: 16
    - name: Lint JavaScript source with standard.js
      run: |
        if [ ! -z "$NPM_PROXY" ]; then
          npm --registry=$NPM_PROXY install standard -g
        else
          npm install standard -g
        fi        
        standard
    - name: Configure git
      run: |
        git config --global user.email "github-actions[bot]@users.noreply.github.com"
        git config --global user.name "github-actions[bot]"
    - name: Increase version
      run: npm version patch
    - name: Run test suite
      run: |
        export RESPONSE_PATH=`pwd`/documentation
        export SCREENSHOT_PATH=`pwd`/documentation/screenshots
        npm install -D --silent --no-audit --no-fund &>/dev/null 
        npm install sqlite3 --silent --no-save --no-audit --no-fund &>/dev/null 
        # node --expose-gc --max-old-space-size=4096 ./node_modules/.bin/mocha --ignore 'node_modules/**/*' --timeout 480000 --slow 480000 --recursive --extension .test.js . 2>&1 | tee tests.txt
        # TESTS=`cat tests.txt`
        # if [[ "$TESTS" == *" 1)"* ]]; then
        #   exit 1
        # fi
      env:
        NODE_ENV: testing
        STORAGE: sqlite 
        DATABASE: test
        GENERATE_RESPONSE: true
        GENERATE_SCREENSHOTS: true
        SCREENSHOT_LANGUAGES: en
        NODE_OPTIONS: --preserve-symlinks
    - name: Generate supporting text files
      run: node main.js
      env:
        NODE_ENV: testing
        EXIT_ON_START: true
        GENERATE_SITEMAP_TXT: true
        GENERATE_API_TXT: true
        GENERATE_ENV_TXT: true
    - name: Publish to Github
      uses: stefanzweifel/git-auto-commit-action@v4.1.6
      with:
        commit_message: Automatically regenerated against current version
        branch: ${{ github.head_ref }}
        commit_options: "--no-verify --signoff"
        repository: .
    - name: Publish to NPM
      uses: JS-DevTools/npm-publish@v1
      with:
        token: ${{ secrets.NPM_TOKEN }}
        access: 'public'
    # - name: Publish documentation assets
    #   uses: peaceiris/actions-gh-pages@v3
    #   with:
    #     external_repository: "layeredapps/layeredapps.github.io"
    #     personal_token: ${{ secrets.DEPLOY_TOKEN }}
    #     publish_dir: documentation
    #     publish_branch: main
    #     keep_files: true
