version: 2.1
orbs:
  node: circleci/node@5.0.2
references:
  tag-filter: &tag-filter
    filters:
      branches:
        ignore: /.*/
      tags:
        only:
          - /^v\d+\.\d+\.\d+$/
  beta-tag-filter: &beta-tag-filter
    filters:
      branches:
        ignore: /.*/
      tags:
        only: /^v\d+\.\d+\.\d+-beta\d+$/
commands:
  deploy:
    parameters:
      beta:
        type: boolean
        default: false
    steps:
      - attach_workspace:
          at: .
      - run:
          name: Authenticate with registry
          command: echo "//registry.npmjs.org/:_authToken=${NPM_TOKEN}" > .npmrc
      - when:
          condition: << parameters.beta >>
          steps:
            - run:
                name: Publish package
                command: npm publish --access public --tag beta
            - run:
                name: Publish Release on GitHub
                command: |
                  ios_version=$(node -e "console.log(require('./package.json').nativeSdkVersions.ios)")
                  android_version=$(node -e "console.log(require('./package.json').nativeSdkVersions.android)")
                  curl -s -X POST -d "{\"tag_name\":\"$CIRCLE_TAG\",\"name\":\"$CIRCLE_TAG\",\"body\":\"iOS SDK version: ${ios_version}\nAndroid SDK version: ${android_version}\",\"draft\":false,\"prerelease\":true}" -H "Authorization: token $GITHUB_TOKEN" -H "Accept: application/vnd.github.v3+json" "https://api.github.com/repos/foursquare/movement-sdk-react-native/releases"
      - when:
          condition:
            not: << parameters.beta >>
          steps:
            - run:
                name: Publish package
                command: npm publish --access public
            - run:
                name: Publish Release on GitHub
                command: |
                  ios_version=$(node -e "console.log(require('./package.json').nativeSdkVersions.ios)")
                  android_version=$(node -e "console.log(require('./package.json').nativeSdkVersions.android)")
                  curl -s -X POST -d "{\"tag_name\":\"$CIRCLE_TAG\",\"name\":\"$CIRCLE_TAG\",\"body\":\"iOS SDK version: ${ios_version}\nAndroid SDK version: ${android_version}\",\"draft\":false,\"prerelease\":false}" -H "Authorization: token $GITHUB_TOKEN" -H "Accept: application/vnd.github.v3+json" "https://api.github.com/repos/foursquare/movement-sdk-react-native/releases"
jobs:
  lint:
    executor: node/default
    steps:
      - checkout
      - run: npm install
      - run: npm run lint
      - persist_to_workspace:
          root: .
          paths:
            - .
  deploy-beta:
    executor: node/default
    steps:
      - deploy:
          beta: true
  deploy:
    executor: node/default
    steps:
      - deploy
workflows:
  version: 2
  untagged-build:
    jobs:
      - lint
  deploy:
    jobs:
      - lint:
          <<: *tag-filter
      - deploy:
          requires:
            - lint
          <<: *tag-filter
  deploy-beta:
    jobs:
      - lint:
          <<: *beta-tag-filter
      - deploy-beta:
          requires:
            - lint
          <<: *beta-tag-filter
