You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by dp...@apache.org on 2018/08/28 23:58:43 UTC

[cordova-common] branch master updated: Make PlatformJson.generateMetadata more readable

This is an automated email from the ASF dual-hosted git repository.

dpogue pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/cordova-common.git


The following commit(s) were added to refs/heads/master by this push:
     new 52b8f4f  Make PlatformJson.generateMetadata more readable
52b8f4f is described below

commit 52b8f4f4c4204a6ce4578db5e8068be62bdd5727
Author: Raphael von der GrĂ¼n <ra...@gmail.com>
AuthorDate: Thu May 24 14:56:45 2018 +0200

    Make PlatformJson.generateMetadata more readable
---
 package.json              |  1 +
 spec/PlatformJson.spec.js | 28 +++++++++++++++++++++-------
 src/PlatformJson.js       | 17 ++++++++---------
 3 files changed, 30 insertions(+), 16 deletions(-)

diff --git a/package.json b/package.json
index 4aff921..19c5e27 100644
--- a/package.json
+++ b/package.json
@@ -26,6 +26,7 @@
     "ansi": "^0.3.1",
     "bplist-parser": "^0.1.0",
     "elementtree": "0.1.7",
+    "endent": "^1.1.1",
     "fs-extra": "^6.0.1",
     "glob": "^7.1.2",
     "minimatch": "^3.0.0",
diff --git a/spec/PlatformJson.spec.js b/spec/PlatformJson.spec.js
index 97d04ff..d2c3c7e 100644
--- a/spec/PlatformJson.spec.js
+++ b/spec/PlatformJson.spec.js
@@ -108,13 +108,27 @@ describe('PlatformJson class', function () {
             });
         });
 
+        function evaluateCordovaDefineStatement (str) {
+            expect(typeof str).toBe('string');
+            const fnString = str.replace(/^\s*cordova\.define\('cordova\/plugin_list',\s*([\s\S]+)\);\s*$/, '($1)');
+            const mod = {exports: {}};
+            global.eval(fnString)(null, mod.exports, mod); // eslint-disable-line no-eval
+            return mod;
+        }
+
+        function expectedMetadata () {
+            // Create plain objects from ModuleMetadata instances
+            const modules = platformJson.root.modules.map(o => Object.assign({}, o));
+            modules.metadata = platformJson.root.plugin_metadata;
+            return modules;
+        }
+
         describe('generateMetadata method', function () {
             it('Test 009 : should generate text metadata containing list of installed modules', function () {
-                var meta = platformJson.addPluginMetadata(fakePlugin).generateMetadata();
-                expect(typeof meta).toBe('string');
-                expect(meta.indexOf(JSON.stringify(platformJson.root.modules, null, 2))).toBeGreaterThan(0);
-                // expect(meta).toMatch(JSON.stringify(platformJson.root.modules, null, 2));
-                expect(meta).toMatch(JSON.stringify(platformJson.root.plugin_metadata, null, 2));
+                const meta = platformJson.addPluginMetadata(fakePlugin).generateMetadata();
+                const mod = evaluateCordovaDefineStatement(meta);
+
+                expect(mod.exports).toEqual(expectedMetadata());
             });
         });
 
@@ -129,8 +143,8 @@ describe('PlatformJson class', function () {
                 expect(spy).toHaveBeenCalledTimes(1);
                 const [file, data] = spy.calls.argsFor(0);
                 expect(file).toBe(dest);
-                expect(data.indexOf(JSON.stringify(platformJson.root.modules, null, 2))).toBeGreaterThan(0);
-                expect(data).toMatch(JSON.stringify(platformJson.root.plugin_metadata, null, 2));
+                const mod = evaluateCordovaDefineStatement(data);
+                expect(mod.exports).toEqual(expectedMetadata());
             });
         });
     });
diff --git a/src/PlatformJson.js b/src/PlatformJson.js
index 4645f9b..206093e 100644
--- a/src/PlatformJson.js
+++ b/src/PlatformJson.js
@@ -16,6 +16,7 @@
 
 var fs = require('fs-extra');
 var path = require('path');
+var endent = require('endent');
 var mungeutil = require('./ConfigChanges/munge-util');
 
 function PlatformJson (filePath, platform, root) {
@@ -182,15 +183,13 @@ PlatformJson.prototype.makeTopLevel = function (pluginId) {
  * @returns {String} cordova_plugins.js contents
  */
 PlatformJson.prototype.generateMetadata = function () {
-    return [
-        'cordova.define(\'cordova/plugin_list\', function(require, exports, module) {',
-        'module.exports = ' + JSON.stringify(this.root.modules, null, 2) + ';',
-        'module.exports.metadata = ',
-        '// TOP OF METADATA',
-        JSON.stringify(this.root.plugin_metadata, null, 2) + ';',
-        '// BOTTOM OF METADATA',
-        '});' // Close cordova.define.
-    ].join('\n');
+    const stringify = o => JSON.stringify(o, null, 2);
+    return endent`
+        cordova.define('cordova/plugin_list', function(require, exports, module) {
+          module.exports = ${stringify(this.root.modules)};
+          module.exports.metadata = ${stringify(this.root.plugin_metadata)};
+        });
+    `;
 };
 
 /**


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org
For additional commands, e-mail: commits-help@cordova.apache.org