Return to note index?

I use them both directly, and within pre-commit

Automaattinen sisällysluettelo / Automatically generated Table of Contents

The packages

Installation

  1. corepack pnpm install -D -E prettier@latest prettier-plugin-nginx@latest @prettier/plugin-ruby@latest prettier-plugin-toml@latest @prettier/plugin-xml@latest prettier-plugin-sh@latest or probably just corepack pnpm install -D if it’s not your project.
  2. If they don’t exist already echo "{}" > .prettierrc && touch .prettierignore
  3. corepack pnpm exec prettier . --write or corepack pnpm exec prettier . --check

Configuration

I do with .editorconfig what I can, but for example my template .prettierrc.json looks like this. I use pre-commit’s pretty-format-json, while the one included on this page gets managed by prettier.

{
  "bracketSameLine": true,
  "endOfLine": "auto",
  "insertPragma": true,
  "overrides": [
    {
      "files": ".prettierrc",
      "options": {
        "parser": "json"
      }
    },
    {
      "files": "conf/librewolf.overrides.cfg",
      "options": {
        "parser": "babel"
      }
    },
    {
      "files": "conf/autoconfig.js.online",
      "options": {
        "parser": "babel"
      }
    }
  ],
  "plugins": [
    "@prettier/plugin-ruby",
    "@prettier/plugin-xml",
    "prettier-plugin-nginx",
    "prettier-plugin-sh",
    "prettier-plugin-toml"
  ],
  "proseWrap": "always",
  "quoteProps": "consistent",
  "requirePragma": false,
  "singleAttributePerLine": true
}

See the documentation linked on the bottom, but here are some explanations anyway:

.pre-commit-config.yaml

This is the file that controls pre-commits behaviour.

Offline

I accidentally wrote this while updating this page to reflect me using prettier outside of pre-commit too nowadays. This has the advantage that the same local environment gets reused and dependencies are managed centrally, but assumes everyone uses pnpm, won’t work in pre-commit ci and may have other issues I am not thinking of as a not-coder myself.

ci:
  skip: [pnpm-install-dev, prettier]

repos:
  - repo: local
    hooks:
      - id: pnpm-install-dev
        name: Install pnpm dev dependencies
        entry: corepack pnpm install -D
        language: system
        always_run: true
        #verbose: true
        pass_filenames: false
      - id: prettier
        name: prettier
        entry: corepack pnpm exec prettier --cache --ignore-unknown --write
        language: system
        # Better handled by pretty-format-json from pre-commit-hooks.
        # Remember to have *.json in .prettierignore!
        #exclude_types: [json]

Further information

Return to note index?

Dear reader, you may be missing a content blocker! 🙀 Please consider installing one to protect yourself, and your close ones, from manipulation and targeted malvertising! Personally I love both Privacy Badger and uBlock Origin (with EFF DNT Policy Allowlist) together, while AdNauseam alone would be more direct protest tool to oppose how the internet is nowadays. Android users may be better served by Rethink while for iOS there is AdGuard. Learn more about targeted advertising! PS. I am sorry if you are already protected and this silly EasyList targeting(?) script doesn't detect that, thank you for taking the steps towards a safer internet! 💜