From 7b2388bb568154e597f9d1b15e3da5039ee69294 Mon Sep 17 00:00:00 2001 From: Jordan Date: Thu, 16 Feb 2012 00:22:53 -0500 Subject: [PATCH 1/3] Allow the location/name of settings.json to be passed through an optional --settings arg --- bin/installDeps.sh | 15 +++++++++++---- bin/run.sh | 4 ++-- node/utils/Settings.js | 8 ++++++-- package.json | 3 ++- 4 files changed, 21 insertions(+), 9 deletions(-) diff --git a/bin/installDeps.sh b/bin/installDeps.sh index 6767b693efb..6693a994f33 100755 --- a/bin/installDeps.sh +++ b/bin/installDeps.sh @@ -40,10 +40,17 @@ if [ ! $(echo $NODE_VERSION | cut -d "." -f 1-2) = "v0.6" ]; then exit 1 fi -#Does a settings.json exist? if no copy the template -if [ ! -f "settings.json" ]; then - echo "Copy the settings template to settings.json..." - cp -v settings.json.template settings.json || exit 1 +#Get the name of the settings file +settings="settings.json" +a=''; +for arg in $*; do + if [ "$a" = "--settings" ]; then settings=$arg; fi + a=$arg +done +#Does a $settings exist? if no copy the template +if [ ! -f $settings ]; then + echo "Copy the settings template to $settings..." + cp -v settings.json.template $settings || exit 1 fi echo "Ensure that all dependencies are up to date..." diff --git a/bin/run.sh b/bin/run.sh index a5245ff7790..c409920e789 100755 --- a/bin/run.sh +++ b/bin/run.sh @@ -21,9 +21,9 @@ if [ "$(id -u)" -eq 0 ]; then fi #prepare the enviroment -bin/installDeps.sh || exit 1 +bin/installDeps.sh $* || exit 1 #Move to the node folder and start echo "start..." cd "node" -node server.js +node server.js $* diff --git a/node/utils/Settings.js b/node/utils/Settings.js index e95702112d1..18832921ea7 100644 --- a/node/utils/Settings.js +++ b/node/utils/Settings.js @@ -22,6 +22,7 @@ var fs = require("fs"); var os = require("os"); var path = require('path'); +var argv = require('optimist').argv; /** * The IP ep-lite should listen to @@ -88,9 +89,12 @@ exports.abiwordAvailable = function() } } +// Discover where the settings file lives +var settingsFilename = argv.settings || "settings.json"; +var settingsPath = settingsFilename.charAt(0) == '/' ? '' : path.normalize(__dirname + "/../../"); + //read the settings sync -var settingsPath = path.normalize(__dirname + "/../../"); -var settingsStr = fs.readFileSync(settingsPath + "settings.json").toString(); +var settingsStr = fs.readFileSync(settingsPath + settingsFilename).toString(); //remove all comments settingsStr = settingsStr.replace(/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/gm,"").replace(/#.*/g,"").replace(/\/\/.*/g,""); diff --git a/package.json b/package.json index c05a909a258..4f761a4fe12 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,8 @@ "formidable" : "1.0.7", "log4js" : "0.4.1", "jsdom-nocontextifiy" : "0.2.10", - "async-stacktrace" : "0.0.2" + "async-stacktrace" : "0.0.2", + "optimist" : "0.3.1" }, "devDependencies": { "jshint" : "*" From 102318497db7cbdf22bd5be2ef3af497f92fbb9d Mon Sep 17 00:00:00 2001 From: Jordan Date: Thu, 16 Feb 2012 08:54:53 -0500 Subject: [PATCH 2/3] Allow settings to be specified with short '-s' option --- bin/installDeps.sh | 1 + node/utils/Settings.js | 8 ++++++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/bin/installDeps.sh b/bin/installDeps.sh index 6693a994f33..a88af2a67a5 100755 --- a/bin/installDeps.sh +++ b/bin/installDeps.sh @@ -45,6 +45,7 @@ settings="settings.json" a=''; for arg in $*; do if [ "$a" = "--settings" ]; then settings=$arg; fi + if [ "$a" = "-s" ]; then settings=$arg; fi a=$arg done #Does a $settings exist? if no copy the template diff --git a/node/utils/Settings.js b/node/utils/Settings.js index 18832921ea7..38496afd1a8 100644 --- a/node/utils/Settings.js +++ b/node/utils/Settings.js @@ -22,7 +22,11 @@ var fs = require("fs"); var os = require("os"); var path = require('path'); -var argv = require('optimist').argv; + +/** + * Read in cli args + */ +exports.argv = require('optimist').argv; /** * The IP ep-lite should listen to @@ -90,7 +94,7 @@ exports.abiwordAvailable = function() } // Discover where the settings file lives -var settingsFilename = argv.settings || "settings.json"; +var settingsFilename = exports.argv.settings || exports.argv.s || "settings.json"; var settingsPath = settingsFilename.charAt(0) == '/' ? '' : path.normalize(__dirname + "/../../"); //read the settings sync From f409914ff2912b4bde5b0ecc086284d27ecbe18f Mon Sep 17 00:00:00 2001 From: Jordan Date: Thu, 16 Feb 2012 18:26:06 -0500 Subject: [PATCH 3/3] Remove optimist dependency. Add help text with options --- bin/installDeps.sh | 3 +-- bin/run.sh | 12 ++++++++-- node/server.js | 1 + node/utils/Cli.js | 53 ++++++++++++++++++++++++++++++++++++++++++ node/utils/Settings.js | 8 ++----- package.json | 3 +-- 6 files changed, 68 insertions(+), 12 deletions(-) create mode 100644 node/utils/Cli.js diff --git a/bin/installDeps.sh b/bin/installDeps.sh index a88af2a67a5..9a75f7aba69 100755 --- a/bin/installDeps.sh +++ b/bin/installDeps.sh @@ -44,8 +44,7 @@ fi settings="settings.json" a=''; for arg in $*; do - if [ "$a" = "--settings" ]; then settings=$arg; fi - if [ "$a" = "-s" ]; then settings=$arg; fi + if [ "$a" = "--settings" ] || [ "$a" = "-s" ]; then settings=$arg; fi a=$arg done #Does a $settings exist? if no copy the template diff --git a/bin/run.sh b/bin/run.sh index c409920e789..05cdf049e24 100755 --- a/bin/run.sh +++ b/bin/run.sh @@ -20,10 +20,18 @@ if [ "$(id -u)" -eq 0 ]; then fi fi +prep=1 +#If merely looking for help, don't prep the environment +for arg in $*; do + if [ "$arg" = "--help" ] || [ "$arg" = "-h" ]; then prep=0; fi +done + #prepare the enviroment -bin/installDeps.sh $* || exit 1 +if [ $prep -eq 1 ]; then + bin/installDeps.sh $* || exit 1 + echo "start..." +fi #Move to the node folder and start -echo "start..." cd "node" node server.js $* diff --git a/node/server.js b/node/server.js index a6a57497aee..44c56748064 100644 --- a/node/server.js +++ b/node/server.js @@ -20,6 +20,7 @@ * limitations under the License. */ +var cli = require('./utils/Cli'); var ERR = require("async-stacktrace"); var log4js = require('log4js'); var os = require("os"); diff --git a/node/utils/Cli.js b/node/utils/Cli.js new file mode 100644 index 00000000000..e95f8f04125 --- /dev/null +++ b/node/utils/Cli.js @@ -0,0 +1,53 @@ +/** + * The CLI module handles command line parameters + */ + +/* + * 2012 Jordan Hollinger + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an + "AS-IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// The help text +exports.helpText = '\ +run.sh [OPTIONS] \n\ + \n\ +OPTIONS \n\ + -s|--settings FILE Specifies the settings.json file\n\ + -h|--help Print this message and exit \n\ +'; + +// An object containing the parsed command-line options +exports.argv = {}; + +var argv = process.argv.slice(2); +var arg, prevArg; + +// Loop through args +for ( var i = 0; i < argv.length; i++ ) { + arg = argv[i]; + + // Override location of settings.json file + if ( prevArg == '--settings' || prevArg == '-s' ) { + exports.argv.settings = arg; + } + + // Print help and exit + else if ( arg == '--help' || arg == '-h' ) { + console.log(exports.helpText); + process.exit(0); + } + + prevArg = arg; +} diff --git a/node/utils/Settings.js b/node/utils/Settings.js index 38496afd1a8..cf059bbfc90 100644 --- a/node/utils/Settings.js +++ b/node/utils/Settings.js @@ -22,11 +22,7 @@ var fs = require("fs"); var os = require("os"); var path = require('path'); - -/** - * Read in cli args - */ -exports.argv = require('optimist').argv; +var cli = require('./Cli'); /** * The IP ep-lite should listen to @@ -94,7 +90,7 @@ exports.abiwordAvailable = function() } // Discover where the settings file lives -var settingsFilename = exports.argv.settings || exports.argv.s || "settings.json"; +var settingsFilename = cli.argv.settings || "settings.json"; var settingsPath = settingsFilename.charAt(0) == '/' ? '' : path.normalize(__dirname + "/../../"); //read the settings sync diff --git a/package.json b/package.json index 4f761a4fe12..c05a909a258 100644 --- a/package.json +++ b/package.json @@ -20,8 +20,7 @@ "formidable" : "1.0.7", "log4js" : "0.4.1", "jsdom-nocontextifiy" : "0.2.10", - "async-stacktrace" : "0.0.2", - "optimist" : "0.3.1" + "async-stacktrace" : "0.0.2" }, "devDependencies": { "jshint" : "*"