|
| 1 | +import { spawn, SubprocessError } from '@react-native-harness/tools'; |
| 2 | + |
| 3 | +const DEBUG_HTTP_HOST_BLOCK_START = |
| 4 | + '<!-- react-native-harness:debug_http_host:start -->'; |
| 5 | +const DEBUG_HTTP_HOST_BLOCK_END = |
| 6 | + '<!-- react-native-harness:debug_http_host:end -->'; |
| 7 | + |
| 8 | +const getSharedPrefsPath = (bundleId: string) => |
| 9 | + `shared_prefs/${bundleId}_preferences.xml`; |
| 10 | + |
| 11 | +const getHarnessDebugHttpHostBlock = (host: string) => |
| 12 | + [ |
| 13 | + DEBUG_HTTP_HOST_BLOCK_START, |
| 14 | + `<string name="debug_http_host">${host}</string>`, |
| 15 | + DEBUG_HTTP_HOST_BLOCK_END, |
| 16 | + ].join('\n'); |
| 17 | + |
| 18 | +const stripHarnessDebugHttpHostBlock = (content: string): string => |
| 19 | + content.replace( |
| 20 | + new RegExp( |
| 21 | + `\\s*${DEBUG_HTTP_HOST_BLOCK_START}\\n[\\s\\S]*?\\n${DEBUG_HTTP_HOST_BLOCK_END}\\s*`, |
| 22 | + 'g' |
| 23 | + ), |
| 24 | + '\n' |
| 25 | + ); |
| 26 | + |
| 27 | +const normalizeSharedPrefsContent = (content: string | null): string => { |
| 28 | + if (!content?.trim()) { |
| 29 | + return ['<?xml version="1.0" encoding="utf-8"?>', '<map>', '</map>'].join( |
| 30 | + '\n' |
| 31 | + ); |
| 32 | + } |
| 33 | + |
| 34 | + return stripHarnessDebugHttpHostBlock(content).trim(); |
| 35 | +}; |
| 36 | + |
| 37 | +const insertBeforeClosingMap = (content: string, block: string): string => { |
| 38 | + if (!content.includes('</map>')) { |
| 39 | + throw new Error('Android shared preferences file is missing </map>.'); |
| 40 | + } |
| 41 | + |
| 42 | + return content.replace(/<\/map>\s*$/, ` ${block.replace(/\n/g, '\n ')}\n</map>`); |
| 43 | +}; |
| 44 | + |
| 45 | +const readSharedPrefsFile = async ( |
| 46 | + adbId: string, |
| 47 | + bundleId: string |
| 48 | +): Promise<string | null> => { |
| 49 | + try { |
| 50 | + const { stdout } = await spawn('adb', [ |
| 51 | + '-s', |
| 52 | + adbId, |
| 53 | + 'shell', |
| 54 | + `run-as ${bundleId} cat ${getSharedPrefsPath(bundleId)}`, |
| 55 | + ]); |
| 56 | + return stdout; |
| 57 | + } catch (error) { |
| 58 | + if (error instanceof SubprocessError && error.exitCode === 1) { |
| 59 | + return null; |
| 60 | + } |
| 61 | + |
| 62 | + throw error; |
| 63 | + } |
| 64 | +}; |
| 65 | + |
| 66 | +const writeSharedPrefsFile = async ( |
| 67 | + adbId: string, |
| 68 | + bundleId: string, |
| 69 | + content: string |
| 70 | +): Promise<void> => { |
| 71 | + await spawn( |
| 72 | + 'adb', |
| 73 | + [ |
| 74 | + '-s', |
| 75 | + adbId, |
| 76 | + 'shell', |
| 77 | + `run-as ${bundleId} sh -c 'mkdir -p shared_prefs && cat > ${getSharedPrefsPath(bundleId)}'`, |
| 78 | + ], |
| 79 | + { stdin: { string: `${content.trim()}\n` } } |
| 80 | + ); |
| 81 | +}; |
| 82 | + |
| 83 | +export const applyHarnessDebugHttpHost = async ( |
| 84 | + adbId: string, |
| 85 | + bundleId: string, |
| 86 | + host: string |
| 87 | +): Promise<void> => { |
| 88 | + const existingContent = await readSharedPrefsFile(adbId, bundleId); |
| 89 | + const normalizedContent = normalizeSharedPrefsContent(existingContent); |
| 90 | + const nextContent = insertBeforeClosingMap( |
| 91 | + normalizedContent, |
| 92 | + getHarnessDebugHttpHostBlock(host) |
| 93 | + ); |
| 94 | + await writeSharedPrefsFile(adbId, bundleId, nextContent); |
| 95 | +}; |
| 96 | + |
| 97 | +export const clearHarnessDebugHttpHost = async ( |
| 98 | + adbId: string, |
| 99 | + bundleId: string |
| 100 | +): Promise<void> => { |
| 101 | + const existingContent = await readSharedPrefsFile(adbId, bundleId); |
| 102 | + |
| 103 | + if (!existingContent) { |
| 104 | + return; |
| 105 | + } |
| 106 | + |
| 107 | + const nextContent = stripHarnessDebugHttpHostBlock(existingContent).trim(); |
| 108 | + |
| 109 | + if (nextContent === existingContent.trim()) { |
| 110 | + return; |
| 111 | + } |
| 112 | + |
| 113 | + await writeSharedPrefsFile(adbId, bundleId, nextContent); |
| 114 | +}; |
0 commit comments