version: 2.1

defaults: &defaults
    working_directory: ~/react-native-sdk
    docker:
        - image: circleci/node:8

jobs:
    lint-test:
        <<: *defaults
        steps:
            - restore_cache: # If any cache exists, it'll way speedup these checkouts
                keys:
                    - source-v2-{{ arch }}-{{ .Branch }}-{{ .Revision }}
                    - source-v2-{{ arch }}-{{ .Branch }}-
                    - source-v2-{{ arch }}-

            - checkout

            - save_cache:
                key: source-v2-{{ arch }}-{{ .Branch }}-{{ .Revision }}
                paths:
                    - ".git"

            - restore_cache:
                key: yarn-v1-{{ checksum "yarn.lock" }}-{{ arch }}

            - restore_cache:
                key: node-v1-{{ checksum "package.json" }}-{{ arch }}

            - run: yarn install

            - run:
                name: lint
                command: |
                    yarn lint
            # cache our yarn cache
            - save_cache:
                key: yarn-v1-{{ checksum "yarn.lock" }}-{{ arch }}
                paths:
                    - ~/.cache/yarn

            # cache our node_modules
            - save_cache:
                key: node-v1-{{ checksum "package.json" }}-{{ arch }}
                paths:
                    - node_modules

            - run:
                name: jest tests
                command: |
                    mkdir -p test-results/jest
                    yarn run test
                environment:
                    JEST_JUNIT_OUTPUT: test-results/jest/junit.xml

            - store_test_results:
                path: test-results

            - store_artifacts:
                path: test-results
    deploy:
        <<: *defaults
        steps:
            - checkout
            - attach_workspace:
                at: ~/react-native-sdk
            - run:
                name: Install dependencies
                command: yarn install
            - run:
                name: Build package
                command: yarn build
            - run:
                name: Authenticate with registry
                command: echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" > ~/react-native-sdk/.npmrc
            - run:
                name: Publish package
                command: npm publish --access=public
workflows:
    version: 2
    react-native-sdk:
        jobs:
            - lint-test
            - deploy:
                filters:
                    tags:
                        only: /^v.*/
                    branches:
                        ignore: /.*/
