forked from expo/expo-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
urlOpts.ts
111 lines (97 loc) · 2.98 KB
/
urlOpts.ts
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
import { Android, ConnectionStatus, ProjectSettings, Simulator, Webpack } from '@expo/xdl';
import { Command } from 'commander';
import indentString from 'indent-string';
import qrcodeTerminal from 'qrcode-terminal';
import CommandError from './CommandError';
import log from './log';
export type URLOptions = {
android?: boolean;
ios?: boolean;
web?: boolean;
host?: 'lan' | 'tunnel' | 'localhost';
tunnel?: boolean;
lan?: boolean;
localhost?: boolean;
};
function addOptions(program: Command) {
program
.option('-a, --android', 'Opens your app in Expo client on a connected Android device')
.option(
'-i, --ios',
'Opens your app in Expo client in a currently running iOS simulator on your computer'
)
.option('-w, --web', 'Opens your app in a web browser')
.option(
'-m, --host [mode]',
'lan (default), tunnel, localhost. Type of host to use. "tunnel" allows you to view your link on other networks'
)
.option('--tunnel', 'Same as --host tunnel')
.option('--lan', 'Same as --host lan')
.option('--localhost', 'Same as --host localhost');
}
async function optsAsync(projectDir: string, options: any) {
var opts = await ProjectSettings.readAsync(projectDir);
if ([options.host, options.lan, options.localhost, options.tunnel].filter(i => i).length > 1) {
throw new CommandError(
'BAD_ARGS',
'Specify at most one of --host, --tunnel, --lan, and --localhost'
);
}
opts.hostType = 'lan';
if (options.offline) {
// TODO: maybe let people know that we will force localhost with offline?
ConnectionStatus.setIsOffline(true);
opts.hostType = 'localhost';
}
if (options.host) {
opts.hostType = options.host;
} else if (options.tunnel) {
opts.hostType = 'tunnel';
} else if (options.lan) {
opts.hostType = 'lan';
} else if (options.localhost) {
opts.hostType = 'localhost';
}
await ProjectSettings.setAsync(projectDir, opts);
return opts;
}
function printQRCode(url: string) {
qrcodeTerminal.generate(url, code => log(`${indentString(code, 2)}\n`));
}
async function handleMobileOptsAsync(
projectRoot: string,
options: Pick<URLOptions, 'ios' | 'android' | 'web'> & { webOnly?: boolean }
) {
await Promise.all([
(async () => {
if (options.android) {
if (options.webOnly) {
await Android.openWebProjectAsync({ projectRoot });
} else {
await Android.openProjectAsync({ projectRoot });
}
}
})(),
(async () => {
if (options.ios) {
if (options.webOnly) {
await Simulator.openWebProjectAsync({ projectRoot, shouldPrompt: false });
} else {
await Simulator.openProjectAsync({ projectRoot, shouldPrompt: false });
}
}
})(),
(async () => {
if (options.web) {
await Webpack.openAsync(projectRoot);
}
})(),
]);
return !!options.android || !!options.ios;
}
export default {
addOptions,
handleMobileOptsAsync,
printQRCode,
optsAsync,
};