Skip to content

Commit

Permalink
Merge pull request #2 from azz/prettier-config
Browse files Browse the repository at this point in the history
feat: respect .prettierrc and .prettierignore
  • Loading branch information
azz authored Oct 21, 2017
2 parents ede69bb + 48ed4f5 commit 14d2907
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 6 deletions.
3 changes: 3 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
"env": {
"es6": true
},
"globals": {
"process": true
},
"rules": {
"curly": ["error"],
"prefer-const": ["error"],
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ Or with `yarn`:
yarn add --dev prettier-tslint
```

## Confituration

`prettier-tslint` find and will respect:

* `prettier`'s `.prettierrc`, or any other config file such as `package.json`.
* `prettier`'s `.prettierignore` file.
* `tslint`'s `tslint.json`

`prettier-tslint` has no additional configuration.

## CLI

```bash
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
],
"dependencies": {
"@std/esm": "^0.11.3",
"globby": "^6.1.0",
"ignore": "^3.3.5",
"minimist": "^1.2.0",
"tslint": "^5.7.0"
},
Expand Down
9 changes: 9 additions & 0 deletions src/expand-glob.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import globby from "globby";

const expandGlob = glob => {
return globby.sync(glob, {
dot: true,
});
};

export default expandGlob;
15 changes: 15 additions & 0 deletions src/filter-ignored.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import fs from "fs";
import path from "path";
import ignore from "ignore";

const filterIgnored = filePaths => {
const ignorePath = path.resolve(process.cwd(), ".prettierignore");
if (fs.existsSync(ignorePath)) {
return ignore()
.add(fs.readFileSync(ignorePath, "utf8"))
.filter(filePaths);
}
return filePaths;
};

export default filterIgnored;
15 changes: 11 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
import fs from "fs";
import minimist from "minimist";

import expandGlob from "./expand-glob";
import filterIgnored from "./filter-ignored";
import runTsLint from "./run-tslint";
import runPrettier from "./run-prettier";

const format = filepath => {
runPrettier(filepath);
runTsLint(filepath);
const format = filePath => {
runPrettier(filePath);
runTsLint(filePath);
};

const formatFiles = filePattern => {
const files = filterIgnored(expandGlob(filePattern));
files.forEach(format);
};

const cliOpts = {};

export const cli = argv => {
const args = minimist(argv, cliOpts);
args._.forEach(format);
args._.forEach(formatFiles);
};

export default format;
3 changes: 2 additions & 1 deletion src/run-prettier.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import fs from "fs";
import prettier from "prettier";

const runPrettier = filepath => {
const config = prettier.resolveConfig.sync(filepath);
const code = fs.readFileSync(filepath, "utf8");
const output = prettier.format(code, { filepath });
const output = prettier.format(code, Object.assign({ filepath }, config));
fs.writeFileSync(filepath, output);
};

Expand Down
12 changes: 11 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -931,6 +931,16 @@ globby@^5.0.0:
pify "^2.0.0"
pinkie-promise "^2.0.0"

globby@^6.1.0:
version "6.1.0"
resolved "https://registry.yarnpkg.com/globby/-/globby-6.1.0.tgz#f5a6d70e8395e21c858fb0489d64df02424d506c"
dependencies:
array-union "^1.0.1"
glob "^7.0.3"
object-assign "^4.0.1"
pify "^2.0.0"
pinkie-promise "^2.0.0"

graceful-fs@^4.1.2, graceful-fs@^4.1.6:
version "4.1.11"
resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.1.11.tgz#0e8bdfe4d1ddb8854d64e04ea7c00e2a026e5658"
Expand Down Expand Up @@ -1037,7 +1047,7 @@ iconv-lite@^0.4.17:
version "0.4.19"
resolved "https://registry.yarnpkg.com/iconv-lite/-/iconv-lite-0.4.19.tgz#f7468f60135f5e5dad3399c0a81be9a1603a082b"

ignore@^3.3.3:
ignore@^3.3.3, ignore@^3.3.5:
version "3.3.5"
resolved "https://registry.yarnpkg.com/ignore/-/ignore-3.3.5.tgz#c4e715455f6073a8d7e5dae72d2fc9d71663dba6"

Expand Down

0 comments on commit 14d2907

Please sign in to comment.