CB-4531 bin/check_reqs scripts exit with code 2 on error

- Return ERROR_VALUE/NOTIMPLEMENTED_VALUE
- Restore number path for symmetry with install-emulator, install-device, build
- Refactor exit_handler into utils

Reviewed by James Keshavarzi <jkeshavarzi@blackberry.com>
Tested by Jenny Gee <jgee@blackberry.com>
diff --git a/blackberry10/bin/check_reqs.bat b/blackberry10/bin/check_reqs.bat
index 44445eb..c25beb1 100755
--- a/blackberry10/bin/check_reqs.bat
+++ b/blackberry10/bin/check_reqs.bat
@@ -24,5 +24,5 @@
 )
 
 call %INITCALL%
-if ERRORLEVEL 1 exit /B 1
+if ERRORLEVEL 1 exit /B %ERRORLEVEL%
 
diff --git a/blackberry10/bin/check_reqs.js b/blackberry10/bin/check_reqs.js
index 739abe0..a8ac214 100644
--- a/blackberry10/bin/check_reqs.js
+++ b/blackberry10/bin/check_reqs.js
@@ -18,6 +18,7 @@
  */
 
 var MIN_NODE_VER = "0.9.9",
+    ERROR_VALUE = 2,
     signingUtils = require('./lib/signing-utils');
 
 function isNodeNewerThanMin () {
@@ -29,7 +30,7 @@
 
 if (!isNodeNewerThanMin()) {
     console.log("Node version '" + process.versions.node + "' is not new enough. Please upgrade to " + MIN_NODE_VER + " or newer. Aborting.");
-    process.exit(2);
+    process.exit(ERROR_VALUE);
 }
 
 if (!signingUtils.getKeyStorePath()) {
diff --git a/blackberry10/bin/create b/blackberry10/bin/create
index 1953e1a..f10cd9c 100755
--- a/blackberry10/bin/create
+++ b/blackberry10/bin/create
@@ -40,7 +40,7 @@
 
     if ! [ $? -eq 0 ]; then
         echo "NPM install failed. Aborting."
-        exit 1
+        exit $?
     fi
 fi
 
diff --git a/blackberry10/bin/create.bat b/blackberry10/bin/create.bat
index 22d3dbd..428d01b 100644
--- a/blackberry10/bin/create.bat
+++ b/blackberry10/bin/create.bat
@@ -20,7 +20,7 @@
 set BIN_DIR=%~dp0
 
 call "%BIN_DIR%init"
-if ERRORLEVEL 1 exit /B 1
+if ERRORLEVEL 1 exit /B %ERRORLEVEL% 
 
 if exist "%BIN_DIR%..\package.json" (
     pushd %BIN_DIR%..
@@ -30,3 +30,4 @@
 )
 
 "%CORDOVA_NODE%\node" "%BIN_DIR%create.js" %*
+if ERRORLEVEL 1 exit /B %ERRORLEVEL%
diff --git a/blackberry10/bin/create.js b/blackberry10/bin/create.js
index 563ef13..2bfc089 100644
--- a/blackberry10/bin/create.js
+++ b/blackberry10/bin/create.js
@@ -42,6 +42,7 @@
     BIN_DIR = path.join(__dirname),
     BUILD_DIR = path.join(__dirname, "build"),
     CORDOVA_JS_SRC = path.join(__dirname, "..", "javascript", "cordova.blackberry10.js"),
+    ERROR_VALUE = 2,
     update_dir = path.join(project_path, "lib", "cordova." + version),
     native_dir = path.join(project_path, "native"),
     js_path = "javascript",
@@ -68,7 +69,7 @@
     if (!process.argv[2]) {
         console.log("You must give a project PATH");
         help();
-        process.exit(2);
+        process.exit(ERROR_VALUE);
         return "";
     } else {
         return path.resolve(process.argv[2]);
@@ -79,7 +80,7 @@
     if (fs.existsSync(project_path)) {
         console.log("The project path must be an empty directory");
         help();
-        process.exit(2);
+        process.exit(ERROR_VALUE);
     }
     if (!validPackageName(app_id)) {
         console.log("[warning] App ID must be sequence of alpha-numeric (optionally separated by '.') characters, no longer than 50 characters.\n" +
@@ -193,6 +194,6 @@
     } catch (ex) {
         console.log("Project creation failed!");
         console.error(os.EOL + ex);
-        process.exit(2);
+        process.exit(ERROR_VALUE);
     }
 }
diff --git a/blackberry10/bin/lib/target.js b/blackberry10/bin/lib/target.js
index a436be4..b055cff 100644
--- a/blackberry10/bin/lib/target.js
+++ b/blackberry10/bin/lib/target.js
@@ -20,6 +20,8 @@
     utils = require('./utils'),
     commander = require('commander'),
     properties = utils.getProperties(),
+    ERROR_VALUE = 2,
+    NOTIMPLEMENTED_VALUE = 1,
     command,
     name,
     ip,
@@ -36,7 +38,7 @@
     if (typeof ip !== 'string') {
         console.log("IP is required");
         console.log(commander.helpInformation());
-        process.exit(2);
+        process.exit(ERROR_VALUE);
     } else {
         ipArray = ip.split('.');
         if (ipArray.length !== 4) {
@@ -58,7 +60,7 @@
     if (typeof type !== 'string') {
         console.log("target type is required");
         console.log(commander.helpInformation());
-        process.exit(2);
+        process.exit(ERROR_VALUE);
     }
     else if (!(type === 'device' || type === 'simulator')) {
         result = false;
@@ -97,7 +99,7 @@
         if (commander.args.length === 1) {
             console.log("Target details not specified");
             console.log(commander.helpInformation());
-            process.exit(2);
+            process.exit(ERROR_VALUE);
         }
         name = commander.args[0];
         ip = commander.args[1];
@@ -111,17 +113,17 @@
         if (!isValidIp(ip)) {
             console.log("Invalid IP: " + ip);
             console.log(commander.helpInformation());
-            process.exit(2);
+            process.exit(ERROR_VALUE);
         }
         if (!isValidType(type)) {
             console.log("Invalid target type: " + type);
             console.log(commander.helpInformation());
-            process.exit(2);
+            process.exit(ERROR_VALUE);
         }
         if (!isValidPin(pin)) {
             console.log("Invalid PIN: " + pin);
             console.log(commander.helpInformation());
-            process.exit(2);
+            process.exit(ERROR_VALUE);
         }
         if (properties.targets.hasOwnProperty(name)) {
             console.log("Overwriting target: " + name);
@@ -136,13 +138,13 @@
         if (commander.args.length === 1) {
             console.log('No target specified');
             console.log(commander.helpInformation());
-            process.exit(2);
+            process.exit(ERROR_VALUE);
         }
         name = commander.args[0];
         if (!properties.targets.hasOwnProperty(name)) {
             console.log("Target: '" + name + "' not found");
             console.log(commander.helpInformation());
-            process.exit(2);
+            process.exit(ERROR_VALUE);
         }
         if (name === properties.defaultTarget) {
             console.log("Deleting default target, please set a new default target");
@@ -165,7 +167,7 @@
         } else {
             console.log("Target '" + name + "' not found");
             console.log(commander.helpInformation());
-            process.exit(2);
+            process.exit(ERROR_VALUE);
         }
     });
 
@@ -174,7 +176,7 @@
     .action(function () {
         console.log('Unrecognized command');
         console.log(commander.helpInformation());
-        process.exit(2);
+        process.exit(NOTIMPLEMENTED_VALUE);
     });
 
 
diff --git a/blackberry10/bin/lib/utils.js b/blackberry10/bin/lib/utils.js
index 2e29916..be1ec42 100644
--- a/blackberry10/bin/lib/utils.js
+++ b/blackberry10/bin/lib/utils.js
@@ -15,6 +15,7 @@
  */
 
 var fs = require('fs'),
+    async = require('async'),
     path = require('path'),
     childProcess = require('child_process'),
     wrench = require('wrench'),
@@ -24,6 +25,7 @@
     DEFAULT_BAR_NAME = "bb10app",
     PROPERTY_FILE_NAME = 'blackberry10.json',
     CORDOVA_DIR = '.cordova',
+    ERROR_VALUE = 2,
     DEFAULT_PROPERTY_FILE = {
         targets: {
         }
@@ -346,6 +348,27 @@
             }
         });
         return to;
+    },
+
+    series: function (steps) {
+        async.series(steps, this.exit_handler);
+    },
+
+    waterfall: function (steps) { 
+        async.waterfall(steps, this.exit_handler);
+    },
+
+    exit_handler: function (err) {
+        if (err) {
+            if (typeof err === "string") {
+                console.error(err);
+            } else {
+                console.error("An error has occurred");
+            }
+            process.exit(ERROR_VALUE);
+        } else {
+            process.exit(0);
+        }
     }
 
 };
diff --git a/blackberry10/bin/templates/project/cordova/lib/build.js b/blackberry10/bin/templates/project/cordova/lib/build.js
index 6bd0879..46d9b74 100755
--- a/blackberry10/bin/templates/project/cordova/lib/build.js
+++ b/blackberry10/bin/templates/project/cordova/lib/build.js
@@ -32,6 +32,7 @@
     childProcess = require("child_process"),
     pkgrUtils = require("./packager-utils"),
     session = require("./session"),
+    ERROR_VALUE = 2,
     commandStr;
 
 function copyArgIfExists(arg) {
@@ -58,10 +59,10 @@
     if (command.debug && command.release) {
         console.error("Invalid build command: cannot specify both debug and release parameters.");
         console.log(command.helpInformation());
-        process.exit(2);
+        process.exit(ERROR_VALUE);
     }
 
-    async.series(
+    utils.series(
         [
             function clean (done) {
                 var cmd = utils.isWindows() ? "clean" : "./clean",
@@ -112,24 +113,10 @@
 
                 require("./packager").start(done);
             }
-        ],
-        function (err, results) {
-            if (err) {
-                if (typeof err === "string") {
-                    console.error(os.EOL + err);
-                    process.exit(2);
-                } else if (typeof err === "number") {
-                    process.exit(err);
-                } else {
-                    process.exit(2);
-                }
-            } else {
-                process.exit(0);
-            }
-        }
+        ]
     );
 } catch (e) {
     console.error(os.EOL + e);
-    process.exit(2);
+    process.exit(ERROR_VALUE);
 }
 
diff --git a/blackberry10/bin/templates/project/cordova/lib/install-device b/blackberry10/bin/templates/project/cordova/lib/install-device
index 5809a41..45f8a07 100644
--- a/blackberry10/bin/templates/project/cordova/lib/install-device
+++ b/blackberry10/bin/templates/project/cordova/lib/install-device
@@ -20,8 +20,7 @@
     utils = require("./utils"),
     options = require('commander'),
     runUtils = require("./run-utils"),
-    logger = require("./logger"),
-    async = require("async");
+    logger = require("./logger");
 
 options
     .usage('[--target=id]')
@@ -47,23 +46,11 @@
 options.parse(process.argv);
 
 options.device = true;
-async.waterfall(
+utils.waterfall(
     [
         runUtils.getValidatedTarget.bind(this, options),
         runUtils.checkBuild,
         runUtils.uninstall.bind(this, options),
         runUtils.deployToTarget.bind(this, options)
-    ],
-    function (err) {
-        if (err) {
-            if (typeof err === "string") {
-                logger.error(err);
-                process.exit(1);
-            } else if (typeof err === "number") {
-                process.exit(err);
-            }
-        } else {
-            process.exit(0);
-        }
-    }
+    ]
 );
diff --git a/blackberry10/bin/templates/project/cordova/lib/install-emulator b/blackberry10/bin/templates/project/cordova/lib/install-emulator
index 972c475..afbc581 100644
--- a/blackberry10/bin/templates/project/cordova/lib/install-emulator
+++ b/blackberry10/bin/templates/project/cordova/lib/install-emulator
@@ -20,8 +20,7 @@
     utils = require("./utils"),
     options = require('commander'),
     runUtils = require("./run-utils"),
-    logger = require("./logger"),
-    async = require("async");
+    logger = require("./logger");
 
 options
     .usage('[--target=id]')
@@ -47,23 +46,11 @@
 options.parse(process.argv);
 
 options.emulator = true;
-async.waterfall(
+utils.waterfall(
     [
         runUtils.getValidatedTarget.bind(this, options),
         runUtils.checkBuild,
         runUtils.uninstall.bind(this, options),
         runUtils.deployToTarget.bind(this, options)
-    ],
-    function (err) {
-        if (err) {
-            if (typeof err === "string") {
-                logger.error(err);
-                process.exit(1);
-            } else if (typeof err === "number") {
-                process.exit(err);
-            }
-        } else {
-            process.exit(0);
-        }
-    }
+    ]
 );
diff --git a/blackberry10/bin/templates/project/cordova/lib/log b/blackberry10/bin/templates/project/cordova/lib/log
index 47be37d..84e76c6 100755
--- a/blackberry10/bin/templates/project/cordova/lib/log
+++ b/blackberry10/bin/templates/project/cordova/lib/log
@@ -18,6 +18,7 @@
        under the License.
 COMMENT
 set -e
+NOTIMPLEMENTED_VALUE=1
 
 echo 'BlackBerry10: logging not supported at this time.'
-exit 1
+exit $NOTIMPLEMENTED_VALUE
diff --git a/blackberry10/bin/templates/project/cordova/lib/run.js b/blackberry10/bin/templates/project/cordova/lib/run.js
index 0c5313a..de3f020 100755
--- a/blackberry10/bin/templates/project/cordova/lib/run.js
+++ b/blackberry10/bin/templates/project/cordova/lib/run.js
@@ -21,8 +21,7 @@
     utils = require("./utils"),
     options = require('commander'),
     runUtils = require("./run-utils"),
-    async = require("async"),
-    ERROR_CODE = 2;
+    async = require("async");
 
 function install(deployTarget, done) {
     var buildCmd = utils.isWindows() ? "build" : "./build",
@@ -69,19 +68,9 @@
 
 options.parse(process.argv);
 
-async.waterfall(
+utils.waterfall(
     [
         runUtils.getValidatedTarget.bind(this, options),
         install
-    ],
-    function (err) {
-        if (err) {
-            if (typeof err === "string") {
-                console.error(os.EOL + err);
-            }
-            process.exit(ERROR_CODE);
-        } else {
-            process.exit(0);
-        }
-    }
+    ]
 );
diff --git a/blackberry10/bin/templates/project/cordova/lib/start-emulator b/blackberry10/bin/templates/project/cordova/lib/start-emulator
index d887007..1ad590b 100755
--- a/blackberry10/bin/templates/project/cordova/lib/start-emulator
+++ b/blackberry10/bin/templates/project/cordova/lib/start-emulator
@@ -23,11 +23,13 @@
 VMWARE_DEFAULT_PATH=/Applications/VMware\ Fusion.app/Contents/Library
 VMWARE_TOOL=vmrun
 OS_NAME=`uname`
+ERROR_VALUE=2
+NOTIMPLEMENTED_VALUE=1
 
 if [ "$1" = "" -o ! -e "$1" ]
 then
     echo "BlackBerry10: must provide path to valid vmwarevm image"
-    exit 2
+    exit $ERROR_VALUE
 fi
 
 if [ "$OS_NAME" = "Darwin" ]; then
@@ -45,9 +47,9 @@
         exit 0 
     else
         echo "BlackBerry10: could not find VMware Fusion 'vmrun' tool"
-        exit 1 
+        exit $ERROR_VALUE
     fi
 else
     echo "BlackBerry10: currently only supports installing to emulator on OS X"
-    exit 1
+    exit $NOTIMPLEMENTED_VALUE 
 fi
diff --git a/blackberry10/bin/templates/project/cordova/lib/start-emulator.bat b/blackberry10/bin/templates/project/cordova/lib/start-emulator.bat
index 6671abb..714d6b4 100644
--- a/blackberry10/bin/templates/project/cordova/lib/start-emulator.bat
+++ b/blackberry10/bin/templates/project/cordova/lib/start-emulator.bat
@@ -16,6 +16,7 @@
 :: under the License.
 
 @ECHO OFF
+set NOTIMPLEMENTED_VALUE=1
 
 ECHO echo 'BlackBerry10: Not able to start emulator images on windows command-line at this time.'
-EXIT /B 1
+EXIT /B %NOTIMPLEMENTED_VALUE%