1import { ProjectConfig } from '@expo/config';
2import { getEntryPoint } from '@expo/config/paths';
3import chalk from 'chalk';
4import path from 'path';
5
6import { CommandError } from '../../../utils/errors';
7
8const supportedPlatforms = ['ios', 'android', 'web', 'none'];
9
10/** Returns the relative entry file for the project.  */
11export function resolveEntryPoint(
12  projectRoot: string,
13  platform?: string,
14  projectConfig?: ProjectConfig
15): string {
16  if (platform && !supportedPlatforms.includes(platform)) {
17    throw new CommandError(
18      `Failed to resolve the project's entry file: The platform "${platform}" is not supported.`
19    );
20  }
21  // TODO(Bacon): support platform extension resolution like .ios, .native
22  // const platforms = [platform, 'native'].filter(Boolean) as string[];
23  const platforms: string[] = [];
24
25  const entry = getEntryPoint(projectRoot, ['./index'], platforms, projectConfig);
26  if (!entry) {
27    // NOTE(Bacon): I purposefully don't mention all possible resolutions here since the package.json is the most standard and users should opt towards that.
28    throw new CommandError(
29      chalk`The project entry file could not be resolved. Please define it in the {bold package.json} "main" field.`
30    );
31  }
32
33  return path.relative(projectRoot, entry);
34}
35