version: 2

refs:
  container: &container
    docker:
      - image: node:12.22.12
    working_directory: ~/repo
  steps:
    - &Versions
      run:
        name: Versions
        command: node -v && npm -v && yarn -v
    - &CacheRestore
      restore_cache:
        key: dependency-cache-{{ checksum "yarn.lock" }}
    - &Install
      run:
        name: Install Dependencies
        command: yarn install --pure-lockfile
    - &CacheSave
      save_cache:
        key: dependency-cache-{{ checksum "yarn.lock" }}
        paths:
          - ./node_modules
    - &Build
      run:
        name: Build
        command: yarn build
    - &Build_Storybook
      run:
        name: Build Storybook
        command: yarn storybook:build
    - &Test
      run:
        name: Test
        command: yarn test
    - &Upload_Storybook
      run:
        name: Upload Storybook
        command: yarn storybook:upload
    - &Release
      run:
        name: Release
        command: yarn release
    - &Commit_Status_Storybook
      run:
        name: Post commit status for Storybook
        command: |
          npx cross-ci :run curl -H "'Authorization: token \${GITHUB_TOKEN}' -H 'Accept: application/vnd.github.v3+json'" \
          "'https://api.github.com/repos/\${PROJECT_OWNER}/\${PROJECT_NAME}/statuses/$CIRCLE_SHA1'" -X POST \
          -d "'{\"state\": \"success\", \"context\": \"Storybook\", \"description\": \"\${BUILD_VERSION}\", \"target_url\": \"https://$CIRCLE_BUILD_NUM-154950925-gh.circle-artifacts.com/0/root/repo/storybook-static/index.html\"}'"

jobs:
  all:
    <<: *container
    steps:
      - checkout
      - *Versions
      - *CacheRestore
      - *Install
      - *CacheSave
      - *Build
      - *Build_Storybook
      - *Test
      - store_artifacts:
          path: ~/repo/storybook-static
      - *Commit_Status_Storybook

  next:
    <<: *container
    steps:
      - checkout
      - *Versions
      - *CacheRestore
      - *Install
      - *CacheSave
      - *Build
      - *Build_Storybook
      - *Test
      - store_artifacts:
          path: ~/repo/storybook-static
      - *Commit_Status_Storybook
      - run:
          name: Setup GitHub
          command: |
            git config --global user.email "streamich@gmail.com"
            git config --global user.name "streamich"
            git remote rm origin
            git remote add origin https://${GITHUB_TOKEN}@github.com/streamich/react-use
      - *Release

  master:
    <<: *container
    steps:
      - checkout
      - *Versions
      - *CacheRestore
      - *Install
      - *CacheSave
      - *Build
      - *Build_Storybook
      - *Test
      - store_artifacts:
          path: ~/repo/storybook-static
      - *Commit_Status_Storybook
      - run:
          name: Setup GitHub
          command: |
            git config --global user.email "streamich@gmail.com"
            git config --global user.name "streamich"
            git remote rm origin
            git remote add origin https://${GITHUB_TOKEN}@github.com/streamich/react-use
      - *Upload_Storybook
      - *Release

workflows:
  version: 2
  all:
    jobs:
      - all:
          context: common-env-vars
          filters:
            branches:
              ignore:
                - master
                - next
                - gh-pages
  next:
    jobs:
      - next:
          context: common-env-vars
          filters:
            branches:
              only: next
  master:
    jobs:
      - master:
          context: common-env-vars
          filters:
            branches:
              only: master
