0.3.0 • Published 1 year ago

@imoverlord/yamlfmt v0.3.0

Weekly downloads
-
License
apache-2.0
Repository
github
Last release
1 year ago

Project license

Pull Requests welcome code with love by ImOverlord


About

When building tools that manipulate YAML files, I would encounter issues where they wouldn't respect the formatting rules. Instead of building another YAML formatter, I wanted to try to include the formatter directly from JS. Using WASM, this tool calls yamlfmt directly with the specified config.

Getting Started

Prerequisites

Explain what is needed to run the project

Installation

npm i @imoverlord/yamlfmt

Usage

import { formatYAML } from "./index.ts"

const yaml = `
app:
  versions:
    - name: cake
      version: 1.0.0
    - name: cookie
      version: 2.0.0
`

await formatYAML(yaml)

formatYAML will start the Go wasm if needed and the directly pass the string to yamlfmt go formatter.

You can specify a config if needed. The config should support all options from yamlfmt config.

import { formatYAML } from "./index.ts"

const yaml = `
app:
  versions:
    - name: cake
      version: 1.0.0
    - name: cookie
      version: 2.0.0
`

await formatYAML(yaml, { indentlessArrays: true })

Config

NameTypeDefaultDescription
indentint2The indentation level in spaces to use for the formatted yaml.
includeDocumentStartboolfalseInclude --- at document start.
lineEndinglf or crlfcrlf on Windows, lf otherwiseParse and write the file with "lf" or "crlf" line endings. This setting will be overwritten by the global line_ending.
retainLineBreaksboolfalseRetain line breaks in formatted yaml.
retainLineBreaksSingleboolfalse(NOTE: Takes precedence over retain_line_breaks) Retain line breaks in formatted yaml, but only keep a single line in groups of many blank lines.
disallowAnchorsboolfalseIf true, reject any YAML anchors or aliases found in the document.
maxLineLengthint0Set the maximum line length (see note below). if not set, defaults to 0 which means no limit.
scanFoldedAsLiteralboolfalseOption that will preserve newlines in folded block scalars (blocks that start with >).
indentlessArraysboolfalseRender - array items (block sequence items) without an increased indent.
dropMergeTagboolfalseAssume that any well formed merge using just a << token will be a merge, and drop the !!merge tag from the formatted result.
padLineCommentsint1The number of padding spaces to insert before line comments.
trimTrailingWhitespaceboolfalseTrim trailing whitespace from lines.
eofNewlineboolfalseAlways add a newline at end of file. Useful in the scenario where retainLineBreaks is disabled but the trailing newline is still needed.
stripDirectivesboolfalseYAML Directives are not supported by this formatter. This feature will attempt to strip the directives before formatting and put them back. Use this feature at your own risk.

Table was taken directly from yamlfmt config documentation. The keys were adapted to be typescript friendly camel case instead of pascal case.

License

This project is licensed under the Apache-2.0 license.

See LICENSE for more information.

0.3.0

1 year ago

0.2.1

1 year ago

0.2.0

1 year ago

0.1.0

1 year ago