Skip to content

Commit 84ec2d9

Browse files
feat(build): add Google Services configuration script
- Add prepareGoogleServices.js for handling google-services.json - Support base64 encoded content from environment variables - Add fallback support for legacy JSON string format - Include SHA256 hash verification for content validation
1 parent e75c6ef commit 84ec2d9

1 file changed

Lines changed: 77 additions & 0 deletions

File tree

scripts/prepareGoogleServices.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Prepare google-services.json from env variables for Android builds.
2+
// Priority:
3+
// 1. GOOGLE_SERVICES_JSON_BASE64 (base64 encoded content)
4+
// 2. (deprecated) GOOGLE_SERVICES_JSON (raw JSON string or file path)
5+
// Output: ./google-services.json (git ignored)
6+
7+
const fs = require('fs');
8+
const crypto = require('crypto');
9+
10+
const TARGET = './google-services.json';
11+
const BASE64 = process.env.GOOGLE_SERVICES_JSON_BASE64 || process.env.GOOGLE_SERVICES_BASE64; // legacy alias
12+
const LEGACY_INLINE = process.env.GOOGLE_SERVICES_JSON; // deprecated
13+
14+
const isLikelyJson = (str) => !!str && str.trim().startsWith('{') && str.trim().endsWith('}');
15+
const sha256 = (d) => crypto.createHash('sha256').update(d).digest('hex').slice(0, 12);
16+
17+
function writeFileIfChanged(path, content) {
18+
if (fs.existsSync(path)) {
19+
const existing = fs.readFileSync(path, 'utf8');
20+
if (existing === content) return false; // unchanged
21+
}
22+
fs.writeFileSync(path, content, 'utf8');
23+
return true;
24+
}
25+
26+
function handleBase64(b64) {
27+
const decoded = Buffer.from(b64, 'base64').toString('utf8');
28+
if (!isLikelyJson(decoded)) {
29+
throw new Error('Decoded content is not valid JSON');
30+
}
31+
const changed = writeFileIfChanged(TARGET, decoded);
32+
console.log(`✅ google-services.json from BASE64 (${changed ? 'written' : 'unchanged'}) sha=${sha256(decoded)}`);
33+
return true;
34+
}
35+
36+
function handleInline(value) {
37+
if (isLikelyJson(value)) {
38+
const changed = writeFileIfChanged(TARGET, value);
39+
console.log(`✅ google-services.json from inline JSON (${changed ? 'written' : 'unchanged'}) sha=${sha256(value)}`);
40+
return true;
41+
}
42+
if (fs.existsSync(value)) {
43+
const fileContent = fs.readFileSync(value, 'utf8');
44+
if (!isLikelyJson(fileContent)) {
45+
console.warn('⚠️ Referenced file content does not look like JSON.');
46+
}
47+
const changed = writeFileIfChanged(TARGET, fileContent);
48+
console.log(`✅ google-services.json copied from path (${changed ? 'written' : 'unchanged'}) sha=${sha256(fileContent)}`);
49+
return true;
50+
}
51+
console.warn('⚠️ Inline var not JSON and path not found:', value);
52+
return false;
53+
}
54+
55+
function main() {
56+
try {
57+
if (BASE64) {
58+
handleBase64(BASE64);
59+
return;
60+
}
61+
if (LEGACY_INLINE) {
62+
handleInline(LEGACY_INLINE);
63+
return;
64+
}
65+
if (fs.existsSync(TARGET)) {
66+
const existing = fs.readFileSync(TARGET, 'utf8');
67+
console.log(`ℹ️ Using existing google-services.json sha=${sha256(existing)}`);
68+
} else {
69+
console.log('ℹ️ No env provided; google-services.json not generated (expected in local dev or provided later).');
70+
}
71+
} catch (err) {
72+
console.error('❌ Failed preparing google-services.json:', err.message || err);
73+
process.exit(1);
74+
}
75+
}
76+
77+
main();

0 commit comments

Comments
 (0)