---
version: '3'

tasks:
  prose:
    deps:
      - :install:pipx:proselint
    desc: Lint text/markdown for English prose with `proselint`
    summary: |
      # Lint for English Prose

      This task uses `proselint` to analyze markdown files for prose. It will generate recommendations
      based on typography, grammar, and wording.

      **Example scanning all markdown files:**
      `task lint:prose`

      **Example scanning specific file (markdown or not):**
      `task lint:prose -- myfile.js`

      For more information, see [Proselint's GitHub page](https://github.com/amperser/proselint).
    log:
      error: Error encountered while validating {{if .CLI_ARGS}}`{{.CLI_ARGS}}`{{else}}the project{{end}} with Proselint
      start: Linting {{if .CLI_ARGS}}`{{.CLI_ARGS}}`{{else}}the project{{end}} prose with Proselint
      success: Successfully validated {{if .CLI_ARGS}}`{{.CLI_ARGS}}`{{else}}the project{{end}} with Proselint
    cmds:
      - task: prose:config
      - |
        PATH="$PATH:$HOME/.local/bin"
        {{if .CLI_ARGS}}
          {{.PYTHON_HANDLE}}proselint {{.CLI_ARGS}}
        {{else}}
          find . -type d \( {{.IGNORE_FOLDERS}} \) -prune -o -type f \( -name '*.md' \) -print0 | xargs -0 -r -n1 {{.PYTHON_HANDLE}}proselint
        {{end}}
      - task: prose:config:restore

  prose:config:
    log:
      error: Error encountered while ensuring Proselint configuration is in proper location
      start: Moving Proselint configuration to proper location
      success: Ensured Proselint configuration is in proper location
    cmds:
      - |
        if [ -f ~/.config/proselint/config.json ]; then
          .config/log info 'Backing up `proselint` from `~/.config/proselint/config.json` to `~/.config/proselint/config.json.backup`'
          mv ~/.config/proselint/config.json ~/.config/proselint/config.json.backup
        fi
      - mkdir -p ~/.config/proselint
      - cp .config/proselint.json ~/.config/proselint/config.json

  prose:config:restore:
    log:
      error: Error restoring original Proselint configuration
      start: Restoring original Proselint configuration
    cmds:
      - mv ~/.config/proselint/config.json.backup ~/.config/proselint/config
    status:
      - '[ ! -f ~/.config/proselint/config.json.backup ]'
