• Stars
    star
    541
  • Rank 82,114 (Top 2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 8 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

CLI for prettier-eslint

prettier-eslint-cli

CLI for prettier-eslint

Build Status Code Coverage version downloads MIT License changesets

Open Collective backers and sponsors All Contributors PRs Welcome Donate Code of Conduct Roadmap Examples

Watch on GitHub Star on GitHub Tweet

The problem

You have a bunch of files that you want to format using prettier-eslint. But prettier-eslint can only operate on strings.

This solution

This is a CLI that allows you to use prettier-eslint on one or multiple files. prettier-eslint-cli forwards on the filePath and other relevant options to prettier-eslint which identifies the applicable ESLint config for each file and uses that to determine the options for prettier and eslint --fix.

Installation

This module is distributed via npm which is bundled with node and should be installed (with yarn) as one of your project's devDependencies:

yarn add --dev prettier-eslint-cli

If you're still using the npm client: npm install --save-dev prettier-eslint-cli

Usage

Typically you'll use this in your npm scripts (or package scripts):

{
  "scripts": {
    "format": "prettier-eslint \"src/**/*.js\""
  }
}

This will format all .js files in the src directory. The argument you pass to the CLI is a glob and you can pass as many as you wish. You can also pass options.

Vim

Vim users can add the following to their .vimrc:

autocmd FileType javascript set formatprg=prettier-eslint\ --stdin

This makes prettier-eslint-cli power the gq command for automatic formatting without any plugins. You can also add the following to your .vimrc to run prettier-eslint-cli when .js files are saved:

autocmd BufWritePre *.js :normal gggqG

CLI Options

prettier-eslint --help
Usage: prettier-eslint <globs>... [--option-1 option-1-value --option-2]

Prefix an option with "no-" to set it to false, such as --no-semi to
disable semicolons and --no-eslint-ignore to disable default ignores.

Options:
  -h, --help               Show help                                   [boolean]
  --version                Show version number                         [boolean]
  --write                  Edit the file in-place (beware!)
                                                      [boolean] [default: false]
  --stdin                  Read input via stdin       [boolean] [default: false]
  --stdin-filepath         Path to the file to pretend that stdin comes from.
  --eslint-ignore          Only format matching files even if they are not
                           ignored by .eslintignore. (can use --no-eslint-ignore
                           to disable this)            [boolean] [default: true]
  --prettier-ignore        Only format matching files even if they are not
                           ignored by .prettierignore. (can use
                           --no-prettier-ignore to disable this)
                                                       [boolean] [default: true]
  --list-different         Print filenames of files that are different from
                           Prettier + Eslint formatting.
                                                      [boolean] [default: false]
  --include-dot-files      Include files that start with a dot in the search.
                                                      [boolean] [default: false]
  --eslint-path            The path to the eslint module to use
                 [default: "./node_modules/eslint"]
  --eslint-config-path     Path to the eslint config to use for eslint --fix
  --prettier-path          The path to the prettier module to use [default: "./node_modules/prettier"]
  --config                 Path to the prettier config
  --ignore                 pattern(s) you wish to ignore (can be used multiple
                           times and includes **/node_modules/** automatically)
  --log-level, -l          The log level to use
        [choices: "silent", "error", "warn", "info", "debug", "trace"] [default:
                                                                         "warn"]
  --prettier-last          Run prettier last          [boolean] [default: false]
  --use-tabs               Indent lines with tabs instead of spaces.   [boolean]
  --print-width            Specify the length of line that the printer will wrap
                           on.                                          [number]
  --tab-width              Specify the number of spaces per indentation-level.
                                                                        [number]
  --trailing-comma         Print trailing commas wherever possible.

                           Valid options:
                           - "none" - no trailing commas
                           - "es5" - trailing commas where valid in ES5
                           (objects, arrays, etc)
                           - "all" - trailing commas wherever possible (function
                           arguments)   [string] [choices: "none", "es5", "all"]
  --bracket-spacing        Print spaces between brackets in object literals.
                           Can use --no-bracket-spacing for "false" to disable
                           it.

                           Valid options:
                           - true - Example: { foo: bar }
                           - false - Example: {foo: bar}               [boolean]
  --jsx-bracket-same-line  Put the > of a multi-line JSX element at the end of
                           the last line instead of being alone on the next line
                                                                       [boolean]
  --parser                 Specify which parser to use.                 [string]
  --semi                   Print semicolons at the ends of statements.
                           Can use --no-semi.

                           Valid options:
                           - true - add a semicolon at the end of every
                           statement
                           - false - only add semicolons at the beginning of
                           lines that may introduce ASI failures       [boolean]
  --single-quote           Use single quotes instead of double quotes. [boolean]

Any number of globs you wish to use to match the files you wish to format. By default, glob will ignore **/node_modules/** unless the glob you provide includes the string node_modules.

--write

By default prettier-eslint will simply log the formatted version to the terminal. If you want to overwrite the file itself (a common use-case) then add --write. You should quote your globs, otherwise your terminal will expand the glob before it gets to prettier-eslint (which can have unexpected results):

{
  "scripts": {
    "format": "prettier-eslint --write \"src/**/*.js\""
  }
}

NOTE: It is recommended that you keep your files under source control and committed before running prettier-eslint --write as it will overwrite your files!

--list-different

Instead of printing the formatted version of the files to the terminal, prettier-eslint will log the name of the files that are different from the expected formatting. This can be usefull when using prettier-eslint in a version control system hook to inform the committer which files need to be formatted.

--stdin

Accept input via stdin. For example:

echo "var   foo =    'bar'" | prettier-eslint --stdin
# results in: "var foo = 'bar';" (depending on your eslint config)

--eslint-path

Forwarded as the eslintPath option to prettier-eslint

--eslint-config-path

Resolve eslint config file, parse and forward config object as the eslintConfig option to prettier-eslint

--prettier-path

Forwarded as the prettierPath option to prettier-eslint

--log-level

Forwarded as logLevel option to prettier-eslint

--no-eslint-ignore

Disables application of .eslintignore to the files resolved from the glob. By default, prettier-eslint-cli will exclude files if they are matched by a .eslintignore. Add this flag to disable this behavior.

Note: You can also set the LOG_LEVEL environment variable to control logging in prettier-eslint

--prettier-last

By default, prettier-eslint-cli will run prettier first, then eslint --fix. This is great if you want to use prettier, but override some of the styles you don't like using eslint --fix.

An alternative approach is to use different tools for different concerns. If you provide the argument --prettier-last, it will run eslint --fix first, then prettier. This allows you to use eslint to look for bugs and/or bad practices, and use prettier to enforce code style.

prettier options

prettier-eslint-cli also supports the same command line options as prettier.

For example: prettier-eslint --trailing-comma es5

Refer to the prettier-eslint docs for documentation on these options

Integration

Any linter that support ESLint CLIEngine interface can be integrate with prettier-eslint

Knowed integrated package helpers

Standalone CLI tools based on prettier-eslint-cli

Related

Contributors

Thanks goes to these people (emoji key):


Kent C. Dodds

๐Ÿ’ป ๐Ÿ“– ๐Ÿš‡ โš ๏ธ

Adam Harris

๐Ÿ’ป ๐Ÿ“– ๐Ÿ‘€

Eric McCormick

๐Ÿ‘€

Joel Sequeira

๐Ÿ“–

Frank Taillandier


Adam Stankiewicz

๐Ÿ’ป

Stephen John Sorensen

๐Ÿ’ป

Gandem

๐Ÿ’ป โš ๏ธ

Matteo Ronchi

๐Ÿ› ๐Ÿ’ป

Benoรฎt Zugmeyer

๐Ÿ’ป โš ๏ธ

Charlike Mike Reagent

๐Ÿ’ป โš ๏ธ

Dion Dirza

๐Ÿ›

mrm007

๐Ÿ› ๐Ÿ’ป

Jack Franklin

๐Ÿ’ป

Ryan Zimmerman

๐Ÿ“–

Paolo Moretti

๐Ÿ› ๐Ÿ’ป โš ๏ธ

bySabi Files

๐Ÿ“– ๐Ÿ”ง

Pavel Pertsev

๐Ÿ’ป โš ๏ธ

Josh English

โš ๏ธ ๐Ÿ› ๐Ÿ’ป ๐Ÿ”Œ

Spenser Isdahl

๐Ÿ’ป ๐Ÿ“– โš ๏ธ

Bjรถrn Dalfors

๐Ÿš‡

Steven Scaffidi

๐Ÿ“–

Daniel Wilhelmsen

๐Ÿ’ป ๐Ÿšง

Glen Whitney

๐Ÿšง

Rebecca Vest

๐Ÿ’ป

JounQin

๐Ÿ’ฌ ๐Ÿ’ป ๐ŸŽจ ๐Ÿ“– ๐Ÿค” ๐Ÿš‡ ๐Ÿšง ๐Ÿ”Œ ๐Ÿ“† ๐Ÿ‘€ โš ๏ธ ๐Ÿ”ง

dorser

๐Ÿ’ป ๐Ÿšง

ใ•ใใ‚‰

๐Ÿ’ป

This project follows the all-contributors specification. Contributions of any kind welcome!

LICENSE

MIT

More Repositories

1

prettier

Prettier is an opinionated code formatter.
JavaScript
49,325
star
2

eslint-config-prettier

Turns off all rules that are unnecessary or might conflict with Prettier.
JavaScript
5,386
star
3

prettier-vscode

Visual Studio Code extension for Prettier
TypeScript
5,130
star
4

prettier-eslint

Code โžก๏ธ prettier โžก๏ธ eslint --fix โžก๏ธ Formatted Code โœจ
JavaScript
3,994
star
5

eslint-plugin-prettier

ESLint plugin for Prettier formatting
JavaScript
3,283
star
6

pretty-quick

โšก Get Pretty Quick
TypeScript
2,211
star
7

vim-prettier

A Vim plugin for Prettier
Vim Script
1,780
star
8

plugin-php

Prettier PHP Plugin
PHP
1,736
star
9

plugin-ruby

Prettier Ruby Plugin
JavaScript
1,462
star
10

tslint-config-prettier

Use TSLint with Prettier without any conflict
TypeScript
1,234
star
11

prettier-atom

An atom package for the prettier formatter.
JavaScript
755
star
12

plugin-python

Prettier Python Plugin
JavaScript
517
star
13

prettier-emacs

Minor mode to format JS code on file save
Emacs Lisp
374
star
14

stylelint-config-prettier

Turns off all rules that are unnecessary or might conflict with prettier.
JavaScript
374
star
15

stylelint-prettier

Stylelint plugin for Prettier formatting
JavaScript
346
star
16

plugin-swift

[DEPRECATED] Prettier Swift Plugin - WARNING: The AST parser is not stable yet
JavaScript
274
star
17

tslint-plugin-prettier

Runs Prettier as a TSLint rule and reports differences as individual TSLint issues
TypeScript
235
star
18

plugin-xml

Prettier XML plugin
JavaScript
229
star
19

prettier-browser-extension

Prettier Browser Extension
JavaScript
200
star
20

plugin-pug

Prettier Pug Plugin
TypeScript
197
star
21

plugin-lua

Prettier Lua Plugin (WIP)
Lua
78
star
22

prettier-logo

The Prettier logo.
JavaScript
39
star
23

prettier-cli

TypeScript
28
star
24

yaml-unist-parser

A YAML parser that produces output compatible with unist
TypeScript
25
star
25

prettier-synchronized

Synchronous version of Prettier
JavaScript
24
star
26

stylelint-config-prettier-scss

Turns off all CSS and SCSS rules that are unnecessary or might conflict with prettier.
JavaScript
24
star
27

prettier-printer

Library for building and pretty printing text documents
JavaScript
23
star
28

prettier-linter-helpers

Helper functions for exposing prettier changes within linting tools
JavaScript
18
star
29

angular-estree-parser

A parser that converts Angular source code into an ESTree-compatible form
TypeScript
16
star
30

prettier-rpc

Single-file build of prettier with JSON-RPC communication
JavaScript
13
star
31

prettier-regression-testing

Automates the prettier/prettier regression checks with GitHub Actions.
TypeScript
12
star
32

parse-srcset

A spec-conformant JavaScript parser for the HTML5 srcset attribute
JavaScript
9
star
33

plugin-haml

Plugin for the HAML template language
JavaScript
5
star
34

pre-commit

Mirror of Prettier package for pre-commit.
JavaScript
5
star
35

core-test-fixtures

Test fixtures for Prettier core.
JavaScript
2
star
36

is-es5-identifier-name

Check if provided string is an `IdentifierName` as specified in ECMA262 edition 5.1 section 7.6.
JavaScript
2
star
37

html-ua-styles

User agent stylesheet defined in the WHATWG HTML specification.
JavaScript
1
star
38

eslint-plugin-prettier-internal-rules

Wrapper for Prettier internal rules
JavaScript
1
star