---
version: '3'

tasks:
  create:
    deps:
      - :install:software:jq
    log:
      error: Error creating production `node_modules/` pack
      start: Creating production `node_modules/` pack
      success: Successfully created production `node_modules/` pack
    cmds:
      - |
        if [ -d node_modules ]; then
          mv node_modules node_modules.bak
        fi
      - task: install
      - task: minify
      - task: pack
      - rm package-lock.json
      - |
        if [ -d node_modules.bak ]; then
          rm -rf node_modules
          mv node_modules.bak node_modules
        fi
    status:
      - '[ $(jq -r ".dependencies | length" package.json) == "0" ]'

  install:
    deps:
      - :install:software:node
    log:
      error: Encountered error while running `npm install --only=prod`
      start: Installing production `node_modules/`
      success: Successfully set-up production `node_modules/` folder
    cmds:
      - SKIP_NPM_START=true npm install --only=prod

  minify:
    deps:
      - :install:go:node-prune
      - :install:npm:modclean
    summary: |
      ## node-prune

      `node-prune` is a Go project that does a good job at cleaning up projects while
      remaining safe.

      ## modclean

      `modclean` does what `node-prune` does and takes it a step further.

      `modclean` provides 3 different patterns by default. They are:

      * `default:safe` - Contains patterns that are considered safe and should not
      affect your application. It also removes the most files/folders of all the configurations.
      * `default:caution` - Contains patterns that could potentially cause issues with
      modules, but includes patterns that will help reduce your modules folder size.
      * `default:danger` - Contains patterns that are known to cause issues with certain
      modules, but can help reduce files and your modules folder size even further.

      **To clean with `default:safe` and `default:caution`, you can run:**
      `task npm:bundle:minify -- default:safe,default:caution

      **To clean with all of them:**
      `task npm:bundle:minify -- default:*`

      If CLI arguments are not passed, then the task looks at `.blueprint.modcleanPatterns` in `package.json`
      for the patterns. And if that is not present, then `default:safe,default:caution` is used by default.

      You can see the [default patterns here](https://github.com/ModClean/modclean-patterns-default/blob/master/patterns.json).
    vars:
      MODCLEAN_PATTERNS:
        sh: |
          {{if .CLI_ARGS}}echo '{{.CLI_ARGS}}'{{else}}PREFERENCE="$(jq -r '.blueprint.modcleanPatterns' package.json)"
          if [ "$PREFERENCE" != 'null' ]; then
            echo "$PREFERENCE"
          else
            echo "default:safe,default:caution"
          fi{{end}}
    log:
      error: Encountered error while pruning production `node_modules/` folder
      start: Pruning production `node_modules/` folder
      success: Successfully pruned production `node_modules/` folder
    cmds:
      - node-prune
      - modclean --patterns="{{.MODCLEAN_PATTERNS}}" --run

  pack:
    deps:
      - :install:npm:pac
    log:
      error: Error compressing production `node_modules/` into `.tgz` files stored in the `.modules/` folder
      start: Packing production `node_modules/` into `.tgz` files stored in the `.modules/` folder
      success: Finished packing production `node_modules/` into the `.modules/` folder
    cmds:
      - mkdir -p .modules
      - .config/log info '`pac` is being replaced by `produndle` - WIP' || produndle pack

  unpack:
    deps:
      - :install:software:jq
    cmds:
      - task: unpack:pac
    status:
      - '[ ! -d .modules ] || ([ -d .modules ] && (! ls .modules | grep .tgz))'

  unpack:pac:
    deps:
      - :install:npm:pac
    log:
      error: Error running `unpack:pac`
      start: Populating `node_modules/` with `.tgz` files in the `.modules` folder
      success: Finished populating `node_modules/` folder
    cmds:
      - .config/log info '`pac` is being replaced by `produndle` - WIP' || produndle unpack
      - |
        BASE_DIR="$(PWD)"
        for MODULE in node_modules/*; do
          cd "$MODULE"
          if [ -f Taskfile.yml ]; then
            task unpack || EXIT_CODE=$?
            if [ -n "$EXIT_CODE" ]; then
              "${PWD}/.config/log" warn 'There was an error running `task unpack` in '"$MODULE"
            fi
          fi
        done
