{"id":16419138,"url":"https://github.com/cameronhunter/prettier-package-json","last_synced_at":"2025-04-04T08:05:48.422Z","repository":{"id":37579797,"uuid":"93027579","full_name":"cameronhunter/prettier-package-json","owner":"cameronhunter","description":"Prettier formatter for package.json files","archived":false,"fork":false,"pushed_at":"2024-07-10T02:44:44.000Z","size":380,"stargazers_count":153,"open_issues_count":15,"forks_count":18,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-28T07:03:12.631Z","etag":null,"topics":["cli","javascript","json","package-json","prettier","pretty-print","printer"],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cameronhunter.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-06-01T07:11:07.000Z","updated_at":"2025-03-10T00:00:34.000Z","dependencies_parsed_at":"2023-10-03T04:34:54.110Z","dependency_job_id":"f4c27463-6751-4d5b-a09c-9ed918d27a34","html_url":"https://github.com/cameronhunter/prettier-package-json","commit_stats":{"total_commits":120,"total_committers":17,"mean_commits":"7.0588235294117645","dds":0.4833333333333333,"last_synced_commit":"cd4b5916758ec8f7a3300060d2847f27379c7b72"},"previous_names":[],"tags_count":30,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cameronhunter%2Fprettier-package-json","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cameronhunter%2Fprettier-package-json/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cameronhunter%2Fprettier-package-json/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cameronhunter%2Fprettier-package-json/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cameronhunter","download_url":"https://codeload.github.com/cameronhunter/prettier-package-json/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247138160,"owners_count":20890002,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cli","javascript","json","package-json","prettier","pretty-print","printer"],"created_at":"2024-10-11T07:16:03.361Z","updated_at":"2025-04-04T08:05:48.404Z","avatar_url":"https://github.com/cameronhunter.png","language":"TypeScript","readme":"# Prettier `package.json`\n\n[![CI](https://github.com/cameronhunter/prettier-package-json/actions/workflows/ci.yml/badge.svg)](https://github.com/cameronhunter/prettier-package-json/actions/workflows/ci.yml)\n[![npm](https://img.shields.io/npm/v/prettier-package-json.svg)](https://www.npmjs.com/package/prettier-package-json)\n\n`prettier-package-json` is a JSON formatter inspired by `prettier`. It removes\nall original styling and ensures that the outputted `package.json` conforms to a\nconsistent style. By default it uses opinionated defaults but can be configured\nto your individual needs.\n\n## Features\n\n### Consistent key order\n\nKeys in `package.json` will be sorted in an\n[opinionated order](src/defaultOptions.ts) but may be configured to your own\npreferences.\n\nInput:\n\n```json\n{\n  \"description\": \"Prettier formatter for package.json files\",\n  \"name\": \"prettier-package-json\",\n  \"license\": \"MIT\",\n  \"version\": \"1.0.1\",\n  \"author\": \"Cameron Hunter \u003chello@cameronhunter.co.uk\u003e\"\n}\n```\n\nOutput:\n\n```json\n{\n  \"name\": \"prettier-package-json\",\n  \"description\": \"Prettier formatter for package.json files\",\n  \"author\": \"Cameron Hunter \u003chello@cameronhunter.co.uk\u003e\",\n  \"license\": \"MIT\",\n  \"version\": \"1.0.1\"\n}\n```\n\n### Sensibly sorted scripts\n\nThe `scripts` field is sorted alphabetically but keeps `pre` and `post` scripts\nsurrounding their named script.\n\nInput:\n\n```json\n{\n  \"name\": \"prettier-package-json\",\n  \"version\": \"1.0.1\",\n  \"scripts\": {\n    \"test\": \"test\",\n    \"pretest\": \"pretest\",\n    \"version\": \"version\",\n    \"postversion\": \"postversion\",\n    \"build\": \"build\"\n  }\n}\n```\n\nOutput:\n\n```json\n{\n  \"name\": \"prettier-package-json\",\n  \"version\": \"1.0.1\",\n  \"scripts\": {\n    \"build\": \"build\",\n    \"pretest\": \"pretest\",\n    \"test\": \"test\",\n    \"version\": \"version\",\n    \"postversion\": \"postversion\"\n  }\n}\n```\n\n### Expand/contract author, contributors, and maintainers\n\nThe `author`, `contributors` and `maintainers` fields will be shortened to their\nstring versions and sorted by name. Use the `--expand-users` option if you\nprefer user objects.\n\nInput:\n\n```json\n{\n  \"name\": \"prettier-package-json\",\n  \"version\": \"1.0.1\",\n  \"author\": {\n    \"name\": \"Cameron Hunter\",\n    \"email\": \"hello@cameronhunter.co.uk\",\n    \"url\": \"https://cameronhunter.co.uk\"\n  },\n  \"contributors\": [\"Barry\", \"Adam \u003cadam@email.com\u003e\"]\n}\n```\n\nOutput:\n\n```json\n{\n  \"name\": \"prettier-package-json\",\n  \"version\": \"1.0.1\",\n  \"author\": \"Cameron Hunter \u003chello@cameronhunter.co.uk\u003e (https://cameronhunter.co.uk)\",\n  \"contributors\": [\"Adam \u003cadam@email.com\u003e\", \"Barry\"]\n}\n```\n\n### Filter and sort files field\n\nSome files are included or excluded automatically by `npm`, these are removed\nfrom the `files` field before sorting alphabetically.\n\nInput:\n\n```json\n{\n  \"name\": \"prettier-package-json\",\n  \"version\": \"1.0.1\",\n  \"main\": \"src/index.js\",\n  \"files\": [\n    \"src/index.js\",\n    \"src\",\n    \"CHANGELOG.md\",\n    \"readme.md\",\n    \"package-lock.json\"\n  ]\n}\n```\n\nOutput:\n\n```json\n{\n  \"name\": \"prettier-package-json\",\n  \"version\": \"1.0.1\",\n  \"main\": \"src/index.js\",\n  \"files\": [\"src\"]\n}\n```\n\n## Usage\n\nInstall:\n\n```sh\nyarn add prettier-package-json --dev\n```\n\nYou can install it globally if you like:\n\n```sh\nyarn global add prettier-package-json\n```\n\n_We're defaulting to yarn but you can use npm if you like:_\n\n```sh\nnpm install [-g] prettier-package-json\n```\n\n### CLI\n\nRun `prettier-package-json` through the CLI with this script. Run it without any\narguments to see the options.\n\nTo format a file in-place, use `--write`. You may want to consider committing\nyour file before doing that, just in case.\n\n```sh\nprettier-package-json [opts] [filename]\n```\n\nIn practice, this may look something like:\n\n```sh\nprettier-package-json --write ./package.json\n```\n\n#### Pre-commit hook for changed files\n\nYou can use this with a pre-commit tool. This can re-format your files that are\nmarked as \"staged\" via git add before you commit.\n\n##### 1. [lint-staged](https://github.com/okonet/lint-staged)\n\nInstall it along with [husky](https://github.com/cameronhunter/husky):\n\n```bash\nyarn add lint-staged husky --dev\n```\n\nand add this config to your `package.json`:\n\n```json\n{\n  \"scripts\": {\n    \"precommit\": \"lint-staged\"\n  },\n  \"lint-staged\": {\n    \"package.json\": [\"prettier-package-json --write\", \"git add\"]\n  }\n}\n```\n\nSee https://github.com/okonet/lint-staged#configuration for more details about\nhow you can configure lint-staged.\n\n##### 2. bash script\n\nAlternately you can just save this script as `.git/hooks/pre-commit` and give it\nexecute permission:\n\n```bash\n#!/bin/sh\npackagejsonfiles=$(git diff --cached --name-only --diff-filter=ACM | grep 'package\\.json$' | tr '\\n' ' ')\n[ -z \"$packagejsonfiles\" ] \u0026\u0026 exit 0\n\ndiffs=$(node_modules/.bin/prettier-package-json -l $packagejsonfiles)\n[ -z \"$diffs\" ] \u0026\u0026 exit 0\n\necho \"here\"\necho \u003e\u00262 \"package.json files must be formatted with prettier-package-json. Please run:\"\necho \u003e\u00262 \"node_modules/.bin/prettier-package-json --write \"$diffs\"\"\n\nexit 1\n```\n\n### API\n\nThe API has two functions, exported as `format` and `check`. Usage is as\nfollows:\n\n```js\nimport { format, check } from 'prettier-package-json';\n\nconst options = {}; // optional\n\nformat(json, options);\ncheck(json, options);\n```\n\n`check` checks to see if the file has been formatted with\n`prettier-package-json` given those options and returns a Boolean. This is\nsimilar to the `--list-different` parameter in the CLI and is useful for running\nin CI scenarios.\n\n### CI\n\nFor usage in CI scenarios, you can use the `--list-different` CLI flag. The\ncommand will list all invalid files and return with a proper default error code,\nso that in case of an error or invalid file the build pipeline automatically\nfails.\n\nThese are the status codes:\n\n- `0`: all files valid, no error occured.\n- `1`: an error ocurred, for example a JSON parse error. See message on `stderr`\n  for details.\n- `2`: not all files are valid.\n\nThese exit codes are only set when in `--list-different` mode.\n\n### Options\n\n`prettier-package-json` ships with a handful of customizable format options,\nusable in both the CLI, API, and configuration file.\n\n| Option                                                              | Default                                      | CLI override                            | API override                  |\n| ------------------------------------------------------------------- | -------------------------------------------- | --------------------------------------- | ----------------------------- |\n| **Tab Width** - Specify the number of spaces per indentation-level. | `2`                                          | `--tab-width \u003cint\u003e`                     | `tabWidth: \u003cint\u003e`             |\n| **Tabs** - Indent lines with tabs instead of spaces.                | `false`                                      | `--use-tabs`                            | `useTabs: \u003cbool\u003e`             |\n| **Expand Users** - Expand author and contributors into objects.     | `false`                                      | `--expand-users`                        | `expandUsers: \u003cbool\u003e`         |\n| **Key Order** - Specify the order of keys.                          | See [default options](src/defaultOptions.ts) | `--key-order \u003ccomma,separated,list...\u003e` | `keyOrder: \u003carray\\|function\u003e` |\n\nA configuration file will be searched for using\n[`cosmiconfig`](https://www.npmjs.com/package/cosmiconfig) rules:\n\n- `prettier-package-json` field in `package.json`.\n- `prettier-package-json` file (JSON or YAML), extentionless \"rc\" file.\n- `prettier-package-json` file with the extensions `.json`, `.yaml`, `.yml`,\n  `.js`, or `.cjs`.\n- `prettier-package-json.config.js` or `prettier-package-json.config.cjs`\n  CommonJS module.\n\nConfiguration file may also be passed using the `--config` CLI parameter.\n\n## Contributing\n\nSee [CONTRIBUTING.md](CONTRIBUTING.md).\n","funding_links":[],"categories":["TypeScript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcameronhunter%2Fprettier-package-json","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcameronhunter%2Fprettier-package-json","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcameronhunter%2Fprettier-package-json/lists"}