From d5caaa7877b095add2ee292c22f26f7e6ad599d6 Mon Sep 17 00:00:00 2001 From: David Majda Date: Wed, 26 Jan 2011 14:01:57 +0100 Subject: [PATCH] Nicer messages in command-line mode on read/write errors --- bin/pegjs | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/bin/pegjs b/bin/pegjs index 20e5e54..7693eca 100755 --- a/bin/pegjs +++ b/bin/pegjs @@ -103,13 +103,25 @@ switch (args.length) { var inputStream = process.openStdin(); var outputStream = process.stdout; break; + case 1: case 2: - var inputStream = fs.createReadStream(args[0]); - var outputStream = fs.createWriteStream( - args.length == 1 ? args[0].replace(/\.[^.]*$/, ".js") : args[1] - ); + var inputFile = args[0]; + var inputStream = fs.createReadStream(inputFile); + inputStream.on("error", function() { + abort("Can't read from file \"" + inputFile + "\"."); + }); + + var outputFile = args.length == 1 + ? args[0].replace(/\.[^.]*$/, ".js") + : args[1]; + var outputStream = fs.createWriteStream(outputFile); + outputStream.on("error", function() { + abort("Can't write to file \"" + outputFile + "\"."); + }); + break; + default: abort("Too many arguments."); }