browser_parser.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 fs = require('fs');
  18. var path = require('path');
  19. var shell = require('shelljs');
  20. var CordovaError = require('cordova-common').CordovaError;
  21. var events = require('cordova-common').events;
  22. var FileUpdater = require('cordova-common').FileUpdater;
  23. function dirExists (dir) {
  24. return fs.existsSync(dir) && fs.statSync(dir).isDirectory();
  25. }
  26. function browser_parser (project) {
  27. if (!dirExists(project) || !dirExists(path.join(project, 'cordova'))) {
  28. throw new CordovaError('The provided path "' + project + '" is not a valid browser project.');
  29. }
  30. this.path = project;
  31. }
  32. module.exports = browser_parser;
  33. // Returns a promise.
  34. browser_parser.prototype.update_from_config = function () {
  35. return Promise.resolve();
  36. };
  37. browser_parser.prototype.www_dir = function () {
  38. return path.join(this.path, 'www');
  39. };
  40. // Used for creating platform_www in projects created by older versions.
  41. browser_parser.prototype.cordovajs_path = function (libDir) {
  42. var jsPath = path.join(libDir, 'cordova-lib', 'cordova.js');
  43. return path.resolve(jsPath);
  44. };
  45. browser_parser.prototype.cordovajs_src_path = function (libDir) {
  46. // console.log("cordovajs_src_path");
  47. var jsPath = path.join(libDir, 'cordova-js-src');
  48. return path.resolve(jsPath);
  49. };
  50. /**
  51. * Logs all file operations via the verbose event stream, indented.
  52. */
  53. function logFileOp (message) {
  54. events.emit('verbose', ' ' + message);
  55. }
  56. // Replace the www dir with contents of platform_www and app www.
  57. browser_parser.prototype.update_www = function (cordovaProject, opts) {
  58. var platform_www = path.join(this.path, 'platform_www');
  59. var my_www = this.www_dir();
  60. // add cordova www and platform_www to sourceDirs
  61. var sourceDirs = [
  62. path.relative(cordovaProject.root, cordovaProject.locations.www),
  63. path.relative(cordovaProject.root, platform_www)
  64. ];
  65. // If project contains 'merges' for our platform, use them as another overrides
  66. var merges_path = path.join(cordovaProject.root, 'merges', 'browser');
  67. if (fs.existsSync(merges_path)) {
  68. events.emit('verbose', 'Found "merges/browser" folder. Copying its contents into the browser project.');
  69. // add merges/browser to sourceDirs
  70. sourceDirs.push(path.join('merges', 'browser'));
  71. }
  72. // targetDir points to browser/www
  73. var targetDir = path.relative(cordovaProject.root, my_www);
  74. events.emit('verbose', 'Merging and updating files from [' + sourceDirs.join(', ') + '] to ' + targetDir);
  75. FileUpdater.mergeAndUpdateDir(sourceDirs, targetDir, { rootDir: cordovaProject.root }, logFileOp);
  76. };
  77. browser_parser.prototype.update_overrides = function () {
  78. // console.log("update_overrides");
  79. // TODO: ?
  80. // var projectRoot = util.isCordova(this.path);
  81. // var mergesPath = path.join(util.appDir(projectRoot), 'merges', 'browser');
  82. // if(fs.existsSync(mergesPath)) {
  83. // var overrides = path.join(mergesPath, '*');
  84. // shell.cp('-rf', overrides, this.www_dir());
  85. // }
  86. };
  87. browser_parser.prototype.config_xml = function () {
  88. return path.join(this.path, 'config.xml');
  89. };
  90. // Returns a promise.
  91. browser_parser.prototype.update_project = function (cfg) {
  92. // console.log("update_project ",cfg);
  93. var defer = this.update_from_config();
  94. var self = this;
  95. var www_dir = self.www_dir();
  96. defer.then(function () {
  97. self.update_overrides();
  98. // Copy munged config.xml to platform www dir
  99. shell.cp('-rf', path.join(www_dir, '..', 'config.xml'), www_dir);
  100. });
  101. return defer;
  102. };