-
-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
3,788 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"presets": [["env", { "targets": { "node": "6.10" } }]] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
extends: | ||
- eslint:recommended | ||
- plugin:jest/recommended | ||
- plugin:prettier/recommended | ||
env: | ||
es6: true | ||
node: true | ||
rules: | ||
no-console: off | ||
overrides: | ||
- files: "src/**/*.js" | ||
parserOptions: | ||
sourceType: module |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
node_modules | ||
*.log | ||
dist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
trailingComma: es5 | ||
singleQuote: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,39 @@ | ||
# `pretty-quick` | ||
|
||
> Get Pretty Quick | ||
Runs [Prettier](https://prettier.io) on your changed files. | ||
|
||
Supported source control managers: | ||
|
||
* Git | ||
* _Add more_ | ||
|
||
## Install | ||
|
||
With `yarn`: | ||
|
||
```shellsession | ||
yarn add --dev pretty-quick | ||
``` | ||
|
||
With `npm`: | ||
|
||
```shellsession | ||
npm install --save-dev pretty-quick | ||
``` | ||
|
||
## Usage | ||
|
||
With `yarn`: | ||
|
||
```shellsession | ||
yarn pretty-quick | ||
``` | ||
|
||
With `npm`: | ||
|
||
1. Add `"pretty-quick": "pretty-quick"` to the scripts section of `package.json`. | ||
2. ```shellsession | ||
npm run pretty-quick | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,35 @@ | ||
#!/usr/bin/env node | ||
|
||
"use strict"; | ||
'use strict'; | ||
|
||
const prettier = require('prettier'); | ||
const chalk = require('chalk'); | ||
const mri = require('mri'); | ||
|
||
const prettyQuick = require('..').default; | ||
|
||
const args = mri(process.argv.slice(2)); | ||
|
||
prettyQuick( | ||
process.cwd(), | ||
Object.assign({}, args, { | ||
onFoundSinceRevision: (scm, revision) => { | ||
console.log( | ||
`🔍 Finding changed files since ${chalk.bold( | ||
scm | ||
)} revision ${chalk.bold(revision)}` | ||
); | ||
}, | ||
onFoundChangedFiles: changedFiles => { | ||
console.log( | ||
`🎯 Found ${changedFiles.length} ${ | ||
changedFiles.length === 1 ? 'file' : 'files' | ||
} changed.` | ||
); | ||
}, | ||
onWriteFile: file => { | ||
console.log(`✍️ Fixing up ${chalk.bold(file)}.`); | ||
}, | ||
}) | ||
); | ||
|
||
console.log('✅ Everything is awesome!'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { existsSync, readFileSync } from 'fs'; | ||
import { join } from 'path'; | ||
import ignore from 'ignore'; | ||
|
||
export default (directory, filename = '.prettierignore') => { | ||
const file = join(directory, filename); | ||
if (existsSync(file)) { | ||
const text = readFileSync(file, 'utf8'); | ||
return ignore() | ||
.add(text) | ||
.createFilter(); | ||
} | ||
|
||
return () => true; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { readFileSync, writeFileSync } from 'fs'; | ||
import { resolveConfig, format } from 'prettier'; | ||
|
||
export default (files, { config, onWriteFile }) => { | ||
for (const file of files) { | ||
const options = resolveConfig.sync(file, { config }); | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
azz
Author
Member
|
||
const input = readFileSync(file, 'utf8'); | ||
const output = format( | ||
input, | ||
Object.assign(options, { | ||
filepath: file, | ||
}) | ||
); | ||
|
||
if (output !== input) { | ||
onWriteFile && onWriteFile(file); | ||
writeFileSync(file, output); | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { relative } from 'path'; | ||
|
||
import scms from './scms'; | ||
import formatFiles from './formatFiles'; | ||
import createIgnorer from './createIgnorer'; | ||
import isSupportedExtension from './isSupportedExtension'; | ||
|
||
export default ( | ||
directory, | ||
{ since, config, onFoundSinceRevision, onFoundChangedFiles, onWriteFile } | ||
) => { | ||
const scm = scms(directory); | ||
if (!scm) { | ||
throw new Error('Unable to detect a source control manager.'); | ||
} | ||
|
||
const revision = since || scm.getSinceRevision(directory); | ||
|
||
onFoundSinceRevision && onFoundSinceRevision(scm.name, revision); | ||
|
||
const changedFiles = scm | ||
.getChangedFiles(directory, revision) | ||
.map(file => relative(directory, file)) | ||
.filter(isSupportedExtension) | ||
.filter(createIgnorer(directory)); | ||
|
||
onFoundChangedFiles && onFoundChangedFiles(changedFiles); | ||
|
||
formatFiles(changedFiles, { config, onWriteFile }); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { extname } from 'path'; | ||
import { getSupportInfo } from 'prettier'; | ||
|
||
const extensions = getSupportInfo().languages.reduce( | ||
(prev, language) => prev.concat(language.extensions || []), | ||
[] | ||
); | ||
|
||
export default file => extensions.includes(extname(file)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { statSync } from 'fs'; | ||
import { join } from 'path'; | ||
import execa from 'execa'; | ||
|
||
export const name = 'git'; | ||
|
||
export const detect = directory => { | ||
try { | ||
return statSync(join(directory, '.git')).isDirectory(); | ||
} catch (error) { | ||
return false; | ||
} | ||
}; | ||
|
||
const runGit = (directory, args) => | ||
execa.sync('git', args, { | ||
cwd: directory, | ||
}); | ||
|
||
const getLines = execaResult => execaResult.stdout.split('\n'); | ||
|
||
export const getSinceRevision = directory => { | ||
return runGit(directory, ['merge-base', 'HEAD', 'master']).stdout.trim(); | ||
}; | ||
|
||
export const getChangedFiles = (directory, revision) => { | ||
return [ | ||
...getLines( | ||
runGit(directory, [ | ||
'diff', | ||
'--name-only', | ||
'--diff-filter=ACMRTUB', | ||
revision, | ||
]) | ||
), | ||
...getLines( | ||
runGit(directory, ['ls-files', '--others', '--exclude-standard']) | ||
), | ||
].filter(Boolean); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import * as gitScm from './git'; | ||
|
||
const scms = [gitScm]; | ||
|
||
export default directory => { | ||
for (const scm of scms) { | ||
if (scm.detect(directory)) { | ||
return scm; | ||
} | ||
} | ||
}; |
Oops, something went wrong.
When would it return a different config file?