Download the PHP package piotrpress/composer-formatter without Composer
On this page you can find all versions of the php package piotrpress/composer-formatter. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download piotrpress/composer-formatter
More information about piotrpress/composer-formatter
Files in piotrpress/composer-formatter
Package composer-formatter
Short Description This Composer plugin formats (sorts and normalizes) composer.json and other JSON files according to defined rules.
License MIT
Homepage https://github.com/PiotrPress/composer-formatter
Informations about the package composer-formatter
Composer Formatter
This Composer plugin formats (sorts and normalizes) composer.json and other JSON files according to defined rules.
It provides composer format command for formatting and checking files, as well as reusable GitHub Workflows for automated formatting in CI/CD pipelines.
Installation
Install the plugin as a development dependency in your project:
Usage
Arguments
input- Path to input JSON file (default:composer.jsonin current directory).
Options
--output|-o- Path to output JSON file. If flag provided without value, outputs to STDOUT (default: overwrite input JSON file).--format|-f- Path to config JSON file with formatting rules or inline JSON string. Falls back toextra.formatsection incomposer.jsonor default rules.--check|-c- Check if the file is properly formatted without making changes. Exits with code0if formatted,1otherwise.
Example
-
Format
composer.jsonin place: -
Format a custom JSON file:
-
Check if file is properly formatted:
-
Print formatted output to STDOUT:
-
Write formatted output to a different file:
- Format file using custom formatting rules:
Configuration
Formatting rules can be defined via --format|-f option or composer.json under the extra.format key. Format option always takes precedence over JSON key.
Configuration options
schema-path- Path to JSON schema used for key ordering (default: Composer schema).order-array- Custom order (default: empty array).breaker-cr,lf,crlf- Line breaker style (default:lf).indent.style-space,tab- Indentation style (default:space).indent.size-integer- Indentation size (default:4).
Default configuration
Custom order configuration
The order option accepts an object where each key is a dot-notation path or glob pattern pointing to a section of JSON file, and the value defines the sorting behavior for that section:
[ "key1", "key2", ... ]- An array of keys that specifies the exact order of keys in that section. Keys not listed will be sorted alphabetically after the specified keys.false- Disables sorting for that section, preserving the original order of keys.null- Sorts all keys in that section alphabetically.
Wildcard * in the path matches any array index (e.g. authors.* applies rules to every item in the authors array).
Example
GitHub Workflows
This plugin also comes with two GitHub Reusable Workflows that can be used to automatically check and fix formatting of JSON files in your project on GitHub:
- format.yml - Automatically formats the file and commits the changes if needed.
- check.yml - Checks if the file is properly formatted and fails if not.
and additionally two ready-to-use workflow files that utilize the above reusable workflows to check and fix formatting of composer.json file in your project:
- composer-format.yml - Automatically formats
composer.jsonfile and commits the changes if needed. - composer-check.yml - Checks if
composer.jsonfile is properly formatted and fails if not.
Options for format.yml reusable workflow
input- Path to input JSON file (default:composer.jsonin current directory).output- Path to JSON file to save formatted content (default: overwrite input file).format- Path to JSON file or string with formatting rules (default: useextra.formatsection incomposer.jsonor default rules if not defined).message- Commit message for changes (default:chore: format composer.json).
Custom example usage of format.yml in your workflow
Options for check.yml reusable workflow
input- Path to input JSON file (default:composer.jsonin current directory).format- Path to JSON file or string with formatting rules (default: useextra.formatsection incomposer.jsonor default rules if not defined).
Custom example usage of check.yml in your workflow
Recommendations
-
Install the plugin as a separate tool in your project:
- Add a script to project's
composer.jsonfile for easy access:
Requirements
- PHP
>= 8.2 - Composer
^2.0
License
MIT
All versions of composer-formatter with dependencies
php Version >=8.2