You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

configXmlHelper.js 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. Helper class to read data from config.xml file.
  3. */
  4. var path = require('path');
  5. var xmlHelper = require('./xmlHelper.js');
  6. var ANDROID = 'android';
  7. var IOS = 'ios';
  8. var CONFIG_FILE_NAME = 'config.xml';
  9. var context;
  10. var projectRoot;
  11. module.exports = ConfigXmlHelper;
  12. // region public API
  13. /**
  14. * Constructor.
  15. *
  16. * @param {Object} cordovaContext - cordova context object
  17. */
  18. function ConfigXmlHelper(cordovaContext) {
  19. context = cordovaContext;
  20. projectRoot = context.opts.projectRoot;
  21. }
  22. /**
  23. * Read config.xml data as JSON object.
  24. *
  25. * @return {Object} JSON object with data from config.xml
  26. */
  27. ConfigXmlHelper.prototype.read = function() {
  28. var filePath = getConfigXmlFilePath();
  29. return xmlHelper.readXmlAsJson(filePath);
  30. }
  31. /**
  32. * Get package name for the application. Depends on the platform.
  33. *
  34. * @param {String} platform - 'ios' or 'android'; for what platform we need a package name
  35. * @return {String} package/bundle name
  36. */
  37. ConfigXmlHelper.prototype.getPackageName = function(platform) {
  38. var configFilePath = getConfigXmlFilePath();
  39. var config = getCordovaConfigParser(configFilePath);
  40. var packageName;
  41. switch (platform) {
  42. case ANDROID:
  43. {
  44. packageName = config.android_packageName();
  45. break;
  46. }
  47. case IOS:
  48. {
  49. packageName = config.ios_CFBundleIdentifier();
  50. break;
  51. }
  52. }
  53. if (packageName === undefined || packageName.length == 0) {
  54. packageName = config.packageName();
  55. }
  56. return packageName;
  57. }
  58. /**
  59. * Get name of the current project.
  60. *
  61. * @return {String} name of the project
  62. */
  63. ConfigXmlHelper.prototype.getProjectName = function() {
  64. return getProjectName();
  65. }
  66. // endregion
  67. // region Private API
  68. /**
  69. * Get config parser from cordova library.
  70. *
  71. * @param {String} configFilePath absolute path to the config.xml file
  72. * @return {Object}
  73. */
  74. function getCordovaConfigParser(configFilePath) {
  75. var ConfigParser;
  76. // If we are running Cordova 5.4 or abova - use parser from cordova-common.
  77. // Otherwise - from cordova-lib.
  78. try {
  79. ConfigParser = context.requireCordovaModule('cordova-common/src/ConfigParser/ConfigParser');
  80. } catch (e) {
  81. ConfigParser = context.requireCordovaModule('cordova-lib/src/configparser/ConfigParser')
  82. }
  83. return new ConfigParser(configFilePath);
  84. }
  85. /**
  86. * Get absolute path to the config.xml.
  87. */
  88. function getConfigXmlFilePath() {
  89. return path.join(projectRoot, CONFIG_FILE_NAME);
  90. }
  91. /**
  92. * Get project name from config.xml
  93. */
  94. function getProjectName() {
  95. var configFilePath = getConfigXmlFilePath();
  96. var config = getCordovaConfigParser(configFilePath);
  97. return config.name();
  98. }
  99. // endregion