prettier-cli-watcher
Runs prettier (from CLI) on project file(s) when modified.
Last updated 6 days ago by cgabriel5 .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install prettier-cli-watcher 
SYNC missed versions from official npm registry.

prettier-cli-watcher

Table of Contents

Overview

Runs prettier (from CLI) on project files when modified.

Features

  • Runs prettier on file when modified.
    • Pressing <kbd>ctrl</kbd> + <kbd>s</kbd> in rapid succession on a file runs prettier only on the last save.
  • Sends OS notification and logs location of error when prettier fails to format file.
    • Logging/notifications on by default (can be disabled).
  • Per-extension prettier configurations.
    • Have different prettier configurations for different file extensions.
    • For example, have a universal configuration for all allowed files but a different one for .md files.

Install

# npm
npm install prettier-cli-watcher --save-dev

# yarn
yarn add prettier-cli-watcher --dev

How To Use

Ubuntu/macOS

// Add following script to your package.json scripts. Make sure to provide the
// actual path to your prettier config file.
...
"scripts": {
  "pretty": "prettier-cli-watcher --configpath='path/to/prettier.config.json'"
}
...

Windows

// If using Windows escape (\") options where needed or don't use (') as shown:
...
"scripts": {
  // Surround value with \" to escape:
  "pretty": "prettier-cli-watcher --configpath=\"/configs/prettier.config.json\"",

  // Or don't use apostrophes ('):
  "pretty": "prettier-cli-watcher --configpath=/configs/prettier.config.json"
}
...
# Then run with npm:
npm run pretty

# or yarn:
yarn run pretty

Options

Available parameters (supplied via script command - see examples):

  • --dir="."
    • The directory to watch.
    • By default watches entire project for file changes.
  • --configpath="path/to/prettier.config.json" (required)
    • The path to your prettier config file.
  • --ignoredirs="node_modules|bower_components|.git|dist"
    • The folders to ignore file changes from.
    • Folders shown above are the default ignored folders.
    • Pass in custom list to override defaults.
  • --extensions="js|ts|jsx|json|css|scss|sass|less|html|vue|md|yaml|graphql"
    • The file extensions to watch out for. Must also be allowed by prettier.
    • Extensions shown above are the default allowed extensions.
    • Pass in custom list to override defaults.
  • --nonotify
    • Provide to disable OS notifications.
  • --nolog
    • Provide to disable command line output.
  • --watcher="chokidar|hound"
    • The file watcher to use.
    • By default uses chokidar but can also use hound.
  • --dtime=500 (deflect time)
    • The amount of time, in milliseconds, to deflect and ignore rapid file modifications.
      • A larger time allows for more rapid file changes to be caught and ignored.
    • By default --dtime=500 is used (500 milliseconds).
      • --dtime=0 disables deflection check logic all together.

Examples

...
"scripts": {
  // Default setup...
  "pretty": "prettier-cli-watcher --configpath='path/to/prettier.config.json' --dir='.' --ignoredirs='node_modules|bower_components|.git|dist' --extensions='js|ts|jsx|json|css|scss|sass|less|html|vue|md|yaml|graphql' --watcher='chokidar' --dtime='500'",
  // ...is the same as:
  "pretty": "prettier-cli-watcher --configpath='path/to/prettier.config.json'",

  // Example 1:
  // Disable OS notifications while keeping --dir, --ignoredirs, and --extensions defaults.
  "pretty": "prettier-cli-watcher --configpath='path/to/prettier.config.json' --nonotify",

  // Example 2:
  // Only watch for ./src file changes made to .js files. Notifications and logging is left on.
  "pretty": "prettier-cli-watcher --configpath='path/to/prettier.config.json' --dir='./src' --extensions='js'",

  // Example 3:
  // Use all defaults but change file watcher to hound.
  "pretty": "prettier-cli-watcher --configpath='path/to/prettier.config.json' --watcher='hound'"
}
...

Per-extension Configurations

The following configuration will be applied to all allowed file extensions (universal configuration).

{ // prettier.config.json
  "bracketSpacing": true,
  "jsxBracketSameLine": false,
  "printWidth": 80,
  "semi": true
  ...other settings
}

Example per-extension configuration file:

  • *: The universal prettier configuration (required).
  • md: Configuration for .md files.
  • Other needed file extension configurations.
{ // prettier.config.json
  "*": { // Universal configuration to apply to all allowed file extensions.
    "bracketSpacing": true,
    "jsxBracketSameLine": false,
    "printWidth": 80,
    "semi": true
    ...other settings
  },
  "md": { // This configuration is only applied to .md files.
    "bracketSpacing": true,
    "jsxBracketSameLine": false,
    "printWidth": 120,
    "semi": true,
    "singleQuote": false,
    "tabWidth": 2,
    "trailingComma": "none",
    "useTabs": false
  }
  ...any other extension configurations...
  "json": {
    ...settings
  },
  "html": {
    ...settings
  }
  ...
}

When using a per-extension configuration file the used prettier configuration will get logged as shown.

OS Support

  • Made using Node.js v8.14.0 on a Linux machine running Ubuntu 16.04.5 LTS.
  • Tested and working on:
    • macOS High Sierra (v10.13).
    • Windows 10 (v1703 OS Build: 15063.674).

Contributing

Contributions are welcome! Found a bug, feel like documentation is lacking/confusing and needs an update, have performance/feature suggestions or simply found a typo? Let me know! :)

See how to contribute here.

Attribution

License

This project uses the MIT License.

Current Tags

  • 0.3.5                                ...           latest (6 days ago)

16 Versions

  • 0.3.5                                ...           6 days ago
  • 0.3.4                                ...           4 months ago
  • 0.3.3                                ...           6 months ago
  • 0.3.2                                ...           6 months ago
  • 0.3.1                                ...           10 months ago
  • 0.3.0                                ...           10 months ago
  • 0.2.9                                ...           a year ago
  • 0.2.8                                ...           a year ago
  • 0.2.7                                ...           a year ago
  • 0.2.6                                ...           a year ago
  • 0.1.5                                ...           a year ago
  • 0.0.5                                ...           a year ago
  • 0.0.4                                ...           a year ago
  • 0.0.3                                ...           a year ago
  • 0.0.2                                ...           a year ago
  • 0.0.1                                ...           a year ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 11
Last Day 0
Last Week 11
Last Month 2
Dependencies (13)
Dev Dependencies (5)
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |