name: Publish
on:
  schedule:
  - cron: '0 0 * * *'
jobs:
  release:
    name: Packages
    if: github.repository == 'AssemblyScript/assemblyscript'
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v1
      with:
        ref: master
    - uses: dcodeIO/setup-node-nvm@master
      with:
        node-version: current
    - name: Install dependencies
      run: npm ci
    - name: Build distribution files
      run: |
        npm run clean
        npm run build
    - name: Test distribution files
      run: npm test
    - name: Make semantic release
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
      # On success, semantic-release will update the version and publish,
      # triggering the postversion script that'll update the loader's version
      # as well. If nothing was published, the version will still be '0.0.0'.
      run: |
        node node_modules/semantic-release/bin/semantic-release.js --unstable
        cd lib/loader
        if [ $(node -pe "require('./package.json').version") != "0.0.0" ]; then
          npm config set "//registry.npmjs.org/:_authToken=${NPM_TOKEN}"
          npm publish --access public
        fi
        cd ../..
