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

platform.js 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. /* globals Promise: true */
  18. var fs = require('fs');
  19. var util = require('./util');
  20. /**
  21. * Launches a server where the root points to the specified platform in a Cordova project.
  22. * @param {string} platform - Cordova platform to serve.
  23. * @param {{root: ?string, port: ?number, urlPathProcessor: ?function, streamHandler: ?function, serverExtender: ?function}} opts
  24. * root - cordova project directory, or any directory within it. If not specified, cwd is used. This will be modified to point to the platform's www_dir.
  25. * All other values are passed unaltered to launchServer().
  26. * @returns {*|promise}
  27. */
  28. module.exports = function (platform, opts) {
  29. // note: `this` is actually an instance of main.js CordovaServe
  30. // this module is a mixin
  31. var that = this;
  32. var retPromise = new Promise(function (resolve, reject) {
  33. if (!platform) {
  34. reject(new Error('Error: A platform must be specified'));
  35. } else {
  36. opts = opts || {};
  37. var projectRoot = findProjectRoot(opts.root);
  38. that.projectRoot = projectRoot;
  39. opts.root = util.getPlatformWwwRoot(projectRoot, platform);
  40. if (!fs.existsSync(opts.root)) {
  41. reject(new Error('Error: Project does not include the specified platform: ' + platform));
  42. } else {
  43. return resolve(that.launchServer(opts));
  44. }
  45. }
  46. });
  47. return retPromise;
  48. };
  49. function findProjectRoot (path) {
  50. var projectRoot = util.cordovaProjectRoot(path);
  51. if (!projectRoot) {
  52. if (!path) {
  53. throw new Error('Current directory does not appear to be in a Cordova project.');
  54. } else {
  55. throw new Error('Directory "' + path + '" does not appear to be in a Cordova project.');
  56. }
  57. }
  58. return projectRoot;
  59. }