bagsy/scripts/build-android.ts

216 lines
5.4 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env node
/**
* Android Build Script
* Automated build process for Android using EAS Build or Gradle
*
* Usage:
* bun run scripts/build-android.ts --apk # Build APK for sideload
* bun run scripts/build-android.ts --aab # Build AAB for Play Store
* bun run scripts/build-android.ts --preview # Preview build
*/
import * as fs from 'fs';
import * as path from 'path';
import { exec } from 'child_process';
import { promisify } from 'util';
const execAsync = promisify(exec);
interface BuildOptions {
buildType: 'apk' | 'aab' | 'preview';
skipCleanBuild: boolean;
generateSigningKey: boolean;
}
async function checkEASCLI() {
try {
await execAsync('eas --version');
console.log('✅ EAS CLI found\n');
return true;
} catch {
console.error('❌ EAS CLI not found');
console.error('Install with: npm install -g eas-cli');
process.exit(1);
}
}
async function checkAndroidSigningKey() {
console.log('🔐 Checking Android signing key...\n');
const keyPath = path.join(process.cwd(), '.deployment', 'keystore.jks');
if (fs.existsSync(keyPath)) {
console.log('✅ Signing key found\n');
return true;
} else {
console.log('ℹ️ No signing key found. EAS will manage it automatically.\n');
return false;
}
}
async function incrementVersion() {
console.log('📦 Updating version...\n');
const appJsonPath = path.join(process.cwd(), 'apps/mobile/app.json');
const appJson = JSON.parse(fs.readFileSync(appJsonPath, 'utf-8'));
const currentVersion = appJson.expo.version;
const [major, minor, patch] = currentVersion.split('.').map(Number);
// Increment patch version
const newVersion = `${major}.${minor}.${patch + 1}`;
appJson.expo.version = newVersion;
appJson.expo.android.versionCode = (appJson.expo.android.versionCode || 1) + 1;
fs.writeFileSync(appJsonPath, JSON.stringify(appJson, null, 2));
console.log(`✅ Version updated: ${currentVersion} → ${newVersion}\n`);
return newVersion;
}
async function buildAndroid(options: BuildOptions, version: string) {
console.log(`🚀 Building Android (${options.buildType})...\n`);
let buildArgs = ['eas build', '--platform android'];
if (options.buildType === 'apk') {
buildArgs.push('--local');
} else if (options.buildType === 'preview') {
buildArgs.push('--preview');
} else {
buildArgs.push('--production');
}
if (!options.skipCleanBuild) {
buildArgs.push('--clean');
}
const command = buildArgs.join(' ');
try {
console.log(`Running: ${command}\n`);
const { stdout, stderr } = await execAsync(command, {
cwd: path.join(process.cwd(), 'apps/mobile'),
stdio: 'inherit',
timeout: 1200000, // 20 minutes
});
console.log(stdout);
if (stderr) {
console.warn('Warnings:', stderr);
}
console.log(`\n✅ Android build completed successfully!\n`);
return true;
} catch (err) {
console.error('❌ Android build failed:', err);
return false;
}
}
async function generateSideloadLink(version: string) {
console.log('🔗 Generating sideload link...\n');
console.log(`
📱 Android APK Sideload Instructions:
1. Get the APK file from the build output
2. Upload to a file hosting service:
- Google Drive (share link)
- Dropbox (share link)
- GitHub Releases (attach APK)
- Or host on your own server
3. Share link with family
4. Family members:
- Click link on their Android phone
- Download APK
- Open Settings → Security
- Enable "Unknown sources" or "Install from unknown sources"
- Tap downloaded APK file
- Tap "Install"
- Done!
⚠️ Note: After first install, they can update by:
- Repeating the process with new APK
- Or using in-app update checker (if implemented)
`);
console.log('✅ APK ready for sideload\n');
}
async function saveDeploymentInfo(version: string, buildType: string) {
const info = {
version,
buildType,
timestamp: new Date().toISOString(),
platform: 'android',
};
if (!fs.existsSync('.deployment')) {
fs.mkdirSync('.deployment', { recursive: true });
}
fs.appendFileSync(path.join('.deployment', 'builds.log'), JSON.stringify(info) + '\n');
console.log(`📝 Build info saved to .deployment/builds.log\n`);
}
async function main() {
const args = process.argv.slice(2);
let buildType: BuildOptions['buildType'] = 'apk';
let skipCleanBuild = false;
let generateSigningKey = false;
for (const arg of args) {
if (arg === '--apk') buildType = 'apk';
if (arg === '--aab') buildType = 'aab';
if (arg === '--preview') buildType = 'preview';
if (arg === '--skip-clean') skipCleanBuild = true;
if (arg === '--generate-key') generateSigningKey = true;
}
console.log('🤖 ShopList Android Build Script\n');
// Check EAS CLI
await checkEASCLI();
// Check signing key
await checkAndroidSigningKey();
// Increment version
const version = await incrementVersion();
// Build Android
const buildOptions: BuildOptions = {
buildType,
skipCleanBuild,
generateSigningKey,
};
const success = await buildAndroid(buildOptions, version);
if (success) {
// Save deployment info
await saveDeploymentInfo(version, buildType);
// Show next steps
if (buildType === 'apk') {
await generateSideloadLink(version);
}
console.log('🎉 Android build process complete!\n');
} else {
process.exit(1);
}
}
main();