|
| 1 | +import fs from 'node:fs'; |
| 2 | +import path from 'node:path'; |
| 3 | +import type { RunTarget } from '@react-native-harness/platforms'; |
| 4 | +import type { ProjectConfig } from './projectType.js'; |
| 5 | + |
| 6 | +const q = (s: string) => `'${s.replace(/'/g, "\\'")}'`; |
| 7 | + |
| 8 | +const getDeviceCall = (target: RunTarget): string => { |
| 9 | + const { device, type, platform } = target; |
| 10 | + if (platform === 'android') { |
| 11 | + if (type === 'emulator') { |
| 12 | + return `androidEmulator(${q(device.name)})`; |
| 13 | + } |
| 14 | + return `physicalAndroidDevice(${q(device.manufacturer)}, ${q( |
| 15 | + device.model |
| 16 | + )})`; |
| 17 | + } |
| 18 | + if (platform === 'ios') { |
| 19 | + if (type === 'emulator') { |
| 20 | + return `appleSimulator(${q(device.name)}, ${q(device.systemVersion)})`; |
| 21 | + } |
| 22 | + return `applePhysicalDevice(${q(device.name)})`; |
| 23 | + } |
| 24 | + return JSON.stringify(device); |
| 25 | +}; |
| 26 | + |
| 27 | +const getPlatformFn = (platform: string): string => { |
| 28 | + if (platform === 'android') return 'androidPlatform'; |
| 29 | + if (platform === 'ios') return 'applePlatform'; |
| 30 | + return `${platform}Platform`; |
| 31 | +}; |
| 32 | + |
| 33 | +export const generateConfig = ( |
| 34 | + projectConfig: ProjectConfig, |
| 35 | + selectedPlatforms: string[], |
| 36 | + selectedTargets: RunTarget[], |
| 37 | + bundleIds: Record<string, string> |
| 38 | +) => { |
| 39 | + const imports: string[] = []; |
| 40 | + if (selectedPlatforms.includes('android')) { |
| 41 | + const androidFactories = ['androidPlatform']; |
| 42 | + if ( |
| 43 | + selectedTargets.some( |
| 44 | + (t) => t.platform === 'android' && t.type === 'emulator' |
| 45 | + ) |
| 46 | + ) |
| 47 | + androidFactories.push('androidEmulator'); |
| 48 | + if ( |
| 49 | + selectedTargets.some( |
| 50 | + (t) => t.platform === 'android' && t.type === 'physical' |
| 51 | + ) |
| 52 | + ) |
| 53 | + androidFactories.push('physicalAndroidDevice'); |
| 54 | + |
| 55 | + imports.push( |
| 56 | + `import { ${androidFactories.join( |
| 57 | + ', ' |
| 58 | + )} } from "@react-native-harness/platform-android";` |
| 59 | + ); |
| 60 | + } |
| 61 | + if (selectedPlatforms.includes('ios')) { |
| 62 | + const iosFactories = ['applePlatform']; |
| 63 | + if ( |
| 64 | + selectedTargets.some((t) => t.platform === 'ios' && t.type === 'emulator') |
| 65 | + ) |
| 66 | + iosFactories.push('appleSimulator'); |
| 67 | + if ( |
| 68 | + selectedTargets.some((t) => t.platform === 'ios' && t.type === 'physical') |
| 69 | + ) |
| 70 | + iosFactories.push('applePhysicalDevice'); |
| 71 | + |
| 72 | + imports.push( |
| 73 | + `import { ${iosFactories.join( |
| 74 | + ', ' |
| 75 | + )} } from "@react-native-harness/platform-apple";` |
| 76 | + ); |
| 77 | + } |
| 78 | + if (selectedPlatforms.includes('web')) { |
| 79 | + const webFactories = ['webPlatform']; |
| 80 | + const browsers = new Set( |
| 81 | + selectedTargets |
| 82 | + .filter((t) => t.platform === 'web') |
| 83 | + .map((t) => t.device.browserType) |
| 84 | + ); |
| 85 | + for (const browser of browsers) { |
| 86 | + if (browser) webFactories.push(browser); |
| 87 | + } |
| 88 | + |
| 89 | + imports.push( |
| 90 | + `import { ${webFactories.join( |
| 91 | + ', ' |
| 92 | + )} } from "@react-native-harness/platform-web";` |
| 93 | + ); |
| 94 | + } |
| 95 | + |
| 96 | + const runnerConfigs = selectedTargets.map((target) => { |
| 97 | + const platformFn = getPlatformFn(target.platform); |
| 98 | + const name = target.name.toLowerCase().replace(/\s+/g, '-'); |
| 99 | + |
| 100 | + if (target.platform === 'web') { |
| 101 | + const url = bundleIds[target.platform]; |
| 102 | + const browserCall = `${target.device.browserType}(${q(url)})`; |
| 103 | + return ` ${platformFn}({ |
| 104 | + name: ${q(name)}, |
| 105 | + browser: ${browserCall}, |
| 106 | + }),`; |
| 107 | + } |
| 108 | + |
| 109 | + const bundleId = bundleIds[target.platform]; |
| 110 | + const deviceCall = getDeviceCall(target); |
| 111 | + |
| 112 | + return ` ${platformFn}({ |
| 113 | + name: ${q(name)}, |
| 114 | + device: ${deviceCall}, |
| 115 | + bundleId: ${q(bundleId)}, |
| 116 | + }),`; |
| 117 | + }); |
| 118 | + |
| 119 | + const configContent = ` |
| 120 | +${imports.join('\n')} |
| 121 | +
|
| 122 | +export default { |
| 123 | + entryPoint: ${q(projectConfig.entryPoint)}, |
| 124 | + appRegistryComponentName: ${q(projectConfig.appRegistryComponentName)}, |
| 125 | +
|
| 126 | + runners: [ |
| 127 | +${runnerConfigs.join('\n')} |
| 128 | + ], |
| 129 | + defaultRunner: ${q( |
| 130 | + selectedTargets[0].name.toLowerCase().replace(/\s+/g, '-') |
| 131 | + )}, |
| 132 | +}; |
| 133 | +`; |
| 134 | + |
| 135 | + fs.writeFileSync( |
| 136 | + path.join(process.cwd(), 'rn-harness.config.mjs'), |
| 137 | + configContent.trim() + '\n' |
| 138 | + ); |
| 139 | +}; |
0 commit comments