UNPKG

react-native-legal

Version:
99 lines (98 loc) 4.19 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getIOSPbxProj = exports.getIOSProjectName = exports.addToAllPbxResourcesBuildPhases = exports.getAllApplicationNativeTargets = exports.getFirstApplicationNativeTarget = void 0; const path_1 = __importDefault(require("path")); const xcode_1 = __importDefault(require("xcode")); const glob = require('glob'); const ignoredPaths = ['**/@(Carthage|Pods|vendor|node_modules)/**']; /** * Used to obtain first target of type "com.apple.product-type.application" */ function getFirstApplicationNativeTarget(pbxproj) { return pbxproj.getTarget('com.apple.product-type.application'); } exports.getFirstApplicationNativeTarget = getFirstApplicationNativeTarget; /** * Used to get all targets of type "com.apple.product-type.application" */ function getAllApplicationNativeTargets(pbxproj) { const targets = pbxproj.getFirstProject().firstProject.targets; const nativeTargets = pbxproj.pbxNativeTargetSection(); return targets.reduce((acc, target) => { const targetUuid = target.value; if (nativeTargets[targetUuid].productType === '"com.apple.product-type.application"') { return [...acc, { uuid: targetUuid, target: nativeTargets[targetUuid] }]; } return acc; }, []); } exports.getAllApplicationNativeTargets = getAllApplicationNativeTargets; /** * Used to add the file to all "Resources" build phases * * It's very handy especially when we have multiple application targets and each of them has separate "Resources" build phase */ function addToAllPbxResourcesBuildPhases(pbxproj, pbxFile) { getAllApplicationNativeTargets(pbxproj).map((nativeTarget) => { nativeTarget.target.buildPhases.map((buildPhase) => { if (buildPhase.comment !== RESOURCES_BUILD_PHASE_IDENTIFIER) { return; } const section = pbxproj.hash.project.objects['PBXResourcesBuildPhase']; const buildPhaseKey = buildPhase.value + '_comment'; for (const key in section) { if (!COMMENT_KEY.test(key) || (buildPhaseKey && buildPhaseKey !== key)) { continue; } if (typeof section[key] === 'string' && section[key] === RESOURCES_BUILD_PHASE_IDENTIFIER) { const sectionKey = key.split(COMMENT_KEY)[0]; const sources = section[sectionKey]; if (typeof sources === 'object') { sources.files.push({ value: pbxFile.uuid, comment: `${pbxFile.basename} in ${pbxFile.group}`, }); pbxproj.hash.project.objects['PBXResourcesBuildPhase'][sectionKey] = sources; } } } }); }); } exports.addToAllPbxResourcesBuildPhases = addToAllPbxResourcesBuildPhases; /** * Returns the name of the main group in the project (the main folder where `AppDelegate` usually lives) */ function getIOSProjectName(iosProjectPath) { const [appDelegatePath] = glob.sync('*/AppDelegate.@(m|mm|swift)', { cwd: iosProjectPath, absolute: true, nocase: true, nodir: true, ignore: ignoredPaths, }); return path_1.default.basename(path_1.default.dirname(appDelegatePath)); } exports.getIOSProjectName = getIOSProjectName; /** * Used to grab the XcodeProject instance */ function getIOSPbxProj(iosProjectPath) { const [xcodeprojPath] = glob.sync('*.xcodeproj', { cwd: iosProjectPath, absolute: true, nocase: true, nodir: false, ignore: ignoredPaths, }); const pbxprojPath = path_1.default.join(xcodeprojPath, 'project.pbxproj'); const pbxproj = xcode_1.default.project(pbxprojPath); pbxproj.parseSync(); return { pbxproj, pbxprojPath }; } exports.getIOSPbxProj = getIOSPbxProj; const RESOURCES_BUILD_PHASE_IDENTIFIER = 'Resources'; const COMMENT_KEY = /_comment$/;