diff --git a/bin/prettier b/bin/prettier.js similarity index 51% rename from bin/prettier rename to bin/prettier.js index 492b086e..6e00d0b2 100755 --- a/bin/prettier +++ b/bin/prettier.js @@ -12,10 +12,10 @@ const argv = minimist(process.argv.slice(2), { } }); -const filename = argv["_"][0]; -const write = argv['write']; +const filenames = argv["_"]; +const write = argv["write"]; -if(!filename || !filename.match(/\S/)) { +if (!filenames.length) { console.log( "Usage: prettier [opts] [filename]\n\n" + "Available options:\n" + @@ -30,27 +30,34 @@ if(!filename || !filename.match(/\S/)) { process.exit(1); } -let input; -try { - input = fs.readFileSync(filename, "utf8") -} -catch(e) { - console.log("Unable to read file: " + filename); - process.exit(2); -} +filenames.forEach(filename => { + fs.readFile(filename, "utf8", (err, input) => { + if (err) { + console.error("Unable to read file: " + filename + "\n" + err); + // Don't exit the process if one file failed + process.exitCode = 2; + return; + } -const output = 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"] + const output = 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"] + }); + + 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); + } + }); }); - -if(write) { - fs.writeFileSync(filename, output, "utf8"); -} -else { - console.log(output); -} diff --git a/package.json b/package.json index 70ff7d91..eea93e41 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "prettier", "version": "0.0.3", "bin": { - "prettier": "./bin/prettier" + "prettier": "./bin/prettier.js" }, "main": "./index.js", "dependencies": { @@ -19,7 +19,9 @@ }, "scripts": { "test": "jest", - "format": "./bin/prettier --write src/printer.js" + "format": "./bin/prettier.js --write", + "format:single": "npm run format -- src/printer.js", + "format:all": "npm run format -- src/*.js bin/*.js" }, "jest": { "setupFiles": [