version: 2

defaults: &defaults
  working_directory: ~/repo
  docker:
    - image: circleci/node:8.10-browsers

jobs:
  test-node:
    <<: *defaults
    steps:
      - checkout
      # For some reason phantomjs-prebuild is failing w/yarn, but npm installing works
      - run: 'npm install phantomjs-prebuilt'
      - run: 'yarn install'
      - run: 'yarn lint:ci'
      - run: 'yarn build:ci'
      - run: 'yarn test:node --maxWorkers=4'
      - run: './scripts/pr-parser-preview.sh'
      - store_artifacts:
          path: tmp/artifacts
      - persist_to_workspace:
          root: ~/repo
          paths: .

  test-web:
    <<: *defaults
    steps:
      - attach_workspace:
          at: ~/repo
      - checkout
      - run: 'yarn install'
      - run: 'yarn add karma-cli --dev'
      - run: 'yarn test:web --maxWorkers=4'
      - run: 'yarn build:web:ci --maxWorkers=4'

  deploy:
    <<: *defaults
    steps:
      - attach_workspace:
          at: ~/repo
      - run:
          name: Authenticate with registry
          command: echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" > ~/repo/.npmrc
      - run:
          name: Publish package
          command: npm publish --access public

  update-fixtures:
    docker:
      - image: circleci/node:8.10
    steps:
      - checkout
      - run: 'yarn install'
      - run: 'node ./scripts/update-fixtures.js'

workflows:
  version: 2
  build_and_test:
    jobs:
      - test-node:
          filters:  # required since `deploy` has tag filters AND requires `test-node`
            branches:
              only: /.*/
            tags:
              only: /.*/
      - test-web:
          filters:  # required since `deploy` has tag filters AND requires `test-web`
            branches:
              only: /.*/
            tags:
              only: /.*/
      - deploy:
          requires:
            - test-node
            - test-web
          filters:
            tags:
              only: /^v.*/
            branches:
              ignore: /.*/
  # UNCOMMENT WHEN READY TO AUTOMATE THIS TASK
  # scheduled_tasks:
  #   triggers:
  #     # run once per hour
  #     - schedule:
  #       cron: "0 * * * *"
  #       filters:
  #         branches:
  #           only:
  #             - master
  #   jobs:
  #     - update-fixtures
