xref: /expo/packages/@expo/cli/src/utils/args.ts (revision 98ecfc87)
1// Common utilities for interacting with `args` library.
2// These functions should be used by every command.
3import arg from 'arg';
4import { existsSync } from 'fs';
5import { resolve } from 'path';
6
7import * as Log from '../log';
8
9/**
10 * Parse the first argument as a project directory.
11 *
12 * @returns valid project directory.
13 */
14export function getProjectRoot(args: arg.Result<arg.Spec>) {
15  const projectRoot = resolve(args._[0] || '.');
16
17  if (!existsSync(projectRoot)) {
18    Log.exit(`Invalid project root: ${projectRoot}`);
19  }
20
21  return projectRoot;
22}
23
24/**
25 * Parse args and assert unknown options.
26 *
27 * @param schema the `args` schema for parsing the command line arguments.
28 * @param argv extra strings
29 * @returns processed args object.
30 */
31export function assertArgs(schema: arg.Spec, argv: string[]): arg.Result<arg.Spec> {
32  try {
33    return arg(schema, { argv });
34  } catch (error) {
35    // Ensure unknown options are handled the same way.
36    if (error.code === 'ARG_UNKNOWN_OPTION') {
37      Log.exit(error.message, 1);
38    }
39    // Otherwise rethrow the error.
40    throw error;
41  }
42}
43