Skip to content

Commit

Permalink
feat(options): allow specifying eslint configuration file as an argum…
Browse files Browse the repository at this point in the history
…ent (#91)

* feat(options): allow specifying eslint configuration file as an argument

closes #37

* test(options): add stub test for eslintConfigPath option

* Update .all-contributorsrc
  • Loading branch information
morhetz authored and Kent C. Dodds committed Aug 16, 2017
1 parent 72057fb commit 87c0cf1
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 12 deletions.
10 changes: 10 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,16 @@
"doc",
"tool"
]
},
{
"login": "morhetz",
"name": "Pavel Pertsev",
"avatar_url": "https://avatars1.githubusercontent.com/u/554231?v=4",
"profile": "http://morhetz.com",
"contributions": [
"code",
"test"
]
}
]
}
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ CLI for [`prettier-eslint`][prettier-eslint]
[![downloads][downloads-badge]][npm-stat]
[![MIT License][license-badge]][LICENSE]

[![All Contributors](https://img.shields.io/badge/all_contributors-17-orange.svg?style=flat-square)](#contributors)
[![All Contributors](https://img.shields.io/badge/all_contributors-18-orange.svg?style=flat-square)](#contributors)
[![PRs Welcome][prs-badge]][prs]
[![Donate][donate-badge]][donate]
[![Code of Conduct][coc-badge]][coc]
Expand Down Expand Up @@ -95,6 +95,7 @@ Options:
[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"]
--ignore pattern(s) you wish to ignore (can be used multiple
Expand Down Expand Up @@ -177,6 +178,11 @@ echo "var foo = 'bar'" | prettier-eslint --stdin

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`
Expand Down Expand Up @@ -237,7 +243,7 @@ Thanks goes to these people ([emoji key][emojis]):
| [<img src="https://avatars.githubusercontent.com/u/1500684?v=3" width="100px;"/><br /><sub>Kent C. Dodds</sub>](https://kentcdodds.com)<br />[💻](https://github.com/prettier/prettier-eslint-cli/commits?author=kentcdodds "Code") [📖](https://github.com/prettier/prettier-eslint-cli/commits?author=kentcdodds "Documentation") [🚇](#infra-kentcdodds "Infrastructure (Hosting, Build-Tools, etc)") [⚠️](https://github.com/prettier/prettier-eslint-cli/commits?author=kentcdodds "Tests") | [<img src="https://avatars3.githubusercontent.com/u/3266363?v=3" width="100px;"/><br /><sub>Adam Harris</sub>](https://github.com/aharris88)<br />[💻](https://github.com/prettier/prettier-eslint-cli/commits?author=aharris88 "Code") [📖](https://github.com/prettier/prettier-eslint-cli/commits?author=aharris88 "Documentation") [👀](#review-aharris88 "Reviewed Pull Requests") | [<img src="https://avatars.githubusercontent.com/u/622118?v=3" width="100px;"/><br /><sub>Eric McCormick</sub>](https://ericmccormick.io)<br />[👀](#review-edm00se "Reviewed Pull Requests") | [<img src="https://avatars.githubusercontent.com/u/12389411?v=3" width="100px;"/><br /><sub>Joel Sequeira</sub>](https://github.com/joelseq)<br />[📖](https://github.com/prettier/prettier-eslint-cli/commits?author=joelseq "Documentation") | [<img src="https://avatars.githubusercontent.com/u/103008?v=3" width="100px;"/><br /><sub>Frank Taillandier</sub>](https://frank.taillandier.me)<br /> | [<img src="https://avatars3.githubusercontent.com/u/292365?v=3" width="100px;"/><br /><sub>Adam Stankiewicz</sub>](http://sheerun.net)<br />[💻](https://github.com/prettier/prettier-eslint-cli/commits?author=sheerun "Code") | [<img src="https://avatars3.githubusercontent.com/u/487068?v=3" width="100px;"/><br /><sub>Stephen John Sorensen</sub>](http://www.stephenjohnsorensen.com/)<br />[💻](https://github.com/prettier/prettier-eslint-cli/commits?author=spudly "Code") |
| :---: | :---: | :---: | :---: | :---: | :---: | :---: |
| [<img src="https://avatars0.githubusercontent.com/u/11560964?v=3" width="100px;"/><br /><sub>Gandem</sub>](https://github.com/Gandem)<br />[💻](https://github.com/prettier/prettier-eslint-cli/commits?author=Gandem "Code") [⚠️](https://github.com/prettier/prettier-eslint-cli/commits?author=Gandem "Tests") | [<img src="https://avatars0.githubusercontent.com/u/129991?v=3" width="100px;"/><br /><sub>Matteo Ronchi</sub>](https://github.com/cef62)<br />[🐛](https://github.com/prettier/prettier-eslint-cli/issues?q=author%3Acef62 "Bug reports") [💻](https://github.com/prettier/prettier-eslint-cli/commits?author=cef62 "Code") | [<img src="https://avatars2.githubusercontent.com/u/61787?v=3" width="100px;"/><br /><sub>Benoît Zugmeyer</sub>](https://github.com/BenoitZugmeyer)<br />[💻](https://github.com/prettier/prettier-eslint-cli/commits?author=BenoitZugmeyer "Code") [⚠️](https://github.com/prettier/prettier-eslint-cli/commits?author=BenoitZugmeyer "Tests") | [<img src="https://avatars0.githubusercontent.com/u/5038030?v=3" width="100px;"/><br /><sub>Charlike Mike Reagent</sub>](https://i.am.charlike.online)<br />[💻](https://github.com/prettier/prettier-eslint-cli/commits?author=tunnckoCore "Code") [⚠️](https://github.com/prettier/prettier-eslint-cli/commits?author=tunnckoCore "Tests") | [<img src="https://avatars0.githubusercontent.com/u/10954870?v=3" width="100px;"/><br /><sub>Dion Dirza</sub>](https://github.com/diondirza)<br />[🐛](https://github.com/prettier/prettier-eslint-cli/issues?q=author%3Adiondirza "Bug reports") | [<img src="https://avatars0.githubusercontent.com/u/3297808?v=3" width="100px;"/><br /><sub>mrm007</sub>](https://github.com/mrm007)<br />[🐛](https://github.com/prettier/prettier-eslint-cli/issues?q=author%3Amrm007 "Bug reports") [💻](https://github.com/prettier/prettier-eslint-cli/commits?author=mrm007 "Code") | [<img src="https://avatars0.githubusercontent.com/u/193238?v=3" width="100px;"/><br /><sub>Jack Franklin</sub>](http://www.jackfranklin.co.uk)<br />[💻](https://github.com/prettier/prettier-eslint-cli/commits?author=jackfranklin "Code") |
| [<img src="https://avatars0.githubusercontent.com/u/17342435?v=3" width="100px;"/><br /><sub>Ryan Zimmerman</sub>](http://www.ryanzim.com)<br />[📖](https://github.com/prettier/prettier-eslint-cli/commits?author=RyanZim "Documentation") | [<img src="https://avatars3.githubusercontent.com/u/1186409?v=3" width="100px;"/><br /><sub>Paolo Moretti</sub>](http://stackoverflow.com/users/63011)<br />[🐛](https://github.com/prettier/prettier-eslint-cli/issues?q=author%3Amoretti "Bug reports") [💻](https://github.com/prettier/prettier-eslint-cli/commits?author=moretti "Code") [⚠️](https://github.com/prettier/prettier-eslint-cli/commits?author=moretti "Tests") | [<img src="https://avatars0.githubusercontent.com/u/6242574?v=3" width="100px;"/><br /><sub>bySabi Files</sub>](https://github.com/bySabi)<br />[📖](https://github.com/prettier/prettier-eslint-cli/commits?author=bySabi "Documentation") [🔧](#tool-bySabi "Tools") |
| [<img src="https://avatars0.githubusercontent.com/u/17342435?v=3" width="100px;"/><br /><sub>Ryan Zimmerman</sub>](http://www.ryanzim.com)<br />[📖](https://github.com/prettier/prettier-eslint-cli/commits?author=RyanZim "Documentation") | [<img src="https://avatars3.githubusercontent.com/u/1186409?v=3" width="100px;"/><br /><sub>Paolo Moretti</sub>](http://stackoverflow.com/users/63011)<br />[🐛](https://github.com/prettier/prettier-eslint-cli/issues?q=author%3Amoretti "Bug reports") [💻](https://github.com/prettier/prettier-eslint-cli/commits?author=moretti "Code") [⚠️](https://github.com/prettier/prettier-eslint-cli/commits?author=moretti "Tests") | [<img src="https://avatars0.githubusercontent.com/u/6242574?v=3" width="100px;"/><br /><sub>bySabi Files</sub>](https://github.com/bySabi)<br />[📖](https://github.com/prettier/prettier-eslint-cli/commits?author=bySabi "Documentation") [🔧](#tool-bySabi "Tools") | [<img src="https://avatars1.githubusercontent.com/u/554231?v=4" width="100px;"/><br /><sub>Pavel Pertsev</sub>](http://morhetz.com)<br />[💻](https://github.com/prettier/prettier-eslint-cli/commits?author=morhetz "Code") |
<!-- ALL-CONTRIBUTORS-LIST:END -->

This project follows the [all-contributors][all-contributors] specification. Contributions of any kind welcome!
Expand Down
10 changes: 9 additions & 1 deletion __mocks__/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,19 @@ const writeFile = jest.fn((filePath, contents, callback) => {
})

const readFileSync = jest.fn(filePath => {
if (filePath.indexOf('.eslintrc') !== -1) {
return '{ "rules": { "semi": "error" } }'
}

if (filePath.indexOf('eslintignore')) {
return '*ignored*\n**/ignored/**\n'
} else {
throw new Error('readFileSync mock does nto yet handle ', filePath)
}
})

module.exports = {readFile, writeFile, readFileSync}
const readdirSync = jest.fn(() => {
return []
})

module.exports = {readFile, writeFile, readFileSync, readdirSync}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"camelcase-keys": "^4.1.0",
"chalk": "^1.1.3",
"common-tags": "^1.4.0",
"eslint": "^3.19.0",
"find-up": "^2.1.0",
"get-stdin": "^5.0.1",
"glob": "^7.1.1",
Expand All @@ -51,7 +52,6 @@
"codecov": "^2.1.0",
"commitizen": "^2.9.6",
"cz-conventional-changelog": "^2.0.0",
"eslint": "^3.19.0",
"eslint-config-kentcdodds": "^12.2.1",
"husky": "^0.13.3",
"jest-cli": "^19.0.2",
Expand Down
7 changes: 7 additions & 0 deletions src/format-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import findUp from 'find-up'
import memoize from 'lodash.memoize'
import indentString from 'indent-string'
import getLogger from 'loglevel-colored-level-prefix'
import ConfigFile from 'eslint/lib/config/config-file'
import * as messages from './messages'

const LINE_SEPERATOR_REGEX = /(\r|\n|\r\n)/
Expand All @@ -36,6 +37,7 @@ function formatFilesFromArgv({
prettierPath,
ignore: ignoreGlobs = [],
eslintIgnore: applyEslintIgnore = true,
eslintConfigPath,
prettierLast,
...prettierOptions
}) {
Expand All @@ -47,6 +49,11 @@ function formatFilesFromArgv({
prettierLast,
prettierOptions,
}

if (eslintConfigPath) {
prettierESLintOptions.eslintConfig = ConfigFile.load(eslintConfigPath)
}

const cliOptions = {write, listDifferent}
if (stdin) {
return formatStdin(prettierESLintOptions)
Expand Down
10 changes: 10 additions & 0 deletions src/format-files.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,3 +280,13 @@ describe('listDifferent', () => {
expect(process.exitCode).toBe(0)
})
})

describe('eslintConfigPath', () => {
test('will use eslintrc', async () => {
await formatFiles({
_: ['src/**/1*.js'],
eslintConfigPath: '.eslintrc',
})
expect(process.exitCode).toBe(0)
})
})
13 changes: 5 additions & 8 deletions src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ const parser = yargs
describe: 'The path to the eslint module to use',
coerce: coercePath,
},
// allow `--eslint-config-path` and `--eslintConfigPath`
'eslint-config-path': {
default: undefined,
describe: 'Path to the eslint config to use for eslint --fix',
},
// allow `--prettier-path` and `--prettierPath`
'prettier-path': {
describe: 'The path to the prettier module to use',
Expand Down Expand Up @@ -150,14 +155,6 @@ const parser = yargs
},
// TODO: support range-start and range-end
// would require changes in prettier-eslint
// TODO: if we allow people to to specify a config path,
// we need to read that somehow. These can come invarious
// formats and we'd have to work out `extends` somehow as well.
// I don't know whether ESLint exposes a way to do this...
// Contributions welcome!
// eslintConfigPath: {
// describe: 'Path to the eslint config to use for eslint --fix',
// },
})
.strict()

Expand Down

0 comments on commit 87c0cf1

Please sign in to comment.