chcpConfigXmlWriter.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /**
  2. Helper module to work with config.xml file.
  3. We will use it to inject plugin-specific options.
  4. */
  5. var path = require('path');
  6. var xmlHelper = require('./xmlHelper.js');
  7. var cordovaContext;
  8. var projectRoot;
  9. var platforms;
  10. module.exports = {
  11. writeOptions: writeOptions
  12. };
  13. // region Public API
  14. /**
  15. * Inject options into config.xml files of each platform.
  16. *
  17. * @param {Object} context - cordova context instance
  18. * @param {Object} options - plugin options to inject
  19. */
  20. function writeOptions(context, options) {
  21. setup(context);
  22. injectOptions(options);
  23. }
  24. // endregion
  25. // region Private API
  26. /**
  27. * Initialize module.
  28. *
  29. * @param {Object} cordovaContext - cordova context instance
  30. */
  31. function setup(context) {
  32. cordovaContext = context;
  33. platforms = context.opts.platforms;
  34. projectRoot = context.opts.projectRoot;
  35. }
  36. /**
  37. * Get name of the current project.
  38. *
  39. * @param {Object} ctx - cordova context instance
  40. * @param {String} projectRoot - current root of the project
  41. *
  42. * @return {String} name of the project
  43. */
  44. function getProjectName(ctx, projectRoot) {
  45. var cordova_util = ctx.requireCordovaModule('cordova-lib/src/cordova/util');
  46. var xml = cordova_util.projectConfig(projectRoot);
  47. var ConfigParser;
  48. // If we are running Cordova 5.4 or abova - use parser from cordova-common.
  49. // Otherwise - from cordova-lib.
  50. try {
  51. ConfigParser = ctx.requireCordovaModule('cordova-common/src/ConfigParser/ConfigParser');
  52. } catch (e) {
  53. ConfigParser = ctx.requireCordovaModule('cordova-lib/src/configparser/ConfigParser')
  54. }
  55. return new ConfigParser(xml).name();
  56. }
  57. /**
  58. * Get path to config.xml inside iOS project.
  59. *
  60. * @return {String} absolute path to config.xml file
  61. */
  62. function pathToIosConfigXml() {
  63. var projectName = getProjectName(cordovaContext, projectRoot);
  64. return path.join(projectRoot, 'platforms', 'ios', projectName, 'config.xml');
  65. }
  66. /**
  67. * Get path to config.xml inside Android project.
  68. *
  69. * @return {String} absolute path to config.xml file
  70. */
  71. function pathToAndroidConfigXml() {
  72. return path.join(projectRoot, 'platforms', 'android', 'res', 'xml', 'config.xml');
  73. }
  74. /**
  75. * Get path to platform-specific config.xml file.
  76. *
  77. * @param {String} platform - for what platform we need config.xml
  78. * @return {String} absolute path to config.xml
  79. */
  80. function getPlatformSpecificConfigXml(platform) {
  81. var configFilePath = null;
  82. switch (platform) {
  83. case 'ios':
  84. {
  85. configFilePath = pathToIosConfigXml();
  86. break;
  87. }
  88. case 'android':
  89. {
  90. configFilePath = pathToAndroidConfigXml();
  91. break;
  92. }
  93. }
  94. return configFilePath;
  95. }
  96. /**
  97. * Write provided options into config.xml file for each platform.
  98. *
  99. * @param {Object} options - plugin options
  100. */
  101. function injectOptions(options) {
  102. platforms.forEach(function(platform) {
  103. var configXmlFilePath = getPlatformSpecificConfigXml(platform);
  104. if (configXmlFilePath == null) {
  105. return;
  106. }
  107. // read data from config.xml
  108. var configData = xmlHelper.readXmlAsJson(configXmlFilePath);
  109. if (configData == null) {
  110. console.warn('Configuration file ' + configXmlFilePath + ' not found');
  111. return;
  112. }
  113. // inject new options
  114. var chcpXmlConfig = {};
  115. for (var preferenceName in options) {
  116. injectPreference(chcpXmlConfig, preferenceName, options[preferenceName]);
  117. }
  118. // write them back to config.xml
  119. configData.widget['chcp'] = [];
  120. configData.widget.chcp.push(chcpXmlConfig);
  121. xmlHelper.writeJsonAsXml(configData, configXmlFilePath);
  122. });
  123. }
  124. /**
  125. * Inject preference into xml.
  126. *
  127. * @param {Object} xml - current xml preferences for the plugin
  128. * @param {String} preferenceName - preference name
  129. * @param {Object} preferenceAttributes - preference attributes
  130. */
  131. function injectPreference(xml, preferenceName, preferenceAttributes) {
  132. xml[preferenceName] = [{
  133. '$': preferenceAttributes
  134. }];
  135. }
  136. // endregion