|
| 1 | +import {HealthCheckInterface} from '../../types'; |
| 2 | +import fs from 'fs'; |
| 3 | +import path from 'path'; |
| 4 | +import {promisify} from 'util'; |
| 5 | +import {findProjectRoot} from '@react-native-community/cli-tools'; |
| 6 | +import {findPodfilePaths} from '@react-native-community/cli-platform-ios'; |
| 7 | + |
| 8 | +const xcodeEnvFile = '.xcode.env'; |
| 9 | +const pathSeparator = '/'; |
| 10 | + |
| 11 | +function removeLastPathComponent(pathString: string): string { |
| 12 | + const components = pathString.split(pathSeparator); |
| 13 | + components.splice(components.length - 1, 1); |
| 14 | + return components.join(pathSeparator); |
| 15 | +} |
| 16 | + |
| 17 | +function pathHasXcodeEnvFile(pathString: string): boolean { |
| 18 | + const xcodeEnvPath = pathString + pathSeparator + xcodeEnvFile; |
| 19 | + return fs.existsSync(xcodeEnvPath); |
| 20 | +} |
| 21 | + |
| 22 | +function pathDoesNotHaveXcodeEnvFile(pathString: string): boolean { |
| 23 | + return !pathHasXcodeEnvFile(pathString); |
| 24 | +} |
| 25 | + |
| 26 | +export default { |
| 27 | + label: '.xcode.env', |
| 28 | + description: 'File to customize Xcode environment', |
| 29 | + getDiagnostics: async () => { |
| 30 | + const projectRoot = findProjectRoot(); |
| 31 | + const allPathsHasXcodeEnvFile = findPodfilePaths(projectRoot) |
| 32 | + .map((pathString) => { |
| 33 | + const basePath = removeLastPathComponent(pathString); |
| 34 | + return pathHasXcodeEnvFile(basePath); |
| 35 | + }) |
| 36 | + .reduce((previousValue, currentValue) => previousValue && currentValue); |
| 37 | + return { |
| 38 | + needsToBeFixed: !allPathsHasXcodeEnvFile, |
| 39 | + }; |
| 40 | + }, |
| 41 | + runAutomaticFix: async () => { |
| 42 | + const templateXcodeEnv = '_xcode.env'; |
| 43 | + const projectRoot = findProjectRoot(); |
| 44 | + |
| 45 | + const templateIosPath = path.dirname( |
| 46 | + require.resolve('react-native/template/ios'), |
| 47 | + ); |
| 48 | + |
| 49 | + const src = templateIosPath + templateXcodeEnv; |
| 50 | + const copyFileAsync = promisify(fs.copyFile); |
| 51 | + |
| 52 | + findPodfilePaths(projectRoot) |
| 53 | + .map(removeLastPathComponent) |
| 54 | + // avoid overriding existing .xcode.env |
| 55 | + .filter(pathDoesNotHaveXcodeEnvFile) |
| 56 | + .forEach(async (pathString) => { |
| 57 | + const destFilePath = pathString + pathSeparator + xcodeEnvFile; |
| 58 | + await copyFileAsync(src, destFilePath); |
| 59 | + }); |
| 60 | + }, |
| 61 | +} as HealthCheckInterface; |
0 commit comments