#!/bin/bash

set -euo pipefail

PATH="$PATH:$(yarn bin)"

main () {
  if [[ $# -lt 1 ]]; then
    cmd_help
  fi

  local command="cmd_$1"
  shift

  "$command" "$@"
}

cmd_help () {
  local available_commands=$(set | perl -nle'print $& while m{^cmd_\K\w+}g')
  echo -e "Available commands:\n$available_commands"
  exit 1
}

cmd_typecheck () {
  local project_path="${1}"
  tsc -p "$project_path" --noEmit true
}

cmd_lint () {
  local project_path="${1}"
  tslint -p "$project_path" -c ./tslint.json
}

cmd_update_submodule () {
  git submodule update --init
  git submodule update --remote
}

# needed because using fork instead of the npm package
# TODO remove me when json-schema-to-typescript natively supports readOnly
cmd_build_json2type () {
  cd ./node_modules/json-schema-to-typescript
  rm -rf dist
  # due to installation on node_modules, some of these steps can fail
  # built version still behaves correctly though
  set +e
  npm i
  npm run clean
  npm run build:server
  set -e
}

main "$@"
