---
version: '3'

tasks:
  check:package:
    interactive: true
    deps:
      - :install:modules:local
      - :install:software:jq
    vars:
      BLUEPRINT_REQUIRED_FIELDS: title description group name overview slug subgroup
    run: once
    log:
      error: Error occurred while validating/prompting for blueprint settings
      start: Ensuring required fields in the blueprint section of `package.json` are present
      succes: Successfully ensured `package.json` minimum blueprint requirements are present
    cmds:
      - task: prime:package
      - task: ensure:gitlab-ci
      - task: :boilerplate:prompt:all
      - task: :boilerplate:populate:type
      - task: update:taskfile

  clean:
    deps:
      - :install:software:jq
    log:
      error: Failed to clean `package.json`
      start: Cleaning `package.json`
      success: Cleaned `package.json`
    cmds:
      - |
        TMP="$(mktemp)"
        jq 'del(."standard-version")' package.json > "$TMP"
        mv "$TMP" package.json
      - |
        TMP="$(mktemp)"
        jq 'del(."lint-staged")' package.json > "$TMP"
        mv "$TMP" package.json

  ensure:gitlab-ci:
    run: once
    cmds:
      - |
        if [ ! -f .gitlab-ci.yml ]; then
          echo '---' > .gitlab-ci.yml
          echo 'stages:' >> .gitlab-ci.yml
          echo '  - lint' >> .gitlab-ci.yml
          echo '' >> .gitlab-ci.yml
        fi

  prime:package:
    deps:
      - :install:software:jq
    run: once
    log:
      error: Failed to merge shared `package.json` settings
      start: Ensuring `package.json` has shared settings
      success: Successfully merged shared `package.json` settings
    cmds:
      - curl -s https://gitlab.com/megabyte-labs/common/shared/-/raw/master/package.json > package-reference.json
      - task: prime:package:ensure-deps
        vars:
          PKG_FILE: package.json
      - task: prime:package:ensure-deps
        vars:
          PKG_FILE: package-reference.json
      - |
        DEPS="$(jq -s '.[0].dependencies * .[1].dependencies' package-reference.json package.json)"
        DEV_DEPS="$(jq -s '.[0].devDependencies * .[1].devDependencies' package-reference.json package.json)"
        OPT_DEPS="$(jq -s '.[0].optionalDependencies * .[1].optionalDependencies' package-reference.json package.json)"
        ESLINT_CONFIG="$(jq -r '.eslintConfig.extends' package-reference.json)"
        PRETTIER_CONFIG="$(jq -r '.prettier' package-reference.json)"
        TMP="$(mktemp)"
        jq --arg deps "$DEPS" --arg devDeps "$DEV_DEPS" --arg optDeps "$OPT_DEPS" --arg eslint "$ESLINT_CONFIG" \
          --arg prettier "$PRETTIER_CONFIG" '.dependencies = ($deps | fromjson) | .devDependencies = ($devDeps
          | fromjson) | .optionalDependencies = ($optDeps | fromjson) | .eslintConfig.extends = $eslint
          | .prettier = $prettier' package.json > "$TMP"
        mv "$TMP" package.json
      - rm package-reference.json

  prime:package:ensure-deps:
    deps:
      - :install:software:jq
    run: once
    cmds:
      - |
        if [ "$(jq -r '.dependencies' {{.PKG_FILE}})" == 'null' ]; then
          TMP="$(mktemp)"
          jq '.dependencies = {}' {{.PKG_FILE}} > "$TMP"
          mv "$TMP" {{.PKG_FILE}}
        fi
      - |
        if [ "$(jq -r '.devDependencies' {{.PKG_FILE}})" == 'null' ]; then
          TMP="$(mktemp)"
          jq '.devDependencies = {}' {{.PKG_FILE}} > "$TMP"
          mv "$TMP" {{.PKG_FILE}}
        fi
      - |
        if [ "$(jq -r '.optionalDependencies' {{.PKG_FILE}})" == 'null' ]; then
          TMP="$(mktemp)"
          jq '.optionalDependencies = {}' {{.PKG_FILE}} > "$TMP"
          mv "$TMP" {{.PKG_FILE}}
        fi

  update:taskfile:
    deps:
      - :install:software:yq
    run: once
    log:
      error: Error encountered while ensuring `Taskfile.yml` has correct settings
      start: Ensuring `Taskfile.yml` has correct settings
      success: Successfully applied `Taskfile.yml` assurances
    cmds:
      - |
        GROUP="$(jq -r '.blueprint.group' package.json)"
        SUBGROUP="$(jq -r '.blueprint.subgroup' package.json)"
        TASK_GROUP="$(yq eval '.vars.REPOSITORY_TYPE' Taskfile.yml)"
        TASK_SUBGROUP="$(yq eval '.vars.REPOSITORY_SUBTYPE' Taskfile.yml)"
        if [ "$GROUP" != "$TASK_GROUP" ]; then
          yq e -i ".vars.REPOSITORY_TYPE = \"$GROUP\"" Taskfile.yml
        fi
        if [ "$SUBGROUP" != "$TASK_SUBGROUP" ]; then
          yq e -i ".vars.REPOSITORY_SUBTYPE = \"$SUBGROUP\"" Taskfile.yml
        fi
        UPSTREAM='upstream:project'
        if [ '{{.REPOSITORY_TYPE}}.{{.REPOSITORY_SUBTYPE}}' == 'common.shared' ]; then
          UPSTREAM='upstream:shared'
        elif [ '{{.REPOSITORY_TYPE}}.{{.REPOSITORY_SUBTYPE}}' == 'documentation.shared' ]; then
          UPSTREAM='upstream:commondocs'
        elif [ '{{.REPOSITORY_TYPE}}' == 'common' ]; then
          UPSTREAM='upstream:common'
        elif [ '{{.REPOSITORY_TYPE}}' == 'documentation' ]; then
          UPSTREAM='upstream:docs'
        fi
        yq e -i ".tasks.start.cmds[0].task = \"$UPSTREAM\"" Taskfile.yml
