-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
cli.ts
executable file
·76 lines (61 loc) · 1.91 KB
/
cli.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
#!/usr/bin/env node
import arg from 'arg';
import chalk from 'chalk';
// TODO: Change to `start`
const defaultCmd = 'config';
export type Command = (argv?: string[]) => void;
const commands: { [command: string]: () => Promise<Command> } = {
// Add a new command here
prebuild: () => import('../cli/prebuild').then((i) => i.expoPrebuild),
config: () => import('../cli/config').then((i) => i.expoConfig),
// Auth
login: () => import('../cli/login').then((i) => i.expoLogin),
logout: () => import('../cli/logout').then((i) => i.expoLogout),
register: () => import('../cli/register').then((i) => i.expoRegister),
whoami: () => import('../cli/whoami').then((i) => i.expoWhoami),
};
const args = arg(
{
// Types
'--version': Boolean,
'--help': Boolean,
// Aliases
'-v': '--version',
'-h': '--help',
},
{
permissive: true,
}
);
if (args['--version']) {
// Version is added in the build script.
console.log(process.env.__EXPO_VERSION);
process.exit(0);
}
// Check if we are running `npx expo <subcommand>` or `npx expo`
const isSubcommand = Boolean(commands[args._[0]]);
// Handle `--help` flag
if (!isSubcommand && args['--help']) {
console.log(chalk`
{bold Usage}
{bold $} npx expo <command>
{bold Available commands}
${Object.keys(commands).sort().join(', ')}
{bold Options}
--version, -v Version number
--help, -h Displays this message
For more information run a command with the --help flag
{bold $} expo start --help
`);
process.exit(0);
}
const command = isSubcommand ? args._[0] : defaultCmd;
const commandArgs = isSubcommand ? args._.slice(1) : args._;
// Push the help flag to the subcommand args.
if (args['--help']) {
commandArgs.push('--help');
}
// Install exit hooks
process.on('SIGINT', () => process.exit(0));
process.on('SIGTERM', () => process.exit(0));
commands[command]().then((exec) => exec(commandArgs));