You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by fi...@apache.org on 2017/03/09 20:39:10 UTC
[07/50] [abbrv] cordova-lib git commit: CB-12001: Added support for
platform/plugin & spec restore to sync config.xml and package.json
http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/7df35cb0/cordova-lib/spec-cordova/pkgJson-restore.spec.js
----------------------------------------------------------------------
diff --git a/cordova-lib/spec-cordova/pkgJson-restore.spec.js b/cordova-lib/spec-cordova/pkgJson-restore.spec.js
index 1a1966d..e4018e3 100644
--- a/cordova-lib/spec-cordova/pkgJson-restore.spec.js
+++ b/cordova-lib/spec-cordova/pkgJson-restore.spec.js
@@ -20,30 +20,39 @@ var helpers = require('./helpers'),
path = require('path'),
fs = require('fs'),
shell = require('shelljs'),
- superspawn = require('cordova-common').superspawn,
- Q = require('q'),
events = require('cordova-common').events,
cordova = require('../src/cordova/cordova'),
rewire = require('rewire'),
prepare = require('../src/cordova/prepare'),
platforms = require('../src/platforms/platforms'),
+ cordova_util = require('util'),
+ ConfigParser = require('cordova-common').ConfigParser,
platform = rewire('../src/cordova/platform.js');
var projectRoot = 'C:\\Projects\\cordova-projects\\move-tracker';
var pluginsDir = path.join(__dirname, 'fixtures', 'plugins');
-// Testing will check if "cordova prepare" is restoring platforms as expected
+/** Testing will check if "cordova prepare" is restoring platforms as expected.
+* Uses different basePkgJson files depending on testing expecations of what should
+* initially be in pkg.json and/or config.xml.
+*/
+
+var req = function(someModule) {
+ delete require.cache[require.resolve(someModule)];
+ return require(someModule);
+}
+
describe('platform end-to-end with --save', function () {
var tmpDir = helpers.tmpDir('platform_test_pkgjson');
- var project = path.join(tmpDir, 'project'); // call it projectPath
+ var project = path.join(tmpDir, 'project');
var results;
beforeEach(function() {
shell.rm('-rf', tmpDir);
- // cp then mv because we need to copy everything, but that means it'll copy the whole directory.
+ // Copy then move because we need to copy everything, but that means it will copy the whole directory.
// Using /* doesn't work because of hidden files.
- shell.cp('-R', path.join(__dirname, 'fixtures', 'basePkgJson2'), tmpDir);
- shell.mv(path.join(tmpDir, 'basePkgJson2'), project);
+ shell.cp('-R', path.join(__dirname, 'fixtures', 'basePkgJson'), tmpDir);
+ shell.mv(path.join(tmpDir, 'basePkgJson'), project);
process.chdir(project);
events.on('results', function(res) { results = res; });
});
@@ -51,7 +60,6 @@ describe('platform end-to-end with --save', function () {
afterEach(function() {
var cwd = process.cwd();
delete require.cache[require.resolve(path.join(process.cwd(),'package.json'))];
- //delete require.cache[require.resolve(path.join(cwd,'platforms/platforms.json'))];
process.chdir(path.join(__dirname, '..')); // Needed to rm the dir on Windows.
shell.rm('-rf', tmpDir);
});
@@ -64,6 +72,7 @@ describe('platform end-to-end with --save', function () {
expect(installed[1].indexOf(helpers.testPlatform)).toBe(-1);
});
}
+
function fullPlatformList() {
return cordova.raw.platform('list').then(function() {
var installed = results.match(/Installed platforms:\n (.*)/);
@@ -71,40 +80,42 @@ describe('platform end-to-end with --save', function () {
expect(installed[1].indexOf(helpers.testPlatform)).toBeGreaterThan(-1);
});
}
-
+ /** Test#001 will add a platform to package.json with the 'save' flag.
+ * It will remove it from pkg.json without the save flag.
+ * After running cordova prepare, that platform should be restored in the
+ * installed platform list in platforms.json.
+ */
it('Test#001 : should restore platform that has been removed from project', function(done) {
var cwd = process.cwd();
var pkgJsonPath = path.join(cwd,'package.json');
+ delete require.cache[require.resolve(pkgJsonPath)];
var pkgJson = require(pkgJsonPath);
- var platformsFolderPath;
+ var platformsFolderPath = path.join(cwd,'platforms/platforms.json');
+ var platformsJson;
emptyPlatformList().then(function() {
- return cordova.raw.platform('rm', [helpers.testPlatform], {'save':true});
- }).then(function() {
// Add the testing platform with --save.
return cordova.raw.platform('add', [helpers.testPlatform], {'save':true});
}).then(function() {
- // Check the platform add was successful.
- platformsFolderPath = (path.join(cwd,'platforms/platforms.json'));
- var platformsJson = require(platformsFolderPath);
-
- expect(pkgJson.cordova.platforms).not.toBeUndefined();
+ // Require platformsFolderPath
+ delete require.cache[require.resolve(platformsFolderPath)];
+ platformsJson = require(platformsFolderPath);
+ // Check the platform add was successful in package.json.
+ expect(pkgJson.cordova.platforms).toBeDefined();
expect(pkgJson.cordova.platforms.indexOf(helpers.testPlatform)).toBeGreaterThan(-1);
- expect(platformsJson).toBeDefined();
// Expect that "helpers.testPlatform" in the installed platform list in platforms.json
+ expect(platformsJson).toBeDefined();
expect(platformsJson[helpers.testPlatform]).toBeDefined();
-
}).then(fullPlatformList) // Platform should still be in platform ls.
- .then(function() {
- // And now remove it without --save.
+ .then(function() {
+ // And now remove helpers.testPlatform 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) and (platforms.json)
delete require.cache[require.resolve(platformsFolderPath)];
platformsJson = require(platformsFolderPath);
delete require.cache[require.resolve(pkgJsonPath)];
pkgJson = require(pkgJsonPath);
-
// Check that the platform removed without --save is still in platforms key.
expect(pkgJson.cordova.platforms.indexOf(helpers.testPlatform)).toBeGreaterThan(-1);
// Check that the platform was removed from the platforms.json
@@ -112,48 +123,59 @@ describe('platform end-to-end with --save', function () {
}).then(function() {
// Run cordova prepare
return cordova.raw.prepare();
- })
- .then(function() {
+ }).then(function() {
+ // Delete any previous caches of platforms.json
delete require.cache[require.resolve(platformsFolderPath)];
platformsJson = require(platformsFolderPath);
- // Expect "helpers.testPlatform" to be in the installed platforms list
+ // Expect "helpers.testPlatform" to be in the installed platforms list in platforms.json.
expect(platformsJson[helpers.testPlatform]).toBeDefined();
- })
- .fail(function(err) {
+ }).fail(function(err) {
expect(err).toBeUndefined();
}).fin(done);
+ // Cordova prepare needs extra wait time to complete.
},30000);
+ /** Test#002 will add two platforms to package.json with the 'save' flag.
+ * It will remove one platform from pkg.json without the 'save' flag and remove
+ * the other platform with the 'save' flag. After running cordova prepare,
+ * the platform removed with the 'save' flag should NOT be restored in platforms.json.
+ */
it('Test#002 : should NOT restore platform that was removed with --save', function(done) {
var cwd = process.cwd();
var pkgJsonPath = path.join(cwd,'package.json');
- var pkgJson = require(pkgJsonPath);
- var platformsFolderPath;
+ var pkgJson;
+ var platformsFolderPath = path.join(cwd,'platforms/platforms.json');
+ var platformsJson;
var secondPlatformAdded = 'ios';
+
emptyPlatformList().then(function() {
- return cordova.raw.platform('rm', [helpers.testPlatform], {'save': true});
- }).then(function() {
// Add the testing platform with --save.
return cordova.raw.platform('add', [helpers.testPlatform], {'save':true});
}).then(function() {
+ // Add the 'ios' platform with --save
return cordova.raw.platform('add',secondPlatformAdded, {'save':true});
}).then(function() {
- // Check the platform add was successful.
- platformsFolderPath = (path.join(cwd,'platforms/platforms.json'));
- var platformsJson = require(platformsFolderPath);
- expect(pkgJson.cordova.platforms).not.toBeUndefined();
+ // Delete any previous caches of require(package.json) and (platforms.json)
+ delete require.cache[require.resolve(pkgJsonPath)];
+ pkgJson = require(pkgJsonPath);
+ delete require.cache[require.resolve(platformsFolderPath)];
+ platformsJson = require(platformsFolderPath);
+
+ // Check the platform add of both platforms was successful.
+ expect(pkgJson.cordova.platforms).toBeDefined();
expect(pkgJson.cordova.platforms.indexOf(helpers.testPlatform)).toEqual(0);
expect(pkgJson.cordova.platforms.indexOf(secondPlatformAdded)).toEqual(1);
- expect(platformsJson).toBeDefined();
// Expect that "helpers.testPlatform" in the installed platform list in platforms.json
+ expect(platformsJson).toBeDefined();
expect(platformsJson[helpers.testPlatform]).toBeDefined();
-
+ expect(platformsJson[secondPlatformAdded]).toBeDefined();
}).then(fullPlatformList) // Platform should still be in platform ls.
.then(function() {
- // And now remove it without --save.
+ // Remove helpers.testPlatform with --save.
return cordova.raw.platform('rm', [helpers.testPlatform], {'save':true});
}).then(function() {
+ // Remove secondPlatformAdded without --save
return cordova.raw.platform('rm', secondPlatformAdded);
}).then(function() {
// Delete any previous caches of require(package.json) and (platformsJson)
@@ -163,91 +185,433 @@ describe('platform end-to-end with --save', function () {
pkgJson = require(pkgJsonPath);
// Check that the platform removed without --save is still in platforms key.
expect(pkgJson.cordova.platforms.indexOf(secondPlatformAdded)).toEqual(0);
- // Check that the platform was removed from the platforms.json
+ expect(pkgJson.cordova.platforms.indexOf(helpers.testPlatform)).toEqual(-1);
+ // Check that the helpers.testPlatform (removed with --save) was removed from the platforms.json
expect(platformsJson[helpers.testPlatform]).toBeUndefined();
+ expect(platformsJson[secondPlatformAdded]).toBeUndefined();
}).then(function() {
// Run cordova prepare
return cordova.raw.prepare();
- })
- .then(function() {
+ }).then(function() {
// Delete any previous caches of platformsJson
delete require.cache[require.resolve(platformsFolderPath)];
platformsJson = require(platformsFolderPath);
- // Expect "helpers.testPlatform" to be in the installed platforms list
+ // Expect "helpers.testPlatform" to be in the installed platforms list in platforms.json
expect(platformsJson[helpers.testPlatform]).toBeUndefined();
+ // Expect 'ios' not to be in platforms.json and has not been restored.
expect(platformsJson[secondPlatformAdded]).toBeDefined();
- })
- .fail(function(err) {
+ }).fail(function(err) {
expect(err).toBeUndefined();
}).fin(done);
+ // Cordova prepare needs extra wait time to complete.
},30000);
-
+ /** Test#003 will add two platforms to package.json - one with the 'save' flag and one
+ * without the 'save' flag. It will remove both platforms without a 'save' flag.
+ * After running cordova prepare, only the platform added with the 'save' flag is restored
+ * in platforms.json.
+ */
it('Test#003 : should NOT restore platform that was not saved and removed', function(done) {
var cwd = process.cwd();
var pkgJsonPath = path.join(cwd,'package.json');
- var pkgJson = require(pkgJsonPath);
- var platformsFolderPath;
+ var pkgJson;
+ var platformsFolderPath = path.join(cwd,'platforms/platforms.json');
var secondPlatformAdded = 'ios';
+ var platformsJson;
emptyPlatformList().then(function() {
- // Add the testing platform with --save.
- return cordova.raw.platform('rm', [helpers.testPlatform], {'save':true});
- }).then(function() {
+ // Add 'ios' platform to project without --save
return cordova.raw.platform('add', secondPlatformAdded);
}).then(function() {
+ // Add helpers.testPlatform to project with --save
return cordova.raw.platform('add', [helpers.testPlatform], {'save':true});
}).then(function() {
- // Check the platform add was successful.
+ // Delete any previous caches of require(package.json) and (platformsJson)
delete require.cache[require.resolve(pkgJsonPath)];
pkgJson = require(pkgJsonPath);
- platformsFolderPath = (path.join(cwd,'platforms/platforms.json'));
- var platformsJson = require(platformsFolderPath);
delete require.cache[require.resolve(platformsFolderPath)];
platformsJson = require(platformsFolderPath);
-
- expect(pkgJson.cordova.platforms).not.toBeUndefined();
- //expect(pkgJson.cordova.platforms.indexOf(helpers.testPlatform)).toEqual(-1);
+ // Check the platform add of both platforms was successful in package.json.
+ expect(pkgJson.cordova.platforms).toBeDefined();
expect(pkgJson.cordova.platforms.indexOf(helpers.testPlatform)).toBeGreaterThan(-1);
- // Expect that "helpers.testPlatform" in the installed platform list in platforms.json
+ expect(pkgJson.cordova.platforms.indexOf(secondPlatformAdded)).toEqual(-1);
+ // Expect both platforms to be installed platform list in platforms.json
expect(platformsJson[helpers.testPlatform]).toBeDefined();
-
- }).then(fullPlatformList) // Platform should still be in platform ls.
+ expect(platformsJson[secondPlatformAdded]).toBeDefined();
+ }).then(fullPlatformList) // Platforms should still be in platform ls.
.then(function() {
- // And now remove it without --save.
+ // Remove helpers.testPlatform without --save.
return cordova.raw.platform('rm', [helpers.testPlatform]);
}).then(function() {
+ // Remove secondPlatformAdded without --save.
return cordova.raw.platform('rm', secondPlatformAdded);
}).then(function() {
- // Delete any previous caches of require(package.json) and (platformsJson)
+ // Delete any previous caches of require(pkgJson) and (platformsJson)
delete require.cache[require.resolve(platformsFolderPath)];
platformsJson = require(platformsFolderPath);
delete require.cache[require.resolve(pkgJsonPath)];
pkgJson = require(pkgJsonPath);
- // Check that the platform removed without --save is still in platforms key.
+ // Check that the platform that was added with --save is still in package.json.
expect(pkgJson.cordova.platforms.indexOf(helpers.testPlatform)).toBeGreaterThan(-1);
- // Check that the platform was removed from the platforms.json
+ // Check that both platforms were removed from the platforms.json.
expect(platformsJson[secondPlatformAdded]).toBeUndefined();
expect(platformsJson[helpers.testPlatform]).toBeUndefined();
}).then(function() {
// Run cordova prepare
return cordova.raw.prepare();
- })
- .then(function() {
+ }).then(function() {
// Delete any previous caches of platformsJson
delete require.cache[require.resolve(platformsFolderPath)];
platformsJson = require(platformsFolderPath);
// Expect "helpers.testPlatform" to be in the installed platforms list
expect(platformsJson[helpers.testPlatform]).toBeDefined();
+ // Expect that 'ios' will not be in platforms.json and has not been restored.
expect(platformsJson[secondPlatformAdded]).toBeUndefined();
- })
- .fail(function(err) {
+ }).fail(function(err) {
expect(err).toBeUndefined();
}).fin(done);
+ // Cordova prepare needs extra wait time to complete.
},30000);
+
});
+// Use basePkgJson6 because pkg.json and config.xml contain only android
+describe('files should not be modified if their platforms are identical', function () {
+ var tmpDir = helpers.tmpDir('platform_test_pkgjson');
+ var project = path.join(tmpDir, 'project');
+ var results;
+ beforeEach(function() {
+ shell.rm('-rf', tmpDir);
+ // Copy then move because we need to copy everything, but that means it will copy the whole directory.
+ // Using /* doesn't work because of hidden files.
+ // Use basePkgJson6 because pkg.json and config.xml contain only android
+ shell.cp('-R', path.join(__dirname, 'fixtures', 'basePkgJson6'), tmpDir);
+ shell.mv(path.join(tmpDir, 'basePkgJson6'), project);
+ process.chdir(project);
+ events.on('results', function(res) { results = res; });
+ });
+
+ afterEach(function() {
+ var cwd = process.cwd();
+ 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);
+ });
+
+ // Factoring out some repeated checks.
+ function emptyPlatformList() {
+ return cordova.raw.platform('list').then(function() {
+ var installed = results.match(/Installed platforms:\n (.*)/);
+ expect(installed).toBeDefined();
+ expect(installed[1].indexOf(helpers.testPlatform)).toBe(-1);
+ });
+ }
+
+ function fullPlatformList() {
+ return cordova.raw.platform('list').then(function() {
+ var installed = results.match(/Installed platforms:\n (.*)/);
+ expect(installed).toBeDefined();
+ expect(installed[1].indexOf(helpers.testPlatform)).toBeGreaterThan(-1);
+ });
+ }
+
+ /** Test#004 will check the platform list in package.json and config.xml.
+ * When both files contain the same platforms and cordova prepare is run,
+ * neither file is modified.
+ */
+ it('Test#004 : if pkg.json and config.xml have the same platforms, do not modify either file', function(done) {
+ var cwd = process.cwd();
+ var configXmlPath = path.join(cwd, 'config.xml');
+ var cfg1 = new ConfigParser(configXmlPath);
+ var engines = cfg1.getEngines();
+ var pkgJsonPath = path.join(cwd,'package.json');
+ var pkgJson;
+ var platformsFolderPath;
+ var engNames = engines.map(function(elem) {
+ return elem.name;
+ });
+ var configEngArray = engNames.slice();
+ // Pkg.json and config.xml contain only android at this point (basePkgJson6)
+ emptyPlatformList().then(function() {
+ // Run cordova prepare
+ return cordova.raw.prepare();
+ }).then(function() {
+ var cfg2 = new ConfigParser(configXmlPath);
+ engines = cfg2.getEngines();
+ engNames = engines.map(function(elem) {
+ return elem.name;
+ });
+ configEngArray = engNames.slice();
+ delete require.cache[require.resolve(pkgJsonPath)];
+ pkgJson = require(pkgJsonPath);
+ // Expect android to be in both pkg.json and config.xml
+ expect(pkgJson.cordova.platforms.indexOf('android')).toBeGreaterThan(-1);
+ expect(configEngArray.indexOf('android')).toBeGreaterThan(-1);
+ // Expect pkg.json and config.xml to have only 1 element each
+ expect(configEngArray.length === 1);
+ expect(pkgJson.cordova.platforms.length === 1);
+ }).fail(function(err) {
+ expect(err).toBeUndefined();
+ }).fin(done);
+ // Cordova prepare needs extra wait time to complete.
+ },30000);
+});
+
+// Use a new basePkgJson5 as config.xml contains android/ios and pkg.json contains android
+describe('update pkg.json to include platforms in config.xml', function () {
+ var tmpDir = helpers.tmpDir('platform_test_pkgjson');
+ var project = path.join(tmpDir, 'project');
+ var results;
+
+ beforeEach(function() {
+ shell.rm('-rf', tmpDir);
+ // Copy then move because we need to copy everything, but that means it will copy the whole directory.
+ // Using /* doesn't work because of hidden files.
+ shell.cp('-R', path.join(__dirname, 'fixtures', 'basePkgJson5'), tmpDir);
+ shell.mv(path.join(tmpDir, 'basePkgJson5'), project);
+ process.chdir(project);
+ events.on('results', function(res) { results = res; });
+ });
+
+ afterEach(function() {
+ var cwd = process.cwd();
+ 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);
+ });
+
+ // Factoring out some repeated checks.
+ function emptyPlatformList() {
+ return cordova.raw.platform('list').then(function() {
+ var installed = results.match(/Installed platforms:\n (.*)/);
+ expect(installed).toBeDefined();
+ expect(installed[1].indexOf(helpers.testPlatform)).toBe(-1);
+ });
+ }
+
+ function fullPlatformList() {
+ return cordova.raw.platform('list').then(function() {
+ var installed = results.match(/Installed platforms:\n (.*)/);
+ expect(installed).toBeDefined();
+ expect(installed[1].indexOf(helpers.testPlatform)).toBeGreaterThan(-1);
+ });
+ }
+ /** Test#005 will check the platform list in package.json and config.xml.
+ * When config.xml has 'android and ios' and pkg.json only contains 'android', run cordova
+ * and pkg.json is updated to include 'ios'.
+ */
+ it('Test#005 : if config.xml has android & ios platforms and pkg.json has android, update pkg.json to also include ios', function(done) {
+ var cwd = process.cwd();
+ var configXmlPath = path.join(cwd, 'config.xml');
+ var cfg = new ConfigParser(configXmlPath);
+ var engines = cfg.getEngines();
+ var pkgJsonPath = path.join(cwd,'package.json');
+ delete require.cache[require.resolve(pkgJsonPath)];
+ var pkgJson = require(pkgJsonPath);
+ var platformsFolderPath;
+ var engNames = engines.map(function(elem) {
+ return elem.name;
+ });
+ var configEngArray = engNames.slice();
+
+ // Config.xml contains(android & ios) and pkg.json contains android (basePkgJson5)
+ expect(configEngArray.indexOf('ios')).toBeGreaterThan(-1);
+ expect(configEngArray.indexOf('android')).toBeGreaterThan(-1);
+ // pkg.json should not contain 'ios' platform before cordova prepare
+ expect(pkgJson.cordova.platforms.indexOf('ios')).toEqual(-1);
+ expect(pkgJson.cordova.platforms.indexOf('android')).toBeGreaterThan(-1);
+ emptyPlatformList().then(function() {
+ return cordova.raw.prepare();
+ }).then(function() {
+ delete require.cache[require.resolve(pkgJsonPath)];
+ pkgJson = require(pkgJsonPath);
+ // Expect 'ios' to be added to pkg.json
+ expect(pkgJson.cordova.platforms.indexOf('ios')).toBeGreaterThan(-1);
+ // Expect 'android' to still be there in pkg.json
+ expect(pkgJson.cordova.platforms.indexOf('android')).toBeGreaterThan(-1);
+ // Expect both pkg.json and config.xml to each have both platforms in their arrays
+ expect(configEngArray.length === 1);
+ expect(pkgJson.cordova.platforms.length === 1);
+ }).fail(function(err) {
+ expect(err).toBeUndefined();
+ }).fin(done);
+ // Cordova prepare needs extra wait time to complete.
+ },30000);
+});
+
+// Use basePkgJson3 as it has 'android' in config.xml
+describe('update empty package.json to match config.xml', function () {
+ var tmpDir = helpers.tmpDir('platform_test_pkgjson');
+ var project = path.join(tmpDir, 'project');
+ var results;
+
+ beforeEach(function() {
+ shell.rm('-rf', tmpDir);
+ // Copy then move because we need to copy everything, but that means it will copy the whole directory.
+ // Using /* doesn't work because of hidden files.
+ shell.cp('-R', path.join(__dirname, 'fixtures', 'basePkgJson3'), tmpDir);
+ shell.mv(path.join(tmpDir, 'basePkgJson3'), project);
+ process.chdir(project);
+ events.on('results', function(res) { results = res; });
+ });
+
+ afterEach(function() {
+ var cwd = process.cwd();
+ 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);
+ });
+
+ // Factoring out some repeated checks.
+ function emptyPlatformList() {
+ return cordova.raw.platform('list').then(function() {
+ var installed = results.match(/Installed platforms:\n (.*)/);
+ expect(installed).toBeDefined();
+ expect(installed[1].indexOf(helpers.testPlatform)).toBe(-1);
+ });
+ }
+
+ function fullPlatformList() {
+ return cordova.raw.platform('list').then(function() {
+ var installed = results.match(/Installed platforms:\n (.*)/);
+ expect(installed).toBeDefined();
+ expect(installed[1].indexOf(helpers.testPlatform)).toBeGreaterThan(-1);
+ });
+ }
+
+ /** Test#006 will check if pkg.json has a cordova key and platforms installed already.
+ * If it does not and config.xml has a platform(s) installed already, run cordova prepare
+ * and it will add a cordova key and the platform(s) from config.xml to package.json.
+ */
+ it('Test#006 : if pkg.json exists without cordova key, create one with same platforms in config.xml ', function(done) {
+ var cwd = process.cwd();
+ var configXmlPath = path.join(cwd, 'config.xml');
+ var pkgJsonPath = path.join(cwd,'package.json');
+ delete require.cache[require.resolve(pkgJsonPath)];
+ var cfg1 = new ConfigParser(configXmlPath);
+ var engines = cfg1.getEngines();
+ var pkgJson = require(pkgJsonPath);
+ var platformsFolderPath;
+ var engNames = engines.map(function(elem) {
+ return elem.name;
+ });
+ var configEngArray = engNames.slice();
+
+ // Expect that pkg.json exists without a cordova key
+ expect(pkgJson).toBeDefined();
+ expect(pkgJson.cordova).toBeUndefined();
+ // Expect that config.xml contains only android at this point
+ expect(configEngArray.indexOf('android')).toBeGreaterThan(-1);
+ expect(configEngArray.length === 1);
+ // Run cordova prepare
+ cordova.raw.prepare();
+ emptyPlatformList().then(function() {
+ var cfg2 = new ConfigParser(configXmlPath);
+ engines = cfg2.getEngines();
+ engNames = engines.map(function(elem) {
+ return elem.name;
+ });
+ configEngArray = engNames.slice();
+ // Delete any previous caches of require(package.json)
+ delete require.cache[require.resolve(pkgJsonPath)];
+ pkgJson = require(pkgJsonPath);
+ // Expect no change to config.xml
+ expect(configEngArray.indexOf('android')).toBeGreaterThan(-1);
+ // Expect cordova key and 'android' platform to be added to pkg.json
+ expect(pkgJson.cordova.platforms.indexOf('android')).toBeGreaterThan(-1);
+ // Expect both pkg.json and config.xml to each have (only) android in their arrays
+ expect(configEngArray.length === 0);
+ expect(pkgJson.cordova.platforms.length === 0);
+ }).fail(function(err) {
+ expect(err).toBeUndefined();
+ }).fin(done);
+ // Cordova prepare needs extra wait time to complete.
+ },30000);
+});
+
+// Use a new basePkgJson4 as pkg.json contains android/ios and config.xml contains android
+describe('update config.xml to include platforms in pkg.json', function () {
+ var tmpDir = helpers.tmpDir('platform_test_pkgjson');
+ var project = path.join(tmpDir, 'project');
+ var results;
+
+ beforeEach(function() {
+ shell.rm('-rf', tmpDir);
+ // Copy then move because we need to copy everything, but that means it will copy the whole directory.
+ // Using /* doesn't work because of hidden files.
+ shell.cp('-R', path.join(__dirname, 'fixtures', 'basePkgJson4'), tmpDir);
+ shell.mv(path.join(tmpDir, 'basePkgJson4'), project);
+ process.chdir(project);
+ events.on('results', function(res) { results = res; });
+ });
+
+ afterEach(function() {
+ var cwd = process.cwd();
+ 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);
+ });
+
+ // Factoring out some repeated checks.
+ function emptyPlatformList() {
+ return cordova.raw.platform('list').then(function() {
+ var installed = results.match(/Installed platforms:\n (.*)/);
+ expect(installed).toBeDefined();
+ expect(installed[1].indexOf(helpers.testPlatform)).toBe(-1);
+ });
+ }
+
+ function fullPlatformList() {
+ return cordova.raw.platform('list').then(function() {
+ var installed = results.match(/Installed platforms:\n (.*)/);
+ expect(installed).toBeDefined();
+ expect(installed[1].indexOf(helpers.testPlatform)).toBeGreaterThan(-1);
+ });
+ }
+ /** Test#007 will check the platform list in package.json and config.xml.
+ * When packge.json has 'android and ios' and config.xml only contains 'android', run cordova
+ * and config.xml is updated to include 'ios'.
+ */
+ it('Test#007 : if pkgJson has android & ios platforms and config.xml has android, update config to also include ios', function(done) {
+ var cwd = process.cwd();
+ var configXmlPath = path.join(cwd, 'config.xml');
+ var cfg1 = new ConfigParser(configXmlPath);
+ var engines = cfg1.getEngines();
+ var pkgJsonPath = path.join(cwd,'package.json');
+ delete require.cache[require.resolve(pkgJsonPath)];
+ var pkgJson = require(pkgJsonPath);
+ var platformsFolderPath;
+ var engNames = engines.map(function(elem) {
+ return elem.name;
+ });
+ var configEngArray = engNames.slice();
+
+ // Expect that config.xml contains only android at this point (basePjgJson4)
+ expect(configEngArray.indexOf('android')).toBeGreaterThan(-1);
+ expect(configEngArray.indexOf('ios')).toEqual(-1);
+ expect(configEngArray.length === 1);
+
+ emptyPlatformList().then(function() {
+ // Run cordova prepare
+ return cordova.raw.prepare();
+ }).then(function() {
+ var cfg2 = new ConfigParser(configXmlPath);
+ engines = cfg2.getEngines();
+ engNames = engines.map(function(elem) {
+ return elem.name;
+ });
+ configEngArray = engNames.slice();
+ // Expect 'ios' to be added to config.xml
+ expect(configEngArray.indexOf('ios')).toBeGreaterThan(-1);
+ // Expect 'android' to still be in config.xml
+ expect(configEngArray.indexOf('android')).toBeGreaterThan(-1);
+ }).fail(function(err) {
+ expect(err).toBeUndefined();
+ }).fin(done);
+ // Cordova prepare needs extra wait time to complete.
+ },30000);
+});
http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/7df35cb0/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 93af1dc..4e90a99 100644
--- a/cordova-lib/spec-cordova/pkgJson.spec.js
+++ b/cordova-lib/spec-cordova/pkgJson.spec.js
@@ -253,7 +253,7 @@ describe('platform end-to-end with --save', function () {
// Check the platform add was successful.
pkgJson = require(pkgJsonPath);
expect(pkgJson.cordova.platforms).not.toBeUndefined();
- expect(pkgJson.cordova.platforms.indexOf(helpers.testPlatform)).toBeGreaterThan(-1);
+ expect(pkgJson.cordova.platforms.indexOf(helpers.testPlatform)).toEqual(0);
}).then(fullPlatformList) // Platform should still be in platform ls.
.then(function() {
// And now remove it with --save.
@@ -302,15 +302,18 @@ describe('platform end-to-end with --save', function () {
var pkgJsonPath = path.join(process.cwd(),'package.json');
var pkgJson;
expect(pkgJsonPath).toExist();
-
+ delete require.cache[require.resolve(pkgJsonPath)];
+ pkgJson = require(pkgJsonPath);
+ // Pkg.json "platforms" should be empty and helpers.testPlatform should not exist in pkg.json.
+ expect(pkgJson.cordova).toBeUndefined();
// Add platform without --save.
cordova.raw.platform('add',[helpers.testPlatform])
.then(function() {
// Check the platform add was successful, reload, skipping cache
delete require.cache[require.resolve(pkgJsonPath)];
pkgJson = require(pkgJsonPath);
- // Cordova key should not exist in package.json
- expect(pkgJson.cordova).toBeUndefined();
+ // Platform list should be empty and helpers.testPlatform should NOT have been added.
+ expect(pkgJson.cordova.platforms.indexOf(helpers.testPlatform)).toEqual(-1);
}).then(fullPlatformList)
.fail(function(err) {
expect(err).toBeUndefined();
http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/7df35cb0/cordova-lib/src/cordova/plugin.js
----------------------------------------------------------------------
diff --git a/cordova-lib/src/cordova/plugin.js b/cordova-lib/src/cordova/plugin.js
index 4203ca3..c03207e 100644
--- a/cordova-lib/src/cordova/plugin.js
+++ b/cordova-lib/src/cordova/plugin.js
@@ -247,23 +247,19 @@ module.exports = function plugin(command, targets, opts) {
} else {
// Create package.json in cordova@7
}
- // If package.json exists, the plugin object and plugin name will be added to package.json
- // if not already there
- if (pkgJson === undefined) {
+ // If package.json exists, the plugin object and plugin name
+ // will be added to package.json if not already there.
+ if (!pkgJson) {
return;
}
- if (pkgJson.cordova === undefined) {
- pkgJson.cordova = {};
- }
- if (pkgJson.cordova.plugins === undefined) {
- pkgJson.cordova.plugins = {};
- }
- if (pkgJson.cordova.plugins !== undefined) {
- pkgJson.cordova.plugins[pluginInfo.id] = opts.cli_variables;
- events.emit('log','Adding '+pluginInfo.id+ ' to package.json');
- // Write to package.json
- fs.writeFileSync(pkgJsonPath, JSON.stringify(pkgJson, null, 4), 'utf8');
- }
+ pkgJson.cordova = pkgJson.cordova || {};
+ pkgJson.cordova.plugins = pkgJson.cordova.plugins || {};
+ // Plugin and variables are added.
+ pkgJson.cordova.plugins[pluginInfo.id] = opts.cli_variables;
+
+ events.emit('log','Adding '+pluginInfo.id+ ' to package.json');
+ // Write to package.json
+ fs.writeFileSync(pkgJsonPath, JSON.stringify(pkgJson, null, 4), 'utf8');
}
});
}, Q());
@@ -339,9 +335,10 @@ module.exports = function plugin(command, targets, opts) {
}
// If package.json exists and contains a specified plugin in cordova['plugins'], it will be removed
if(pkgJson !== undefined && pkgJson.cordova !== undefined && pkgJson.cordova.plugins !== undefined) {
- events.emit('log', 'Removing ' + target + ' from package.json');
+ events.emit('log', 'Removing [' + target + '] from package.json');
+ // Remove plugin from package.json
delete pkgJson.cordova.plugins[target];
- //Write out new package.json
+ //Write out new package.json with plugin removed correctly.
fs.writeFileSync(pkgJsonPath, JSON.stringify(pkgJson, null, 4), 'utf8');
}
}
http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/7df35cb0/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 ce14903..2ae8ec3 100644
--- a/cordova-lib/src/cordova/restore-util.js
+++ b/cordova-lib/src/cordova/restore-util.js
@@ -32,43 +32,188 @@ exports.installPlatformsFromConfigXML = installPlatformsFromConfigXML;
function installPlatformsFromConfigXML(platforms, opts) {
- events.emit('verbose', 'Checking config.xml for saved platforms that haven\'t been added to the project');
+ events.emit('verbose', 'Checking config.xml and package.json for saved platforms that haven\'t been added to the project');
var projectHome = cordova_util.cdProjectRoot();
var configPath = cordova_util.projectConfig(projectHome);
var cfg = new ConfigParser(configPath);
+ var engines = cfg.getEngines();
+ var pkgJsonPath = path.join(projectHome,'package.json');
+ var pkgJson;
+ var targetsPkgJson;
+ var comboArray;
+ var targets;
+ var targetsConfig;
+ var engines;
+ var installAllPlatforms;
+ var platformPath;
+ var platformAlreadyAdded;
+ var t;
+ var ConfigAndPkgJsonArray = [];
+ var modifiedPkgJson = false;
+ var modifiedConfigXML = false;
+ var modifiedPkgJsonPlugin = false;
+ var modifiedConfigXMLPlugin = false;
- var engines = cfg.getEngines(projectHome);
- var installAllPlatforms = !platforms || platforms.length === 0;
+ var targetPlatformsArray = [];
- var targets = engines.map(function(engine) {
- var platformPath = path.join(projectHome, 'platforms', engine.name);
- var platformAlreadyAdded = fs.existsSync(platformPath);
+ if(fs.existsSync(pkgJsonPath)) {
+ pkgJson = require(pkgJsonPath);
+ }
+ if(pkgJson !== undefined && pkgJson.cordova !== undefined && pkgJson.cordova.platforms !== undefined) {
+ targetsPkgJson = pkgJson.cordova.platforms;
+ }
+ if(cfg !== undefined) {
+ targetsConfig = [];
+ engines = cfg.getEngines(projectHome);
+ installAllPlatforms = !platforms || platforms.length === 0;
+
+ targets = engines.map(function(engine) {
+ platformPath = path.join(projectHome, 'platforms', engine.name);
+ platformAlreadyAdded = fs.existsSync(platformPath);
- //if no platforms are specified we add all.
- if ((installAllPlatforms || platforms.indexOf(engine.name) > -1) && !platformAlreadyAdded) {
- var t = engine.name;
- if (engine.spec) {
- t += '@' + engine.spec;
+ // If no platforms are specified we add all.
+ if ((installAllPlatforms || platforms.indexOf(engine.name) > -1) && !platformAlreadyAdded) {
+ t = engine.name;
+ if (engine.spec) {
+ //t += '@' + engine.spec;
+ targetsConfig.push(t);
+ }
+ return t;
}
- return t;
+ });
+ if (targetsPkgJson !== undefined) {
+ // Combining arrays and checking duplicates
+ comboArray = targetsPkgJson.slice();
+ } else {
+ comboArray = [];
}
- });
+ targetsConfig.forEach(function(item) {
+ if(comboArray.indexOf(item) < 0 ) {
+ comboArray.push(item);
+ }
+ });
+ // If config.xml & pkgJson exist and the cordova key is undefined, create a cordova key.
+ if (cfg !== undefined && pkgJson !== undefined && pkgJson.cordova === undefined) {
+ pkgJson.cordova = {};
+ }
+ // If there is no platforms array, create an empty one.
+ if (pkgJson.cordova.platforms === undefined) {
+ pkgJson.cordova.platforms = [];
+ // Get a list of platforms names from config.xml.
+ targetPlatformsArray = engines.map(function(mEngine) {
+ t = mEngine.name;
+ return t;
+ });
- if (!targets || !targets.length) {
- return Q('No platforms found in config.xml that haven\'t been added to the project');
- }
+ if (!targets || !targets.length) {
+ return Q('No platforms found in config.xml that haven\'t been added to the project');
+ }
+
+ var uniq = targetPlatformsArray.reduce(function(a,b){
+ if (a.indexOf(b) < 0 ) a.push(b);
+ return a;
+ },[]);
+ targetPlatformsArray = uniq;
+
+ // Add the platforms from config.xml to package.json
+ pkgJson.cordova.platforms = targetPlatformsArray;
+ modifiedPkgJson = true;
+ //fs.writeFileSync(pkgJsonPath, JSON.stringify(pkgJson, null, 4), 'utf8');
+ }
+ // If config.xml and pkg.json exist and both already contain platforms, run cordova prepare
+ // so that both files are identical
+ if(cfg !== undefined && targetsConfig !== undefined && pkgJson.cordova.platforms !== undefined) {
+
+ targetsConfig = engines.map(function(mEngine) {
+ t = mEngine.name;
+ //targetsConfig.push(t);
+ return t;
+ });
+ var uniq = targetsConfig.reduce(function(a,b){
+ if (a.indexOf(b) < 0 ) a.push(b);
+ return a;
+ },[]);
+ targetsConfig = uniq;
+ }
+ if (pkgJson.cordova.platforms !== undefined) {
+ // Create a new array based on cordova.platforms
+ pkgJson.cordova.platforms.forEach(function(item) {
+ if (ConfigAndPkgJsonArray.indexOf(item) < 0 )
+ ConfigAndPkgJsonArray.push(item);
+ });
+ }
+ targetsConfig.forEach(function(item) {
+ if(ConfigAndPkgJsonArray.indexOf(item) < 0 ) {
+ ConfigAndPkgJsonArray.push(item);
+ }
+ });
+
+ //sort the arrays alphabetically
+ ConfigAndPkgJsonArray = ConfigAndPkgJsonArray.sort();
+ pkgJson.cordova.platforms = pkgJson.cordova.platforms.sort();
+
+ // Get list of engine names and create a new array of engines from config.xml.
+ var engNames = engines.map(function(elem) {
+ return elem.name;
+ });
+ var configEngArray = engNames.sort().slice();
+
+ // If ConfigAndPkgJson array has the same platforms as pkg.json, no modification to pkg.json.
+ if(ConfigAndPkgJsonArray.toString() === pkgJson.cordova.platforms.toString()) {
+ events.emit('warn', 'Config.xml and package.json platforms are the same. No pkg.json modification.');
+ }
+ // If ConfigAndPkgJson array has the same platforms as config.xml, no modification to config.xml.
+ if(ConfigAndPkgJsonArray.toString() === configEngArray.toString()) {
+ events.emit('warn', 'Package.json and config.xml are the same. No config.xml modification.');
+ }
+ // If ConfigAndPkgJson array do not have the same platforms, modify pkg.json to include the elements
+ // from the ConfigAndPkgJson array so that the arrays are identical
+ if(ConfigAndPkgJsonArray.toString() !== pkgJson.cordova.platforms.toString()) {
+ events.emit('warn', 'Config.xml and package.json platforms are not the same. Updating package.json with most current list of platforms.');
+ ConfigAndPkgJsonArray.forEach(function(item) {
+ if(pkgJson.cordova.platforms.indexOf(item) < 0 ) {
+ pkgJson.cordova.platforms.push(item);
+ modifiedPkgJson = true;
+ }
+ });
+ }
+ // If ConfigAndPkgJson array do not have the same platforms, modify config.xml to include the elements
+ // from the ConfigAndPkgJson array so that the arrays are identical
+ if(ConfigAndPkgJsonArray.toString() !== configEngArray.toString()) {
+ events.emit('warn', 'Package.json and config.xml platforms are not the same. Updating config.xml with most current list of platforms.');
+ ConfigAndPkgJsonArray.forEach(function(item) {
+ if(configEngArray.indexOf(item) < 0 ) {
+ configEngArray.push(item);
+ modifiedConfigXML = true;
+ cfg.addEngine(item);
+ }
+ });
+ }
+ // Write and update pkg.json if it has been modified.
+ if (modifiedPkgJson === true) {
+ //sort then write
+ pkgJson.cordova.platforms = pkgJson.cordova.platforms.sort();
+ fs.writeFileSync(pkgJsonPath, JSON.stringify(pkgJson, null, 4), 'utf8');
+ }
+ if (modifiedConfigXML === true) {
+ cfg.write();
+ }
+ if (!comboArray || !comboArray.length) {
+ return Q('No platforms found in config.xml and/or package.json that haven\'t been added to the project');
+ }
+ }
// Run `platform add` for all the platforms separately
// so that failure on one does not affect the other.
// CB-9278 : Run `platform add` serially, one platform after another
// 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(targets, function(target) {
+ return promiseutil.Q_chainmap_graceful(comboArray, function(target) {
if (target) {
- events.emit('log', 'Discovered platform \"' + target + '\" in config.xml. Adding it to the project');
+ 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();
@@ -86,14 +231,99 @@ function installPluginsFromConfigXML(args) {
var configPath = cordova_util.projectConfig(projectRoot);
var cfg = new ConfigParser(configPath);
var plugins_dir = path.join(projectRoot, 'plugins');
+ var pkgJsonPath = path.join(projectRoot,'package.json');
+ var pkgJson;
+
+ var pkgJsonArray = [];
+ var comboPluginArray;
// Get all configured plugins
+
var plugins = cfg.getPluginIdList();
if (0 === plugins.length) {
return Q('No plugins found in config.xml that haven\'t been added to the project');
}
+ // Check if path exists and require pkgJsonPath
+ if(fs.existsSync(pkgJsonPath)) {
+ pkgJson = require(pkgJsonPath);
+ }
+ if (pkgJson.cordova === undefined) {
+ pkgJson.cordova = {};
+ }
+ // (In pkg.json), if there is a platforms array and not plugins array, create a new plugins array
+ // and add all plugins from config.xml to the new plugins array.
+ if (cfg !== undefined && pkgJson !== undefined && pkgJson.cordova.platforms !== undefined &&
+ pkgJson.cordova.plugins === undefined) {
+ pkgJson.cordova.plugins = {};
+ plugins.forEach(function(foo) {
+ pkgJson.cordova.plugins[foo] = {}
+ })
+ fs.writeFileSync(pkgJsonPath, JSON.stringify(pkgJson, null, 4), 'utf8');
+ }
+ // Created, but don't think this is necessary because there has to be at least one platform
+ // added for restore to run.
+ // (In pkg.json), if there is no platforms array and no plugins array, create a new plugins array
+ // and add all plugins from config.xml to the new plugins array.
+ if (cfg !== undefined && pkgJson !== undefined && pkgJson.cordova.platforms === undefined &&
+ pkgJson.cordova.plugins === undefined) {
+ pkgJson.cordova.platforms = [];
+ pkgJson.cordova.plugins = {};
+ plugins.forEach(function(foo) {
+ pkgJson.cordova.plugins[foo] = {}
+ })
+ fs.writeFileSync(pkgJsonPath, JSON.stringify(pkgJson, null, 4), 'utf8');
+ }
+ pkgJsonArray = Object.keys(pkgJson.cordova.plugins);
+ var pluginNames = plugins.map(function(elem) {
+ return elem.name;
+ });
+
+ // If config.xml and pkg.json exist and both already contain plugins, run cordova prepare
+ // to check if these plugins are identical.
+ if(cfg !== undefined && pkgJson.cordova.plugins !== undefined) {
+ if(pkgJsonArray.toString() === plugins.toString() && pkgJsonArray.length === plugins.length) {
+ events.emit('warn', 'Config.xml and pkgJson plugins are the same. No pkg.json or config.xml modification.');
+ }
+
+ if(pkgJsonArray.toString() !== plugins.toString() || pkgJsonArray.length !== plugins.length) {
+ events.emit('warn', 'Config.xml and pkgJson plugins are different.');
+ // Combining arrays and checking duplicates
+
+ comboPluginArray = pkgJsonArray.slice();
+ plugins.forEach(function(item) {
+ if(comboPluginArray.indexOf(item) < 0) {
+ comboPluginArray.push(item);
+ }
+ });
+
+ // Update pkg.json if it's missing any plugins based on plugin.id
+ if(comboPluginArray.sort().toString() !== pkgJsonArray.sort().toString() && pkgJsonArray.length > 0) {
+ events.emit('warn', 'Config.xml and package.json plugins are not the same. Updating package.json with most current list of plugins.');
+ comboPluginArray.forEach(function(item) {
+ if(pkgJsonArray.indexOf(item) < 0) {
+ pkgJsonArray.push(item);
+ var pluginVar = cfg.getPlugin(item);
+ var variables = pluginVar.variables;
+ pkgJson.cordova.plugins[item] = variables;
+ }
+ });
+ fs.writeFileSync(pkgJsonPath, JSON.stringify(pkgJson, null, 4), 'utf8');
+ }
+ // Update pkg.json if it's missing any plugins based on plugin.id
+ if(comboPluginArray.toString() !== plugins.toString()) {
+ events.emit('warn', 'Config.xml and package.json plugins are not the same. Updating config.xml with most current list of plugins.');
+ comboPluginArray.forEach(function(item) {
+ if(plugins.indexOf(item) < 0) {
+ plugins.push(item);
+ cfg.addPlugin({name:item}, pkgJson.cordova.plugins[item]);
+ cfg.write();
+ }
+ });
+ }
+ }
+ }
// Intermediate variable to store current installing plugin name
// to be able to create informative warning on plugin failure
var pluginName;
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org
For additional commands, e-mail: commits-help@cordova.apache.org