cordova-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lorinb...@apache.org
Subject [07/15] git commit: parse config json moved to a function
Date Thu, 21 Aug 2014 18:23:08 GMT
parse config json moved to a function

JSON.parse is now wrapped in a try/catch block
current behaviour is to crash on an invalid string
behaviour implemented here preserves the effect of exiting when an error is encountered, but
provides an error message, exiting gracefully


Project: http://git-wip-us.apache.org/repos/asf/cordova-cli/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-cli/commit/64ac2fbe
Tree: http://git-wip-us.apache.org/repos/asf/cordova-cli/tree/64ac2fbe
Diff: http://git-wip-us.apache.org/repos/asf/cordova-cli/diff/64ac2fbe

Branch: refs/heads/master
Commit: 64ac2fbe197e2b91dc6fc1c26011559af806012a
Parents: eeaca77
Author: Lorin Beer <lorin.beer@gmail.com>
Authored: Fri Jun 13 13:46:28 2014 -0700
Committer: Lorin Beer <lorin.beer@gmail.com>
Committed: Wed Aug 20 11:19:59 2014 -0700

----------------------------------------------------------------------
 src/create.js | 29 ++++++++++++++++++++++++-----
 1 file changed, 24 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/64ac2fbe/src/create.js
----------------------------------------------------------------------
diff --git a/src/create.js b/src/create.js
index 19d2acb..9e7f728 100644
--- a/src/create.js
+++ b/src/create.js
@@ -1,6 +1,28 @@
 
 
 
+
+
+/**
+ * parseConfig
+ * generic parser, if it's valid json, returns the resulting object
+ * if anything resolving to false is passed in, return an empty object 
+ * invalid json results in an error message and process exit with status code 2.
+ *
+ * jsondata - a json data string
+ *
+ */
+function parseConfig(jsondata) {
+    if (!jsondata) return {};
+
+    try {
+        cfg = JSON.parse(jsondata);
+    } catch (e) {
+        console.error('Error while parsing json data\nError: '+ e +'\nData:' + jsondata);
+        process.exit(2); 
+    }
+};
+
 /**
  * provides logic for exposing cordova-lib create functionality to the command line
  * the create argument is implied from the call to this function, all other cl arguments
should be passed in unmodified
@@ -12,10 +34,8 @@ var CordovaCLICreate = function (args, undashed) {
     var cfg = {},
         customWww;
 
-        // If we got a fourth parameter, consider it to be JSON to init the config.
-        if ( undashed[4] ) {
-            cfg = JSON.parse(undashed[4]);
-        }
+    // parseConfig will determine if there's a valid config JSON string
+    cfg = parseCofig(undashed[4]);
 
         // handle custom www
         if (customWww = args['copy-from'] || args['link-to']) {
@@ -46,4 +66,3 @@ var CordovaCLICreate = function (args, undashed) {
 };
 
 module.exports = CordovaCLICreate;
-


Mime
View raw message