Log filename with [update] or [ignore] flags during `--write` process. (#584)
* Make `--write` logs more informative. * Add format duration to logs. * Use colors instead of [ignore] and [update] labels. * Remove `colors` dependency. * Use `chalk` to colorize logs. * Print filename before `format` and add duration info later. * Print filename before read error.master
parent
91d4f400a4
commit
97fd8a8d81
|
@ -5,6 +5,7 @@
|
|||
const fs = require("fs");
|
||||
const getStdin = require("get-stdin");
|
||||
const glob = require("glob");
|
||||
const chalk = require("chalk");
|
||||
const minimist = require("minimist");
|
||||
const prettier = require("../index");
|
||||
|
||||
|
@ -184,28 +185,46 @@ if (stdin) {
|
|||
eachFilename(filepatterns, filename => {
|
||||
fs.readFile(filename, "utf8", (err, input) => {
|
||||
if (write || argv["debug-check"]) {
|
||||
console.log(filename);
|
||||
// Don't use `console.log` here since we need to replace this line.
|
||||
process.stdout.write(filename);
|
||||
}
|
||||
|
||||
if (err) {
|
||||
// Add newline to split errors from filename line.
|
||||
process.stdout.write("\n");
|
||||
|
||||
console.error("Unable to read file: " + filename + "\n" + err);
|
||||
// Don't exit the process if one file failed
|
||||
process.exitCode = 2;
|
||||
return;
|
||||
}
|
||||
|
||||
const start = Date.now();
|
||||
|
||||
let output;
|
||||
|
||||
try {
|
||||
output = format(input);
|
||||
} catch (e) {
|
||||
// Add newline to split errors from filename line.
|
||||
process.stdout.write("\n");
|
||||
|
||||
handleError(filename, e);
|
||||
return;
|
||||
}
|
||||
|
||||
if (write) {
|
||||
// Remove previously printed filename to log it with duration.
|
||||
process.stdout.clearLine();
|
||||
process.stdout.cursorTo(0);
|
||||
|
||||
// Don't write the file if it won't change in order not to invalidate
|
||||
// mtime based caches.
|
||||
if (output !== input) {
|
||||
if (output === input) {
|
||||
console.log(chalk.grey("%s %dms"), filename, Date.now() - start);
|
||||
} else {
|
||||
console.log("%s %dms", filename, Date.now() - start);
|
||||
|
||||
fs.writeFile(filename, output, "utf8", err => {
|
||||
if (err) {
|
||||
console.error("Unable to write file: " + filename + "\n" + err);
|
||||
|
|
|
@ -19,6 +19,7 @@
|
|||
"ast-types": "0.9.4",
|
||||
"babel-code-frame": "6.22.0",
|
||||
"babylon": "6.15.0",
|
||||
"chalk": "1.1.3",
|
||||
"esutils": "2.0.2",
|
||||
"flow-parser": "0.38.0",
|
||||
"get-stdin": "5.0.1",
|
||||
|
|
|
@ -562,7 +562,7 @@ center-align@^0.1.1:
|
|||
align-text "^0.1.3"
|
||||
lazy-cache "^1.0.3"
|
||||
|
||||
chalk@^1.1.0, chalk@^1.1.1, chalk@^1.1.3:
|
||||
chalk@1.1.3, chalk@^1.1.0, chalk@^1.1.1, chalk@^1.1.3:
|
||||
version "1.1.3"
|
||||
resolved "https://registry.yarnpkg.com/chalk/-/chalk-1.1.3.tgz#a8115c55e4a702fe4d150abd3872822a7e09fc98"
|
||||
dependencies:
|
||||
|
|
Loading…
Reference in New Issue