Skip to content

Commit

Permalink
feat(cli): implement the first features for release 💫
Browse files Browse the repository at this point in the history
  • Loading branch information
Kent C. Dodds committed Jan 19, 2017
1 parent 849a309 commit 3e66f8f
Show file tree
Hide file tree
Showing 24 changed files with 757 additions and 86 deletions.
18 changes: 18 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,24 @@
"infra",
"test"
]
},
{
"login": "aharris88",
"name": "Adam Harris",
"avatar_url": "https://avatars3.githubusercontent.com/u/3266363?v=3",
"profile": "https://github.com/aharris88",
"contributions": [
"review"
]
},
{
"login": "edm00se",
"name": "Eric McCormick",
"avatar_url": "https://avatars.githubusercontent.com/u/622118?v=3",
"profile": "https://ericmccormick.io",
"contributions": [
"review"
]
}
]
}
6 changes: 6 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,11 @@
}
}],
"stage-2"
],
"plugins": [
["transform-runtime", {
"polyfill": false,
"regenerator": true
}]
]
}
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ node_modules
.nyc_output
coverage
dist
cli-test/fixtures
56 changes: 46 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# prettier-eslint-cli

CLI for prettier-eslint
CLI for [`prettier-eslint`][prettier-eslint]

[![Build Status][build-badge]][build]
[![Code Coverage][coverage-badge]][coverage]
Expand All @@ -9,7 +9,7 @@ CLI for prettier-eslint
[![downloads][downloads-badge]][npm-stat]
[![MIT License][license-badge]][LICENSE]

[![All Contributors](https://img.shields.io/badge/all_contributors-1-orange.svg?style=flat-square)](#contributors)
[![All Contributors](https://img.shields.io/badge/all_contributors-3-orange.svg?style=flat-square)](#contributors)
[![PRs Welcome][prs-badge]][prs]
[![Donate][donate-badge]][donate]
[![Code of Conduct][coc-badge]][coc]
Expand All @@ -22,40 +22,71 @@ CLI for prettier-eslint

## The problem


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

## This solution


This is a [CLI](https://en.wikipedia.org/wiki/Command-line_interface) that allows you to use
`prettier-eslint` on one or multiple files.

## Installation

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

```
npm install --save-dev prettier-eslint-cli
yarn add --dev prettier-eslint-cli
```

> If you're still using the [`npm`][npm] client: `npm install --save-dev prettier-eslint-cli`
## Usage

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

```json
{
"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][glob] and you can pass as many as you wish.

You can also pass options:

### CLI Options

Everything other than the flags listed below will be considered a file glob and used to find
files to format. By default, `glob` will ignore `**/node_modules/**` unless the glob you provide
includes the string `node_modules`.

> **NOTE:** It is recommended that you keep your files under source control and committed
> before running `prettier-eslint` as it will overwrite your files!
## Inspiration
#### --log

If `prettier-eslint` encounters an error formatting a file, it logs an error to the console.
`prettier-eslint-cli` disables this behavior by default. You can turn it on with `--log`.

#### --eslint-path

## Other Solutions
Forwarded as the `eslintPath` option to `prettier-eslint`

#### --prettier-path

Forwarded as the `prettierPath` option to `prettier-eslint`

## Contributors

Thanks goes to these people ([emoji key][emojis]):

<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
| [<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/kentcdodds/prettier-eslint-cli/commits?author=kentcdodds) [📖](https://github.com/kentcdodds/prettier-eslint-cli/commits?author=kentcdodds) 🚇 [⚠️](https://github.com/kentcdodds/prettier-eslint-cli/commits?author=kentcdodds) |
| :---: |
| [<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/kentcdodds/prettier-eslint-cli/commits?author=kentcdodds) [📖](https://github.com/kentcdodds/prettier-eslint-cli/commits?author=kentcdodds) 🚇 [⚠️](https://github.com/kentcdodds/prettier-eslint-cli/commits?author=kentcdodds) | [<img src="https://avatars3.githubusercontent.com/u/3266363?v=3" width="100px;"/><br /><sub>Adam Harris</sub>](https://github.com/aharris88)<br />👀 | [<img src="https://avatars.githubusercontent.com/u/622118?v=3" width="100px;"/><br /><sub>Eric McCormick</sub>](https://ericmccormick.io)<br />👀 |
| :---: | :---: | :---: |
<!-- ALL-CONTRIBUTORS-LIST:END -->

This project follows the [all-contributors][all-contributors] specification. Contributions of any kind welcome!
Expand All @@ -64,6 +95,7 @@ This project follows the [all-contributors][all-contributors] specification. Con

MIT

[yarn]: https://yarnpkg.com/
[npm]: https://www.npmjs.com/
[node]: https://nodejs.org
[build-badge]: https://img.shields.io/travis/kentcdodds/prettier-eslint-cli.svg?style=flat-square
Expand Down Expand Up @@ -96,3 +128,7 @@ MIT
[twitter-badge]: https://img.shields.io/twitter/url/https/github.com/kentcdodds/prettier-eslint-cli.svg?style=social
[emojis]: https://github.com/kentcdodds/all-contributors#emoji-key
[all-contributors]: https://github.com/kentcdodds/all-contributors
[prettier-eslint]: https://github.com/kentcdodds/prettier-eslint
[npm scripts]: https://docs.npmjs.com/misc/scripts
[package scripts]: https://github.com/kentcdodds/p-s
[glob]: https://github.com/isaacs/node-glob
8 changes: 8 additions & 0 deletions __mocks__/fs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
readFile: jest.fn((filePath, encoding, callback) => {
callback(null, `console.log('this is a mock thing for file: ${filePath}')`)
}),
writeFile: jest.fn((filePath, contents, callback) => {
callback(null)
}),
}
5 changes: 5 additions & 0 deletions __mocks__/get-stdin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = jest.fn(async function mockGetStdin() {
return module.exports.stdin
})

module.exports.stdin = ''
39 changes: 39 additions & 0 deletions __mocks__/glob.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
module.exports = jest.fn(function mockGlob(globString, options, callback) {
/* eslint complexity:0 */
if (globString.includes('1')) {
callback(null, [
fredProject('index.js'),
fredProject('start.js'),
fredProject('stop/index.js'),
fredProject('stop/log.js'),
])
} else if (globString.includes('2')) {
callback(null, [
fredProject('index.js'),
fredProject('start.js'),
fredProject('continue/index.js'),
fredProject('continue/forever.js'),
])
} else if (globString.includes('node_modules')) {
callback(null, [
fredProject('foo/node_modules/stuff1.js'),
fredProject('foo/node_modules/stuff2.js'),
fredProject('foo/node_modules/stuff3.js'),
])
} else if (globString.includes('files-with-syntax-errors')) {
callback(null, [
fredProject('syntax-error1.js'),
fredProject('syntax-error2.js'),
])
} else if (globString.includes('no-match')) {
callback(null, [])
} else if (globString.includes('throw-error')) {
callback(new Error('something weird happened'))
} else {
throw new Error(`Your test globString: "${globString}" doesn't have associated mock data.`)
}
})

function fredProject(path) {
return `/Users/fredFlintstone/Developer/top-secret/footless-carriage/${path}`
}
11 changes: 11 additions & 0 deletions __mocks__/prettier-eslint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// This is the mock that will be used in tests
// Jest sets this up automatically http://facebook.github.io/jest/docs/manual-mocks.html
// so we just return some spies here and assert that we're calling prettier-eslint APIs correctly
const format = jest.fn(({text, filePath = ''}) => {
if (text === 'MOCK_SYNTAX_ERROR' || filePath.includes('syntax-error')) {
throw new Error('Mock error for a syntax error')
}
return `MOCK_OUTPUT for ${filePath || 'stdin'}`
})

export default format
13 changes: 13 additions & 0 deletions cli-test/fixtures/stdout1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import baz , { stuff } from
'fdjakfdlfw-baz'

export {
bazzy

}


function bazzy(something) {
return baz( stuff( something ) );
};;;;;;;;;;;;;;;;;;;;;

10 changes: 10 additions & 0 deletions cli-test/fixtures/stdout2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export
default foo

function foo (
thing
) {
return (
thing)
}

12 changes: 12 additions & 0 deletions cli-test/jest.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"testPathDirs": [
"<rootDir>/cli-test/tests"
],
"testEnvironment": "jest-environment-node",
"testPathIgnorePatterns": [
"/node_modules/",
"/helpers/",
"/fixtures/"
],
"testRegex": "^.*.js$"
}
20 changes: 20 additions & 0 deletions cli-test/tests/__snapshots__/index.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
exports[`file contents: prettier-eslint cli-test/fixtures/example1.js cli-test/fixtures/example2.js 1`] = `
Object {
"example1Result": "const {example1} = baz.bar
",
"example2Result": "function example2(thing) {
return thing
}
",
}
`;

exports[`stdout: --version 1`] = `
"0.0.0-semantically-released
"
`;

exports[`stdout: prettier-eslint cli-test/fixtures/example1.js cli-test/fixtures/example2.js 1`] = `
"success formatting 2 files with prettier-eslint
"
`;
Loading

0 comments on commit 3e66f8f

Please sign in to comment.