2016-11-29 20:14:10 +03:00
|
|
|
#!/usr/bin/env node
|
2017-01-10 20:18:22 +03:00
|
|
|
"use strict";
|
2016-11-29 20:14:10 +03:00
|
|
|
const fs = require("fs");
|
2017-01-11 18:57:16 +03:00
|
|
|
const getStdin = require("get-stdin");
|
2016-12-31 07:01:07 +03:00
|
|
|
const minimist = require("minimist");
|
2016-11-29 23:23:00 +03:00
|
|
|
const jscodefmt = require("../index");
|
2016-11-29 20:14:10 +03:00
|
|
|
|
2016-12-31 07:01:07 +03:00
|
|
|
const argv = minimist(process.argv.slice(2), {
|
2017-01-13 23:03:53 +03:00
|
|
|
boolean: [
|
|
|
|
"write",
|
|
|
|
"stdin",
|
|
|
|
"flow-parser",
|
|
|
|
"bracket-spacing",
|
|
|
|
"single-quote",
|
|
|
|
"trailing-comma"
|
|
|
|
],
|
|
|
|
default: { "bracket-spacing": true }
|
2016-12-31 07:01:07 +03:00
|
|
|
});
|
|
|
|
|
2017-01-10 23:41:36 +03:00
|
|
|
const filenames = argv["_"];
|
2017-01-10 23:45:04 +03:00
|
|
|
const write = argv["write"];
|
2017-01-11 18:57:16 +03:00
|
|
|
const stdin = argv["stdin"];
|
2016-11-29 20:14:10 +03:00
|
|
|
|
2017-01-11 18:57:16 +03:00
|
|
|
if (!filenames.length && !stdin) {
|
2017-01-10 07:03:35 +03:00
|
|
|
console.log(
|
2017-01-13 23:03:53 +03:00
|
|
|
"Usage: prettier [opts] [filename ...]\n\n" + "Available options:\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" +
|
|
|
|
" --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" +
|
|
|
|
" --single-quote Use single quotes instead of double\n" +
|
|
|
|
" --trailing-comma Print trailing commas wherever possible\n" +
|
|
|
|
" --bracket-spacing Put spaces between brackets. Defaults to true, set false to turn off"
|
2017-01-10 07:03:35 +03:00
|
|
|
);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
2017-01-11 18:57:16 +03:00
|
|
|
function format(input) {
|
|
|
|
return jscodefmt.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"]
|
|
|
|
});
|
|
|
|
}
|
2017-01-10 07:03:35 +03:00
|
|
|
|
2017-01-11 18:57:16 +03:00
|
|
|
if (stdin) {
|
|
|
|
getStdin().then(input => {
|
2017-01-11 09:10:00 +03:00
|
|
|
try {
|
2017-01-11 18:57:16 +03:00
|
|
|
console.log(format(input));
|
2017-01-11 09:10:00 +03:00
|
|
|
} catch (e) {
|
|
|
|
process.exitCode = 2;
|
|
|
|
console.error(e);
|
|
|
|
return;
|
|
|
|
}
|
2017-01-11 18:57:16 +03:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
filenames.forEach(filename => {
|
|
|
|
fs.readFile(filename, "utf8", (err, input) => {
|
|
|
|
if (write) {
|
|
|
|
console.log(filename);
|
|
|
|
}
|
2016-12-31 07:01:07 +03:00
|
|
|
|
2017-01-11 18:57:16 +03:00
|
|
|
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 {
|
2017-01-13 21:39:07 +03:00
|
|
|
output = format(input);
|
2017-01-11 18:57:16 +03:00
|
|
|
} 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);
|
|
|
|
}
|
|
|
|
});
|
2017-01-10 23:45:04 +03:00
|
|
|
});
|
2017-01-11 18:57:16 +03:00
|
|
|
}
|