version: 2.1
jobs:
  build:
    working_directory: ~/project
    docker:
      - image: circleci/node:12
    steps:
      - checkout
      - restore_cache:
          key: dependency-cache-{{ checksum "package-lock.json" }}
      - run:
          name: install
          command: npm install --ignore-engines
      - save_cache:
          key: dependency-cache-{{ checksum "package-lock.json" }}
          paths:
            - ./node_modules
      - persist_to_workspace:
          root: ~/project
          paths:
            - ./node_modules

  lint:
    working_directory: ~/project
    docker:
      - image: circleci/node:12
    steps:
      - checkout
      - attach_workspace:
          at: ./
      - run:
          name: lint
          command: npm run lint

  test:
    working_directory: ~/project
    docker:
      - image: circleci/node:12
    steps:
      - checkout
      - attach_workspace:
          at: ./
      - run:
          name: test
          command: npm run test
      - run:
          name: coverage
          command: npx codecov
          
  test-e2e:
    working_directory: ~/project
    docker:
      - image: circleci/node:12
      - image: verdaccio/verdaccio:4
    steps:
      - checkout
      - attach_workspace:
          at: ./
      - run:
          name: npm-verbosity
          command: npm config set loglevel verbose
      - run:
          name: npm-set-registry
          command: npm set registry http://0.0.0.0:4873
      - run:
          name: create-registry-user
          command: |
            curl -X PUT http://0.0.0.0:4873/-/user/org.couchdb.user:liran -H "Content-Type: application/json" -d '{"name":"liran","password":"liran","email":"liran@example.com","type":"user"}' | node -pe 'JSON.parse(fs.readFileSync(0)).token' | xargs npm config set _authToken
      - run:
          name: npm-publish
          command: npm publish --verbose
      - run:
          name: install
          command: |
            mkdir ~/tmp-project
            cd ~/tmp-project
            npm init -y
            npm install security-report --verbose
            
  release:
    working_directory: ~/project
    docker:
      - image: circleci/node:12
    steps:
      - checkout
      - attach_workspace:
          at: ./
      - run:
          name: release
          command: npm run semantic-release

workflows:
  version: 2.1
  project:
    jobs:
      - build
      - lint:
          requires:
            - build
      - test:
          requires:
            - lint
      - test-e2e:
          requires:
            - test
      - release:
          filters:
            branches:
              only:
                - master
          requires:
            - test-e2e
