prettier/docs/cli.md

139 lines
4.1 KiB
Markdown
Raw Normal View History

---
id: cli
title: CLI
---
Run Prettier through the CLI with this script. Run it without any arguments to see the [options](options.md).
To format a file in-place, use `--write`. You may want to consider committing your code before doing that, just in case.
```bash
prettier [opts] [filename ...]
```
In practice, this may look something like:
```bash
prettier --single-quote --trailing-comma es5 --write "{app,__{tests,mocks}__}/**/*.js"
```
Don't forget the quotes around the globs! The quotes make sure that Prettier expands the globs rather than your shell, for cross-platform usage. The [glob syntax from the glob module](https://github.com/isaacs/node-glob/blob/master/README.md#glob-primer) is used.
Prettier CLI will ignore files located in `node_modules` directory. To opt-out from this behavior use `--with-node-modules` flag.
## `--debug-check`
If you're worried that Prettier will change the correctness of your code, add `--debug-check` to the command. This will cause Prettier to print an error message if it detects that code correctness might have changed. Note that `--write` cannot be used with `--debug-check`.
## `--find-config-path` and `--config`
If you are repeatedly formatting individual files with `prettier`, you will incur a small performance cost when prettier attempts to look up a [configuration file](configuration.md). In order to skip this, you may ask prettier to find the config file once, and re-use it later on.
```bash
prettier --find-config-path ./my/file.js
./my/.prettierrc
```
This will provide you with a path to the configuration file, which you can pass to `--config`:
```bash
prettier --config ./my/.prettierrc --write ./my/file.js
```
You can also use `--config` if your configuration file lives somewhere where prettier cannot find it, such as a `config/` directory.
If you don't have a configuration file, or want to ignore it if it does exist, you can pass `--no-config` instead.
## `--ignore-path`
Path to a file containing patterns that describe files to ignore. By default, prettier looks for `./.prettierignore`.
## `--require-pragma`
Require a special comment, called a pragma, to be present in the file's first docblock comment in order for prettier to format it.
```js
/**
* @prettier
*/
```
Valid pragmas are `@prettier` and `@format`.
## `--insert-pragma`
Insert a `@format` pragma to the top of formatted files when pragma is absent. Works well when used in tandem with `--require-pragma`.
## `--list-different`
Another useful flag is `--list-different` (or `-l`) which prints the filenames of files that are different from Prettier formatting. If there are differences the script errors out, which is useful in a CI scenario.
```bash
prettier --single-quote --list-different "src/**/*.js"
```
## `--no-config`
Do not look for a configuration file. The default settings will be used.
## `--config-precedence`
Defines how config file should be evaluated in combination of CLI options.
**cli-override (default)**
CLI options take precedence over config file
**file-override**
Config file take precedence over CLI options
**prefer-file**
If a config file is found will evaluate it and ignore other CLI options. If no config file is found CLI options will evaluate as normal.
This option adds support to editor integrations where users define their default configuration but want to respect project specific configuration.
Re-add EditorConfig support (undo #3213) (#3255) * Revert "Revert "Respect EditorConfig settings" (#3213)" This reverts commit d2241fc0d52d807701d9a5ac580bc01010e7a93b. * Comment out EditorConfig docs See https://github.com/prettier/prettier/pull/3213#issuecomment-343009769 * editorconfig: Support `indent_size = 0` See https://github.com/prettier/prettier/pull/2760#discussion_r137447715 and https://github.com/josephfrazier/editorconfig-to-prettier/commit/c38b84c42a2e022067c104c494298a8c9533e7a7 * Revert "Comment out EditorConfig docs" This reverts commit ddfa529c55cac4853a1e76e00c8b5e3ef158c01f. * Mark EditorConfig functionality as v1.9.0+ See https://github.com/prettier/prettier/pull/3255#discussion_r150432508 * editorconfig: Upgrade editorconfig-to-prettier to 0.0.4 * editorconfig: Only enable for CLI, by default https://github.com/prettier/prettier/pull/3255#issuecomment-348420546 * editorconfig: Add tests confirming that editorconfig is ignored by default in the API https://github.com/prettier/prettier/pull/3255#issuecomment-348420546 * editorconfig: Add/fix CLI option parsing * editorconfig: Move docs from configuration.md to options.md * editorconfig: Add `oppositeDescription` to show docs for `--no-editorconfig` Addresses https://github.com/prettier/prettier/pull/3255#discussion_r154542792 * editorconfig: Update test snapshots * editorconfig: Remove unnecessary options parsing code Addresses https://github.com/prettier/prettier/pull/3255#discussion_r154544560 * editorconfig: Move docs from options.md to api.md and cli.md Addresses https://github.com/prettier/prettier/pull/3255#discussion_r154545979 * resolveConfig: return null if both .prettierrc and .editorconfig are missing Addresses https://github.com/prettier/prettier/pull/3255#discussion_r154574613 * Don't add now-failing tests The way these tests work, both `tests_integration/cli/config/.prettierrc` and `.prettierrc` apply to `tests_integration/cli/config/editorconfig/file.shouldnotexist`, so the test wouldn't work even on master. Here's a way to confirm that: ```js const path = require('path') const assert = require('assert') const prettier = require('./') const file = './tests_integration/cli/config/editorconfig/file.shouldnotexist' console.log(prettier.resolveConfig.sync(file)) assert(prettier.resolveConfig.sync(file) === null) ```
2017-12-05 01:28:27 +03:00
## `--no-editorconfig`
Don't take .editorconfig into account when parsing configuration. See the [`prettier.resolveConfig` docs](./api.md) for details.
## `--with-node-modules`
Prettier CLI will ignore files located in `node_modules` directory. To opt-out from this behavior use `--with-node-modules` flag.
## `--write`
This rewrites all processed files in place. This is comparable to the `eslint --fix` workflow.
## `--loglevel`
Change the level of logging for the CLI. Valid options are:
2018-05-27 21:37:44 +03:00
- `error`
- `warn`
- `log` (default)
- `debug`
- `silent`
## `--stdin-filepath`
A path to the file that the Prettier CLI will treat like stdin. For example:
_abc.css_
2018-06-28 22:25:56 +03:00
```css
2018-06-28 22:25:56 +03:00
.name {
display: none;
}
```
_shell_
2018-06-28 22:25:56 +03:00
```bash
$ cat abc.css | prettier --stdin-filepath abc.css
.name {
display: none;
}
```