xref: /expo/packages/@expo/cli/bin/cli.ts (revision bdc5bc41)
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  console.log(chalk`
53    {bold Usage}
54      {bold $} npx expo <command>
55
56    {bold Available commands}
57      ${Object.keys(commands).sort().join(', ')}
58
59    {bold Options}
60      --version, -v   Version number
61      --help, -h      Displays this message
62
63    For more information run a command with the --help flag
64      {bold $} expo start --help
65  `);
66  process.exit(0);
67}
68
69const command = isSubcommand ? args._[0] : defaultCmd;
70const commandArgs = isSubcommand ? args._.slice(1) : args._;
71
72// Push the help flag to the subcommand args.
73if (args['--help']) {
74  commandArgs.push('--help');
75}
76
77// Install exit hooks
78process.on('SIGINT', () => process.exit(0));
79process.on('SIGTERM', () => process.exit(0));
80
81commands[command]().then((exec) => exec(commandArgs));
82