name: publish

on: [push]

jobs:
  publish:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    - uses: actions/setup-node@v1
      with:
        node-version: 14
    - 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
    - uses: supercharge/mongodb-github-action@1.3.0
      with:
        mongodb-version: 4.4.2
    - uses: zhulik/redis-action@1.1.0
      with:
        redis version: 5
        number of databases: 1
    - name: Optionally install Chromium via APT instead of puppeteer
      run: |
        if [ ! -z "$PUPPETEER_SKIP_CHROMIUM_DOWNLOAD" ]; then
          APT_INSTALL="chromium gconf-service libasound2 libatk1.0-0 libc6 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 ca-certificates fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils wget"
          if [ ! -z "$APT_PROXY" ]; then
            echo "deb $APT_PROXY buster main" > /etc/apt/sources.list
          fi
          apt-get update
          apt-get install -y $APT_INSTALL
        fi
    - name: Configure git and increase version
      run: |
        if [ ! -z "$INSTALL_GIT" ]; then
          if [ ! -z "$APT_PROXY" ]; then
            echo "deb $APT_PROXY buster main" > /etc/apt/sources.list
          fi
          if [ -z "$PUPPETEER_SKIP_CHROMIUM_DOWNLOAD" ]; then
            apt-get update
          fi
          apt-get install -y git
        fi
        git config --local user.email "action@github.com"
        git config --local user.name "GitHub Action"
        npm version patch
    - name: Set up project
      run: |
        HERE=`pwd`
        mkdir /tmp/project
        cd /tmp/project
        if [ -d /opt/hostedtoolcache/node/ ]; then
          alias node=`find /opt/hostedtoolcache/node/ -type f -name node`
          alias npm=`find /opt/hostedtoolcache/node/ -type f -name npm`
        fi
        npm init -y
        npm install "$HERE" @userdashboard/dashboard @userdashboard/organizations @userdashboard/storage-redis faker puppeteer mocha
        PACKAGE=`cat package.json`
        PACKAGE=${PACKAGE/\"author\"\: \"\"/\"dashboard\": \{ \"modules\"\: \[\"@userdashboard/organizations\"\] \}}
        echo $PACKAGE > package.json
        echo "global.applicationPath = __dirname" > main.js
        echo "require('@userdashboard/storage-mongodb/test-helper.js')" >> main.js
    - name: Test as only storage for Dashboard and modules
      run: |
        ./node_modules/.bin/mocha --file main.js --bail --exit --recursive --timeout 90000000 $(find ./node_modules/@userdashboard/*/ -name "*.test.js" | grep -i -v [a-z]/node_modules | sort -k5.1,8.1 -t /) | tee tests.txt
        TESTS=`cat tests.txt`
        if [[ "$TESTS" == *" 1)"* ]]; then
          exit 1
        fi
      working-directory: /tmp/project
      env:
        NODE_ENV: testing
        STORAGE: "@userdashboard/storage-mongodb"
        MONGODB_URL: mongodb://localhost:27017
        NODE_OPTIONS: --preserve-symlinks
    - name: Test as Dashboard storage with module on other type
      run: |
        ./node_modules/.bin/mocha --file main.js --bail --exit --recursive --timeout 90000000 $(find ./node_modules/@userdashboard/*/ -name "*.test.js" | grep -i -v [a-z]/node_modules | sort -k5.1,8.1 -t /) | tee tests.txt
        TESTS=`cat tests.txt`
        if [[ "$TESTS" == *" 1)"* ]]; then
          exit 1
        fi
      working-directory: /tmp/project
      env:
        NODE_ENV: testing
        STORAGE: "@userdashboard/storage-mongodb"
        MONGODB_URL: mongodb://localhost:27017
        ORGANIZATIONS_STORAGE: "@userdashboard/storage-redis"
        ORGANIZATIONS_REDIS_URL: redis://localhost:6379
        NODE_OPTIONS: --preserve-symlinks
    - name: Test as module storage with Dashboard on other type
      run: |
        ./node_modules/.bin/mocha --file main.js --bail --exit --recursive --timeout 90000000 $(find ./node_modules/@userdashboard/*/ -name "*.test.js" | grep -i -v [a-z]/node_modules | sort -k5.1,8.1 -t /) | tee tests.txt
        TESTS=`cat tests.txt`
        if [[ "$TESTS" == *" 1)"* ]]; then
          exit 1
        fi
      working-directory: /tmp/project
      env:
        NODE_ENV: testing
        STORAGE: "@userdashboard/storage-redis"
        REDIS_URL: redis://localhost:6379
        ORGANIZATIONS_STORAGE: "@userdashboard/storage-mongodb"
        ORGANIZATIONS_MONGODB_URL: mongodb://localhost:27017
        NODE_OPTIONS: --preserve-symlinks
    - name: Test as Dashboard and module storage using separate servers
      run: |
        ./node_modules/.bin/mocha --file main.js --bail --exit --recursive --timeout 90000000 $(find ./node_modules/@userdashboard/*/ -name "*.test.js" | grep -i -v [a-z]/node_modules | sort -k5.1,8.1 -t /) | tee tests.txt
        TESTS=`cat tests.txt`
        if [[ "$TESTS" == *" 1)"* ]]; then
          exit 1
        fi
      working-directory: /tmp/project
      env:
        NODE_ENV: testing
        STORAGE: "@userdashboard/storage-mongodb"
        MONGODB_URL: "mongodb://localhost:27017/database1"
        ORGANIZATIONS_STORAGE: "@userdashboard/storage-mongodb"
        ORGANIZATIONS_MONGODB_URL: "mongodb://localhost:27017/database2"
        NODE_OPTIONS: --preserve-symlinks
    - 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 }} 