version: 2
aliases:

  #- &node_installation
  #  run:
  #    name: "Update Node.js and npm if on MacOS"
  #    command: .circleci/install_node.sh

  - &upgrade_yarn
    run:
      name: "Updates yarn"
      command: .circleci/upgrade_yarn.sh

  # Custom task removes unnecessary build files before uploading release directory
  - &before_store_artifacts
    run:
      name: Preparing for artifact upload
      command: rm -rf release/*-unpacked release/mac

  - &upload_artifacts
    store_artifacts:
      path: release/

  #- &install
  #  run: node --version && yarn install

  - &install
    run: node --version && npm install

  - &test_e2e
    run: yarn test:e2e -s

  - &run_script
    run: 
      name: "Building app (deploy if on master)"
      command: | 
        if [[ "${CIRCLE_BRANCH}" == 'master' ]]; then
          yarn release --$GRID_ENV;
        else
          yarn dist --$GRID_ENV;
        fi;
  - &run_steps
    steps:
      - checkout
      - run: node -v
      # - *restore_modules_cache
      - *install
      # - *test_e2e
      - *run_script
      # - *save_modules_cache
      - *before_store_artifacts
      - *upload_artifacts

jobs:
  build:
    docker:
      - image: electronuserland/builder:10
    environment:
      GRID_ENV: linux
    working_directory: ~/repo
    <<: *run_steps

  build-win:
    docker:
      - image: electronuserland/builder:wine
    environment:
      GRID_ENV: win
    working_directory: ~/repo
    <<: *run_steps

  build-mac:
    macos:
      xcode: '10.0'
    environment:
      GRID_ENV: mac
    working_directory: ~/repo
    <<: *run_steps

workflows:
  version: 2
  build:
    jobs:
      - build
      - build-mac
      - build-win
