1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.startExpoServerAsync = startExpoServerAsync;
|
7 | exports.stopExpoServerAsync = stopExpoServerAsync;
|
8 | function _config() {
|
9 | const data = require("@expo/config");
|
10 | _config = function () {
|
11 | return data;
|
12 | };
|
13 | return data;
|
14 | }
|
15 | function _axios() {
|
16 | const data = _interopRequireDefault(require("axios"));
|
17 | _axios = function () {
|
18 | return data;
|
19 | };
|
20 | return data;
|
21 | }
|
22 | function _express() {
|
23 | const data = _interopRequireDefault(require("express"));
|
24 | _express = function () {
|
25 | return data;
|
26 | };
|
27 | return data;
|
28 | }
|
29 | function _internal() {
|
30 | const data = require("../internal");
|
31 | _internal = function () {
|
32 | return data;
|
33 | };
|
34 | return data;
|
35 | }
|
36 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
37 | function _isIgnorableBugReportingExtraData(body) {
|
38 | return body.length === 2 && body[0] === 'BugReporting extraData:';
|
39 | }
|
40 | function _isAppRegistryStartupMessage(body) {
|
41 | return body.length === 1 && (/^Running application "main" with appParams:/.test(body[0]) || /^Running "main" with \{/.test(body[0]));
|
42 | }
|
43 | function _handleDeviceLogs(projectRoot, deviceId, deviceName, logs) {
|
44 | for (let i = 0; i < logs.length; i++) {
|
45 | const log = logs[i];
|
46 | let body = typeof log.body === 'string' ? [log.body] : log.body;
|
47 | let {
|
48 | level
|
49 | } = log;
|
50 | if (_isIgnorableBugReportingExtraData(body)) {
|
51 | level = 'debug';
|
52 | }
|
53 | if (_isAppRegistryStartupMessage(body)) {
|
54 | body = [`Running application on ${deviceName}.`];
|
55 | }
|
56 | const args = body.map(obj => {
|
57 | if (typeof obj === 'undefined') {
|
58 | return 'undefined';
|
59 | }
|
60 | if (obj === 'null') {
|
61 | return 'null';
|
62 | }
|
63 | if (typeof obj === 'string' || typeof obj === 'number' || typeof obj === 'boolean') {
|
64 | return obj;
|
65 | }
|
66 | try {
|
67 | return JSON.stringify(obj);
|
68 | } catch {
|
69 | return obj.toString();
|
70 | }
|
71 | });
|
72 | const logLevel = level === 'info' || level === 'warn' || level === 'error' || level === 'debug' ? level : 'info';
|
73 | _internal().ProjectUtils.getLogger(projectRoot)[logLevel]({
|
74 | tag: 'device',
|
75 | deviceId,
|
76 | deviceName,
|
77 | groupDepth: log.groupDepth,
|
78 | shouldHide: log.shouldHide,
|
79 | includesStack: log.includesStack
|
80 | }, ...args);
|
81 | }
|
82 | }
|
83 | async function startExpoServerAsync(projectRoot) {
|
84 | (0, _internal().assertValidProjectRoot)(projectRoot);
|
85 | await stopExpoServerAsync(projectRoot);
|
86 | const app = (0, _express().default)();
|
87 | app.use(_express().default.json({
|
88 | limit: '10mb'
|
89 | }));
|
90 | app.use(_express().default.urlencoded({
|
91 | limit: '10mb',
|
92 | extended: true
|
93 | }));
|
94 | if ((_internal().ConnectionStatus.isOffline() ? await _internal().Doctor.validateWithoutNetworkAsync(projectRoot) : await _internal().Doctor.validateWithNetworkAsync(projectRoot)) === _internal().Doctor.FATAL) {
|
95 | throw new Error(`Couldn't start project. Please fix the errors and restart the project.`);
|
96 | }
|
97 |
|
98 | const manifestHandler = _internal().ManifestHandler.getManifestHandler(projectRoot);
|
99 | const loadingHandler = _internal().LoadingPageHandler.getLoadingPageHandler(projectRoot);
|
100 | app.get('/', manifestHandler);
|
101 | app.get('/manifest', manifestHandler);
|
102 | app.get('/index.exp', manifestHandler);
|
103 | app.get(_internal().LoadingPageHandler.LoadingEndpoint, loadingHandler);
|
104 | app.get(_internal().LoadingPageHandler.DeepLinkEndpoint, loadingHandler);
|
105 | app.post('/logs', async (req, res) => {
|
106 | try {
|
107 | const deviceId = req.get('Device-Id');
|
108 | const deviceName = req.get('Device-Name');
|
109 | if (deviceId && deviceName && req.body) {
|
110 | _handleDeviceLogs(projectRoot, deviceId, deviceName, req.body);
|
111 | }
|
112 | } catch (e) {
|
113 | _internal().ProjectUtils.logError(projectRoot, 'expo', `Error getting device logs: ${e} ${e.stack}`);
|
114 | }
|
115 | res.send('Success');
|
116 | });
|
117 | app.post('/shutdown', async (req, res) => {
|
118 | server.close();
|
119 | res.send('Success');
|
120 | });
|
121 | const expRc = await (0, _config().readExpRcAsync)(projectRoot);
|
122 | const expoServerPort = expRc.manifestPort ? expRc.manifestPort : await (0, _internal().getFreePortAsync)(19000);
|
123 | await _internal().ProjectSettings.setPackagerInfoAsync(projectRoot, {
|
124 | expoServerPort
|
125 | });
|
126 | let server = app.listen(expoServerPort, () => {
|
127 | const info = server.address();
|
128 | const host = info.address;
|
129 | const port = info.port;
|
130 | _internal().ProjectUtils.logDebug(projectRoot, 'expo', `Local server listening at http://${host}:${port}`);
|
131 | });
|
132 | }
|
133 | async function stopExpoServerAsync(projectRoot) {
|
134 | (0, _internal().assertValidProjectRoot)(projectRoot);
|
135 | const packagerInfo = await _internal().ProjectSettings.readPackagerInfoAsync(projectRoot);
|
136 | if (packagerInfo && packagerInfo.expoServerPort) {
|
137 | try {
|
138 | await _axios().default.request({
|
139 | method: 'post',
|
140 | url: `http://127.0.0.1:${packagerInfo.expoServerPort}/shutdown`
|
141 | });
|
142 | } catch {}
|
143 | }
|
144 | await _internal().ProjectSettings.setPackagerInfoAsync(projectRoot, {
|
145 | expoServerPort: null
|
146 | });
|
147 | }
|
148 |
|
\ | No newline at end of file |