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.

webSiteHook.js 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. Class creates android_web_hook.html file in your Cordova project root folder.
  3. File holds <link /> tags, which are generated based on data, specified in config.xml.
  4. You need to include those tags on your website to link web pages to the content inside your application.
  5. More documentation on that can be found here:
  6. https://developer.android.com/training/app-indexing/enabling-app-indexing.html
  7. */
  8. var fs = require('fs');
  9. var path = require('path');
  10. var mkpath = require('mkpath');
  11. var ConfigXmlHelper = require('../configXmlHelper.js');
  12. var WEB_HOOK_FILE_PATH = path.join('ul_web_hooks', 'android', 'android_web_hook.html');
  13. var WEB_HOOK_TPL_FILE_PATH = path.join('plugins', 'cordova-mirtech-plugin-universal-links', 'ul_web_hooks', 'android_web_hook_tpl.html');
  14. var LINK_PLACEHOLDER = '[__LINKS__]';
  15. var LINK_TEMPLATE = '<link rel="alternate" href="android-app://<package_name>/<scheme>/<host><path>" />';
  16. module.exports = {
  17. generate: generateWebHook
  18. };
  19. // region Public API
  20. /**
  21. * Generate website hook for android application.
  22. *
  23. * @param {Object} cordovaContext - cordova context object
  24. * @param {Object} pluginPreferences - plugin preferences from config.xml file; already parsed
  25. */
  26. function generateWebHook(cordovaContext, pluginPreferences) {
  27. var projectRoot = cordovaContext.opts.projectRoot;
  28. var configXmlHelper = new ConfigXmlHelper(cordovaContext);
  29. var packageName = configXmlHelper.getPackageName('android');
  30. var template = readTemplate(projectRoot);
  31. // if template was not found - exit
  32. if (template == null || template.length == 0) {
  33. return;
  34. }
  35. // generate hook content
  36. var linksToInsert = generateLinksSet(projectRoot, packageName, pluginPreferences);
  37. var hookContent = template.replace(LINK_PLACEHOLDER, linksToInsert);
  38. // save hook
  39. saveWebHook(projectRoot, hookContent);
  40. }
  41. // endregion
  42. // region Public API
  43. /**
  44. * Read hook teplate from plugin directory.
  45. *
  46. * @param {String} projectRoot - absolute path to cordova's project root
  47. * @return {String} data from the template file
  48. */
  49. function readTemplate(projectRoot) {
  50. var filePath = path.join(projectRoot, WEB_HOOK_TPL_FILE_PATH);
  51. var tplData = null;
  52. try {
  53. tplData = fs.readFileSync(filePath, 'utf8');
  54. } catch (err) {
  55. console.warn('Template file for android web hook is not found!');
  56. console.warn(err);
  57. }
  58. return tplData;
  59. }
  60. /**
  61. * Generate list of <link /> tags based on plugin preferences.
  62. *
  63. * @param {String} projectRoot - absolute path to cordova's project root
  64. * @param {String} packageName - android application package name
  65. * @param {Object} pluginPreferences - plugin preferences, defined in config.xml; already parsed
  66. * @return {String} list of <link /> tags
  67. */
  68. function generateLinksSet(projectRoot, packageName, pluginPreferences) {
  69. var linkTpl = LINK_TEMPLATE.replace('<package_name>', packageName);
  70. var content = '';
  71. pluginPreferences.hosts.forEach(function(host) {
  72. host.paths.forEach(function(hostPath) {
  73. content += generateLinkTag(linkTpl, host.scheme, host.name, hostPath) + '\n';
  74. });
  75. });
  76. return content;
  77. }
  78. /**
  79. * Generate <link /> tag.
  80. *
  81. * @param {String} linkTpl - template to use for tag generation
  82. * @param {String} scheme - host scheme
  83. * @param {String} host - host name
  84. * @param {String} path - host path
  85. * @return {String} <link /> tag
  86. */
  87. function generateLinkTag(linkTpl, scheme, host, path) {
  88. linkTpl = linkTpl.replace('<scheme>', scheme).replace('<host>', host);
  89. if (path == null || path === '*') {
  90. return linkTpl.replace('<path>', '');
  91. }
  92. // for android we need to replace * with .* for pattern matching
  93. if (path.indexOf('*') >= 0) {
  94. path = path.replace(/\*/g, '.*');
  95. }
  96. // path should start with /
  97. if (path.indexOf('/') != 0) {
  98. path = '/' + path;
  99. }
  100. return linkTpl.replace('<path>', path);
  101. }
  102. /**
  103. * Save data to website hook file.
  104. *
  105. * @param {String} projectRoot - absolute path to project root
  106. * @param {String} hookContent - data to save
  107. * @return {boolean} true - if data was saved; otherwise - false;
  108. */
  109. function saveWebHook(projectRoot, hookContent) {
  110. var filePath = path.join(projectRoot, WEB_HOOK_FILE_PATH);
  111. var isSaved = true;
  112. // ensure directory exists
  113. createDirectoryIfNeeded(path.dirname(filePath));
  114. // write data to file
  115. try {
  116. fs.writeFileSync(filePath, hookContent, 'utf8');
  117. } catch (err) {
  118. console.warn('Failed to create android web hook!');
  119. console.warn(err);
  120. isSaved = false;
  121. }
  122. return isSaved;
  123. }
  124. /**
  125. * Create directory if it doesn't exist yet.
  126. *
  127. * @param {String} dir - absolute path to directory
  128. */
  129. function createDirectoryIfNeeded(dir) {
  130. try {
  131. mkpath.sync(dir);
  132. } catch (err) {
  133. console.log(err);
  134. }
  135. }
  136. // endregion