#!/bin/env zsh

#
# Commands and shortcuts for Meteor core development, you can load these in your terminal by running `source .envrc`.
# Or by adding `[[ -s .envrc ]] && source .envrc` to your `.zshrc` or `.bashrc`.
#

export ROOT_DIR=$(git rev-parse --show-toplevel)

########
# Core #
########

function @meteor {
   "$ROOT_DIR/meteor" "$@"
}

function @get-ready {
  @meteor --get-ready
}

function @test-packages {
  TINYTEST_FILTER="$1" @meteor test-packages --exclude-archs=web.browser.legacy,web.cordova
}

function @test-self {
  @meteor self-test "$@"
}

function @test-in-console {
  "$ROOT_DIR/packages/test-in-console/run.sh" "$@"
}

function @check-syntax {
  node "$ROOT_DIR/scripts/admin/check-legacy-syntax/check-syntax.js"
}

function @generate-dev-bundle {
  rm -rf $ROOT_DIR/dev_bundle*
  "$ROOT_DIR/scripts/generate-dev-bundle.sh"
}

function @init-submodule {
  git submodule update --init --recursive
}

#################
# Documentation #
#################

function @docs-start {
  npm run docs:dev --prefix "$ROOT_DIR/v3-docs/docs"
}

function @docs-migration-start {
  npm run docs:dev --prefix "$ROOT_DIR/v3-docs/v3-migration-docs"
}

function @get-changes {
  git diff --numstat HEAD~1 HEAD | awk '($1 + $2) <= 5000 {print $3}'
}

function @summarize-changes {
  changes=$(@get-changes)

  if [ -n "$changes" ]; then
    changes=$(git diff HEAD~1 HEAD -- $(echo "$changes" | tr '\n' ' '))
  else
    changes=$(git diff HEAD~1 HEAD)
  fi

  echo "$changes" | llm -s "Summarize the following changes in a few sentences:"
}

function @packages-bumped {
  git diff --name-only devel...$(git branch --show-current) | grep "packages/.*/package.js$" | while IFS= read -r file; do
    if ! git show devel:$file > /dev/null 2>&1; then
      continue
    fi

    old=$(git show devel:$file | grep -o "version: *['\"][^'\"]*['\"]" | sed "s/version: *.['\"]//;s/['\"].*//")
    version=$(grep -o "version: *['\"][^'\"]*['\"]" "$file" | sed "s/version: *.['\"]//;s/['\"].*//")
    name=$(grep -o "name: *['\"][^'\"]*['\"]" "$file" | sed "s/name: *.['\"]//;s/['\"].*//")

    pkg_name=$(echo "$file" | sed -E 's|packages/([^/]*/)?([^/]*)/package\.js|\2|')

    version_in_red=$(tput setaf 1)$version$(tput sgr0)

    if [[ "$version" != "$old" ]]; then
      echo "- $pkg_name@$version_in_red"
    fi
  done
}

function @packages-bumped-npm {
  git diff --name-only devel...$(git branch --show-current) | grep "npm-packages/.*/package.json$" | while IFS= read -r file; do
    if ! git show devel:$file > /dev/null 2>&1; then
      continue
    fi

    old=$(git show devel:$file | grep -o "version: *['\"][^'\"]*['\"]" | sed "s/version: *.['\"]//;s/['\"].*//")
    version=$(grep -o "\"version\": *['\"][^'\"]*['\"]" "$file" | sed "s/\"version\": *.['\"]//;s/['\"].*//")
    name=$(grep -o "\"name\": *['\"][^'\"]*['\"]" "$file" | sed "s/\"name\": *.['\"]//;s/['\"].*//")

    pkg_name=$(echo "$file" | sed -E 's|npm-packages/([^/]*/)?([^/]*)/package\.json|\2|')

    version_in_red=$(tput setaf 1)$version$(tput sgr0)

    if [[ "$version" != "$old" ]]; then
      echo "- $pkg_name@$version_in_red"
    fi
  done
}
