serve.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. "use strict";
  2. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  3. if (k2 === undefined) k2 = k;
  4. var desc = Object.getOwnPropertyDescriptor(m, k);
  5. if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
  6. desc = { enumerable: true, get: function() { return m[k]; } };
  7. }
  8. Object.defineProperty(o, k2, desc);
  9. }) : (function(o, m, k, k2) {
  10. if (k2 === undefined) k2 = k;
  11. o[k2] = m[k];
  12. }));
  13. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  14. Object.defineProperty(o, "default", { enumerable: true, value: v });
  15. }) : function(o, v) {
  16. o["default"] = v;
  17. });
  18. var __importStar = (this && this.__importStar) || function (mod) {
  19. if (mod && mod.__esModule) return mod;
  20. var result = {};
  21. if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  22. __setModuleDefault(result, mod);
  23. return result;
  24. };
  25. Object.defineProperty(exports, "__esModule", { value: true });
  26. exports.ReactServeCLI = exports.ReactServeRunner = void 0;
  27. const utils_network_1 = require("@ionic/utils-network");
  28. const utils_terminal_1 = require("@ionic/utils-terminal");
  29. const color_1 = require("../../color");
  30. const serve_1 = require("../../serve");
  31. class ReactServeRunner extends serve_1.ServeRunner {
  32. constructor(e) {
  33. super();
  34. this.e = e;
  35. }
  36. async getCommandMetadata() {
  37. return {
  38. description: `
  39. This command will convert options to the environment variables used by React Scripts. See the ${(0, color_1.input)('create-react-app')} docs[^cra-build-docs] for explanations.
  40. `,
  41. footnotes: [
  42. {
  43. id: 'cra-build-docs',
  44. url: 'https://facebook.github.io/create-react-app/docs/advanced-configuration',
  45. },
  46. ],
  47. options: [
  48. {
  49. name: 'https',
  50. summary: 'Use HTTPS for the dev server',
  51. type: Boolean,
  52. groups: ['cordova'],
  53. hint: (0, color_1.weak)('[react-scripts]'),
  54. },
  55. {
  56. name: 'react-editor',
  57. summary: `Specify the editor that opens files upon crash`,
  58. type: String,
  59. spec: { value: 'editor' },
  60. groups: ['cordova'],
  61. hint: (0, color_1.weak)('[react-scripts]'),
  62. },
  63. {
  64. name: 'ci',
  65. summary: `Treat warnings as build failures, test runner does not watch`,
  66. type: Boolean,
  67. groups: ['cordova'],
  68. hint: (0, color_1.weak)('[react-scripts]'),
  69. },
  70. ],
  71. };
  72. }
  73. createOptionsFromCommandLine(inputs, options) {
  74. const baseOptions = super.createOptionsFromCommandLine(inputs, options);
  75. const ci = options['ci'] ? Boolean(options['ci']) : undefined;
  76. const https = options['https'] ? Boolean(options['https']) : undefined;
  77. const reactEditor = options['react-editor'] ? String(options['react-editor']) : undefined;
  78. return {
  79. ...baseOptions,
  80. ci,
  81. https,
  82. reactEditor,
  83. };
  84. }
  85. modifyOpenUrl(url, options) {
  86. return url;
  87. }
  88. async serveProject(options) {
  89. const [externalIP, availableInterfaces] = await this.selectExternalIP(options);
  90. const port = options.port = await (0, utils_network_1.findClosestOpenPort)(options.port);
  91. const reactScripts = new ReactServeCLI(this.e);
  92. await reactScripts.serve(options);
  93. return {
  94. custom: reactScripts.resolvedProgram !== reactScripts.program,
  95. protocol: options.https ? 'https' : 'http',
  96. localAddress: 'localhost',
  97. externalAddress: externalIP,
  98. externalNetworkInterfaces: availableInterfaces,
  99. port,
  100. externallyAccessible: ![serve_1.BIND_ALL_ADDRESS, ...serve_1.LOCAL_ADDRESSES].includes(externalIP),
  101. };
  102. }
  103. }
  104. exports.ReactServeRunner = ReactServeRunner;
  105. class ReactServeCLI extends serve_1.ServeCLI {
  106. constructor() {
  107. super(...arguments);
  108. this.name = 'React Scripts';
  109. this.pkg = 'react-scripts';
  110. this.program = 'react-scripts';
  111. this.prefix = 'react-scripts';
  112. this.script = serve_1.SERVE_SCRIPT;
  113. this.chunks = 0;
  114. }
  115. async serve(options) {
  116. this.on('compile', chunks => {
  117. if (chunks > 0) {
  118. this.e.log.info(`... and ${(0, color_1.strong)(chunks.toString())} additional chunks`);
  119. }
  120. });
  121. return super.serve(options);
  122. }
  123. stdoutFilter(line) {
  124. if (this.resolvedProgram !== this.program) {
  125. return super.stdoutFilter(line);
  126. }
  127. const strippedLine = (0, utils_terminal_1.stripAnsi)(line);
  128. const compileMsgs = ['Compiled successfully', 'Compiled with', 'Failed to compile'];
  129. if (compileMsgs.some(msg => strippedLine.includes(msg))) {
  130. this.emit('ready');
  131. return false;
  132. }
  133. if (strippedLine.match(/.*chunk\s{\d+}.+/)) {
  134. this.chunks++;
  135. return false;
  136. }
  137. if (strippedLine.includes('Compiled successfully')) {
  138. this.emit('compile', this.chunks);
  139. this.chunks = 0;
  140. }
  141. return true;
  142. }
  143. async buildArgs(options) {
  144. const { pkgManagerArgs } = await Promise.resolve().then(() => __importStar(require('../../utils/npm')));
  145. if (this.resolvedProgram === this.program) {
  146. return ['start'];
  147. }
  148. else {
  149. const [, ...pkgArgs] = await pkgManagerArgs(this.e.config.get('npmClient'), { command: 'run', script: this.script });
  150. return pkgArgs;
  151. }
  152. }
  153. async buildEnvVars(options) {
  154. const env = {};
  155. // Tell CRA not to open the dev server URL. We do this in Ionic CLI.
  156. env.BROWSER = 'none';
  157. // CRA binds to `localhost` by default, but if specified it prints a
  158. // warning, so don't set `HOST` if the host is set to `localhost`.
  159. if (options.host !== serve_1.DEFAULT_ADDRESS) {
  160. env.HOST = options.host;
  161. }
  162. env.PORT = String(options.port);
  163. env.HTTPS = options.https ? 'true' : 'false';
  164. if (options.ci) {
  165. env.CI = '1';
  166. }
  167. if (options.reactEditor) {
  168. env.REACT_EDITOR = options.reactEditor;
  169. }
  170. return { ...await super.buildEnvVars(options), ...env };
  171. }
  172. }
  173. exports.ReactServeCLI = ReactServeCLI;