diff --git a/bin/prettier.js b/bin/prettier.js index 3c7fc73d9..f7b32acb6 100755 --- a/bin/prettier.js +++ b/bin/prettier.js @@ -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); diff --git a/package.json b/package.json index 3546f11f5..9e3cde518 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/yarn.lock b/yarn.lock index a0544fcce..ad920b5ed 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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: