serve.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.ServeCommand = void 0;
  4. const tslib_1 = require("tslib");
  5. const utils_process_1 = require("@ionic/utils-process");
  6. const lodash = tslib_1.__importStar(require("lodash"));
  7. const color_1 = require("../lib/color");
  8. const command_1 = require("../lib/command");
  9. const errors_1 = require("../lib/errors");
  10. const serve_1 = require("../lib/serve");
  11. class ServeCommand extends command_1.Command {
  12. async getMetadata() {
  13. const groups = [];
  14. let options = [
  15. ...serve_1.COMMON_SERVE_COMMAND_OPTIONS,
  16. {
  17. name: 'open',
  18. summary: 'Do not open a browser window',
  19. type: Boolean,
  20. default: true,
  21. // TODO: Adding 'b' to aliases here has some weird behavior with minimist.
  22. },
  23. {
  24. name: 'browser',
  25. summary: `Specifies the browser to use (${serve_1.BROWSERS.map(b => (0, color_1.input)(b)).join(', ')})`,
  26. aliases: ['w'],
  27. groups: ["advanced" /* MetadataGroup.ADVANCED */],
  28. },
  29. {
  30. name: 'browseroption',
  31. summary: `Specifies a path to open to (${(0, color_1.input)('/#/tab/dash')})`,
  32. aliases: ['o'],
  33. groups: ["advanced" /* MetadataGroup.ADVANCED */],
  34. spec: { value: 'path' },
  35. },
  36. ];
  37. const exampleCommands = ['', '--external'];
  38. const footnotes = [];
  39. let description = `
  40. Easily spin up a development server which launches in your browser. It watches for changes in your source files and automatically reloads with the updated build.
  41. By default, ${(0, color_1.input)('ionic serve')} boots up a development server on ${(0, color_1.input)('localhost')}. To serve to your LAN, specify the ${(0, color_1.input)('--external')} option, which will use all network interfaces and print the external address(es) on which your app is being served.`;
  42. const runner = this.project && await this.project.getServeRunner();
  43. if (runner) {
  44. const libmetadata = await runner.getCommandMetadata();
  45. groups.push(...libmetadata.groups || []);
  46. options = lodash.uniqWith([...libmetadata.options || [], ...options], (optionA, optionB) => optionA.name === optionB.name);
  47. description += `\n\n${(libmetadata.description || '').trim()}`;
  48. footnotes.push(...libmetadata.footnotes || []);
  49. exampleCommands.push(...libmetadata.exampleCommands || []);
  50. }
  51. return {
  52. name: 'serve',
  53. type: 'project',
  54. summary: 'Start a local dev server for app dev/testing',
  55. description,
  56. footnotes,
  57. groups,
  58. exampleCommands,
  59. options,
  60. };
  61. }
  62. async preRun(inputs, options, { location }) {
  63. if (options['nolivereload']) {
  64. this.env.log.warn(`The ${(0, color_1.input)('--nolivereload')} option has been deprecated. Please use ${(0, color_1.input)('--no-livereload')}.`);
  65. options['livereload'] = false;
  66. }
  67. if (options['nobrowser']) {
  68. this.env.log.warn(`The ${(0, color_1.input)('--nobrowser')} option has been deprecated. Please use ${(0, color_1.input)('--no-open')}.`);
  69. options['open'] = false;
  70. }
  71. if (options['b']) {
  72. options['open'] = false;
  73. }
  74. if (options['noproxy']) {
  75. this.env.log.warn(`The ${(0, color_1.input)('--noproxy')} option has been deprecated. Please use ${(0, color_1.input)('--no-proxy')}.`);
  76. options['proxy'] = false;
  77. }
  78. if (options['x']) {
  79. options['proxy'] = false;
  80. }
  81. }
  82. async run(inputs, options, runinfo) {
  83. if (!this.project) {
  84. throw new errors_1.FatalException(`Cannot run ${(0, color_1.input)('ionic serve')} outside a project directory.`);
  85. }
  86. try {
  87. const runner = await this.project.requireServeRunner();
  88. const runnerOpts = runner.createOptionsFromCommandLine(inputs, options);
  89. await runner.run(runnerOpts);
  90. }
  91. catch (e) {
  92. if (e instanceof errors_1.RunnerException) {
  93. throw new errors_1.FatalException(e.message);
  94. }
  95. throw e;
  96. }
  97. await (0, utils_process_1.sleepForever)();
  98. }
  99. }
  100. exports.ServeCommand = ServeCommand;