commit
ac6cb883c8
11
README.md
11
README.md
|
@ -150,6 +150,17 @@ Emacs users should see [this
|
||||||
folder](https://github.com/jlongster/prettier/tree/master/editors/emacs)
|
folder](https://github.com/jlongster/prettier/tree/master/editors/emacs)
|
||||||
for on-demand formatting.
|
for on-demand formatting.
|
||||||
|
|
||||||
|
### Vim
|
||||||
|
|
||||||
|
Vim users can add the following to their `.vimrc`:
|
||||||
|
|
||||||
|
```
|
||||||
|
autocmd FileType javascript set formatprg=prettier\ --stdin
|
||||||
|
```
|
||||||
|
|
||||||
|
This makes Prettier power the [`gq` command](http://vimdoc.sourceforge.net/htmldoc/change.html#gq)
|
||||||
|
for automatic formatting without any plugins.
|
||||||
|
|
||||||
More editors are coming soon.
|
More editors are coming soon.
|
||||||
|
|
||||||
## Language Support
|
## Language Support
|
||||||
|
|
|
@ -2,11 +2,12 @@
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
const fs = require("fs");
|
const fs = require("fs");
|
||||||
|
const getStdin = require("get-stdin");
|
||||||
const minimist = require("minimist");
|
const minimist = require("minimist");
|
||||||
const jscodefmt = require("../index");
|
const jscodefmt = require("../index");
|
||||||
|
|
||||||
const argv = minimist(process.argv.slice(2), {
|
const argv = minimist(process.argv.slice(2), {
|
||||||
boolean: ["write", "flow-parser", "bracket-spacing", "single-quote", "trailing-comma"],
|
boolean: ["write", "stdin", "flow-parser", "bracket-spacing", "single-quote", "trailing-comma"],
|
||||||
default: {
|
default: {
|
||||||
"bracket-spacing": true
|
"bracket-spacing": true
|
||||||
}
|
}
|
||||||
|
@ -14,12 +15,14 @@ const argv = minimist(process.argv.slice(2), {
|
||||||
|
|
||||||
const filenames = argv["_"];
|
const filenames = argv["_"];
|
||||||
const write = argv["write"];
|
const write = argv["write"];
|
||||||
|
const stdin = argv["stdin"];
|
||||||
|
|
||||||
if (!filenames.length) {
|
if (!filenames.length && !stdin) {
|
||||||
console.log(
|
console.log(
|
||||||
"Usage: prettier [opts] [filename ...]\n\n" +
|
"Usage: prettier [opts] [filename ...]\n\n" +
|
||||||
"Available options:\n" +
|
"Available options:\n" +
|
||||||
" --write Edit the file in-place (beware!)\n" +
|
" --write Edit the file in-place (beware!)\n" +
|
||||||
|
" --stdin Read input from stdin\n" +
|
||||||
" --print-width <int> Specify the length of line that the printer will wrap on. Defaults to 80.\n" +
|
" --print-width <int> Specify the length of line that the printer will wrap on. Defaults to 80.\n" +
|
||||||
" --tab-width <int> Specify the number of spaces per indentation-level. Defaults to 2.\n" +
|
" --tab-width <int> Specify the number of spaces per indentation-level. Defaults to 2.\n" +
|
||||||
" --flow-parser Use the flow parser instead of babylon\n" +
|
" --flow-parser Use the flow parser instead of babylon\n" +
|
||||||
|
@ -30,45 +33,61 @@ if (!filenames.length) {
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
filenames.forEach(filename => {
|
function format(input) {
|
||||||
fs.readFile(filename, "utf8", (err, input) => {
|
return jscodefmt.format(input, {
|
||||||
if (write) {
|
printWidth: argv["print-width"],
|
||||||
console.log(file);
|
tabWidth: argv["tab-width"],
|
||||||
}
|
bracketSpacing: argv["bracket-spacing"],
|
||||||
|
useFlowParser: argv["flow-parser"],
|
||||||
|
singleQuote: argv["single-quote"],
|
||||||
|
trailingComma: argv["trailing-comma"]
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
if (err) {
|
if (stdin) {
|
||||||
console.error("Unable to read file: " + filename + "\n" + err);
|
getStdin().then(input => {
|
||||||
// Don't exit the process if one file failed
|
|
||||||
process.exitCode = 2;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
let output;
|
|
||||||
try {
|
try {
|
||||||
output = jscodefmt.format(input, {
|
console.log(format(input));
|
||||||
printWidth: argv["print-width"],
|
|
||||||
tabWidth: argv["tab-width"],
|
|
||||||
bracketSpacing: argv["bracket-spacing"],
|
|
||||||
useFlowParser: argv["flow-parser"],
|
|
||||||
singleQuote: argv["single-quote"],
|
|
||||||
trailingComma: argv["trailing-comma"]
|
|
||||||
});
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
process.exitCode = 2;
|
process.exitCode = 2;
|
||||||
console.error(e);
|
console.error(e);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (write) {
|
|
||||||
fs.writeFile(filename, output, "utf8", err => {
|
|
||||||
if (err) {
|
|
||||||
console.error("Unable to write file: " + filename + "\n" + err);
|
|
||||||
// Don't exit the process if one file failed
|
|
||||||
process.exitCode = 2;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
console.log(output);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
});
|
} else {
|
||||||
|
filenames.forEach(filename => {
|
||||||
|
fs.readFile(filename, "utf8", (err, input) => {
|
||||||
|
if (write) {
|
||||||
|
console.log(filename);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (err) {
|
||||||
|
console.error("Unable to read file: " + filename + "\n" + err);
|
||||||
|
// Don't exit the process if one file failed
|
||||||
|
process.exitCode = 2;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let output;
|
||||||
|
try {
|
||||||
|
output = format(input);
|
||||||
|
} catch (e) {
|
||||||
|
process.exitCode = 2;
|
||||||
|
console.error(e);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (write) {
|
||||||
|
fs.writeFile(filename, output, "utf8", err => {
|
||||||
|
if (err) {
|
||||||
|
console.error("Unable to write file: " + filename + "\n" + err);
|
||||||
|
// Don't exit the process if one file failed
|
||||||
|
process.exitCode = 2;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
console.log(output);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
|
@ -9,6 +9,7 @@
|
||||||
"ast-types": "git+https://github.com/jlongster/ast-types.git",
|
"ast-types": "git+https://github.com/jlongster/ast-types.git",
|
||||||
"babylon": "^6.15.0",
|
"babylon": "^6.15.0",
|
||||||
"flow-parser": "^0.37.0",
|
"flow-parser": "^0.37.0",
|
||||||
|
"get-stdin": "^5.0.1",
|
||||||
"minimist": "^1.2.0",
|
"minimist": "^1.2.0",
|
||||||
"private": "^0.1.6",
|
"private": "^0.1.6",
|
||||||
"recast": "^0.11.18",
|
"recast": "^0.11.18",
|
||||||
|
|
Loading…
Reference in New Issue