set dotenv-load := true

oops:
  echo "OOPS! The pickleback submodule got removed, so you'll need to make this work without it!" && exit 1

list:
  @just --list

update-pickleback:
  @just git-set-remote-url './pickleback' 'upstream' 'git@github.com:hapijs/shot.git'
  @just update-fork ./pickleback upstream master origin patches

update-pickleback-types:
  @just update-types ./types-galore origin base main shot pickleback

# tasks for updating general forks and types, respectively. right now these
# are practically the same task, but with minor modifications to support
# various use cases.
#
# in a flextrek situation, if on environment variables loaded/set by the
# flextrek runner.

update-fork REPO_HOME UPSTREAM_REMOTE UPSTREAM_BRANCH PATCH_REMOTE PATCH_BRANCH:
  @just update-step-1 '{{ REPO_HOME }}' '{{ UPSTREAM_REMOTE }}' '{{ UPSTREAM_BRANCH }}'
  @just update-step-3 '{{ REPO_HOME }}' '{{ UPSTREAM_BRANCH }}' '{{ PATCH_REMOTE }}' '{{ PATCH_BRANCH }}' "after rebasing, run: just continue-update-fork '{{ REPO_HOME }}' '{{ PATCH_REMOTE }}' '{{ PATCH_BRANCH }}'"
  @just continue-update-fork '{{ REPO_HOME }}' '{{ PATCH_REMOTE }}' '{{ PATCH_BRANCH }}'

continue-update-fork REPO_HOME PATCH_REMOTE PATCH_BRANCH:
  @just update-step-4 '{{ REPO_HOME }}' '{{ PATCH_REMOTE }}' '{{ PATCH_BRANCH }}'
  @just npm-publish '{{ REPO_HOME }}' '{{ PATCH_BRANCH }}'

update-types REPO_HOME REMOTE UPSTREAM_BRANCH PATCH_BRANCH UPSTREAM_PKG PATCH_PKG:
  @just update-step-1 '{{ REPO_HOME }}' '{{ REMOTE }}' '{{ UPSTREAM_BRANCH }}'
  @just populate-upstream-type-definitions '{{ REPO_HOME }}' '{{ UPSTREAM_BRANCH }}' '{{ UPSTREAM_PKG }}' '{{ PATCH_PKG }}'
  @just update-step-3 '{{ REPO_HOME }}' '{{ UPSTREAM_BRANCH }}' '{{ REMOTE }}' '{{ PATCH_BRANCH }}' "after rebasing, run: just continue-update-fork '{{ REPO_HOME }}' '{{ REMOTE }}' '{{ PATCH_BRANCH }}'"
  @just continue-update-types '{{ REPO_HOME }}' '{{ REMOTE }}' '{{ PATCH_BRANCH }}'

continue-update-types REPO_HOME REMOTE PATCH_BRANCH:
  @just update-step-4 '{{ REPO_HOME }}' '{{ REMOTE }}' '{{ PATCH_BRANCH }}'
  @just npm-publish '{{ REPO_HOME }}' '{{ PATCH_BRANCH }}'

# syncing steps:

# step 1: pull upstream remote branch
update-step-1 REPO_HOME UPSTREAM_REMOTE UPSTREAM_BRANCH:
  @just git-pull-fast-forward '{{ REPO_HOME }}' '{{ UPSTREAM_REMOTE }}' '{{ UPSTREAM_BRANCH }}'

# step 2: that's on you

# step 3: push 
update-step-3 REPO_HOME UPSTREAM_BRANCH PATCH_REMOTE PATCH_BRANCH FAILURE_MESSAGE:
  just git-push '{{ REPO_HOME }}' '{{ PATCH_REMOTE }}' '{{ UPSTREAM_BRANCH }}'
  @just git-pull-fast-forward '{{ REPO_HOME }}' '{{ PATCH_REMOTE }}' '{{ PATCH_BRANCH }}'
  @just git-rebase '{{ REPO_HOME }}' '{{ UPSTREAM_BRANCH }}' '{{ PATCH_BRANCH }}' || echo '{{ FAILURE_MESSAGE }}'

# step 4: push changes
update-step-4 REPO_HOME PATCH_REMOTE PATCH_BRANCH:
  @just git-push '{{ REPO_HOME }}' '{{ PATCH_REMOTE }}' '{{ PATCH_BRANCH }}'

# step 5: that's on you

# helpers

git-set-remote-url REPO_HOME REMOTE_NAME REMOTE_URL:
  cd '{{ REPO_HOME }}' && if [ -z "$(git config --get 'remote.{{ REMOTE_NAME }}.url')" ]; then git remote add '{{ REMOTE_NAME }}' '{{ REMOTE_URL }}'; else git remote set-url '{{ REMOTE_NAME }}' '{{ REMOTE_URL }}'; fi

git-checkout REPO_HOME BRANCH:
  cd '{{ REPO_HOME }}' && git checkout '{{ BRANCH }}'

git-pull-fast-forward REPO_HOME REMOTE BRANCH:
  @just git-checkout '{{ REPO_HOME }}' '{{ BRANCH }}'
  cd '{{ REPO_HOME }}' && git pull --ff-only '{{ REMOTE }}' '{{ BRANCH }}'

git-rebase REPO_HOME BASE_BRANCH REBASED_BRANCH:
  @just git-checkout '{{ REPO_HOME }}' '{{ REBASED_BRANCH }}'
  cd '{{ REPO_HOME }}' && git rebase '{{ BASE_BRANCH }}'

git-add-and-commit REPO_HOME BRANCH MESSAGE:
  @just git-checkout '{{ REPO_HOME }}' '{{ BRANCH }}'
  cd '{{ REPO_HOME }}' && git add .
  cd '{{ REPO_HOME }}' && git commit -m '{{ MESSAGE }}'

git-push REPO_HOME ORIGIN BRANCH:
  @just git-checkout '{{ REPO_HOME }}' '{{ BRANCH }}'
  cd '{{ REPO_HOME }}' && git push '{{ ORIGIN }}' '{{ BRANCH }}'

npm-publish REPO_HOME BRANCH:
  @just git-checkout '{{ REPO_HOME }}' '{{ BRANCH }}'
  cd '{{ REPO_HOME }}' && npm publish

populate-upstream-type-definitions REPO_HOME BRANCH UPSTREAM_PKG PATCH_PKG:
  @just git-checkout '{{ REPO_HOME }}' '{{ BRANCH }}'
  npm i --save-dev '@types/{{ UPSTREAM_PKG }}@latest'
  mkdir -p '{{ REPO_HOME }}/types/{{ PATCH_PKG }}'
  cp -r './node_modules/@types/{{ UPSTREAM_PKG }}/*' '{{ REPO_HOME }}/types/{{ PATCH_PKG }}/'
  @just update-types-registry '{{ REPO_HOME }}' '{{ PATCH_PKG }}'
  @just git-add-and-commit '{{ REPO_HOME }}' '{{ BRANCH }}' 'Pulled type definitions for {{ PATCH_PKG }} from DefinitelyTyped'

update-types-registry REPO_HOME PATCH_PKG:
  #!/usr/bin/env node

  const { readFileSync, writeFileSync } = require('fs');

  const json = JSON.parse(readFileSync('{{ REPO_HOME }}/registry.json'));

  json['{{ PATCH_PKG }}'] = `${process.env.TYPES_REGISTRY_URL}/types/{{ PATCH_PKG }}/index.d.ts`

  writeFileSync('{{ REPO_HOME }}/registry.json', JSON.stringify(json, null, 2));

