cordova-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fil...@apache.org
Subject [10/50] [abbrv] cordova-lib git commit: CB-12001: Added more restore tests for platform/plugin/spec.
Date Thu, 09 Mar 2017 20:39:13 GMT
http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/ed9ba79a/cordova-lib/spec-cordova/pkgJson.spec.js
----------------------------------------------------------------------
diff --git a/cordova-lib/spec-cordova/pkgJson.spec.js b/cordova-lib/spec-cordova/pkgJson.spec.js
index 45eb8ee..3920c1a 100644
--- a/cordova-lib/spec-cordova/pkgJson.spec.js
+++ b/cordova-lib/spec-cordova/pkgJson.spec.js
@@ -20,6 +20,7 @@ var helpers = require('./helpers'),
     path = require('path'),
     shell = require('shelljs'),
     events = require('cordova-common').events,
+    ConfigParser = require('cordova-common').ConfigParser,
     cordova = require('../src/cordova/cordova');
 
 // This group of tests checks if plugins are added and removed as expected from package.json.
@@ -41,7 +42,6 @@ describe('plugin end-to-end', function() {
         // Copy some platform to avoid working on a project with no platforms.
         shell.cp('-R', path.join(__dirname, 'fixtures', 'platforms', helpers.testPlatform),
path.join(project, 'platforms'));
         process.chdir(project);
-
         delete process.env.PWD;
     });
 
@@ -50,30 +50,48 @@ describe('plugin end-to-end', function() {
         shell.rm('-rf', tmpDir);
     });
 
-    it('Test#001 : should successfully add and remove a plugin with save', function(done)
{
+    it('Test#001 : should successfully add and remove a plugin with save and correct spec',
function(done) {
         var pkgJsonPath = path.join(process.cwd(),'package.json');
-        var pkgJson;
-    
+        var pkgJson = require(pkgJsonPath);
+        var cwd = process.cwd();
+        var configXmlPath = path.join(cwd, 'config.xml');
+        var cfg = new ConfigParser(configXmlPath);
+        var configPlugins = cfg.getPluginIdList();
+        var configPlugin = cfg.getPlugin(configPlugins);
+
+        // No plugins in config or pkg.json yet.
+        expect(configPlugins.length).toEqual(0);
+        expect(pkgJson.cordova).toBeUndefined();
         expect(pkgJsonPath).toExist();
 
-        // Add the plugin with --save
-        return cordova.raw.plugin('add', pluginId, {'save':true})
+        // Add the plugin with --save.
+        return cordova.raw.plugin('add', pluginId+'@1.1.2', {'save':true, 'fetch':true})
         .then(function() {
-            // Check that the plugin add was successful.
+            // Delete any previous caches of require(package.json).
             delete require.cache[require.resolve(pkgJsonPath)];
             pkgJson = require(pkgJsonPath);
-            expect(pkgJson).not.toBeUndefined();
-            expect(pkgJson.cordova.plugins).not.toBeUndefined();
+            // Check that the plugin and spec add was successful to pkg.json.
+            expect(pkgJson).toBeDefined();
+            expect(pkgJson.cordova.plugins).toBeDefined();
             expect(pkgJson.cordova.plugins[pluginId]).toBeDefined();
+            expect(pkgJson.dependencies['cordova-plugin-device']).toEqual('^1.1.2');
+            // Check that the plugin and spec add was successful to config.xml.
+            var cfg2 = new ConfigParser(configXmlPath);
+            configPlugins = cfg2.getPluginIdList();
+            configPlugin = cfg2.getPlugin(configPlugins);
+            expect(configPlugins.length).toEqual(1);
+            expect(configPlugin).toEqual({ name: 'cordova-plugin-device', spec: '~1.1.2',
variables: {} });
         }).then(function() {
             // And now remove it with --save.
-            return cordova.raw.plugin('rm', pluginId, {'save':true})
+            return cordova.raw.plugin('rm', pluginId, {'save':true, 'fetch':true});
         }).then(function() {
-            // Delete any previous caches of require(package.json)
+            // Delete any previous caches of require(package.json).
             delete require.cache[require.resolve(pkgJsonPath)];
             pkgJson = require(pkgJsonPath);
-            // Checking that the plugin removed is in not in the platforms
+            // Checking that the plugin removed is in not in the pkg.json.
             expect(pkgJson.cordova.plugins[pluginId]).toBeUndefined();
+            // Spec should be removed from dependencies.
+            expect(pkgJson.dependencies['cordova-plugin-device']).toBeUndefined();
         }).fail(function(err) {
             expect(err).toBeUndefined();
         }).fin(done);
@@ -85,15 +103,16 @@ describe('plugin end-to-end', function() {
 
         expect(pkgJsonPath).toExist();
 
-        // Add the plugin with --save.
+        // Add the camera plugin with --save.
         return cordova.raw.plugin('add', 'cordova-plugin-camera', {'save':true})
         .then(function() {
-            // Add a second plugin without save
+            // Add a second plugin without save.
             return cordova.raw.plugin('add', pluginId);
         }).then(function() {
-            // Check the plugin add was successful for the first plugin that had --save.
+            // Delete any previous caches of require(package.json).
             delete require.cache[require.resolve(pkgJsonPath)];
             pkgJson = require(pkgJsonPath);
+            // Check the plugin add was successful for the first plugin that had --save.
             expect(pkgJson).not.toBeUndefined();
             expect(pkgJson.cordova.plugins['cordova-plugin-camera']).toBeDefined();
             // Expect that the second plugin is not added.
@@ -112,17 +131,18 @@ describe('plugin end-to-end', function() {
         // Add the plugin with --save.
         return cordova.raw.plugin('add', pluginId, {'save':true})
         .then(function() {
-            // Check the platform add was successful.
+            // Delete any previous caches of require(package.json).
             delete require.cache[require.resolve(pkgJsonPath)];
             pkgJson = require(pkgJsonPath);
+            // Check the platform add was successful.
             expect(pkgJson).not.toBeUndefined();
             expect(pkgJson.cordova.plugins).toBeDefined();
             expect(pkgJson.cordova.plugins[pluginId]).toBeDefined();
         }).then(function() {
             // And now remove it, but without --save.
-            return cordova.raw.plugin('rm', 'cordova-plugin-device')
+            return cordova.raw.plugin('rm', 'cordova-plugin-device');
         }).then(function() {
-            // Delete any previous caches of require(package.json)
+            // Delete any previous caches of require(package.json).
             delete require.cache[require.resolve(pkgJsonPath)];
             pkgJson = require(pkgJsonPath);
             // The plugin should still be in package.json.
@@ -135,25 +155,26 @@ describe('plugin end-to-end', function() {
     it('Test#004 : should successfully add and remove a plugin with variables and save to
package.json', function(done) {
         var pkgJsonPath = path.join(process.cwd(),'package.json');
         var pkgJson;
+        var someKey = 'someKey';
 
         expect(pkgJsonPath).toExist();
 
         // Add the plugin with --save.
         return cordova.raw.plugin('add', pluginId, {'save':true, 'cli_variables': {'someKey':'someValue'}})
         .then(function() {
-            // Delete any previous caches of require(package.json)
+            // Delete any previous caches of require(package.json).
             delete require.cache[require.resolve(pkgJsonPath)];
             pkgJson = require(pkgJsonPath);
             // Check the plugin add was successful and that variables have been added too.
             expect(pkgJson).not.toBeUndefined();
             expect(pkgJson.cordova.plugins).toBeDefined();
             expect(pkgJson.cordova.plugins[pluginId]).toBeDefined();
-            expect(pkgJson.cordova.plugins[pluginId]['someKey']).toEqual('someValue');
+            expect(pkgJson.cordova.plugins[pluginId][someKey]).toEqual('someValue');
         }).then(function() {
             // And now remove it with --save.
-            return cordova.raw.plugin('rm', pluginId, {'save':true})
+            return cordova.raw.plugin('rm', pluginId, {'save':true});
         }).then(function() {
-            // Delete any previous caches of require(package.json)
+            // Delete any previous caches of require(package.json).
             delete require.cache[require.resolve(pkgJsonPath)];
             pkgJson = require(pkgJsonPath);
             // Checking that the plugin and variables were removed successfully.
@@ -165,15 +186,19 @@ describe('plugin end-to-end', function() {
     it('Test#005 : should successfully add and remove multiple plugins with save & fetch',
function(done) {
         var pkgJsonPath = path.join(process.cwd(),'package.json');
         var pkgJson;
+        // Delete any previous caches of require(package.json).
+        delete require.cache[require.resolve(pkgJsonPath)];
+        pkgJson = require(pkgJsonPath);
     
         expect(pkgJsonPath).toExist();
 
-        // Add the plugin with --save
+        // Add the plugin with --save.
         return cordova.raw.plugin('add', [pluginId,'cordova-plugin-device-motion'], {'save':true,
'fetch':true})
         .then(function() {
-            // Check that the plugin add was successful.
+            // Delete any previous caches of require(package.json).
             delete require.cache[require.resolve(pkgJsonPath)];
             pkgJson = require(pkgJsonPath);
+            // Check that the plugin add was successful.
             expect(pkgJson).not.toBeUndefined();
             expect(pkgJson.cordova.plugins).not.toBeUndefined();
             expect(pkgJson.cordova.plugins[pluginId]).toBeDefined();
@@ -182,12 +207,12 @@ describe('plugin end-to-end', function() {
             expect(pkgJson.dependencies['cordova-plugin-device-motion']).toBeDefined();
         }).then(function() {
             // And now remove it with --save.
-            return cordova.raw.plugin('rm', [pluginId,'cordova-plugin-device-motion'], {'save':true,
'fetch':true})
+            return cordova.raw.plugin('rm', [pluginId,'cordova-plugin-device-motion'], {'save':true,
'fetch':true});
         }).then(function() {
-            // Delete any previous caches of require(package.json)
+            // Delete any previous caches of require(package.json).
             delete require.cache[require.resolve(pkgJsonPath)];
             pkgJson = require(pkgJsonPath);
-            // Checking that the plugin removed is in not in the platforms
+            // Checking that the plugin removed is in not in the platforms.
             expect(pkgJson.cordova.plugins[pluginId]).toBeUndefined();
             expect(pkgJson.cordova.plugins['cordova-plugin-device-motion']).toBeUndefined();
             expect(pkgJson.dependencies[pluginId]).toBeUndefined();
@@ -216,6 +241,7 @@ describe('platform end-to-end with --save', function () {
     });
 
     afterEach(function() {
+        // Delete any previous caches of require(package.json).
         delete require.cache[require.resolve(path.join(process.cwd(),'package.json'))];
         process.chdir(path.join(__dirname, '..'));  // Needed to rm the dir on Windows.
         shell.rm('-rf', tmpDir);
@@ -256,10 +282,10 @@ describe('platform end-to-end with --save', function () {
             // And now remove it with --save.
             return cordova.raw.platform('rm', [helpers.testPlatform], {'save':true});
         }).then(function() {
-            // Delete any previous caches of require(package.json)
+            // Delete any previous caches of require(package.json).
             delete require.cache[require.resolve(pkgJsonPath)];
             pkgJson = require(pkgJsonPath);
-            // Checking that the platform removed is in not in the platforms key
+            // Checking that the platform removed is in not in the platforms key.
             expect(pkgJson.cordova.platforms.indexOf(helpers.testPlatform)).toEqual(-1);
         }).then(emptyPlatformList) // platform ls should be empty too.
         .fail(function(err) {
@@ -284,7 +310,7 @@ describe('platform end-to-end with --save', function () {
             // And now remove it without --save.
             return cordova.raw.platform('rm', [helpers.testPlatform]);
         }).then(function() {
-            // Delete any previous caches of require(package.json)
+            // Delete any previous caches of require(package.json).
             delete require.cache[require.resolve(pkgJsonPath)];
             pkgJson = require(pkgJsonPath);
             // Check that the platform removed without --save is still in platforms key.
@@ -299,6 +325,7 @@ describe('platform end-to-end with --save', function () {
         var pkgJsonPath = path.join(process.cwd(),'package.json');
         var pkgJson;
         expect(pkgJsonPath).toExist();
+        // Delete any previous caches of require(package.json).
         delete require.cache[require.resolve(pkgJsonPath)];
         pkgJson = require(pkgJsonPath);
         // Pkg.json "platforms" should be empty and helpers.testPlatform should not exist
in pkg.json.
@@ -306,11 +333,11 @@ describe('platform end-to-end with --save', function () {
         // Add platform without --save.
         cordova.raw.platform('add',[helpers.testPlatform])
         .then(function() {
-            // Check the platform add was successful, reload, skipping cache
+            // Check the platform add was successful, reload, skipping cache.
             delete require.cache[require.resolve(pkgJsonPath)];
             pkgJson = require(pkgJsonPath);
-            // Platform list should be empty and helpers.testPlatform should NOT have been
added.
-            expect(pkgJson.cordova.platforms.length).toEqual(0);
+            // PkgJson.cordova should not be defined and helpers.testPlatform should NOT
have been added.
+            expect(pkgJson.cordova).toBeUndefined();
         }).then(fullPlatformList)
         .fail(function(err) {
             expect(err).toBeUndefined();
@@ -330,51 +357,90 @@ describe('platform end-to-end with --save', function () {
             // And now add another platform with --save.
             return cordova.raw.platform('add', [helpers.testPlatform], {'save':true});
         }).then(function() {
-            // Check the platform add was successful, reload, skipping cache
+            // Check the platform add was successful, reload, skipping cache.
             delete require.cache[require.resolve(pkgJsonPath)];
             pkgJson = require(pkgJsonPath);
-            // Beware empty/missing cordova object
-            var pckJsonCordova = pkgJson.cordova || {platforms:[]};
+            // Beware empty/missing cordova object.
+            var pkgJsonCordova = pkgJson.cordova || {platforms:[]};
             // Check that only the platform added with --save was added to package.json.
-            expect(pckJsonCordova.platforms.indexOf(helpers.testPlatform)).toBeGreaterThan(-1);
-            expect(pckJsonCordova.platforms.indexOf(platformNotToAdd)).toEqual(-1);
+            expect(pkgJsonCordova.platforms.indexOf(helpers.testPlatform)).toBeGreaterThan(-1);
+            expect(pkgJsonCordova.platforms.indexOf(platformNotToAdd)).toEqual(-1);
         })
         .fail(function(err) {
             expect(err).toBeUndefined();
         })
         .fin(done);
-    });
+    }, 30000);
     it('Test#010 : two platforms are added and removed correctly with --save --fetch', function(done)
{
         var pkgJsonPath = path.join(process.cwd(),'package.json');
         expect(pkgJsonPath).toExist();
         var pkgJson;
+        // Delete any previous caches of require(package.json).
+        delete require.cache[require.resolve(pkgJsonPath)];
+        pkgJson = require(pkgJsonPath);
+        var cwd = process.cwd();
+        var configXmlPath = path.join(cwd, 'config.xml');
+        var cfg = new ConfigParser(configXmlPath);
+        var engines = cfg.getEngines();
+        var engNames = engines.map(function(elem) {
+            return elem.name;
+        });
+        var configEngArray = engNames.slice();
 
+        // No platforms in config or pkg.json yet.
+        expect(pkgJson.cordova).toBeUndefined();
+        expect(configEngArray.length === 0);
         // Check there are no platforms yet.
         emptyPlatformList().then(function() {
-            // Add the testing platform with --save.
-            return cordova.raw.platform('add', ['android', 'ios'], {'save':true, 'fetch':true});
+            // Add the testing platform with --save and add specific version to android platform.
+            return cordova.raw.platform('add', ['android@6.1.0', 'ios'], {'save':true, 'fetch':true});
         }).then(function() {
-            // Check the platform add was successful.
+            // Delete any previous caches of require(package.json).
             delete require.cache[require.resolve(pkgJsonPath)];
             pkgJson = require(pkgJsonPath);
+            // Check the platform add was successful in platforms list and in dependencies.
             expect(pkgJson.cordova.platforms).toBeDefined();
             expect(pkgJson.cordova.platforms.indexOf('android')).toEqual(0);
             expect(pkgJson.cordova.platforms.indexOf('ios')).toEqual(1);
+            expect(pkgJson.dependencies).toBeDefined();
             expect(pkgJson.dependencies['cordova-android']).toBeDefined();
             expect(pkgJson.dependencies['cordova-ios']).toBeDefined();
+            // Android platform should have specific version from add.
+            expect(pkgJson.dependencies['cordova-android']).toEqual('^6.1.0');
+
+            var cfg3 = new ConfigParser(configXmlPath);
+            engines = cfg3.getEngines();
+            engNames = engines.map(function(elem) {
+                return elem.name;
+            });
+            configEngArray = engNames.slice();
+            // Check that android and ios were added to config.xml with the correct spec.
+            expect(configEngArray.length === 2);
+            expect(engines).toEqual([ { name: 'android', spec: '~6.1.0' }, { name: 'ios',
spec: '~4.3.0' } ]);
+
         }).then(fullPlatformList) // Platform should still be in platform ls.
         .then(function() {
             // And now remove it with --save.
             return cordova.raw.platform('rm', ['android', 'ios'], {'save':true, 'fetch':true});
         }).then(function() {
-            // Delete any previous caches of require(package.json)
+            // Delete any previous caches of require(package.json).
             delete require.cache[require.resolve(pkgJsonPath)];
             pkgJson = require(pkgJsonPath);
-            // Checking that the platform removed is in not in the platforms key
+            // Checking that the platform removed is in not in the platforms key.
             expect(pkgJson.cordova.platforms.indexOf('android')).toEqual(-1);
             expect(pkgJson.cordova.platforms.indexOf('ios')).toEqual(-1);
+            // Dependencies are removed.
             expect(pkgJson.dependencies['cordova-android']).toBeUndefined();
             expect(pkgJson.dependencies['cordova-ios']).toBeUndefined();
+            // Platforms are removed from config.xml.
+            var cfg4 = new ConfigParser(configXmlPath);
+            engines = cfg4.getEngines();
+            engNames = engines.map(function(elem) {
+                return elem.name;
+            });
+            configEngArray = engNames.slice();
+            // Platforms are removed from config.xml.
+            expect(configEngArray.length === 0);
         }).then(emptyPlatformList) // platform ls should be empty too.
         .fail(function(err) {
             expect(err).toBeUndefined();

http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/ed9ba79a/cordova-lib/src/cordova/platform.js
----------------------------------------------------------------------
diff --git a/cordova-lib/src/cordova/platform.js b/cordova-lib/src/cordova/platform.js
index 2b82e8a..e395835 100644
--- a/cordova-lib/src/cordova/platform.js
+++ b/cordova-lib/src/cordova/platform.js
@@ -125,7 +125,9 @@ function addHelper(cmd, hooksRunner, projectRoot, targets, opts) {
                 }
                 return downloadPlatform(projectRoot, platform, spec, opts);
             }).then(function(platDetails) {
-                delete require.cache[require.resolve(path.join(projectRoot, 'package.json'))];
+                if(fs.existsSync(path.join(projectRoot, 'package.json'))) {
+                    delete require.cache[require.resolve(path.join(projectRoot, 'package.json'))];
+                }
                 platform = platDetails.platform;
                 var platformPath = path.join(projectRoot, 'platforms', platform);
                 var platformAlreadyAdded = fs.existsSync(platformPath);
@@ -168,7 +170,6 @@ function addHelper(cmd, hooksRunner, projectRoot, targets, opts) {
                 }
 
                 events.emit('log', (cmd === 'add' ? 'Adding ' : 'Updating ') + platform +
' project...');
-
                 var PlatformApi;
                 try {
                     // Try to get PlatformApi class from platform
@@ -190,12 +191,10 @@ function addHelper(cmd, hooksRunner, projectRoot, targets, opts) {
                         PlatformApi = require('../platforms/PlatformApiPoly');
                     }
                 }
-
                 var destination = path.resolve(projectRoot, 'platforms', platform);
                 var promise = cmd === 'add' ?
                     PlatformApi.createPlatform.bind(null, destination, cfg, options, events)
:
                     PlatformApi.updatePlatform.bind(null, destination, options, events);
-
                 return promise()
                 .then(function () {
                     if (!opts.restoring) {
@@ -250,13 +249,13 @@ function addHelper(cmd, hooksRunner, projectRoot, targets, opts) {
             var pkgJson;
             var pkgJsonPath = path.join(projectRoot, 'package.json');
             var modifiedPkgJson = false;
-
             if(fs.existsSync(pkgJsonPath)) {
                 delete require.cache[require.resolve(pkgJsonPath)]; 
                 pkgJson = require(pkgJsonPath);
             } else {
-                // Create package.json in cordova@7
+                // TODO: Create package.json in cordova@7
             }
+
             if (pkgJson === undefined) {
                 return;
             }

http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/ed9ba79a/cordova-lib/src/cordova/restore-util.js
----------------------------------------------------------------------
diff --git a/cordova-lib/src/cordova/restore-util.js b/cordova-lib/src/cordova/restore-util.js
index bfb3830..044941c 100644
--- a/cordova-lib/src/cordova/restore-util.js
+++ b/cordova-lib/src/cordova/restore-util.js
@@ -17,19 +17,20 @@
     under the License.
 */
 
-var cordova_util = require('./util'),
-    ConfigParser = require('cordova-common').ConfigParser,
-    path         = require('path'),
-    Q            = require('q'),
-    fs           = require('fs'),
-    events       = require('cordova-common').events,
-    cordova      = require('./cordova'),
-    semver      = require('semver'),
-    promiseutil = require('../util/promise-util');
+var cordova_util    = require('./util'),
+    ConfigParser    = require('cordova-common').ConfigParser,
+    path            = require('path'),
+    Q               = require('q'),
+    fs              = require('fs'),
+    events          = require('cordova-common').events,
+    cordova         = require('./cordova'),
+    semver          = require('semver'),
+    platformsList   = require('../platforms/platforms.js'),
+    promiseutil     = require('../util/promise-util');
 
 exports.installPluginsFromConfigXML = installPluginsFromConfigXML;
 exports.installPlatformsFromConfigXML = installPlatformsFromConfigXML;
-
+// Install platforms looking at config.xml and package.json (if there is one).
 function installPlatformsFromConfigXML(platforms, opts) {
     events.emit('verbose', 'Checking config.xml and package.json for saved platforms that
haven\'t been added to the project');
 
@@ -41,16 +42,13 @@ function installPlatformsFromConfigXML(platforms, opts) {
     var pkgJson;
     var pkgJsonPlatforms;
     var comboArray = []; 
-    var targets;
     var configPlatforms = [];
-    var installAllPlatforms;
-    var platformPath;
-    var platformAlreadyAdded;
-    var t;
     var modifiedPkgJson = false;
     var modifiedConfigXML = false;
-    var configObject;
+    var mergedPlatformSpecs = {};
+    var key;
 
+    // Check if path exists and require pkgJsonPath.
     if(fs.existsSync(pkgJsonPath)) {
         pkgJson = require(pkgJsonPath);
     }
@@ -61,23 +59,26 @@ function installPlatformsFromConfigXML(platforms, opts) {
     if(cfg !== undefined) {
 
         if (pkgJsonPlatforms !== undefined) {
-            // Combining arrays and checking duplicates
+            // Combining arrays and checking duplicates.
             comboArray = pkgJsonPlatforms.slice();
         }
 
-        engines = cfg.getEngines(projectHome)
+        engines = cfg.getEngines(projectHome);
         configPlatforms = engines.map(function(Engine) {
-            configPlatName = Engine.name;
+            var configPlatName = Engine.name;
+            // Add specs from config into mergedPlatformSpecs.
+            if(mergedPlatformSpecs[configPlatName] === undefined && Engine.spec)
{
+                mergedPlatformSpecs[configPlatName] = Engine.spec;
+            }
             return configPlatName;
         });
-        
         configPlatforms.forEach(function(item) {
             if(comboArray.indexOf(item) < 0 ) {
                 comboArray.push(item);
             }
         });
-        //comboArray should have all platforms from config.xml & package.json
-        //remove dupes in comboArray & sort
+        // ComboArray should have all platforms from config.xml & package.json.
+        // Remove duplicates in comboArray & sort.
         var uniq = comboArray.reduce(function(a,b) {
             if (a.indexOf(b) < 0 ) a.push(b);
                 return a;
@@ -85,14 +86,13 @@ function installPlatformsFromConfigXML(platforms, opts) {
         comboArray = uniq;
         comboArray = comboArray.sort();
 
-        // No platforms to restore from either config.xml or package.json
+        // No platforms to restore from either config.xml or package.json.
         if (comboArray.length <= 0) {
            return Q('No platforms found in config.xml or package.json. Nothing to restore');
         }
 
-        //if no package.json, don't bother
+        // If no package.json, don't continue.
         if (pkgJson !== undefined) { 
-
             // If config.xml & pkgJson exist and the cordova key is undefined, create
a cordova key.
             if (pkgJson.cordova === undefined) {
                 pkgJson.cordova = {};
@@ -105,33 +105,71 @@ function installPlatformsFromConfigXML(platforms, opts) {
             if (comboArray.toString() === pkgJson.cordova.platforms.sort().toString()) {
                 events.emit('verbose', 'Config.xml and package.json platforms are the same.
No pkg.json modification.');
             } else {
-                // modify pkg.json to include the elements
-                // from the comboArray array so that the arrays are identical
+                // Modify pkg.json to include the elements.
+                // From the comboArray array so that the arrays are identical.
                 events.emit('verbose', 'Config.xml and package.json platforms are different.
Updating package.json with most current list of platforms.');
                 modifiedPkgJson = true;
             }
 
-            // If comboArray has the same platforms as config.xml, no modification to config.xml.
-            if(comboArray.length === configPlatforms.length && comboArray.toString()
=== configPlatforms.sort().toString()) {
-                events.emit('verbose', 'Package.json and config.xml are the same. No config.xml
modification.');
-            } else {
-                events.emit('verbose', 'Package.json and config.xml platforms are different.
Updating config.xml with most current list of platforms.');
-                comboArray.forEach(function(item) {
-                    if(configPlatforms.indexOf(item) < 0 ) {
-                        cfg.addEngine(item);
-                        modifiedConfigXML = true;
+            events.emit('verbose', 'Package.json and config.xml platforms are different.
Updating config.xml with most current list of platforms.');
+            comboArray.forEach(function(item) {
+                var prefixItem = ('cordova-'+item);
+                // Modify package.json if any of these cases are true:
+                if((pkgJson.dependencies === undefined && Object.keys(mergedPlatformSpecs).length)||
+                    (pkgJson.dependencies[item] === undefined && mergedPlatformSpecs[item])
||
+                    (pkgJson.dependencies[prefixItem] === undefined && mergedPlatformSpecs[prefixItem]))
{
+                    modifiedPkgJson = true;
+                }
+
+                // Get the cordova- prefixed spec from package.json and add it to mergedPluginSpecs.
+                if (pkgJson.dependencies && pkgJson.dependencies[prefixItem]) {
+                    if(mergedPlatformSpecs[prefixItem] != pkgJson.dependencies[prefixItem])
{
+                        modifiedPkgJson = true;
                     }
-                });
-            }
+                    mergedPlatformSpecs[item] = pkgJson.dependencies[prefixItem];
+                }
+
+                // Get the spec from package.json and add it to mergedPluginSpecs.
+                if (pkgJson.dependencies && pkgJson.dependencies[item] &&
pkgJson.dependencies[prefixItem] === undefined) {
+                    if(mergedPlatformSpecs[item] != pkgJson.dependencies[item]) {
+                        modifiedPkgJson = true;
+                    }
+                    mergedPlatformSpecs[item] = pkgJson.dependencies[item];
+                }
+
+                // First remove the engine and then add missing engine and elements to config.xml.
+                // Remove to avoid duplicate engines.
+                if(mergedPlatformSpecs[item]) {
+                    cfg.removeEngine(item);
+                    cfg.addEngine(item, mergedPlatformSpecs[item]);
+                    modifiedConfigXML = true;
+                // If there is no spec, just remove and add the engine.
+                } else if (configPlatforms.indexOf(item) < 0) {
+                    cfg.removeEngine(item);
+                    cfg.addEngine(item);
+                    modifiedConfigXML = true;
+                } 
+            });
         }
+
         // Write and update pkg.json if it has been modified.
         if (modifiedPkgJson === true) {
             pkgJson.cordova.platforms = comboArray;
-            //pkgJson.cordova.dependencies[configPlatName] =configPlatSpec;
+            if(pkgJson.dependencies === undefined) {
+                pkgJson.dependencies = {};
+            }
+            // Check if key is part of cordova alias list.
+            // Add prefix if it is.
+            for (key in mergedPlatformSpecs) {
+                var prefixKey = key;
+                if (key in platformsList) {
+                    prefixKey = 'cordova-' + key;
+                }
+                pkgJson.dependencies[prefixKey] = mergedPlatformSpecs[key];
+            }
             fs.writeFileSync(pkgJsonPath, JSON.stringify(pkgJson, null, 4), 'utf8');
         }
         if (modifiedConfigXML === true) {
-            configPlatforms = comboArray;
             cfg.write();
         }
         if (!comboArray || !comboArray.length) {
@@ -145,9 +183,19 @@ function installPlatformsFromConfigXML(platforms, opts) {
     // Otherwise, we get a bug where the following line: https://github.com/apache/cordova-lib/blob/0b0dee5e403c2c6d4e7262b963babb9f532e7d27/cordova-lib/src/util/npm-helper.js#L39
     // gets executed simultaneously by each platform and leads to an exception being thrown
     return promiseutil.Q_chainmap_graceful(comboArray, function(target) {
+        var cwd = process.cwd();
+        var platformsFolderPath = path.join(cwd,'platforms');
+        var platformsInstalled = path.join(platformsFolderPath, target);
         if (target) {
-            events.emit('log', 'Discovered platform \"' + target + '\" in config.xml or package.json.
Adding it to the project');
-            return cordova.raw.platform('add', target, opts);
+            // Add the spec to the target
+            if(mergedPlatformSpecs[target]) {
+                target = target + '@' + mergedPlatformSpecs[target];
+            }
+            // If the platform is already installed, no need to re-install it.
+            if (!fs.existsSync(platformsInstalled)) {
+                events.emit('log', 'Discovered platform \"' + target + '\" in config.xml
or package.json. Adding it to the project');
+                return cordova.raw.platform('add', target, opts);
+            }
         }
         return Q();
     }, function(err) {
@@ -155,10 +203,10 @@ function installPlatformsFromConfigXML(platforms, opts) {
     });
 }
 
-//returns a Promise
+// Returns a promise.
 function installPluginsFromConfigXML(args) {
     events.emit('verbose', 'Checking config.xml for saved plugins that haven\'t been added
to the project');
-    //Install plugins that are listed on config.xml
+    //Install plugins that are listed in config.xml.
     var projectRoot = cordova_util.cdProjectRoot();
     var configPath = cordova_util.projectConfig(projectRoot);
     var cfg = new ConfigParser(configPath);
@@ -167,29 +215,30 @@ function installPluginsFromConfigXML(args) {
     var pkgJson;
     var modifiedPkgJson = false;
     var modifiedConfigXML = false;
-    //TODO: rename to comboObject?
-    var mergedPluginDataObj;
+    var comboObject;
+    var mergedPluginSpecs = {};
     var comboPluginIdArray;
     var configPlugin;
     var configPluginVariables;
     var pkgJsonPluginVariables;
+    var key;
 
-    //Check if path exists and require pkgJsonPath
+    // Check if path exists and require pkgJsonPath.
     if(fs.existsSync(pkgJsonPath)) {
         pkgJson = require(pkgJsonPath);
     }
 
     if(pkgJson !== undefined && pkgJson.cordova !== undefined && pkgJson.cordova.plugins
!== undefined) {
         comboPluginIdArray = Object.keys(pkgJson.cordova.plugins);
-        // Create a merged plugin data array (mergedPluginDataObj)
-        // and add all of the package.json plugins to mergedPluginDataObj
-        mergedPluginDataObj = pkgJson.cordova.plugins;
+        // Create a merged plugin data array (comboObject)
+        // and add all of the package.json plugins to comboObject.
+        comboObject = pkgJson.cordova.plugins;
     } else {
-        mergedPluginDataObj = {};
+        comboObject = {};
         comboPluginIdArray = [];
     }
 
-    // Get all config.xml plugin ids
+    // Get all config.xml plugin ids (names).
     var pluginIdConfig = cfg.getPluginIdList();
     if(pluginIdConfig === undefined) {
         pluginIdConfig = [];
@@ -203,63 +252,86 @@ function installPluginsFromConfigXML(args) {
             pkgJson.cordova.plugins = {};
         }
 
-        // Check to see which plugins are initially the same in pkg.json and config.xml
-        // add missing plugin variables in package.json from config.xml
+        // Check to see which plugins are initially the same in pkg.json and config.xml.
+        // Add missing plugin variables in package.json from config.xml.
         comboPluginIdArray.forEach(function(item) {
             if(pluginIdConfig.includes(item)) {
                 configPlugin = cfg.getPlugin(item);
                 configPluginVariables = configPlugin.variables;
-                pkgJsonPluginVariables = mergedPluginDataObj[item];
+                pkgJsonPluginVariables = comboObject[item];
                 for(var key in configPluginVariables) {
-                    // Handle conflicts, package.json wins
-                    // only add the variable to package.json if it doesn't already exist
+                    // Handle conflicts, package.json wins.
+                    // Only add the variable to package.json if it doesn't already exist.
                     if(pkgJsonPluginVariables[key] === undefined) {
                         pkgJsonPluginVariables[key] = configPluginVariables[key];
-                        mergedPluginDataObj[item][key] = configPluginVariables[key];
+                        comboObject[item][key] = configPluginVariables[key];
                         modifiedPkgJson = true;
                     }
                 }
             }
-        })
+            // Get the spec from package.json and add it to mergedPluginSpecs.
+            if (pkgJson.dependencies && pkgJson.dependencies[item]) {
+                mergedPluginSpecs[item] = pkgJson.dependencies[item];
+            }
+        });
 
-        // Check to see if pkg.json plugin(id) and config plugin(id) match
+        // Check to see if pkg.json plugin(id) and config plugin(id) match.
         if(comboPluginIdArray.sort().toString() !== pluginIdConfig.sort().toString()) {
             // If there is a config plugin that does NOT already exist in
-            // mergedPluginDataArray, add it and its variables
+            // mergedPluginDataArray, add it and its variables.
             pluginIdConfig.forEach(function(item) {
                 if(comboPluginIdArray.indexOf(item) < 0) {
                     comboPluginIdArray.push(item);
                     var configXMLPlugin = cfg.getPlugin(item);
-                    mergedPluginDataObj[item] = configXMLPlugin.variables;
+                    comboObject[item] = configXMLPlugin.variables;
                     modifiedPkgJson = true;
                 }
             });
         }
 
+        // Add specs from config.xml to mergedPluginSpecs.
+        pluginIdConfig.forEach(function(item) {
+            var configXMLPlugin = cfg.getPlugin(item);
+            if(mergedPluginSpecs[item] === undefined && configXMLPlugin.spec) {
+                mergedPluginSpecs[item] = configXMLPlugin.spec;
+                modifiedPkgJson = true;
+            }
+        });
+        // If pkg.json plugins have been modified, write to it.
         if (modifiedPkgJson === true) {
-            pkgJson.cordova.plugins = mergedPluginDataObj;
+            pkgJson.cordova.plugins = comboObject;
+            if(pkgJson.dependencies === undefined) {
+                pkgJson.dependencies = {};
+            }
+            for(key in mergedPluginSpecs) {
+                pkgJson.dependencies[key] = mergedPluginSpecs[key];
+            }
             fs.writeFileSync(pkgJsonPath, JSON.stringify(pkgJson, null, 4), 'utf8');
         }
     }
-    
-    // Write config.xml
+    // Write config.xml (only if plugins exist in package.json).
     comboPluginIdArray.forEach(function(plugID) {
-        pluginIdConfig.push(plugID);
+        if(pluginIdConfig.indexOf(plugID) < 0) {
+            pluginIdConfig.push(plugID);
+        }
         cfg.removePlugin(plugID);
-        //TODO: spec needs to be added to addPlugin
-        cfg.addPlugin({name:plugID}, mergedPluginDataObj[plugID]); 
-        modifiedConfigXML = true;
+        if (mergedPluginSpecs[plugID]) {
+            cfg.addPlugin({name:plugID, spec: mergedPluginSpecs[plugID]}, comboObject[plugID]);

+            modifiedConfigXML = true;
+        // If no spec, just add the plugin.
+        } else {
+            cfg.addPlugin({name:plugID}, comboObject[plugID]); 
+            modifiedConfigXML = true;
+        }
     });
 
     if (modifiedConfigXML === true) {
         cfg.write();    
     }
     
-
     // Intermediate variable to store current installing plugin name
     // to be able to create informative warning on plugin failure
     var pluginName;
-
     // CB-9560 : Run `plugin add` serially, one plugin after another
     // We need to wait for the plugin and its dependencies to be installed
     // before installing the next root plugin otherwise we can have common
@@ -280,8 +352,11 @@ function installPluginsFromConfigXML(args) {
 
         // CB-10761 If plugin spec is not specified, use plugin name
         var installFrom = pluginSpec || pluginName;
-        if (pluginSpec && semver.validRange(pluginSpec, true))
+        if (pluginSpec && semver.validRange(pluginSpec, true)) {
             installFrom = pluginName + '@' + pluginSpec;
+        } else if(args.fetch && pluginSpec) {
+            installFrom = pluginName + '@' + pluginSpec;
+        }
 
         // Add feature preferences as CLI variables if have any
         var options = {


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


Mime
View raw message