-
Notifications
You must be signed in to change notification settings - Fork 47
/
Project.js
1076 lines (906 loc) · 33.9 KB
/
Project.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @flow
*/
import 'instapromise';
import bodyParser from 'body-parser';
import child_process from 'child_process';
import delayAsync from 'delay-async';
import express from 'express';
import freeportAsync from 'freeport-async';
import fs from 'fs';
import joi from 'joi';
import _ from 'lodash';
import ngrok from '@exponent/ngrok';
import os from 'os';
import path from 'path';
import request from 'request';
import spawnAsync from '@exponent/spawn-async';
import treekill from 'tree-kill';
import md5hex from 'md5hex';
import * as Analytics from './Analytics';
import * as Android from './Android';
import Api from './Api';
import Config from './Config';
import * as Doctor from './project/Doctor';
import ErrorCode from './ErrorCode';
import * as ExponentTools from './detach/ExponentTools';
import * as Exp from './Exp';
import * as ExpSchema from './project/ExpSchema';
import * as ProjectSettings from './ProjectSettings';
import * as ProjectUtils from './project/ProjectUtils';
import * as UrlUtils from './UrlUtils';
import UserManager from './User';
import UserSettings from './UserSettings';
import * as Watchman from './Watchman';
import XDLError from './XDLError';
import type { User as ExpUser } from './User'; //eslint-disable-line
const MINIMUM_BUNDLE_SIZE = 500;
type CachedSignedManifest = {
manifestString: ?string,
signedManifest: ?string,
};
let _cachedSignedManifest: CachedSignedManifest = {
manifestString: null,
signedManifest: null,
};
export type ProjectStatus = 'running' | 'ill' | 'exited';
export async function currentStatus(projectDir: string): Promise<ProjectStatus> {
const manifestUrl = await UrlUtils.constructManifestUrlAsync(projectDir, { urlType: 'http' });
const packagerUrl = await UrlUtils.constructBundleUrlAsync(projectDir, { urlType: 'http' });
let packagerRunning = false;
try {
const res = await request.promise(`${packagerUrl}/debug`);
if (res.statusCode < 400) {
packagerRunning = true;
}
} catch (e) { }
let manifestServerRunning = false;
try {
const res = await request.promise(manifestUrl);
if (res.statusCode < 400) {
manifestServerRunning = true;
}
} catch (e) { }
if (packagerRunning && manifestServerRunning) {
return 'running';
} else if (packagerRunning || manifestServerRunning) {
return 'ill';
} else {
return 'exited';
}
}
async function _assertValidProjectRoot(projectRoot) {
if (!projectRoot) {
throw new XDLError(ErrorCode.NO_PROJECT_ROOT, 'No project root specified');
}
}
async function _getFreePortAsync(rangeStart) {
let port = await freeportAsync(rangeStart);
if (!port) {
throw new XDLError(ErrorCode.NO_PORT_FOUND, 'No available port found');
}
return port;
}
async function _getForPlatformAsync(projectRoot, url, platform, { errorCode, minLength }) {
url = UrlUtils.getPlatformSpecificBundleUrl(url, platform);
let fullUrl = `${url}&platform=${platform}`;
let response = await request.promise.get({
url: fullUrl,
headers: {
'Exponent-Platform': platform,
},
});
if (response.statusCode !== 200) {
if (response.body) {
let body = JSON.parse(response.body);
if (body && body.message) {
ProjectUtils.logError(projectRoot, 'expo', body.message);
}
}
throw new XDLError(errorCode, `Packager url ${fullUrl} returned unexpected code ${response.statusCode}. Please open your project in the Expo app and see if there are any errors. Also scroll up and make sure there were no errors or warnings when opening your project.`);
}
if (!response.body || (minLength && response.body.length < minLength)) {
throw new XDLError(errorCode, `Body is: ${response.body}`);
}
return response.body;
}
async function _resolveManifestAssets(projectRoot, manifest, resolver) {
try {
// Asset fields that the user has set
const assetSchemas = (await ExpSchema.getAssetSchemasAsync(manifest.sdkVersion))
.filter(({ fieldPath }) => _.get(manifest, fieldPath));
// Get the URLs
const urls = await Promise.all(assetSchemas.map(async ({ fieldPath }) => {
const pathOrURL = _.get(manifest, fieldPath);
if (fs.existsSync(path.resolve(projectRoot, pathOrURL))) {
return await resolver(pathOrURL);
} else {
return pathOrURL; // Assume already a URL
}
}));
// Set the corresponding URL fields
assetSchemas.forEach(({ fieldPath }, index) =>
_.set(manifest, fieldPath + 'Url', urls[index]));
} catch (e) {
ProjectUtils.logWarning(projectRoot, 'expo', `Warning: Unable to resolve manifest assets. Icons might not work. ${e.message}.`);
}
}
export async function publishAsync(projectRoot: string, options: Object = {}) {
const user = await UserManager.ensureLoggedInAsync();
_assertValidProjectRoot(projectRoot);
Analytics.logEvent('Publish', {
projectRoot,
});
let schema = joi.object().keys({
// empty
});
try {
await joi.promise.validate(options, schema);
} catch (e) {
throw new XDLError(ErrorCode.INVALID_OPTIONS, e.toString());
}
let packagerInfo = await ProjectSettings.readPackagerInfoAsync(projectRoot);
if (!packagerInfo.packagerPort) {
throw new XDLError(ErrorCode.NO_PACKAGER_PORT, `No packager found for project at ${projectRoot}.`);
}
let { exp, pkg } = await ProjectUtils.readConfigJsonAsync(projectRoot);
if (!exp || !pkg) {
const configName = await ProjectUtils.configFilenameAsync(projectRoot);
throw new XDLError(ErrorCode.NO_PACKAGE_JSON, `Couldn't read ${configName} file in project at ${projectRoot}`);
}
// Support version and name being specified in package.json for legacy
// support pre: exp.json
if (!exp.version && pkg.version) {
exp.version = pkg.version;
}
if (!exp.slug && pkg.name) {
exp.slug = pkg.name;
}
if (exp.android && exp.android.config) {
delete exp.android.config;
}
if (exp.ios && exp.ios.config) {
delete exp.ios.config;
}
// Only allow test-suite to be published with UNVERSIONED
if (exp.sdkVersion === 'UNVERSIONED' && !exp.slug.includes('test-suite')) {
throw new XDLError(ErrorCode.INVALID_OPTIONS, 'Cannot publish with sdkVersion UNVERSIONED.');
}
let entryPoint = await Exp.determineEntryPointAsync(projectRoot);
let publishUrl = await UrlUtils.constructPublishUrlAsync(projectRoot, entryPoint);
let assetsUrl = await UrlUtils.constructAssetsUrlAsync(projectRoot, entryPoint);
let [
iosBundle,
androidBundle,
iosAssetsJson,
androidAssetsJson,
] = await Promise.all([
_getForPlatformAsync(projectRoot, publishUrl, 'ios', {
errorCode: ErrorCode.INVALID_BUNDLE,
minLength: MINIMUM_BUNDLE_SIZE,
}),
_getForPlatformAsync(projectRoot, publishUrl, 'android', {
errorCode: ErrorCode.INVALID_BUNDLE,
minLength: MINIMUM_BUNDLE_SIZE,
}),
_getForPlatformAsync(projectRoot, assetsUrl, 'ios', {
errorCode: ErrorCode.INVALID_ASSETS,
}),
_getForPlatformAsync(projectRoot, assetsUrl, 'android', {
errorCode: ErrorCode.INVALID_ASSETS,
}),
]);
// Resolve manifest assets to their S3 URL and add them to the list of assets to
// be uploaded
const manifestAssets = [];
await _resolveManifestAssets(projectRoot, exp, async (assetPath) => {
const absolutePath = path.resolve(projectRoot, assetPath);
const contents = await fs.promise.readFile(absolutePath);
const hash = md5hex(contents);
manifestAssets.push({ files: [absolutePath], fileHashes: [hash] });
return 'https://d1wp6m56sqw74a.cloudfront.net/~assets/' + hash;
});
// Upload asset files
const iosAssets = JSON.parse(iosAssetsJson);
const androidAssets = JSON.parse(androidAssetsJson);
const assets = iosAssets.concat(androidAssets).concat(manifestAssets);
if (assets.length > 0 && assets[0].fileHashes) {
await uploadAssetsAsync(projectRoot, assets);
}
let formData = {
expJson: JSON.stringify(exp),
iosBundle: {
value: iosBundle,
options: {
filename: 'iosBundle',
},
},
androidBundle: {
value: androidBundle,
options: {
filename: 'androidBundle',
},
},
};
let response = await Api.callMethodAsync('publish', [options], 'put', null, {formData});
if (exp.android && exp.android.publishBundlePath) {
await fs.promise.writeFile(path.resolve(projectRoot, exp.android.publishBundlePath), androidBundle);
}
if (exp.ios && exp.ios.publishBundlePath) {
await fs.promise.writeFile(path.resolve(projectRoot, exp.ios.publishBundlePath), iosBundle);
}
if (exp.isKernel) {
let kernelBundleUrl = `${Config.api.scheme}://${Config.api.host}`;
if (Config.api.port) {
kernelBundleUrl = `${kernelBundleUrl}:${Config.api.port}`;
}
kernelBundleUrl = `${kernelBundleUrl}/@${user.username}/${exp.slug}/bundle`;
if (exp.kernel.androidManifestPath) {
let manifest = await ExponentTools.getManifestAsync(response.url, {
'Exponent-SDK-Version': exp.sdkVersion,
'Exponent-Platform': 'android',
});
manifest.bundleUrl = kernelBundleUrl;
manifest.sdkVersion = 'UNVERSIONED';
await fs.promise.writeFile(path.resolve(projectRoot, exp.kernel.androidManifestPath), JSON.stringify(manifest));
}
if (exp.kernel.iosManifestPath) {
let manifest = await ExponentTools.getManifestAsync(response.url, {
'Exponent-SDK-Version': exp.sdkVersion,
'Exponent-Platform': 'ios',
});
manifest.bundleUrl = kernelBundleUrl;
manifest.sdkVersion = 'UNVERSIONED';
await fs.promise.writeFile(path.resolve(projectRoot, exp.kernel.iosManifestPath), JSON.stringify(manifest));
}
}
return response;
}
// TODO(jesse): Add analytics for upload
async function uploadAssetsAsync(projectRoot, assets) {
// Collect paths by key, also effectively handles duplicates in the array
const paths = {};
assets.forEach(asset => {
asset.files.forEach((path, index) => {
paths[asset.fileHashes[index]] = path;
});
});
// Collect list of assets missing on host
const metas = (await Api.callMethodAsync('assetsMetadata', [], 'post', {
keys: Object.keys(paths),
})).metadata;
const missing = Object.keys(paths).filter(key => !metas[key].exists);
// Upload them!
await Promise.all(_.chunk(missing, 5).map(async (keys) => {
let formData = {};
keys.forEach(key => {
ProjectUtils.logDebug(projectRoot, 'expo', `uploading ${paths[key]}`);
formData[key] = {
value: fs.createReadStream(paths[key]),
options: {
filename: paths[key],
},
};
});
await Api.callMethodAsync('uploadAssets', [], 'put', null, {formData});
}));
}
export async function buildAsync(projectRoot: string, options: {
current?: bool,
mode?: string,
platform?: string,
expIds?: Array<string>,
} = {}) {
await UserManager.ensureLoggedInAsync();
_assertValidProjectRoot(projectRoot);
Analytics.logEvent('Build Shell App', {
projectRoot,
});
let schema = joi.object().keys({
current: joi.boolean(),
mode: joi.string(),
platform: joi.any().valid('ios', 'android', 'all'),
expIds: joi.array(),
});
try {
await joi.promise.validate(options, schema);
} catch (e) {
throw new XDLError(ErrorCode.INVALID_OPTIONS, e.toString());
}
let { exp, pkg } = await ProjectUtils.readConfigJsonAsync(projectRoot);
const configName = await ProjectUtils.configFilenameAsync(projectRoot);
const configPrefix = (configName === 'app.json') ? 'expo.' : '';
if (!exp || !pkg) {
throw new XDLError(ErrorCode.NO_PACKAGE_JSON, `Couldn't read ${configName} file in project at ${projectRoot}`);
}
// Support version and name being specified in package.json for legacy
// support pre: exp.json
if (!exp.version && pkg.version) {
exp.version = pkg.version;
}
if (!exp.slug && pkg.name) {
exp.slug = pkg.name;
}
if (options.platform === 'ios' || options.platform === 'all') {
if (!exp.ios || !exp.ios.bundleIdentifier) {
throw new XDLError(ErrorCode.INVALID_MANIFEST, `Must specify a bundle identifier in order to build this experience for iOS. Please specify one in ${configName} at "${configPrefix}ios.bundleIdentifier"`);
}
}
if (options.platform === 'android' || options.platform === 'all') {
if (!exp.android || !exp.android.package) {
throw new XDLError(ErrorCode.INVALID_MANIFEST, `Must specify a java package in order to build this experience for Android. Please specify one in ${configName} at "${configPrefix}android.package"`);
}
}
let response = await Api.callMethodAsync(
'build',
[],
'put',
{
manifest: exp,
options,
},
);
return response;
}
async function _waitForRunningAsync(url) {
try {
let response = await request.promise(url);
// Looking for "Cached Bundles" string is hacky, but unfortunately
// ngrok returns a 200 when it succeeds but the port it's proxying
// isn't bound.
if (response.statusCode >= 200 && response.statusCode < 300 &&
response.body && response.body.includes('Cached Bundles')) {
return true;
}
} catch (e) {
// Try again after delay
}
await delayAsync(100);
return _waitForRunningAsync(url);
}
function _stripPackagerOutputBox(output: string) {
let re = /Running packager on port (\d+)/;
let found = output.match(re);
if (found && found.length >= 2) {
return `Running packager on port ${found[1]}\n`;
} else {
return null;
}
}
function _processPackagerLine(line: string) {
// [10:02:59 AM]
let timestampRe = /\s*\[\d+\:\d+\:\d+\ (AM)?(PM)?\]\s+/;
// [11/8/2016, 10:02:59 AM]
let sdk11AndUpTimestampRe = /\s*\[\d+\/\d+\/\d+, \d+\:\d+\:\d+\ (AM)?(PM)?\]\s+/;
return line.replace(timestampRe, '').replace(sdk11AndUpTimestampRe, '');
}
async function _restartWatchmanAsync(projectRoot: string) {
try {
let result = await spawnAsync('watchman', ['watch-del', projectRoot]);
await spawnAsync('watchman', ['watch-project', projectRoot]);
if (result.stdout.includes('root')) {
ProjectUtils.logInfo(projectRoot, 'expo', 'Restarted watchman.');
return;
}
} catch (e) {}
ProjectUtils.logError(projectRoot, 'expo', 'Attempted to restart watchman but failed. Please try running `watchman watch-del-all`.');
}
function _logPackagerOutput(projectRoot: string, level: string, data: Object) {
let output = data.toString();
if (output.includes('─────')) {
output = _stripPackagerOutputBox(output);
if (output) {
ProjectUtils.logInfo(projectRoot, 'expo', output);
}
return;
}
if (!output) {
return;
}
// Fix watchman if it's being dumb
if (Watchman.isPlatformSupported() && output.includes('watchman watch-del')) {
_restartWatchmanAsync(projectRoot);
return;
}
let lines = output.split(/\r?\n/);
for (let i = 0; i < lines.length; i++) {
lines[i] = _processPackagerLine(lines[i]);
}
output = lines.join('\n');
if (level === 'info') {
ProjectUtils.logInfo(projectRoot, 'packager', output);
} else {
ProjectUtils.logError(projectRoot, 'packager', output);
}
}
function _handleDeviceLogs(projectRoot: string, deviceId: string, deviceName: string, logs: any) {
for (let i = 0; i < logs.length; i++) {
let log = logs[i];
let body = typeof log.body === 'string' ? [log.body] : log.body;
let string = body.map(obj => {
if (typeof obj === 'undefined') {
return 'undefined';
}
if (obj === 'null') {
return 'null';
}
if (typeof obj === 'string' || typeof obj === 'number' || typeof obj === 'boolean') {
return obj;
}
try {
return JSON.stringify(obj);
} catch (e) {
return obj.toString();
}
}).join(' ');
let level = log.level;
let groupDepth = log.groupDepth;
let shouldHide = log.shouldHide;
ProjectUtils.logWithLevel(projectRoot, level, {
tag: 'device',
deviceId,
deviceName,
groupDepth,
shouldHide,
}, string);
}
}
export async function startReactNativeServerAsync(projectRoot: string, options: Object = {}, verbose: boolean = true) {
await UserManager.ensureLoggedInAsync();
_assertValidProjectRoot(projectRoot);
await stopReactNativeServerAsync(projectRoot);
await Watchman.addToPathAsync();
// Attempt to fix watchman if it's hanging
await Watchman.unblockAndGetVersionAsync(projectRoot);
let packagerPort = await _getFreePortAsync(19001);
let { exp } = await ProjectUtils.readConfigJsonAsync(projectRoot);
// Create packager options
let packagerOpts = {
port: packagerPort,
projectRoots: projectRoot,
};
const userPackagerOpts = _.get(exp, 'packagerOpts');
if (userPackagerOpts) {
packagerOpts = {
...packagerOpts,
...userPackagerOpts,
};
}
let cliOpts = _.reduce(packagerOpts, (opts, val, key) => {
if (val && val !== '') {
opts.push(`--${key}`, val);
}
return opts;
}, ['start']);
if (options.reset) {
cliOpts.push('--reset-cache');
}
// Get custom CLI path from project package.json, but fall back to node_module path
let defaultCliPath = path.join(projectRoot, 'node_modules', 'react-native', 'local-cli', 'cli.js');
const cliPath = _.get(exp, 'rnCliPath', defaultCliPath);
let nodePath;
// When using a custom path for the RN CLI, we want it to use the project
// root to look up config files and Node modules
if (exp.rnCliPath) {
nodePath = _nodePathForProjectRoot(projectRoot);
} else {
nodePath = null;
}
ProjectUtils.logInfo(projectRoot, 'expo', 'Starting React Native packager...');
// Run the copy of Node that's embedded in Electron by setting the
// ELECTRON_RUN_AS_NODE environment variable
// Note: the CLI script sets up graceful-fs and sets ulimit to 4096 in the
// child process
let packagerProcess = child_process.fork(cliPath, cliOpts, {
cwd: projectRoot,
env: {
...process.env,
NODE_PATH: nodePath,
ELECTRON_RUN_AS_NODE: 1,
},
silent: true,
});
await ProjectSettings.setPackagerInfoAsync(projectRoot, {
packagerPort,
packagerPid: packagerProcess.pid,
});
// TODO: do we need this? don't know if it's ever called
process.on('exit', () => {
treekill(packagerProcess.pid);
});
packagerProcess.stdout.setEncoding('utf8');
packagerProcess.stderr.setEncoding('utf8');
packagerProcess.stdout.on('data', (data) => {
if (verbose) {
_logPackagerOutput(projectRoot, 'info', data);
}
});
packagerProcess.stderr.on('data', (data) => {
if (verbose) {
_logPackagerOutput(projectRoot, 'error', data);
}
});
packagerProcess.on('exit', async (code) => {
ProjectUtils.logDebug(projectRoot, 'expo', `packager process exited with code ${code}`);
});
let packagerUrl = await UrlUtils.constructBundleUrlAsync(projectRoot, {
urlType: 'http',
hostType: 'localhost',
});
await _waitForRunningAsync(`${packagerUrl}/debug`);
}
// Simulate the node_modules resolution
// If you project dir is /Jesse/Expo/Universe/BubbleBounce, returns
// "/Jesse/node_modules:/Jesse/Expo/node_modules:/Jesse/Expo/Universe/node_modules:/Jesse/Expo/Universe/BubbleBounce/node_modules"
function _nodePathForProjectRoot(projectRoot: string): string {
let paths = [];
let directory = path.resolve(projectRoot);
while (true) {
paths.push(path.join(directory, 'node_modules'));
let parentDirectory = path.dirname(directory);
if (directory === parentDirectory) {
break;
}
directory = parentDirectory;
}
return paths.join(path.delimiter);
}
export async function stopReactNativeServerAsync(projectRoot: string) {
await UserManager.ensureLoggedInAsync();
_assertValidProjectRoot(projectRoot);
let packagerInfo = await ProjectSettings.readPackagerInfoAsync(projectRoot);
if (!packagerInfo.packagerPort || !packagerInfo.packagerPid) {
ProjectUtils.logDebug(projectRoot, 'expo', `No packager found for project at ${projectRoot}.`);
return;
}
ProjectUtils.logDebug(projectRoot, 'expo', `Killing packager process tree: ${packagerInfo.packagerPid}`);
try {
await treekill.promise(packagerInfo.packagerPid, 'SIGKILL');
} catch (e) {
ProjectUtils.logDebug(projectRoot, 'expo', `Error stopping packager process: ${e.toString()}`);
}
await ProjectSettings.setPackagerInfoAsync(projectRoot, {
packagerPort: null,
packagerPid: null,
});
}
export async function startExpoServerAsync(projectRoot: string) {
await UserManager.ensureLoggedInAsync();
_assertValidProjectRoot(projectRoot);
await stopExpoServerAsync(projectRoot);
let app = express();
app.use(bodyParser.json({ limit: '10mb' }));
app.use(bodyParser.urlencoded({ limit: '10mb', extended: true }));
if (await Doctor.validateWithNetworkAsync(projectRoot) === Doctor.FATAL) {
throw new Error(`Couldn't start project. Please fix the errors and restart the project.`);
}
// Serve the manifest.
let manifestHandler = async (req, res) => {
try {
// We intentionally don't `await`. We want to continue trying even
// if there is a potential error in the package.json and don't want to slow
// down the request
Doctor.validateWithNetworkAsync(projectRoot);
let { exp: manifest } = await ProjectUtils.readConfigJsonAsync(projectRoot);
if (!manifest) {
const configName = await ProjectUtils.configFilenameAsync(projectRoot);
throw new Error(`No ${configName} file found`);
}
// Get packager opts and then copy into bundleUrlPackagerOpts
let packagerOpts = await ProjectSettings.getPackagerOptsAsync(projectRoot);
let bundleUrlPackagerOpts = JSON.parse(JSON.stringify(packagerOpts));
bundleUrlPackagerOpts.urlType = 'http';
if (bundleUrlPackagerOpts.hostType === 'redirect') {
bundleUrlPackagerOpts.hostType = 'tunnel';
}
manifest.xde = true; // deprecated
manifest.developer = {
tool: Config.developerTool,
};
manifest.packagerOpts = packagerOpts;
let entryPoint = await Exp.determineEntryPointAsync(projectRoot);
let platform = req.headers['exponent-platform'] || 'ios';
entryPoint = UrlUtils.getPlatformSpecificBundleUrl(entryPoint, platform);
let mainModuleName = UrlUtils.guessMainModulePath(entryPoint);
let queryParams = await UrlUtils.constructBundleQueryParamsAsync(projectRoot, packagerOpts);
let path = `/${mainModuleName}.bundle?platform=${platform}&${queryParams}`;
manifest.bundleUrl = await UrlUtils.constructBundleUrlAsync(projectRoot, bundleUrlPackagerOpts) + path;
manifest.debuggerHost = await UrlUtils.constructDebuggerHostAsync(projectRoot);
manifest.mainModuleName = mainModuleName;
manifest.logUrl = `${await UrlUtils.constructManifestUrlAsync(projectRoot, {
urlType: 'http',
})}/logs`;
// Resolve manifest assets to their packager URL
await _resolveManifestAssets(projectRoot, manifest, async (path) =>
manifest.bundleUrl.match(/^https?:\/\/.*?\//)[0] + 'assets/' + path);
// the server normally inserts this but if we're offline we'll do it here
const hostUUID = await UserSettings.anonymousIdentifier();
if (Config.offline) {
manifest.id = `@anonymous/${manifest.slug}-${hostUUID}`;
}
let manifestString = JSON.stringify(manifest);
let currentUser;
if (!Config.offline) {
currentUser = await UserManager.getCurrentUserAsync();
}
if (req.headers['exponent-accept-signature'] && (currentUser || Config.offline)) {
if (_cachedSignedManifest.manifestString === manifestString) {
manifestString = _cachedSignedManifest.signedManifest;
} else {
if (Config.offline) {
const unsignedManifest = {
manifestString,
signature: 'UNSIGNED',
};
_cachedSignedManifest.manifestString = manifestString;
manifestString = JSON.stringify(unsignedManifest);
_cachedSignedManifest.signedManifest = manifestString;
} else {
let publishInfo = await Exp.getPublishInfoAsync(projectRoot);
let signedManifest = await Api.callMethodAsync('signManifest', [publishInfo.args], 'post', manifest);
_cachedSignedManifest.manifestString = manifestString;
_cachedSignedManifest.signedManifest = signedManifest.response;
manifestString = signedManifest.response;
}
}
}
const hostInfo = {
host: hostUUID,
server: 'xdl',
serverVersion: require('../package.json').version,
serverDriver: Config.developerTool,
serverOS: os.platform(),
serverOSVersion: os.release(),
};
res.append('Exponent-Server', JSON.stringify(hostInfo));
res.send(manifestString);
Analytics.logEvent('Serve Manifest', {
projectRoot,
});
} catch (e) {
ProjectUtils.logDebug(projectRoot, 'expo', `Error in manifestHandler: ${e} ${e.stack}`);
// 5xx = Server Error HTTP code
res.status(520).send({"error": e.toString()});
}
};
app.get('/', manifestHandler);
app.get('/manifest', manifestHandler);
app.get('/index.exp', manifestHandler);
app.post('/logs', async (req, res) => {
try {
let deviceId = req.get('Device-Id');
let deviceName = req.get('Device-Name');
if (deviceId && deviceName && req.body) {
_handleDeviceLogs(projectRoot, deviceId, deviceName, req.body);
}
} catch (e) {
ProjectUtils.logError(projectRoot, 'expo', `Error getting device logs: ${e} ${e.stack}`);
}
res.send('Success');
});
app.post('/shutdown', async(req, res) => {
server.close();
res.send('Success');
});
let expRc = await ProjectUtils.readExpRcAsync(projectRoot);
let expoServerPort = expRc.manifestPort ? expRc.manifestPort : await _getFreePortAsync(19000);
await ProjectSettings.setPackagerInfoAsync(projectRoot, {
expoServerPort,
});
let server = app.listen(expoServerPort, () => {
let host = server.address().address;
let port = server.address().port;
ProjectUtils.logDebug(projectRoot, 'expo', `Local server listening at http://${host}:${port}`);
});
await Exp.saveRecentExpRootAsync(projectRoot);
}
export async function stopExpoServerAsync(projectRoot: string) {
await UserManager.ensureLoggedInAsync();
_assertValidProjectRoot(projectRoot);
let packagerInfo = await ProjectSettings.readPackagerInfoAsync(projectRoot);
if (packagerInfo && packagerInfo.expoServerPort) {
try {
await request.post.promise(`http://localhost:${packagerInfo.expoServerPort}/shutdown`);
} catch (e) {}
}
await ProjectSettings.setPackagerInfoAsync(projectRoot, {
expoServerPort: null,
});
}
async function _connectToNgrokAsync(projectRoot: string, args: mixed, hostnameAsync: Function, ngrokPid: ?number, attempts: number = 0) {
try {
let configPath = path.join(UserSettings.dotExpoHomeDirectory(), 'ngrok.yml');
let hostname = await hostnameAsync();
let url = await ngrok.promise.connect({
hostname,
configPath,
...args,
});
return url;
} catch (e) {
// Attempt to connect 3 times
if (attempts >= 2) {
if (e.message) {
throw new XDLError(ErrorCode.NGROK_ERROR, e.toString());
} else {
throw new XDLError(ErrorCode.NGROK_ERROR, JSON.stringify(e));
}
}
if (!attempts) {
attempts = 0;
}
// Attempt to fix the issue
if (e.error_code && e.error_code === 103) {
if (attempts === 0) {
// Failed to start tunnel. Might be because url already bound to another session.
if (ngrokPid) {
try {
process.kill(ngrokPid, 'SIGKILL');
} catch (e) {
ProjectUtils.logDebug(projectRoot, 'expo', `Couldn't kill ngrok with PID ${ngrokPid}`);
}
} else {
await ngrok.promise.kill();
}
} else {
// Change randomness to avoid conflict if killing ngrok didn't help
await Exp.resetProjectRandomnessAsync(projectRoot);
}
}
// Wait 100ms and then try again
await delayAsync(100);
return _connectToNgrokAsync(projectRoot, args, hostnameAsync, null, attempts + 1);
}
}
export async function startTunnelsAsync(projectRoot: string) {
const user = await UserManager.ensureLoggedInAsync();
if (!user) {
throw new Error("Internal error -- tunnel started in offline mode.");
}
_assertValidProjectRoot(projectRoot);
let packagerInfo = await ProjectSettings.readPackagerInfoAsync(projectRoot);
if (!packagerInfo.packagerPort) {
throw new XDLError(ErrorCode.NO_PACKAGER_PORT, `No packager found for project at ${projectRoot}.`);
}
if (!packagerInfo.expoServerPort) {
throw new XDLError(ErrorCode.NO_EXPO_SERVER_PORT, `No Expo server found for project at ${projectRoot}.`);
}
await stopTunnelsAsync(projectRoot);
if (await Android.startAdbReverseAsync(projectRoot)) {
ProjectUtils.logInfo(projectRoot, 'expo', 'Sucessfully ran `adb reverse`. Localhost urls should work on the connected Android device.', 'project-adb-reverse');
} else {
ProjectUtils.clearNotification(projectRoot, 'project-adb-reverse');
}
const { username } = user;
let packageShortName = path.parse(projectRoot).base;
let expRc = await ProjectUtils.readExpRcAsync(projectRoot);
try {
let expoServerNgrokUrl = await _connectToNgrokAsync(projectRoot, {
authtoken: Config.ngrok.authToken,
port: packagerInfo.expoServerPort,
proto: 'http',
}, async () => {
let randomness = expRc.manifestTunnelRandomness ? expRc.manifestTunnelRandomness : await Exp.getProjectRandomnessAsync(projectRoot);
return [randomness, UrlUtils.domainify(username), UrlUtils.domainify(packageShortName), Config.ngrok.domain].join('.');
}, packagerInfo.ngrokPid);
let packagerNgrokUrl = await _connectToNgrokAsync(projectRoot, {
authtoken: Config.ngrok.authToken,
port: packagerInfo.packagerPort,
proto: 'http',
}, async () => {
let randomness = expRc.manifestTunnelRandomness ? expRc.manifestTunnelRandomness : await Exp.getProjectRandomnessAsync(projectRoot);
return ['packager', randomness, UrlUtils.domainify(username), UrlUtils.domainify(packageShortName), Config.ngrok.domain].join('.');
}, packagerInfo.ngrokPid);
await ProjectSettings.setPackagerInfoAsync(projectRoot, {
expoServerNgrokUrl,
packagerNgrokUrl,
ngrokPid: ngrok.process().pid,
});
} catch (e) {
ProjectUtils.logError(projectRoot, 'expo', `Error starting tunnel: ${e.toString()}`);
throw e;
}
}
export async function stopTunnelsAsync(projectRoot: string) {
await UserManager.ensureLoggedInAsync();
_assertValidProjectRoot(projectRoot);
// This will kill all ngrok tunnels in the process.
// We'll need to change this if we ever support more than one project
// open at a time in XDE.
let packagerInfo = await ProjectSettings.readPackagerInfoAsync(projectRoot);
let ngrokProcess = ngrok.process();
let ngrokProcessPid = ngrokProcess ? ngrokProcess.pid : null;
if (packagerInfo.ngrokPid && packagerInfo.ngrokPid !== ngrokProcessPid) {
// Ngrok is running in some other process. Kill at the os level.
try {
process.kill(packagerInfo.ngrokPid);
} catch (e) {
ProjectUtils.logDebug(projectRoot, 'expo', `Couldn't kill ngrok with PID ${packagerInfo.ngrokPid}`);
}
} else {
// Ngrok is running from the current process. Kill using ngrok api.
await ngrok.promise.kill();
}
await ProjectSettings.setPackagerInfoAsync(projectRoot, {
expoServerNgrokUrl: null,
packagerNgrokUrl: null,
ngrokPid: null,
});
await Android.stopAdbReverseAsync(projectRoot);
}
export async function setOptionsAsync(projectRoot: string, options: { packagerPort?: number }) {
await UserManager.ensureLoggedInAsync();
_assertValidProjectRoot(projectRoot);
// Check to make sure all options are valid
let schema = joi.object().keys({
packagerPort: joi.number().integer(),
});
try {
await joi.promise.validate(options, schema);
} catch (e) {
throw new XDLError(ErrorCode.INVALID_OPTIONS, e.toString());
}
await ProjectSettings.setPackagerInfoAsync(projectRoot, options);
}