xref: /expo/packages/@expo/cli/bin/cli.ts (revision 83d464dc)
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  export: () => import('../src/export').then((i) => i.expoExport),
15
16  // Auxiliary commands
17  install: () => import('../src/install').then((i) => i.expoInstall),
18
19  // Auth
20  login: () => import('../src/login').then((i) => i.expoLogin),
21  logout: () => import('../src/logout').then((i) => i.expoLogout),
22  register: () => import('../src/register').then((i) => i.expoRegister),
23  whoami: () => import('../src/whoami').then((i) => i.expoWhoami),
24};
25
26const args = arg(
27  {
28    // Types
29    '--version': Boolean,
30    '--help': Boolean,
31
32    // Aliases
33    '-v': '--version',
34    '-h': '--help',
35  },
36  {
37    permissive: true,
38  }
39);
40
41if (args['--version']) {
42  // Version is added in the build script.
43  console.log(process.env.__EXPO_VERSION);
44  process.exit(0);
45}
46
47// Check if we are running `npx expo <subcommand>` or `npx expo`
48const isSubcommand = Boolean(commands[args._[0]]);
49
50// Handle `--help` flag
51if (!isSubcommand && args['--help']) {
52  const {
53    login,
54    logout,
55    whoami,
56    register,
57    start,
58    install,
59    export: _export,
60    config,
61    ...others
62  } = commands;
63
64  console.log(chalk`
65  {bold Usage}
66    {dim $} npx expo <command>
67
68  {bold Commands}
69    ${Object.keys({ start, install, export: _export, config, ...others }).join(', ')}
70    {dim ${Object.keys({ login, logout, whoami, register }).join(', ')}}
71
72  {bold Options}
73    --version, -v   Version number
74    --help, -h      Usage info
75
76  For more info run a command with the {bold --help} flag
77    {dim $} npx expo start --help
78`);
79  process.exit(0);
80}
81
82const command = isSubcommand ? args._[0] : defaultCmd;
83const commandArgs = isSubcommand ? args._.slice(1) : args._;
84
85// Push the help flag to the subcommand args.
86if (args['--help']) {
87  commandArgs.push('--help');
88}
89
90// Install exit hooks
91process.on('SIGINT', () => process.exit(0));
92process.on('SIGTERM', () => process.exit(0));
93
94commands[command]().then((exec) => exec(commandArgs));
95