Skip to content
This repository has been archived by the owner on Nov 29, 2021. It is now read-only.

Commit

Permalink
Parse .prettierrc for config as well as package.json
Browse files Browse the repository at this point in the history
This uses [config-attendant] instead of [pkg-conf], in order to look for
`.prettierrc` as well as `package.json` (see prettier/prettier#918 (comment)).
I would have just used [rc], but it doesn't support reading package.json files (see dominictarr/rc#64).

[config-attendant]: https://github.com/GarthDB/config-attendant
[pkg-conf]: https://www.npmjs.com/package/pkg-conf
[rc]: https://www.npmjs.com/package/rc

---

Also, add documentation for `--pkg-conf` in general.
  • Loading branch information
josephfrazier committed May 4, 2017
1 parent da4426b commit 448139b
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 74 deletions.
8 changes: 8 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"printWidth": 80,
"tabWidth": 2,
"singleQuote": true,
"trailingComma": "es5",
"bracketSpacing": true,
"semi": false
}
40 changes: 38 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,46 @@ Options specific to prettier_d:
--local-only Fail if prettier is not in ./node_modules. Defaults to false.
If --json is specified, it will still be formatted.
If --fallback is specified, the original input will be printed.
--pkg-conf Read prettier configuration from nearest package.json to working directory.
--pkg-conf Read prettier configuration from nearest package.json/.prettierrc to working directory.
If a .prettierrc file is found, it will override package.json.
NOTE: CLI options pertaining to formatting will be ignored.
```

## Configuration file

In the spirit of https://github.com/prettier/prettier/issues/918, the `--pkg-conf` option uses the `prettier` configuration read from the nearest `.prettierrc` or `package.json` file. If `.prettierrc` is present, it's expected to be JSON, for example:

```json
{
"printWidth": 80,
"tabWidth": 2,
"singleQuote": true,
"trailingComma": "es5",
"bracketSpacing": true,
"semi": false
}
```

If `package.json` is present, its `prettier` key is expected to have the same format as `.prettierrc` above. For example:

```json
{
"prettier": {
"useTabs": false,
"printWidth": 80,
"tabWidth": 2,
"singleQuote": false,
"trailingComma": "none",
"bracketSpacing": true,
"jsxBracketSameLine": false,
"parser": "babylon",
"semi": true
}
}
```

Note that a `.prettierrc` file will override a `package.json` file. Also, these files are cached, so if you change them, you'll need to restart the server for the changes to apply: `prettier_d restart`

## Editor integration

See https://github.com/prettier/prettier#editor-integration
Expand All @@ -129,7 +165,7 @@ If you're in a repository that has `prettier` in its `node_modules/`, this will
* whenever text is changed in normal mode
* whenever you leave insert mode

The `prettier` configuration is read from `package.json` if available. Otherwise, the default prettier configuration is used. If you'd prefer a different configuration, replace `--pkg-conf` with your own options.
The `prettier` configuration is read from `.prettierrc` or `package.json` if available. Otherwise, the default prettier configuration is used. If you'd prefer a different configuration, replace `--pkg-conf` with your own options.

[Neoformat]: https://github.com/sbdchd/neoformat

Expand Down
7 changes: 5 additions & 2 deletions lib/linter.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var resolve = require('resolve')
var options = require('./options')
var path = require('path')
var unquote = require('unquote')
var pkgConf = require('pkg-conf')
var configAttendant = require('config-attendant')
var stringifySorted = require('json-stable-stringify')
var stringifyAligned = require('json-align')

Expand Down Expand Up @@ -44,7 +44,10 @@ module.exports = function(cwd, args, text) {
}
cwdDeps = prettierMap[cwd] = {
prettier: require(prettierPath),
prettierOptions: pkgConf.sync('prettier'),
// The empty objects are:
// 1. The defaults object (omitted since those are included in currentOptions)
// 2. The argv object to override what minimist might think it has (because this is the server)
prettierOptions: configAttendant('prettier', {}, {}),
}
}

Expand Down
3 changes: 2 additions & 1 deletion lib/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ module.exports.generateHelp = function() {
' --local-only Fail if prettier is not in ./node_modules. Defaults to false.',
' If --json is specified, it will still be formatted.',
' If --fallback is specified, the original input will be printed.',
' --pkg-conf Read prettier configuration from nearest package.json to working directory.',
' --pkg-conf Read prettier configuration from nearest package.json/.prettierrc to working directory.',
' If a .prettierrc file is found, it will override package.json.',
' NOTE: CLI options pertaining to formatting will be ignored.',
].join('\n')

Expand Down
10 changes: 1 addition & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,18 @@
"test": "./test.sh",
"prepublish": "npm test"
},
"prettier": {
"printWidth": 80,
"tabWidth": 2,
"singleQuote": true,
"trailingComma": "es5",
"bracketSpacing": true,
"semi": false
},
"repository": {
"type": "git",
"url": "https://github.com/josephfrazier/prettier_d.js.git"
},
"dependencies": {
"camelcase-keys": "^4.1.0",
"chalk": "^1.1.1",
"config-attendant": "^0.1.0",
"json-align": "^0.1.0",
"json-stable-stringify": "^1.0.1",
"minimist": "^1.2.0",
"optionator": "^0.8.1",
"pkg-conf": "^2.0.0",
"prettier": "https://github.com/prettier/prettier",
"resolve": "^1.1.7",
"supports-color": "^3.1.2",
Expand Down
78 changes: 18 additions & 60 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,15 @@ concat-stream@^1.5.2:
readable-stream "^2.2.2"
typedarray "^0.0.6"

config-attendant@^0.1.0:
version "0.1.0"
resolved "https://registry.yarnpkg.com/config-attendant/-/config-attendant-0.1.0.tgz#f2522c21b5932c88499f6ca3d11498e41bb47762"
dependencies:
deep-extend "^0.4.0"
ini "^1.3.0"
minimist "^1.2.0"
strip-json-comments "^2.0.1"

contains-path@^0.1.0:
version "0.1.0"
resolved "https://registry.yarnpkg.com/contains-path/-/contains-path-0.1.0.tgz#fe8cf184ff6670b6baef01a9d4861a5cbec4120a"
Expand All @@ -206,6 +215,10 @@ debug@^2.1.1, debug@^2.2.0:
dependencies:
ms "0.7.3"

deep-extend@^0.4.0:
version "0.4.1"
resolved "https://registry.yarnpkg.com/deep-extend/-/deep-extend-0.4.1.tgz#efe4113d08085f4e6f9687759810f807469e2253"

deep-is@~0.1.3:
version "0.1.3"
resolved "https://registry.yarnpkg.com/deep-is/-/deep-is-0.1.3.tgz#b369d6fb5dbc13eecf524f91b070feedc357cf34"
Expand Down Expand Up @@ -236,12 +249,6 @@ doctrine@^2.0.0:
esutils "^2.0.2"
isarray "^1.0.0"

error-ex@^1.2.0:
version "1.3.1"
resolved "https://registry.yarnpkg.com/error-ex/-/error-ex-1.3.1.tgz#f855a86ce61adc4e8621c3cda21e7a7612c3a8dc"
dependencies:
is-arrayish "^0.2.1"

es5-ext@^0.10.14, es5-ext@^0.10.9, es5-ext@~0.10.14:
version "0.10.15"
resolved "https://registry.yarnpkg.com/es5-ext/-/es5-ext-0.10.15.tgz#c330a5934c1ee21284a7c081a86e5fd937c91ea6"
Expand Down Expand Up @@ -483,12 +490,6 @@ find-up@^1.0.0:
path-exists "^2.0.0"
pinkie-promise "^2.0.0"

find-up@^2.0.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/find-up/-/find-up-2.1.0.tgz#45d1b7e506c717ddd482775a2b77920a3c0c57a7"
dependencies:
locate-path "^2.0.0"

flat-cache@^1.2.1:
version "1.2.2"
resolved "https://registry.yarnpkg.com/flat-cache/-/flat-cache-1.2.2.tgz#fa86714e72c21db88601761ecf2f555d1abc6b96"
Expand Down Expand Up @@ -589,6 +590,10 @@ inherits@2, inherits@^2.0.3, inherits@~2.0.1:
version "2.0.3"
resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.3.tgz#633c2c83e3da42a502f52466022480f4208261de"

ini@^1.3.0:
version "1.3.4"
resolved "https://registry.yarnpkg.com/ini/-/ini-1.3.4.tgz#0537cb79daf59b59a1a517dff706c86ec039162e"

inquirer@^0.12.0:
version "0.12.0"
resolved "https://registry.yarnpkg.com/inquirer/-/inquirer-0.12.0.tgz#1ef2bfd63504df0bc75785fff8c2c41df12f077e"
Expand All @@ -611,10 +616,6 @@ interpret@^1.0.0:
version "1.0.3"
resolved "https://registry.yarnpkg.com/interpret/-/interpret-1.0.3.tgz#cbc35c62eeee73f19ab7b10a801511401afc0f90"

is-arrayish@^0.2.1:
version "0.2.1"
resolved "https://registry.yarnpkg.com/is-arrayish/-/is-arrayish-0.2.1.tgz#77c99840527aa8ecb1a8ba697b80645a7a926a9d"

is-fullwidth-code-point@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/is-fullwidth-code-point/-/is-fullwidth-code-point-1.0.0.tgz#ef9e31386f031a7f0d643af82fde50c457ef00cb"
Expand Down Expand Up @@ -722,22 +723,6 @@ levn@^0.3.0, levn@~0.3.0:
prelude-ls "~1.1.2"
type-check "~0.3.2"

load-json-file@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/load-json-file/-/load-json-file-2.0.0.tgz#7947e42149af80d696cbf797bcaabcfe1fe29ca8"
dependencies:
graceful-fs "^4.1.2"
parse-json "^2.2.0"
pify "^2.0.0"
strip-bom "^3.0.0"

locate-path@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/locate-path/-/locate-path-2.0.0.tgz#2b568b265eec944c6d9c0de9c3dbbbca0354cd8e"
dependencies:
p-locate "^2.0.0"
path-exists "^3.0.0"

lodash.cond@^4.3.0:
version "4.5.2"
resolved "https://registry.yarnpkg.com/lodash.cond/-/lodash.cond-4.5.2.tgz#f471a1da486be60f6ab955d17115523dd1d255d5"
Expand Down Expand Up @@ -819,32 +804,12 @@ os-homedir@^1.0.0:
version "1.0.2"
resolved "https://registry.yarnpkg.com/os-homedir/-/os-homedir-1.0.2.tgz#ffbc4988336e0e833de0c168c7ef152121aa7fb3"

p-limit@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/p-limit/-/p-limit-1.1.0.tgz#b07ff2d9a5d88bec806035895a2bab66a27988bc"

p-locate@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/p-locate/-/p-locate-2.0.0.tgz#20a0103b222a70c8fd39cc2e580680f3dde5ec43"
dependencies:
p-limit "^1.1.0"

parse-json@^2.2.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/parse-json/-/parse-json-2.2.0.tgz#f480f40434ef80741f8469099f8dea18f55a4dc9"
dependencies:
error-ex "^1.2.0"

path-exists@^2.0.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/path-exists/-/path-exists-2.1.0.tgz#0feb6c64f0fc518d9a754dd5efb62c7022761f4b"
dependencies:
pinkie-promise "^2.0.0"

path-exists@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/path-exists/-/path-exists-3.0.0.tgz#ce0ebeaa5f78cb18925ea7d810d7b59b010fd515"

path-is-absolute@^1.0.0:
version "1.0.1"
resolved "https://registry.yarnpkg.com/path-is-absolute/-/path-is-absolute-1.0.1.tgz#174b9268735534ffbc7ace6bf53a5a9e1b5c5f5f"
Expand All @@ -871,13 +836,6 @@ pinkie@^2.0.0:
version "2.0.4"
resolved "https://registry.yarnpkg.com/pinkie/-/pinkie-2.0.4.tgz#72556b80cfa0d48a974e80e77248e80ed4f7f870"

pkg-conf@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/pkg-conf/-/pkg-conf-2.0.0.tgz#071c87650403bccfb9c627f58751bfe47c067279"
dependencies:
find-up "^2.0.0"
load-json-file "^2.0.0"

pkg-dir@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/pkg-dir/-/pkg-dir-1.0.0.tgz#7a4b508a8d5bb2d629d447056ff4e9c9314cf3d4"
Expand Down Expand Up @@ -1052,7 +1010,7 @@ strip-bom@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/strip-bom/-/strip-bom-3.0.0.tgz#2334c18e9c759f7bdd56fdef7e9ae3d588e68ed3"

strip-json-comments@~2.0.1:
strip-json-comments@^2.0.1, strip-json-comments@~2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/strip-json-comments/-/strip-json-comments-2.0.1.tgz#3c531942e908c2697c0ec344858c286c7ca0a60a"

Expand Down

0 comments on commit 448139b

Please sign in to comment.