browser_handler.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /**
  2. Licensed to the Apache Software Foundation (ASF) under one
  3. or more contributor license agreements. See the NOTICE file
  4. distributed with this work for additional information
  5. regarding copyright ownership. The ASF licenses this file
  6. to you under the Apache License, Version 2.0 (the
  7. "License"); you may not use this file except in compliance
  8. with the License. You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing,
  11. software distributed under the License is distributed on an
  12. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  13. KIND, either express or implied. See the License for the
  14. specific language governing permissions and limitations
  15. under the License.
  16. */
  17. var path = require('path');
  18. var fs = require('fs');
  19. var shell = require('shelljs');
  20. var events = require('cordova-common').events;
  21. module.exports = {
  22. www_dir: function (project_dir) {
  23. return path.join(project_dir, 'www');
  24. },
  25. package_name: function (project_dir) {
  26. // this method should the id from root config.xml => <widget id=xxx
  27. // return common.package_name(project_dir, this.www_dir(project_dir));
  28. // console.log('package_name called with ' + project_dir);
  29. var pkgName = 'io.cordova.hellocordova';
  30. var widget_id_regex = /(?:<widget\s+id=['"])(\S+)(?:['"])/;
  31. var configPath = path.join(project_dir, 'config.xml');
  32. if (fs.existsSync(configPath)) {
  33. var configStr = fs.readFileSync(configPath, 'utf8');
  34. var res = configStr.match(widget_id_regex);
  35. if (res && res.length > 1) {
  36. pkgName = res[1];
  37. }
  38. }
  39. return pkgName;
  40. },
  41. 'js-module': {
  42. install: function (jsModule, plugin_dir, plugin_id, www_dir) {
  43. // Copy the plugin's files into the www directory.
  44. var moduleSource = path.resolve(plugin_dir, jsModule.src);
  45. // Get module name based on existing 'name' attribute or filename
  46. // Must use path.extname/path.basename instead of path.parse due to CB-9981
  47. var moduleName = plugin_id + '.' + (jsModule.name || path.basename(jsModule.src, path.extname(jsModule.src)));
  48. // Read in the file, prepend the cordova.define, and write it back out.
  49. var scriptContent = fs.readFileSync(moduleSource, 'utf-8').replace(/^\ufeff/, ''); // Window BOM
  50. if (moduleSource.match(/.*\.json$/)) {
  51. scriptContent = 'module.exports = ' + scriptContent;
  52. }
  53. scriptContent = 'cordova.define("' + moduleName + '", function(require, exports, module) { ' + scriptContent + '\n});\n';
  54. var moduleDestination = path.resolve(www_dir, 'plugins', plugin_id, jsModule.src);
  55. shell.mkdir('-p', path.dirname(moduleDestination));
  56. fs.writeFileSync(moduleDestination, scriptContent, 'utf-8');
  57. },
  58. uninstall: function (jsModule, www_dir, plugin_id) {
  59. var pluginRelativePath = path.join('plugins', plugin_id, jsModule.src);
  60. // common.removeFileAndParents(www_dir, pluginRelativePath);
  61. console.log('js-module uninstall called : ' + pluginRelativePath);
  62. }
  63. },
  64. 'source-file': {
  65. install: function (obj, plugin_dir, project_dir, plugin_id, options) {
  66. // var dest = path.join(obj.targetDir, path.basename(obj.src));
  67. // common.copyFile(plugin_dir, obj.src, project_dir, dest);
  68. console.log('install called');
  69. },
  70. uninstall: function (obj, project_dir, plugin_id, options) {
  71. // var dest = path.join(obj.targetDir, path.basename(obj.src));
  72. // common.removeFile(project_dir, dest);
  73. console.log('uninstall called');
  74. }
  75. },
  76. 'header-file': {
  77. install: function (obj, plugin_dir, project_dir, plugin_id, options) {
  78. events.emit('verbose', 'header-fileinstall is not supported for browser');
  79. },
  80. uninstall: function (obj, project_dir, plugin_id, options) {
  81. events.emit('verbose', 'header-file.uninstall is not supported for browser');
  82. }
  83. },
  84. 'resource-file': {
  85. install: function (obj, plugin_dir, project_dir, plugin_id, options) {
  86. events.emit('verbose', 'resource-file.install is not supported for browser');
  87. },
  88. uninstall: function (obj, project_dir, plugin_id, options) {
  89. events.emit('verbose', 'resource-file.uninstall is not supported for browser');
  90. }
  91. },
  92. 'framework': {
  93. install: function (obj, plugin_dir, project_dir, plugin_id, options) {
  94. events.emit('verbose', 'framework.install is not supported for browser');
  95. },
  96. uninstall: function (obj, project_dir, plugin_id, options) {
  97. events.emit('verbose', 'framework.uninstall is not supported for browser');
  98. }
  99. },
  100. 'lib-file': {
  101. install: function (obj, plugin_dir, project_dir, plugin_id, options) {
  102. events.emit('verbose', 'lib-file.install is not supported for browser');
  103. },
  104. uninstall: function (obj, project_dir, plugin_id, options) {
  105. events.emit('verbose', 'lib-file.uninstall is not supported for browser');
  106. }
  107. },
  108. asset: {
  109. install: function (asset, plugin_dir, wwwDest) {
  110. var src = path.join(plugin_dir, asset.src);
  111. var dest = path.join(wwwDest, asset.target);
  112. var destDir = path.parse(dest).dir;
  113. if (destDir !== '' && !fs.existsSync(destDir)) {
  114. shell.mkdir('-p', destDir);
  115. }
  116. if (fs.statSync(src).isDirectory()) {
  117. shell.cp('-Rf', src + '/*', dest);
  118. } else {
  119. shell.cp('-f', src, dest);
  120. }
  121. },
  122. uninstall: function (asset, wwwDest, plugin_id) {
  123. shell.rm('-rf', path.join(wwwDest, asset.target));
  124. shell.rm('-rf', path.join(wwwDest, 'plugins', plugin_id));
  125. }
  126. }
  127. };