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.

appleAppSiteAssociationFile.js 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. Script generates apple-app-site-association files: one for each domain, defined in config.xml.
  3. It is executed on 'after prepare' stage, usually when you execute 'cordova build'. Files are placed in 'ul_web_hooks/ios/' folder
  4. of your projects root.
  5. Files are created with the following name:
  6. hostname#apple-app-site-association
  7. Prefix 'hostname#' describes on which host this file should be placed. Don't forget to remove it before uploading file on your host.
  8. Also, in the file you need to replace <YOUR_TEAM_ID_FROM_MEMBER_CENTER> with the real team id from the member center, if <ios-team-id> preference was not set in projects config.xml.
  9. In order to activate support for Universal Links on iOS you need to sign them with the valid SSL certificate and place in the root of your domain.
  10. Additional documentation regarding apple-app-site-association file can be found here:
  11. - https://developer.apple.com/library/ios/documentation/General/Conceptual/AppSearch/UniversalLinks.html
  12. - https://developer.apple.com/library/ios/documentation/Security/Reference/SharedWebCredentialsRef/index.html#//apple_ref/doc/uid/TP40014989
  13. */
  14. var path = require('path');
  15. var mkpath = require('mkpath');
  16. var fs = require('fs');
  17. var rimraf = require('rimraf');
  18. var ConfigXmlHelper = require('../configXmlHelper.js');
  19. var IOS_TEAM_ID = '<YOUR_TEAM_ID_FROM_MEMBER_CENTER>';
  20. var ASSOCIATION_FILE_NAME = 'apple-app-site-association';
  21. var bundleId;
  22. var context;
  23. module.exports = {
  24. generate: generate
  25. };
  26. // region Public API
  27. /**
  28. * Generate apple-app-site-association files.
  29. *
  30. * @param {Object} cordovaContext - cordova context object
  31. * @param {Object} pluginPreferences - list of hosts from the config.xml; already parsed
  32. */
  33. function generate(cordovaContext, pluginPreferences) {
  34. context = cordovaContext;
  35. removeOldFiles();
  36. createNewAssociationFiles(pluginPreferences);
  37. }
  38. // endregion
  39. // region Content generation
  40. /**
  41. * Remove old files from ul_web_hooks/ios folder.
  42. */
  43. function removeOldFiles() {
  44. rimraf.sync(getWebHookDirectory());
  45. }
  46. /**
  47. * Generate new set of apple-app-site-association files.
  48. *
  49. * @param {Object} pluginPreferences - list of hosts from config.xml
  50. */
  51. function createNewAssociationFiles(pluginPreferences) {
  52. var teamId = pluginPreferences.iosTeamId;
  53. if (!teamId) {
  54. teamId = IOS_TEAM_ID;
  55. }
  56. pluginPreferences.hosts.forEach(function(host) {
  57. var content = generateFileContentForHost(host, teamId);
  58. saveContentToFile(host.name, content);
  59. });
  60. }
  61. /**
  62. * Generate content of the apple-app-site-association file for the specific host.
  63. *
  64. * @param {Object} host - host information
  65. * @return {Object} content of the file as JSON object
  66. */
  67. function generateFileContentForHost(host, teamId) {
  68. var appID = teamId + '.' + getBundleId();
  69. var paths = host.paths.slice();
  70. // if paths are '*' - we should add '/' to it to support root domains.
  71. // https://github.com/martindrapeau/cordova-universal-links-plugin/issues/46
  72. if (paths.length == 1 && paths[0] === '*') {
  73. paths.push('/');
  74. }
  75. return {
  76. "applinks": {
  77. "apps": [],
  78. "details": [{
  79. "appID": appID,
  80. "paths": paths
  81. }]
  82. }
  83. };
  84. }
  85. /**
  86. * Save data to the the apple-app-site-association file.
  87. *
  88. * @param {String} filePrefix - prefix for the generated file; usually - hostname
  89. * @param {Object} content - file content as JSON object
  90. */
  91. function saveContentToFile(filePrefix, content) {
  92. var dirPath = getWebHookDirectory();
  93. var filePath = path.join(dirPath, filePrefix + '#' + ASSOCIATION_FILE_NAME);
  94. // create all directories from file path
  95. createDirectoriesIfNeeded(dirPath);
  96. // write content to the file
  97. try {
  98. fs.writeFileSync(filePath, JSON.stringify(content, null, 2), 'utf8');
  99. } catch (err) {
  100. console.log(err);
  101. }
  102. }
  103. /**
  104. * Create all directories from the given path.
  105. *
  106. * @param {String} dirPath - full path to directory
  107. */
  108. function createDirectoriesIfNeeded(dirPath) {
  109. try {
  110. mkpath.sync(dirPath);
  111. } catch (err) {
  112. console.log(err);
  113. }
  114. }
  115. // endregion
  116. // region Support methods
  117. /**
  118. * Path to the ios web hook directory.
  119. *
  120. * @return {String} path to web hook directory
  121. */
  122. function getWebHookDirectory() {
  123. return path.join(getProjectRoot(), 'ul_web_hooks', 'ios');
  124. }
  125. /**
  126. * Project root directory
  127. *
  128. * @return {String} absolute path to project root
  129. */
  130. function getProjectRoot() {
  131. return context.opts.projectRoot;
  132. }
  133. /**
  134. * Get bundle id from the config.xml file.
  135. *
  136. * @return {String} bundle id
  137. */
  138. function getBundleId() {
  139. if (bundleId === undefined) {
  140. var configXmlHelper = new ConfigXmlHelper(context);
  141. bundleId = configXmlHelper.getPackageName('ios');
  142. }
  143. return bundleId;
  144. }
  145. // endregion