mbland/custom-links

View on GitHub
scripts/setup

Summary

Maintainability
Test Coverage
#! /usr/bin/env bash
#
# Runs first-time setup commands for a freshly-cloned repository

export CL_PLATFORM="${CL_PLATFORM:-$(node -e \
  'console.log(process.platform)')}"

cl.check_for_prerequisite_tools() {
  local required=('redis-server' 'jq' 'nc')
  local cmd_name
  local missing=()

  if ! command -v node >/dev/null; then
    @go.printf 'Please install Node.js before continuing.\n' >&2
    return 1
  elif [[ "$CL_PLATFORM" == 'win32' ]]; then
    return
  fi

  for cmd_name in "${required[@]}"; do
    if ! command -v "$cmd_name" >/dev/null; then
      missing+=("$cmd_name")
    fi
  done

  if [[ "${#missing[@]}" -ne '0' ]]; then
    @go.printf 'Please install the following programs before continuing:\n' >&2
    printf '  %s\n' "${missing[@]}" >&2
    return 1
  fi
}

# Some Node.js processes hang on Windows, most notably NPM, due to the way file
# descriptors that reference the terminal are managed. On top of that, npm can
# be rather noisy. Consequently, we save all output to a file for consistency
# across platforms.
cl.npm_install() {
  local npm_log="$_GO_ROOTDIR/npm.log"

  if ! npm install -q --no-progress "$@" >"$npm_log" 2>&1; then
    @go.log ERROR "npm install failed (from $npm_log):"
    printf '%s\n' "$(< "$npm_log")" >&2
    return '1'
  else
    rm "$npm_log"
  fi
}

cl.setup() {
  local result=0
  local start_msg='Setting up project...'

  export PATH="node_modules/.bin:$PATH"
  export FORCE_COLOR='true'
  export COVERAGE_REPORT_SERVER='false'

  if [[ "$CL_PLATFORM" == 'win32' ]]; then
    start_msg='Setting up browser-only development with Karma on Windows...'
  fi

  @go.critical_section_begin
  @go.log START "$start_msg"
  @go.log_command cl.check_for_prerequisite_tools

  if [[ -z "$CI" ]]; then
    @go.log_command cl.npm_install
  fi
  @go.critical_section_end

  if ! @go.log_command @go lint; then
    result=1
  fi

  if [[ "$CL_PLATFORM" == 'win32' ]]; then
    if ! @go.log_command karma start --single-run; then
      result='1'
    fi
  elif ! @go.log_command @go test --coverage; then
    result=1
  fi

  if [[ "$result" -eq '0' ]]; then
    @go.log FINISH 'Project setup completed successfully.'
  else
    @go.log ERROR 'Project setup encountered errors; see above for details.'
  fi
  return "$result"
}

cl.setup "$@"