1import { NodePackageManagerForProject } from '@expo/package-manager';
2
3import { CommandError } from '../utils/errors';
4import { assertUnexpectedVariadicFlags, parseVariadicArguments } from '../utils/variadic';
5
6export type Options = Pick<NodePackageManagerForProject, 'npm' | 'pnpm' | 'yarn'> & {
7  /** Check which packages need to be updated, does not install any provided packages. */
8  check?: boolean;
9  /** Should the dependencies be fixed automatically. */
10  fix?: boolean;
11  /** Should disable install output, used for commands like `prebuild` that run install internally. */
12  silent?: boolean;
13};
14
15function resolveOptions(options: Options): Options {
16  if (options.fix && options.check) {
17    throw new CommandError('BAD_ARGS', 'Specify at most one of: --check, --fix');
18  }
19  if ([options.npm, options.pnpm, options.yarn].filter(Boolean).length > 1) {
20    throw new CommandError('BAD_ARGS', 'Specify at most one of: --npm, --pnpm, --yarn');
21  }
22  return {
23    ...options,
24  };
25}
26
27export async function resolveArgsAsync(
28  argv: string[]
29): Promise<{ variadic: string[]; options: Options; extras: string[] }> {
30  const { variadic, extras, flags } = parseVariadicArguments(argv);
31
32  assertUnexpectedVariadicFlags(
33    ['--check', '--fix', '--npm', '--pnpm', '--yarn'],
34    { variadic, extras, flags },
35    'npx expo install'
36  );
37
38  return {
39    // Variadic arguments like `npx expo install react react-dom` -> ['react', 'react-dom']
40    variadic,
41    options: resolveOptions({
42      fix: !!flags['--fix'],
43      check: !!flags['--check'],
44      yarn: !!flags['--yarn'],
45      npm: !!flags['--npm'],
46      pnpm: !!flags['--pnpm'],
47    }),
48    extras,
49  };
50}
51