diff --git a/native/utils/dev-hostname.js b/native/utils/dev-hostname.js index 0a7b3e817..8528335bd 100644 --- a/native/utils/dev-hostname.js +++ b/native/utils/dev-hostname.js @@ -1,33 +1,47 @@ // @flow +import Constants from 'expo-constants'; -let natDevHostname: string; let warnNatDevHostnameUndefined = true; const defaultNatDevHostname = '192.168.1.1'; -try { - // this is your machine's hostname in the local network - // usually it looks like this: 192.168.1.x - // to find it you may want to use `ifconfig | grep '192.168'` - // command in the terminal - // example of native/facts/network.json: - // { "natDevHostname": "192.168.1.x" } - // $FlowExpectedError: That's a conditional require so the file may not exist - const hostname: string = require('../facts/network').natDevHostname; - natDevHostname = hostname; +function readHostnameFromExpoConfig(): ?string { + const { natDevHostname: detectedHostname } = Constants.expoConfig.extra || {}; + if (!detectedHostname || typeof detectedHostname !== 'string') { + return null; + } warnNatDevHostnameUndefined = false; -} catch (e) { - natDevHostname = defaultNatDevHostname; + return detectedHostname; } +function readHostnameFromNetworkJson(): string { + try { + // this is your machine's hostname in the local network + // usually it looks like this: 192.168.1.x + // to find it you may want to use `ifconfig | grep '192.168'` + // command in the terminal + // example of native/facts/network.json: + // { "natDevHostname": "192.168.1.x" } + // $FlowExpectedError: It's a conditional require so the file may not exist + const hostname: string = require('../facts/network').natDevHostname; + warnNatDevHostnameUndefined = false; + return hostname; + } catch (e) { + return defaultNatDevHostname; + } +} + +const natDevHostname: string = + readHostnameFromExpoConfig() ?? readHostnameFromNetworkJson(); + function checkForMissingNatDevHostname() { if (!warnNatDevHostnameUndefined) { return; } console.warn( - 'Failed to read natDevHostname. ' + - 'Please specify it in native/facts/network.json', + 'Failed to autodetect natDevHostname. ' + + 'Please specify it manually in native/facts/network.json', ); warnNatDevHostnameUndefined = false; } export { natDevHostname, checkForMissingNatDevHostname };