nginx-config-formatter
v1.4.5
Published
A formatter for Nginx config files
Downloads
2,530
Readme
Nginx Config Formatter
A formatter for Nginx config files. Both API and CLI are available.
Features
Features from nginxbeautifier
- all lines are indented in an uniform manner, with 4 spaces per level
- neighbouring empty lines are collapsed to at most two empty lines
- curly braces placement follows Java convention
- whitespaces are collapsed, except in comments and quotation marks
Plus we have:
- empty lines after
{
or before}
are trimmed - an API with full TypeScript support
- tests to ensure consistency
CLI Usage
npm i -g nginx-config-formatter
Or
pnpm i -g nginx-config-formatter
nginx-config-formatter -p /etc/nginx
available options:
-p, --path (required) the path of files to be formatted. Support glob syntax
-h, --help display usage help
-i, --indentStyle style of indentation, space or tab, defaults to space
-c, --dontJoinCurlyBracket if true, the opening bracket starts with a new line, defaults to false
-a, --align align the values of all directives in the same block, defaults to false
-l, --trailingBlankLines Append a trailing blank line after the opening bracket, defaults to false
-ext, --extension the file extension of nginx config files, defaults to conf
API Usage
formatFile(path: string, options?: OptionType): Promise<void>
path
can be the path of a .conf
file, or a folder contains .conf
file. Glob syntax is also supported. If a folder is provided, all .conf
files including those in its sub folders will be formatted.
formatContent(content: string, options?: OptionType): string
content
is the full content of a config file. Returns a string of the formatted content.
The APIs above accepts a option object. All options are optional and its default value is the same as the CLI.
interface OptionType {
indentStyle?: 'space' | 'tab'
dontJoinCurlyBracket?: boolean
align?: boolean
trailingBlankLines?: boolean
extension?: string
}
Example
import { formatFile } from 'nginx-config-formatter'
formatFile('/etc/nginx', {
indentStyle: 'tab'
})
Changelog
See CHANGELOG
Credits
This project is inspired by nginxbeautifier and use some of its code.
Acknowledgment
If you found it useful somehow, I would be grateful if you could leave a star in the project's GitHub repository.
Thank you.