# Javascript Node CircleCI 2.0 configuration file
#
# Check https://circleci.com/docs/2.0/language-javascript/ for more details
#
version: 2

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

jobs:
    build_and_test:
        <<: *defaults

        steps:
            - checkout

            # Download and cache dependencies
            - restore_cache:
                  keys:
                      - v1-dependencies-{{ checksum "yarn.lock" }}
                      # fallback to using the latest cache if no exact match is found
                      - v1-dependencies-

            - run: yarn install

            - save_cache:
                  paths:
                      - node_modules
                  key: v1-dependencies-{{ checksum "yarn.lock" }}

            - run: yarn lint

            - run:
                  name: run test
                  command: yarn test

    publish_canary:
        <<: *defaults

        steps:
            - checkout
            # Download and cache dependencies
            - restore_cache:
                  keys:
                      - v1-dependencies-{{ checksum "yarn.lock" }}
                      # fallback to using the latest cache if no exact match is found
                      - v1-dependencies-
            - run: git config --global user.email "admin@nexxtway.com"
            - run: git config --global user.name "TheAdmin"
            - run: yarn install
            - run:
                  name: Auth with registry
                  command: echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" > ~/repo/.npmrc
            - run: yarn prepare:canary
            - run: yarn publish --tag next

    publish_npm:
        <<: *defaults

        steps:
            - checkout
            # Download and cache dependencies
            - restore_cache:
                  keys:
                      - v1-dependencies-{{ checksum "yarn.lock" }}
                      # fallback to using the latest cache if no exact match is found
                      - v1-dependencies-
            - run: git config --global user.email "admin@nexxtway.com"
            - run: git config --global user.name "TheAdmin"
            - run: yarn install
            - run:
                  name: Auth with registry
                  command: echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" > ~/repo/.npmrc
            - run: yarn publish

    deploy_firebase:
        <<: *defaults

        steps:
            - checkout

            # Download and cache dependencies
            - restore_cache:
                  keys:
                      - v1-dependencies-{{ checksum "yarn.lock" }}
                      # fallback to using the latest cache if no exact match is found
                      - v1-dependencies-

            - run: yarn install
            - run: yarn deploy:library --token=$FIREBASE_TOKEN

workflows:
    version: 2
    build_and_test:
        jobs:
            - build_and_test
            - deploy_firebase:
                  requires:
                      - build_and_test
                  filters:
                      branches:
                          only: master
            - publish_canary:
                  requires:
                      - build_and_test
                  filters:
                      branches:
                          only: master
            - publish_npm:
                  requires:
                      - build_and_test
                  filters:
                      branches:
                          only: npm
