All files / src/ios pbxproj.ts

80% Statements 16/20
60% Branches 6/10
100% Functions 3/3
80% Lines 16/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69                                                7x   7x       7x     7x     7x 7x   7x   7x 7x 14x 7x   7x       7x 7x       7x           7x              
import { projectConfig } from '@react-native-community/cli-platform-ios';
import path from 'path';
import { project as PbxProject, UUID, PBXNativeTarget } from 'xcode';
 
export interface IosProject {
  projectName: string;
  /**
   * Root path to directory containing project source files.
   */
  projectPath: string;
  /**
   * pbxProject reference that allows to modify `.pbxproj` file.
   */
  pbxProject: PbxProject;
  /**
   * main application PBXNativeTarget from `.pbxproj` file.
   */
  applicationNativeTarget: { uuid: UUID; target: PBXNativeTarget };
}
 
/**
 * Reads iOS project and locates `.pbxproj` file for further parsing and modifications.
 */
export default async function readPbxProject(projectRootPath: string): Promise<IosProject> {
  const config = projectConfig(projectRootPath, { plist: [] });
 
  Iif (!config) {
    throw new Error(`Couldn't find iOS project. Cannot configure iOS.`);
  }
 
  const { projectPath: xcodeProjPath, pbxprojPath } = config;
 
  // xcodeProjPath contains path to .xcodeproj directory
  Iif (!xcodeProjPath.endsWith('.xcodeproj')) {
    throw new Error(`Couldn't find .xcodeproj directory.`);
  }
  const projectPath = xcodeProjPath.substring(0, xcodeProjPath.length - '.xcodeproj'.length);
  const projectName = path.basename(projectPath);
 
  const pbxProject = new PbxProject(pbxprojPath);
 
  await new Promise(resolve =>
    pbxProject.parse(err => {
      if (err) {
        throw new Error(`.pbxproj file parsing issue: ${err.message}.`);
      }
      resolve();
    })
  );
 
  const applicationNativeTarget = pbxProject.getTarget('com.apple.product-type.application');
  Iif (!applicationNativeTarget) {
    throw new Error(`Couldn't locate application PBXNativeTarget in '.xcodeproj' file.`);
  }
 
  Iif (applicationNativeTarget.target.name !== projectName) {
    throw new Error(
      `Application native target name mismatch. Expected ${projectName}, but found ${applicationNativeTarget.target.name}.`
    );
  }
 
  return {
    projectName,
    projectPath,
    pbxProject,
    applicationNativeTarget,
  };
}