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.

afterPrepareHook.js 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /**
  2. Hook is executed at the end of the 'prepare' stage. Usually, when you call 'cordova build'.
  3. It will inject required preferences in the platform-specific projects, based on <universal-links>
  4. data you have specified in the projects config.xml file.
  5. */
  6. var configParser = require('./lib/configXmlParser.js');
  7. var androidManifestWriter = require('./lib/android/manifestWriter.js');
  8. var androidWebHook = require('./lib/android/webSiteHook.js');
  9. var iosProjectEntitlements = require('./lib/ios/projectEntitlements.js');
  10. var iosAppSiteAssociationFile = require('./lib/ios/appleAppSiteAssociationFile.js');
  11. var iosProjectPreferences = require('./lib/ios/xcodePreferences.js');
  12. var ANDROID = 'android';
  13. var IOS = 'ios';
  14. module.exports = function(ctx) {
  15. run(ctx);
  16. };
  17. /**
  18. * Execute hook.
  19. *
  20. * @param {Object} cordovaContext - cordova context object
  21. */
  22. function run(cordovaContext) {
  23. var pluginPreferences = configParser.readPreferences(cordovaContext);
  24. var platformsList = cordovaContext.opts.platforms;
  25. // if no preferences are found - exit
  26. if (pluginPreferences == null) {
  27. return;
  28. }
  29. // if no host is defined - exit
  30. if (pluginPreferences.hosts == null || pluginPreferences.hosts.length == 0) {
  31. console.warn('No host is specified in the config.xml. Universal Links plugin is not going to work.');
  32. return;
  33. }
  34. platformsList.forEach(function(platform) {
  35. switch (platform) {
  36. case ANDROID:
  37. {
  38. activateUniversalLinksInAndroid(cordovaContext, pluginPreferences);
  39. break;
  40. }
  41. case IOS:
  42. {
  43. activateUniversalLinksInIos(cordovaContext, pluginPreferences);
  44. break;
  45. }
  46. }
  47. });
  48. }
  49. /**
  50. * Activate Deep Links for Android application.
  51. *
  52. * @param {Object} cordovaContext - cordova context object
  53. * @param {Object} pluginPreferences - plugin preferences from the config.xml file. Basically, content from <universal-links> tag.
  54. */
  55. function activateUniversalLinksInAndroid(cordovaContext, pluginPreferences) {
  56. // inject preferenes into AndroidManifest.xml
  57. androidManifestWriter.writePreferences(cordovaContext, pluginPreferences);
  58. // generate html file with the <link> tags that you should inject on the website.
  59. androidWebHook.generate(cordovaContext, pluginPreferences);
  60. }
  61. /**
  62. * Activate Universal Links for iOS application.
  63. *
  64. * @param {Object} cordovaContext - cordova context object
  65. * @param {Object} pluginPreferences - plugin preferences from the config.xml file. Basically, content from <universal-links> tag.
  66. */
  67. function activateUniversalLinksInIos(cordovaContext, pluginPreferences) {
  68. // modify xcode project preferences
  69. iosProjectPreferences.enableAssociativeDomainsCapability(cordovaContext);
  70. // generate entitlements file
  71. iosProjectEntitlements.generateAssociatedDomainsEntitlements(cordovaContext, pluginPreferences);
  72. // generate apple-site-association-file
  73. iosAppSiteAssociationFile.generate(cordovaContext, pluginPreferences);
  74. }