run.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #!/usr/bin/env node
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. var fs = require('fs');
  21. var path = require('path');
  22. var url = require('url');
  23. var cordovaServe = require('cordova-serve');
  24. module.exports.run = function (args) {
  25. // defaults
  26. args.port = args.port || 8000;
  27. args.target = args.target || 'default'; // make default the system browser
  28. args.noLogOutput = args.silent || false;
  29. var wwwPath = path.join(__dirname, '../../www');
  30. var manifestFilePath = path.resolve(path.join(wwwPath, 'manifest.json'));
  31. var startPage;
  32. // get start page from manifest
  33. if (fs.existsSync(manifestFilePath)) {
  34. try {
  35. var manifest = require(manifestFilePath);
  36. startPage = manifest.start_url;
  37. } catch (err) {
  38. console.log('failed to require manifest ... ' + err);
  39. }
  40. }
  41. var server = cordovaServe();
  42. server.servePlatform('browser', { port: args.port, noServerInfo: true, noLogOutput: args.noLogOutput })
  43. .then(function () {
  44. if (!startPage) {
  45. // failing all else, set the default
  46. startPage = 'index.html';
  47. }
  48. var projectUrl = (new url.URL(`http://localhost:${server.port}/${startPage}`)).href;
  49. console.log('startPage = ' + startPage);
  50. console.log('Static file server running @ ' + projectUrl + '\nCTRL + C to shut down');
  51. return server.launchBrowser({ 'target': args.target, 'url': projectUrl });
  52. })
  53. .catch(function (error) {
  54. console.log(error.message || error.toString());
  55. if (server.server) {
  56. server.server.close();
  57. }
  58. });
  59. };