Files
cardy/mobile-client/scripts/android-dev.mjs
T

40 lines
1.9 KiB
JavaScript

// Live reload on an Android device/emulator connected via adb.
//
// 1. `pnpm dev` in mobile-client must be running (Vite on PORT, default 10003).
// 2. adb reverse makes http://localhost:PORT on the phone reach the dev server on this computer.
// localhost counts as a secure context, so the camera and crypto.subtle (PIN) keep working.
// 3. `cap run android` syncs a config pointing the WebView at the dev server, builds and installs the app.
// Extra arguments are passed to `cap run` (e.g. --target <deviceId>).
//
// Afterwards run `pnpm build:android` before building an APK to share: it bundles the web build again.
import { spawnSync } from 'node:child_process';
import { existsSync } from 'node:fs';
import path from 'node:path';
const port = process.env.PORT || '10003';
const devUrl = `http://localhost:${port}/m/`;
function findAdb() {
const exe = process.platform === 'win32' ? 'adb.exe' : 'adb';
const sdk = process.env.ANDROID_HOME || process.env.ANDROID_SDK_ROOT || (process.env.LOCALAPPDATA && path.join(process.env.LOCALAPPDATA, 'Android', 'Sdk'));
const fromSdk = sdk && path.join(sdk, 'platform-tools', exe);
return fromSdk && existsSync(fromSdk) ? fromSdk : exe;
}
const adb = findAdb();
const reverse = spawnSync(adb, ['reverse', `tcp:${port}`, `tcp:${port}`], { stdio: 'inherit' });
if (reverse.status !== 0) {
console.error(`\n"${adb} reverse" failed: connect a device with USB debugging (or start an emulator) and make sure adb is installed.`);
process.exit(1);
}
console.log(`adb reverse tcp:${port} -> this computer. The app will load ${devUrl}\n`);
const run = spawnSync('pnpm', ['exec', 'cap', 'run', 'android', ...process.argv.slice(2)], {
stdio: 'inherit',
shell: true,
env: { ...process.env, CAP_DEV_URL: devUrl },
});
console.log('\nReminder: the installed app now loads the dev server. Run `pnpm build:android` before building an APK to share.');
process.exit(run.status ?? 1);