broadcastActivateEvent.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #!/usr/bin/env node
  2. /*
  3. * Copyright (c) 2013-2015 by appPlant UG. All rights reserved.
  4. *
  5. * @APPPLANT_LICENSE_HEADER_START@
  6. *
  7. * This file contains Original Code and/or Modifications of Original Code
  8. * as defined in and that are subject to the Apache License
  9. * Version 2.0 (the 'License'). You may not use this file except in
  10. * compliance with the License. Please obtain a copy of the License at
  11. * http://opensource.org/licenses/Apache-2.0/ and read it before using this
  12. * file.
  13. *
  14. * The Original Code and all software distributed under the License are
  15. * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  16. * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
  17. * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
  19. * Please see the License for the specific language governing rights and
  20. * limitations under the License.
  21. *
  22. * @APPPLANT_LICENSE_HEADER_END@
  23. */
  24. // Includes a snippet into the cordova-core js file
  25. // to fire the activated event after device is ready
  26. var fs = require('fs'),
  27. rootdir = process.argv[2];
  28. if (!rootdir)
  29. return;
  30. /**
  31. * Replaces a string with another one in a file.
  32. *
  33. * @param {String} path
  34. * Absolute or relative file path from cordova root project.
  35. * @param {String} to_replace
  36. * The string to replace.
  37. * @param {String}
  38. * The string to replace with.
  39. */
  40. function replace (filename, to_replace, replace_with) {
  41. var data = fs.readFileSync(filename, 'utf8'),
  42. result;
  43. if (data.indexOf(replace_with) > -1)
  44. return;
  45. result = data.replace(to_replace, replace_with);
  46. fs.writeFileSync(filename, result, 'utf8');
  47. }
  48. // Fires the activated event again after device is ready
  49. var snippet =
  50. "var activatedHandler = function (args) {" +
  51. "channel.deviceready.subscribe(function () {" +
  52. "app.queueEvent(args);" +
  53. "});" +
  54. "};" +
  55. "app.addEventListener('activated', activatedHandler, false);" +
  56. "document.addEventListener('deviceready', function () {" +
  57. "app.removeEventListener('activated', activatedHandler);" +
  58. "}, false);\n" +
  59. " app.start();";
  60. // Path to cordova-core js files where the snippet needs to be included
  61. var files = [
  62. 'platforms/windows/www/cordova.js',
  63. 'platforms/windows/platform_www/cordova.js'
  64. ];
  65. // Includes the snippet before app.start() is called
  66. for (var i = 0; i < files.length; i++) {
  67. replace(files[i], 'app.start();', snippet);
  68. }