From 5d4bf92948ec100b9a8de36cae41482f300db632 Mon Sep 17 00:00:00 2001 From: Andrew Miner Date: Thu, 1 Jan 2015 19:58:06 -0800 Subject: [PATCH] Allow reformat to accept diff src/target --- scripts/reformat | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/scripts/reformat b/scripts/reformat index c4b32795f..77d5a0184 100755 --- a/scripts/reformat +++ b/scripts/reformat @@ -8,14 +8,36 @@ RecipeBookParser = require '../src/scripts/models/recipe_book_parser' ######################################################################################################################## -if process.argv.length isnt 3 - throw new Error "Expected a file name as an argument" +if process.argv.length is 3 + sourceFileName = process.argv[2] + targetFileName = process.argv[2] +else if process.argv.length is 4 + sourceFileName = process.argv[2] + targetFileName = process.argv[3] +else + console.error """ -fileName = process.argv[2] -text = fs.readFileSync fileName, 'UTF-8' + USAGE: reformat () + + Reformats a given recipe book into the latest version of the recipe book + format. This includes alphabetizing entries, simplifying item lists, and + upgrading to the latest version. + + If given a single argument, the named file will be replaced with the + reformatted version. If two arguments are given, then the output will be + written to the second file. If a "-" is given as the second file, the + output will be written to stdout. + + """ + process.exit -1 + +text = fs.readFileSync sourceFileName, 'UTF-8' data = JSON.parse text parser = new RecipeBookParser recipeBook = parser.parse data text = parser.unparse recipeBook -fs.writeFileSync fileName, text, encoding:'UTF-8' +if targetFileName is '-' + console.log text +else + fs.writeFileSync targetFileName, text, encoding:'UTF-8'