Make PlatformJson.generateMetadata more readable
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 @@
});
});
+ 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 @@
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 @@
* @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)};
+ });
+ `;
};
/**