1import { sync as globSync } from 'glob';
2
3import { CommandError } from '../../../utils/errors';
4import { ProjectInfo } from '../XcodeBuild.types';
5
6const ignoredPaths = ['**/@(Carthage|Pods|vendor|node_modules)/**'];
7
8function findXcodeProjectPaths(
9  projectRoot: string,
10  extension: 'xcworkspace' | 'xcodeproj'
11): string[] {
12  return globSync(`ios/*.${extension}`, {
13    absolute: true,
14    cwd: projectRoot,
15    ignore: ignoredPaths,
16  });
17}
18
19/** Return the path and type of Xcode project in the given folder. */
20export function resolveXcodeProject(projectRoot: string): ProjectInfo {
21  let paths = findXcodeProjectPaths(projectRoot, 'xcworkspace');
22  if (paths.length) {
23    return {
24      // Use full path instead of relative project root so that warnings and errors contain full paths as well, this helps with filtering.
25      // Also helps keep things consistent in monorepos.
26      name: paths[0],
27      // name: path.relative(projectRoot, paths[0]),
28      isWorkspace: true,
29    };
30  }
31  paths = findXcodeProjectPaths(projectRoot, 'xcodeproj');
32  if (paths.length) {
33    return { name: paths[0], isWorkspace: false };
34  }
35  throw new CommandError(
36    'IOS_MALFORMED',
37    `Xcode project not found in project: ${projectRoot}. You can generate a project with \`npx expo prebuild\``
38  );
39}
40