diff --git a/docs/site/reference/formatter-spec.md b/docs/site/reference/formatter-spec.md index 30051133..792f51e5 100644 --- a/docs/site/reference/formatter-spec.md +++ b/docs/site/reference/formatter-spec.md @@ -30,8 +30,8 @@ The formatter's CLI must be of the form: Where: - `` is the name of the formatter executable. -- `[options]` is any number of flags and options that the formatter accepts. -- `[...]` is one or more files given to the formatter for processing. +- `[options]` are any number of flags and options that the formatter accepts. +- `[...]` are one or more files given to the formatter for processing. Example: diff --git a/docs/site/reference/stdin-spec.md b/docs/site/reference/stdin-spec.md new file mode 100644 index 00000000..99e33d7e --- /dev/null +++ b/docs/site/reference/stdin-spec.md @@ -0,0 +1,55 @@ +--- +outline: deep +--- + +# Stdin Specification + +Formatters **MAY** also implement the Stdin Specification, which allows +formatting "virtual files" passed via stdin. + +A formatter **MUST** implement the Stdin Specification if its formatting behavior +can depend on the name of the file being formatted. + +## Rules + +In order for the formatter to comply with this spec, it **MUST** implement the +vanilla [Formatter Specification](/reference/formatter-spec), and additionally +satisfy the following: + +### 1. `--stdin-filepath` flag + +The formatter's CLI **MUST** be of the form: + +``` + [options] [--stdin-filepath ] +``` + +Where: + +- `` is the name of the formatting tool. +- `[options]` are any number of flags and options that the formatter accepts. +- `--stdin-filepath ` is an optional flag that puts the formatter in + "stdin mode". In stdin mode, the formatter reads file contents from stdin + rather than the filesystem. + - The formatter _MAY_ alter its behavior based on the given ``. For + example, if a there are different formatting rules in different + directories. If the formatter's behavior doesn't depend on the given + ``, it's ok to ignore it. + - The formatter _MAY_ understand `--stdin-filepath=` as well, but **MUST** + understand the space separated variant. + +Example: + +``` +$ echo "{}" | nixfmt --stdin-filepath path/to/file.nix +``` + +### 2. Print to stdout, do not assume file is present on filesystem + +When in stdin mode, the formatter: + +1. **MUST** print the formatted file to stdout. +2. **MUST NOT** attempt to read the file on the filesystem. Instead, it + **MUST** read from stdin. +3. **MUST NOT** write to the given path on the filesytem. It _MAY_ write to + temporary files elsewhere on disk, but _SHOULD_ clean them up when done.