Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

util.js 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. // Some helpful utility stuff copied from cordova-lib. This is a bit nicer than taking a dependency on cordova-lib just
  20. // to get this minimal stuff. Hopefully we won't need the platform stuff (finding platform www_dir) once it is moved
  21. // into the actual platform.
  22. var platforms = {
  23. amazon_fireos: { www_dir: 'assets/www' },
  24. android: { www_dir: 'assets/www' },
  25. blackberry10: { www_dir: 'www' },
  26. browser: { www_dir: 'www' },
  27. firefoxos: { www_dir: 'www' },
  28. ios: { www_dir: 'www' },
  29. ubuntu: { www_dir: 'www' },
  30. windows: { www_dir: 'www' },
  31. wp8: { www_dir: 'www' }
  32. };
  33. /**
  34. * @desc Look for a Cordova project's root directory, starting at the specified directory (or CWD if none specified).
  35. * @param {string=} dir - the directory to start from (we check this directory then work up), or CWD if none specified.
  36. * @returns {string} - the Cordova project's root directory, or null if not found.
  37. */
  38. function cordovaProjectRoot (dir) {
  39. if (!dir) {
  40. // Prefer PWD over cwd so that symlinked dirs within your PWD work correctly.
  41. var pwd = process.env.PWD;
  42. var cwd = process.cwd();
  43. if (pwd && pwd !== cwd && pwd !== 'undefined') {
  44. return cordovaProjectRoot(pwd) || cordovaProjectRoot(cwd);
  45. }
  46. return cordovaProjectRoot(cwd);
  47. }
  48. var bestReturnValueSoFar = null;
  49. for (var i = 0; i < 1000; ++i) {
  50. var result = isRootDir(dir);
  51. if (result === 2) {
  52. return dir;
  53. }
  54. if (result === 1) {
  55. bestReturnValueSoFar = dir;
  56. }
  57. var parentDir = path.normalize(path.join(dir, '..'));
  58. // Detect fs root.
  59. if (parentDir === dir) {
  60. return bestReturnValueSoFar;
  61. }
  62. dir = parentDir;
  63. }
  64. return null;
  65. }
  66. function getPlatformWwwRoot (cordovaProjectRoot, platformName) {
  67. var platform = platforms[platformName];
  68. if (!platform) {
  69. throw new Error('Unrecognized platform: ' + platformName);
  70. }
  71. try {
  72. var Api = require(path.join(cordovaProjectRoot, 'platforms', platformName, 'cordova/Api'));
  73. return new Api().locations.www;
  74. } catch (e) {
  75. // Fallback on hardcoded paths if platform api not found
  76. return path.join(cordovaProjectRoot, 'platforms', platformName, platform.www_dir);
  77. }
  78. }
  79. function isRootDir (dir) {
  80. if (fs.existsSync(path.join(dir, 'www'))) {
  81. if (fs.existsSync(path.join(dir, 'config.xml'))) {
  82. // For sure is.
  83. if (fs.existsSync(path.join(dir, 'platforms'))) {
  84. return 2;
  85. } else {
  86. return 1;
  87. }
  88. }
  89. // Might be (or may be under platforms/).
  90. if (fs.existsSync(path.join(dir, 'www', 'config.xml'))) {
  91. return 1;
  92. }
  93. }
  94. return 0;
  95. }
  96. module.exports = {
  97. cordovaProjectRoot: cordovaProjectRoot,
  98. getPlatformWwwRoot: getPlatformWwwRoot,
  99. platforms: platforms
  100. };