xref: /expo/packages/@expo/cli/bin/cli.ts (revision dad749c2)
1#!/usr/bin/env node
2import arg from 'arg';
3import chalk from 'chalk';
4
5const defaultCmd = 'start';
6
7export type Command = (argv?: string[]) => void;
8
9const commands: { [command: string]: () => Promise<Command> } = {
10  // Add a new command here
11  start: () => import('../src/start').then((i) => i.expoStart),
12  prebuild: () => import('../src/prebuild').then((i) => i.expoPrebuild),
13  config: () => import('../src/config').then((i) => i.expoConfig),
14
15  // Auxiliary commands
16  install: () => import('../src/install').then((i) => i.expoInstall),
17
18  // Auth
19  login: () => import('../src/login').then((i) => i.expoLogin),
20  logout: () => import('../src/logout').then((i) => i.expoLogout),
21  register: () => import('../src/register').then((i) => i.expoRegister),
22  whoami: () => import('../src/whoami').then((i) => i.expoWhoami),
23};
24
25const args = arg(
26  {
27    // Types
28    '--version': Boolean,
29    '--help': Boolean,
30
31    // Aliases
32    '-v': '--version',
33    '-h': '--help',
34  },
35  {
36    permissive: true,
37  }
38);
39
40if (args['--version']) {
41  // Version is added in the build script.
42  console.log(process.env.__EXPO_VERSION);
43  process.exit(0);
44}
45
46// Check if we are running `npx expo <subcommand>` or `npx expo`
47const isSubcommand = Boolean(commands[args._[0]]);
48
49// Handle `--help` flag
50if (!isSubcommand && args['--help']) {
51  console.log(chalk`
52    {bold Usage}
53      {bold $} npx expo <command>
54
55    {bold Available commands}
56      ${Object.keys(commands).sort().join(', ')}
57
58    {bold Options}
59      --version, -v   Version number
60      --help, -h      Displays this message
61
62    For more information run a command with the --help flag
63      {bold $} expo start --help
64  `);
65  process.exit(0);
66}
67
68const command = isSubcommand ? args._[0] : defaultCmd;
69const commandArgs = isSubcommand ? args._.slice(1) : args._;
70
71// Push the help flag to the subcommand args.
72if (args['--help']) {
73  commandArgs.push('--help');
74}
75
76// Install exit hooks
77process.on('SIGINT', () => process.exit(0));
78process.on('SIGTERM', () => process.exit(0));
79
80commands[command]().then((exec) => exec(commandArgs));
81