2016-11-29 20:14:10 +03:00
|
|
|
#!/usr/bin/env node
|
2017-01-18 01:16:40 +03:00
|
|
|
|
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");
|
2017-01-23 18:37:57 +03:00
|
|
|
const glob = require("glob");
|
2016-12-31 07:01:07 +03:00
|
|
|
const minimist = require("minimist");
|
2017-01-22 03:42:13 +03:00
|
|
|
const prettier = 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",
|
|
|
|
"single-quote",
|
2017-01-18 23:45:44 +03:00
|
|
|
"trailing-comma",
|
2017-01-22 03:48:18 +03:00
|
|
|
"bracket-spacing",
|
2017-01-24 00:29:57 +03:00
|
|
|
// The supports-color package (a sub sub dependency) looks directly at
|
|
|
|
// `process.argv` for `--no-color` and such-like options. The reason it is
|
|
|
|
// listed here is to avoid "Ignored unknown option: --no-color" warnings.
|
|
|
|
// See https://github.com/chalk/supports-color/#info for more information.
|
2017-01-23 20:10:51 +03:00
|
|
|
"color",
|
2017-01-20 01:35:12 +03:00
|
|
|
"version",
|
Add debugging support for doc IR (#347)
This PR adds two things:
`--debug-print-doc` command that prints the formatted doc
```js
echo "<div><</div>" | ./bin/prettier.js --stdin --debug-print-doc
[
groupConditional(
group([
group([ "<", "div", group([ indent(2, []), softline() ]), ">" ]),
"<",
"</",
"div",
">"
]),
[
group([
group([ "<", "div", group([ indent(2, []), softline() ]), ">" ]),
"<",
"</",
"div",
">"
]),
group([
group([ "<", "div", group([ indent(2, []), softline() ]), ">" ]),
indent(2, groupBreak([ hardline(), "<" ])),
hardline(),
"</",
"div",
">"
])
]
),
";",
hardline()
];
```
The ability to view the IR in real time on the browser display:
![image](https://cloud.githubusercontent.com/assets/197597/22134741/4f172f20-de7e-11e6-84bc-5f813976dc19.png)
The way it works is pretty cool, we take the doc IR and print a valid JavaScript string out of it, that we then send to prettier in order to make it look good :)
2017-01-20 20:54:32 +03:00
|
|
|
"debug-print-doc",
|
2017-01-20 01:35:12 +03:00
|
|
|
// Deprecated in 0.0.10
|
|
|
|
"flow-parser"
|
|
|
|
],
|
2017-01-22 03:48:18 +03:00
|
|
|
string: [ "print-width", "tab-width", "parser" ],
|
2017-01-23 20:10:51 +03:00
|
|
|
default: { color: true, "bracket-spacing": true, parser: "babylon" },
|
2017-01-21 20:11:34 +03:00
|
|
|
unknown: param => {
|
|
|
|
if (param.startsWith("-")) {
|
|
|
|
console.warn("Ignored unknown option: " + param + "\n");
|
|
|
|
}
|
|
|
|
}
|
2016-12-31 07:01:07 +03:00
|
|
|
});
|
|
|
|
|
2017-01-18 23:45:44 +03:00
|
|
|
if (argv["version"]) {
|
2017-01-22 03:42:13 +03:00
|
|
|
console.log(prettier.version);
|
2017-01-18 23:45:44 +03:00
|
|
|
process.exit(0);
|
|
|
|
}
|
|
|
|
|
2017-01-23 18:37:57 +03:00
|
|
|
const filepatterns = 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-23 18:37:57 +03:00
|
|
|
if (!filepatterns.length && !stdin) {
|
2017-01-10 07:03:35 +03:00
|
|
|
console.log(
|
2017-01-19 02:31:46 +03:00
|
|
|
"Usage: prettier [opts] [filename ...]\n\n" +
|
|
|
|
"Available options:\n" +
|
2017-01-22 03:48:18 +03:00
|
|
|
" --write Edit the file in-place. (Beware!)\n" +
|
|
|
|
" --stdin Read input from stdin.\n" +
|
2017-01-20 01:35:12 +03:00
|
|
|
" --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" +
|
2017-01-22 03:48:18 +03:00
|
|
|
" --single-quote Use single quotes instead of double.\n" +
|
|
|
|
" --trailing-comma Print trailing commas wherever possible.\n" +
|
2017-01-23 20:10:51 +03:00
|
|
|
" --bracket-spacing Put spaces between brackets. Defaults to true.\n" +
|
2017-01-22 03:48:18 +03:00
|
|
|
" --parser <flow|babylon> Specify which parse to use. Defaults to babylon.\n" +
|
2017-01-23 20:10:51 +03:00
|
|
|
" --color Colorize error messages. Defaults to true.\n" +
|
|
|
|
" --version Print prettier version.\n" +
|
|
|
|
"\n" +
|
|
|
|
"Boolean options can be turned off like this:\n" +
|
|
|
|
" --no-bracket-spacing\n" +
|
|
|
|
" --bracket-spacing=false"
|
2017-01-10 07:03:35 +03:00
|
|
|
);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
2017-01-20 01:35:12 +03:00
|
|
|
function getParser() {
|
|
|
|
// For backward compatibility. Deprecated in 0.0.10
|
|
|
|
if (argv["flow-parser"]) {
|
2017-01-21 20:23:29 +03:00
|
|
|
console.warn("`--flow-parser` is deprecated. Use `--parser flow` instead.");
|
2017-01-20 01:35:12 +03:00
|
|
|
return "flow";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argv["parser"] === "flow") {
|
|
|
|
return "flow";
|
|
|
|
}
|
|
|
|
|
|
|
|
return "babylon";
|
|
|
|
}
|
|
|
|
|
Add debugging support for doc IR (#347)
This PR adds two things:
`--debug-print-doc` command that prints the formatted doc
```js
echo "<div><</div>" | ./bin/prettier.js --stdin --debug-print-doc
[
groupConditional(
group([
group([ "<", "div", group([ indent(2, []), softline() ]), ">" ]),
"<",
"</",
"div",
">"
]),
[
group([
group([ "<", "div", group([ indent(2, []), softline() ]), ">" ]),
"<",
"</",
"div",
">"
]),
group([
group([ "<", "div", group([ indent(2, []), softline() ]), ">" ]),
indent(2, groupBreak([ hardline(), "<" ])),
hardline(),
"</",
"div",
">"
])
]
),
";",
hardline()
];
```
The ability to view the IR in real time on the browser display:
![image](https://cloud.githubusercontent.com/assets/197597/22134741/4f172f20-de7e-11e6-84bc-5f813976dc19.png)
The way it works is pretty cool, we take the doc IR and print a valid JavaScript string out of it, that we then send to prettier in order to make it look good :)
2017-01-20 20:54:32 +03:00
|
|
|
const options = {
|
2017-01-25 00:07:05 +03:00
|
|
|
printWidth: argv["print-width"] && parseInt(argv["print-width"]),
|
|
|
|
tabWidth: argv["tab-width"] && parseInt(argv["tab-width"]),
|
Add debugging support for doc IR (#347)
This PR adds two things:
`--debug-print-doc` command that prints the formatted doc
```js
echo "<div><</div>" | ./bin/prettier.js --stdin --debug-print-doc
[
groupConditional(
group([
group([ "<", "div", group([ indent(2, []), softline() ]), ">" ]),
"<",
"</",
"div",
">"
]),
[
group([
group([ "<", "div", group([ indent(2, []), softline() ]), ">" ]),
"<",
"</",
"div",
">"
]),
group([
group([ "<", "div", group([ indent(2, []), softline() ]), ">" ]),
indent(2, groupBreak([ hardline(), "<" ])),
hardline(),
"</",
"div",
">"
])
]
),
";",
hardline()
];
```
The ability to view the IR in real time on the browser display:
![image](https://cloud.githubusercontent.com/assets/197597/22134741/4f172f20-de7e-11e6-84bc-5f813976dc19.png)
The way it works is pretty cool, we take the doc IR and print a valid JavaScript string out of it, that we then send to prettier in order to make it look good :)
2017-01-20 20:54:32 +03:00
|
|
|
bracketSpacing: argv["bracket-spacing"],
|
|
|
|
parser: getParser(),
|
|
|
|
singleQuote: argv["single-quote"],
|
|
|
|
trailingComma: argv["trailing-comma"]
|
|
|
|
};
|
|
|
|
|
2017-01-11 18:57:16 +03:00
|
|
|
function format(input) {
|
Add debugging support for doc IR (#347)
This PR adds two things:
`--debug-print-doc` command that prints the formatted doc
```js
echo "<div><</div>" | ./bin/prettier.js --stdin --debug-print-doc
[
groupConditional(
group([
group([ "<", "div", group([ indent(2, []), softline() ]), ">" ]),
"<",
"</",
"div",
">"
]),
[
group([
group([ "<", "div", group([ indent(2, []), softline() ]), ">" ]),
"<",
"</",
"div",
">"
]),
group([
group([ "<", "div", group([ indent(2, []), softline() ]), ">" ]),
indent(2, groupBreak([ hardline(), "<" ])),
hardline(),
"</",
"div",
">"
])
]
),
";",
hardline()
];
```
The ability to view the IR in real time on the browser display:
![image](https://cloud.githubusercontent.com/assets/197597/22134741/4f172f20-de7e-11e6-84bc-5f813976dc19.png)
The way it works is pretty cool, we take the doc IR and print a valid JavaScript string out of it, that we then send to prettier in order to make it look good :)
2017-01-20 20:54:32 +03:00
|
|
|
if (argv["debug-print-doc"]) {
|
2017-01-22 03:42:13 +03:00
|
|
|
const doc = prettier.__debug.printToDoc(input, options);
|
|
|
|
return prettier.__debug.formatDoc(doc);
|
Add debugging support for doc IR (#347)
This PR adds two things:
`--debug-print-doc` command that prints the formatted doc
```js
echo "<div><</div>" | ./bin/prettier.js --stdin --debug-print-doc
[
groupConditional(
group([
group([ "<", "div", group([ indent(2, []), softline() ]), ">" ]),
"<",
"</",
"div",
">"
]),
[
group([
group([ "<", "div", group([ indent(2, []), softline() ]), ">" ]),
"<",
"</",
"div",
">"
]),
group([
group([ "<", "div", group([ indent(2, []), softline() ]), ">" ]),
indent(2, groupBreak([ hardline(), "<" ])),
hardline(),
"</",
"div",
">"
])
]
),
";",
hardline()
];
```
The ability to view the IR in real time on the browser display:
![image](https://cloud.githubusercontent.com/assets/197597/22134741/4f172f20-de7e-11e6-84bc-5f813976dc19.png)
The way it works is pretty cool, we take the doc IR and print a valid JavaScript string out of it, that we then send to prettier in order to make it look good :)
2017-01-20 20:54:32 +03:00
|
|
|
}
|
2017-01-22 03:42:13 +03:00
|
|
|
return prettier.format(input, options);
|
2017-01-11 18:57:16 +03:00
|
|
|
}
|
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-24 00:10:26 +03:00
|
|
|
// Don't use `console.log` here since it adds an extra newline at the end.
|
|
|
|
process.stdout.write(format(input));
|
2017-01-11 09:10:00 +03:00
|
|
|
} catch (e) {
|
|
|
|
process.exitCode = 2;
|
2017-01-23 20:10:51 +03:00
|
|
|
console.error("stdin: " + e);
|
2017-01-11 09:10:00 +03:00
|
|
|
return;
|
|
|
|
}
|
2017-01-11 18:57:16 +03:00
|
|
|
});
|
|
|
|
} else {
|
2017-01-23 18:37:57 +03:00
|
|
|
eachFilename(filepatterns, filename => {
|
2017-01-11 18:57:16 +03:00
|
|
|
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;
|
2017-01-24 02:47:11 +03:00
|
|
|
if(e.loc) {
|
|
|
|
console.error(filename + ": " + e);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
console.error(filename + ":", e);
|
|
|
|
}
|
2017-01-11 18:57:16 +03:00
|
|
|
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 {
|
2017-01-24 00:10:26 +03:00
|
|
|
// Don't use `console.log` here since it adds an extra newline at the end.
|
|
|
|
process.stdout.write(output);
|
2017-01-11 18:57:16 +03:00
|
|
|
}
|
|
|
|
});
|
2017-01-10 23:45:04 +03:00
|
|
|
});
|
2017-01-11 18:57:16 +03:00
|
|
|
}
|
2017-01-23 18:37:57 +03:00
|
|
|
|
|
|
|
function eachFilename(patterns, callback) {
|
|
|
|
patterns.forEach(pattern => {
|
|
|
|
glob(pattern, (err, filenames) => {
|
|
|
|
if (err) {
|
|
|
|
console.error("Unable to expand glob pattern: " + pattern + "\n" + err);
|
|
|
|
// Don't exit the process if one pattern failed
|
|
|
|
process.exitCode = 2;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
filenames.forEach(filename => {
|
|
|
|
callback(filename);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|