cmvr-wms/mobile-app/plugins/with-internal-network.cjs

26 lines
1.7 KiB
JavaScript
Raw Normal View History

const { withAndroidManifest, withDangerousMod } = require('expo/config-plugins');
const fs = require('node:fs/promises');
const path = require('node:path');
const { environment } = require('../config/environment.cjs');
// Production keeps exact-host cleartext restrictions. Development supports runtime
// private-IP selection, enforced by the app before probing, saving or using it.
module.exports = (config, options) => {
const env = environment();
const hosts = [...new Set(options?.hosts || [])];
const expected = [...new Set([...([env.apiUrl, env.updateOrigin].filter(s => s.startsWith('http:')).map(s => new URL(s).hostname)), ...(env.variant === 'prod' ? ['192.168.28.10'] : [])])];
if (JSON.stringify(hosts) !== JSON.stringify(expected)) throw Error('Native network hosts must match the environment config');
config = withAndroidManifest(config, cfg => {
const app = cfg.modResults.manifest.application[0];
app.$['android:networkSecurityConfig'] = '@xml/cmvr_network_security';
app.$['android:usesCleartextTraffic'] = 'false';
return cfg;
});
return withDangerousMod(config, ['android', async cfg => {
const dir = path.join(cfg.modRequest.platformProjectRoot, 'app/src/main/res/xml');
await fs.mkdir(dir, { recursive: true });
await fs.writeFile(path.join(dir, 'cmvr_network_security.xml'),
`<?xml version="1.0" encoding="utf-8"?><network-security-config><base-config cleartextTrafficPermitted="${env.variant==='dev'?'true':'false'}"/>${hosts.length ? `<domain-config cleartextTrafficPermitted="true">${hosts.map(h => `<domain includeSubdomains="false">${h}</domain>`).join('')}</domain-config>` : ''}</network-security-config>`);
return cfg;
}]);
};