codeplay-common 2.1.13 → 2.1.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,835 +1,835 @@
1
- /*
2
- Can be run like
3
- node finalrelease
4
- node finalrelease 1
5
- node finalrelease "1,2,7"
6
- */
7
-
8
- import { existsSync, readFileSync, readdirSync, promises, constants, writeFileSync, unlinkSync, mkdirSync, renameSync } from 'fs';
9
- import { join, resolve } from 'path';
10
- import { execSync } from 'child_process';
11
- import { createInterface } from 'readline';
12
-
13
- import { fileURLToPath } from 'url';
14
- import { dirname } from 'path';
15
-
16
- /* const path = require('path');
17
- const fs = require('fs'); */
18
-
19
- import fs from 'fs';
20
- import path from 'path';
21
-
22
- // Define a mapping between store IDs and store names
23
- /* const storeNames = {
24
- "1": "PlayStore",
25
- "2": "SamsungStore",
26
- "7": "AmazonStore"
27
- }; */
28
-
29
- const storeNames = {
30
- "1": "PlayStore",
31
- "2": "SamsungStore",
32
- "7": "AmazonStore",
33
-
34
- "3": "MiStore",
35
- "4": "HuaweiStore",
36
- "5": "OppoStore",
37
- //"6": "iOSStore",
38
- "8": "VivoStore"
39
- };
40
-
41
-
42
- //1=> PlayStore 2=> SamsungStore 3=>MiStore 4=>HuaweiStore 5=>OppoStore 6=>iOSStore 7=> AmazonStore 8=> VivoStore
43
-
44
-
45
-
46
-
47
-
48
-
49
-
50
- let isAdmobFound = false;
51
-
52
- const amazonMinSdkVersion=23;
53
-
54
- const androidManifestPath = join("android", "app", "src", "main", "AndroidManifest.xml");
55
-
56
-
57
- const configPath = path.join(process.cwd(), 'capacitor.config.json');
58
- const config = JSON.parse(fs.readFileSync(configPath, 'utf8'));
59
-
60
- const _appUniqueId = config.android?.APP_UNIQUE_ID;
61
- const _appName=config.appName;
62
- const _appPackageId=config.appId;
63
-
64
-
65
-
66
- const __filename = fileURLToPath(import.meta.url);
67
- const __dirname = dirname(__filename);
68
-
69
- function fileExists(filePath) {
70
- return existsSync(filePath);
71
- }
72
-
73
-
74
- const ffmpegPluginXmlPath = path.join(__dirname, 'node_modules', 'codeplay-fmpg', 'plugin.xml');
75
- const capacitorConfigPath = join(process.cwd(), 'capacitor.config.json');
76
-
77
-
78
-
79
- function isFfmpegUsed() {
80
- try {
81
- const xmlContent = fs.readFileSync(ffmpegPluginXmlPath, 'utf8');
82
- // Simple check: if plugin.xml exists and contains <plugin> tag (or any FFmpeg-specific tag)
83
- return /<plugin\b/.test(xmlContent);
84
- } catch (err) {
85
- console.log('⚠️ FFmpeg plugin not found:', err.message);
86
- return false;
87
- }
88
- }
89
-
90
- const _isffmpegUsed = isFfmpegUsed();
91
-
92
- function getAdMobConfig() {
93
- if (!fileExists(capacitorConfigPath)) {
94
- throw new Error('❌ capacitor.config.json not found. Ensure this is a Capacitor project.');
95
- }
96
-
97
- const config = JSON.parse(readFileSync(capacitorConfigPath, 'utf8'));
98
- const admobConfig = config.plugins?.AdMob;
99
-
100
- //_appPackageId=config.appId;
101
-
102
- if (!admobConfig) {
103
- throw new Error('❌ AdMob configuration is missing in capacitor.config.json.');
104
- }
105
-
106
- // Default to true if ADMOB_ENABLED is not specified
107
- const isEnabled = admobConfig.ADMOB_ENABLED !== false;
108
-
109
- if (!isEnabled) {
110
- return { ADMOB_ENABLED: false }; // Skip further validation
111
- }
112
-
113
- if (!admobConfig.APP_ID_ANDROID || !admobConfig.APP_ID_IOS) {
114
- throw new Error(' ❌ AdMob configuration is incomplete. Ensure APP_ID_ANDROID and APP_ID_IOS are defined.');
115
- }
116
-
117
- return {
118
- ADMOB_ENABLED: true,
119
- APP_ID_ANDROID: admobConfig.APP_ID_ANDROID,
120
- APP_ID_IOS: admobConfig.APP_ID_IOS,
121
- USE_LITE_ADS: admobConfig.USE_LITE_ADS === "lite",
122
- };
123
- }
124
-
125
-
126
-
127
- const _capacitorConfig = getAdMobConfig();
128
-
129
- const _isADMOB_ENABLED=_capacitorConfig.ADMOB_ENABLED;
130
-
131
- // Proceed only if ADMOB_ENABLED is true
132
- //if (_capacitorConfig.ADMOB_ENABLED)
133
-
134
-
135
- const admobConfigPath = join('src', 'js','Ads', 'admob-ad-configuration.json');
136
- let admobConfig;
137
-
138
- if (_isADMOB_ENABLED)
139
- {
140
- try
141
- {
142
- admobConfig = JSON.parse(readFileSync(admobConfigPath, 'utf8'));
143
- }
144
- catch (err)
145
- {
146
- console.error("❌ Failed to read admob-ad-configuration.json", err);
147
- process.exit(1);
148
- }
149
- }
150
-
151
-
152
-
153
-
154
-
155
-
156
-
157
-
158
-
159
-
160
-
161
-
162
-
163
-
164
-
165
-
166
-
167
-
168
-
169
-
170
-
171
-
172
- const checkCommonFileStoreId=()=>{
173
- const possibleConfigFiles = ['vite.config.mjs', 'vite.config.js'];
174
- let viteConfigPath;
175
- for (const configFile of possibleConfigFiles) {
176
- const fullPath = resolve( configFile);
177
- if (existsSync(fullPath)) {
178
- viteConfigPath = fullPath;
179
- break;
180
- }
181
- }
182
-
183
- if (!viteConfigPath) {
184
- console.error('❌ Error: No vite.config.mjs or vite.config.js file found.');
185
- process.exit(1);
186
- }
187
-
188
- try {
189
- // Read vite config file
190
- const viteConfigContent = readFileSync(viteConfigPath, 'utf-8');
191
-
192
- // Extract @common alias path
193
- const aliasPattern = /'@common':\s*path\.resolve\(__dirname,\s*'(.+?)'\)/;
194
- const match = viteConfigContent.match(aliasPattern);
195
-
196
- if (!match) {
197
- console.error(`❌ Error: @common alias not found in ${viteConfigPath}`);
198
- process.exit(1);
199
- }
200
-
201
- const commonFilePath = match[1];
202
- const resolvedCommonPath = resolve(__dirname, commonFilePath);
203
-
204
- // Read the common file content
205
- if (!existsSync(resolvedCommonPath)) {
206
- console.error(`❌ Error: Resolved common file does not exist: ${resolvedCommonPath}`);
207
- process.exit(1);
208
- }
209
-
210
- const commonFileContent = readFileSync(resolvedCommonPath, 'utf-8');
211
-
212
- // Check for the _storeid export line
213
- const storeIdPattern = /export\s+let\s+_storeid\s*=\s*import\.meta\.env\.VITE_STORE_ID\s*\|\|\s*1\s*;/;
214
- if (!storeIdPattern.test(commonFileContent)) {
215
- console.error(`❌ Error: _storeid value is wrong in ${commonFilePath}`);
216
- process.exit(1);
217
- }
218
-
219
- console.log(commonFilePath,'Success - No problem found');
220
- } catch (error) {
221
- console.error('❌ Error:', error);
222
- process.exit(1);
223
- }
224
-
225
- }
226
-
227
- const checkIsTestingInAdmob=()=>{
228
-
229
- if (admobConfig.config && admobConfig.config.isTesting === true) {
230
- console.error(`❌ Problem found while generating the AAB file. Please change "isTesting: true" to "isTesting: false" in the "admob-ad-configuration.json" file.`);
231
- process.exit(1); // Exit with an error code to halt the process
232
- } else {
233
- console.log('✅ No problem found. "isTesting" is either already false or not defined.');
234
- }
235
- }
236
-
237
- const checkIsAdsDisableByReturnStatement=()=>{
238
-
239
-
240
- const adsFolder = join('src', 'js', 'Ads');
241
- const filePattern = /^admob-emi-(\d+\.)+\d+\.js$/;
242
-
243
- // Step 1: Find the admob file
244
- const files = readdirSync(adsFolder);
245
- const admobFile = files.find(f => filePattern.test(f));
246
-
247
- if (!admobFile) {
248
- console.log('❌ No Admob file found.');
249
- process.exit(1);
250
- }
251
-
252
- const filePath = join(adsFolder, admobFile);
253
- const content = readFileSync(filePath, 'utf-8');
254
-
255
- // Step 2: Extract the adsOnDeviceReady function body
256
- const functionRegex = /async\s+function\s+adsOnDeviceReady\s*\([^)]*\)\s*{([\s\S]*?)^}/m;
257
- const match = content.match(functionRegex);
258
-
259
- if (!match) {
260
- console.log(`❌ Function 'adsOnDeviceReady' not found in file: ${admobFile}`);
261
- process.exit(1);
262
- }
263
-
264
- const body = match[1];
265
- const lines = body.split('\n').map(line => line.trim());
266
-
267
- // Step 3: Skip blank lines and comments, get the first real code line
268
- let firstCodeLine = '';
269
- for (const line of lines) {
270
- if (line === '' || line.startsWith('//')) continue;
271
- firstCodeLine = line;
272
- break;
273
- }
274
-
275
- // Step 4: Block if it's any of the unwanted returns
276
- const badReturnPattern = /^return\s*(true|false)?\s*;?$/;
277
-
278
- if (badReturnPattern.test(firstCodeLine)) {
279
- console.log(`❌ BLOCKED in file '${admobFile}': First active line in 'adsOnDeviceReady' is '${firstCodeLine}'`);
280
- process.exit(2);
281
- } else {
282
- console.log(`✅ Safe: No early return (true/false) found in 'adsOnDeviceReady' of file '${admobFile}'.`);
283
- }
284
- }
285
-
286
-
287
-
288
- const addPermission_AD_ID=async()=>{
289
-
290
- const admobPluginXmlPath = join('node_modules', 'emi-indo-cordova-plugin-admob', 'plugin.xml');
291
-
292
-
293
- //let isAdmobFound = false;
294
- try {
295
- await promises.access(admobPluginXmlPath, constants.F_OK);
296
- isAdmobFound = true;
297
- } catch (err) {
298
- isAdmobFound = false;
299
- }
300
-
301
-
302
-
303
- if (isAdmobFound) {
304
- if (existsSync(androidManifestPath)) {
305
- let manifestContent = readFileSync(androidManifestPath, 'utf8');
306
- let modified = false;
307
-
308
- // --- Step 1: Ensure AD_ID permission exists ---
309
- const adIdPermission = '<uses-permission android:name="com.google.android.gms.permission.AD_ID" />';
310
- if (!manifestContent.includes(adIdPermission)) {
311
- console.log("📄 AD_ID permission not found. Adding to AndroidManifest.xml.");
312
- manifestContent = manifestContent.replace('</manifest>', ` ${adIdPermission}\n</manifest>`);
313
- console.log("✅ AD_ID permission added successfully.");
314
- modified = true;
315
- } else {
316
- console.log("ℹ️ AD_ID permission already exists in AndroidManifest.xml.");
317
- }
318
-
319
- // --- Step 2: Ensure OPTIMIZE_AD_LOADING meta-data exists ---
320
- const optimizeAdMeta = `<meta-data android:name="com.google.android.gms.ads.flag.OPTIMIZE_AD_LOADING" android:value="true" />`;
321
-
322
- if (!manifestContent.includes(optimizeAdMeta)) {
323
- console.log("📄 OPTIMIZE_AD_LOADING meta-data not found. Adding to AndroidManifest.xml.");
324
-
325
- const appTagPattern = /<application[^>]*>/;
326
- if (appTagPattern.test(manifestContent)) {
327
- manifestContent = manifestContent.replace(appTagPattern, match => `${match}\n ${optimizeAdMeta}`);
328
- console.log("✅ OPTIMIZE_AD_LOADING meta-data added successfully.");
329
- modified = true;
330
- } else {
331
- console.error("❌ <application> tag not found in AndroidManifest.xml.");
332
- }
333
- } else {
334
- console.log("ℹ️ OPTIMIZE_AD_LOADING meta-data already exists in AndroidManifest.xml.");
335
- }
336
-
337
- // --- Step 3: Write only if modified ---
338
- if (modified) {
339
- writeFileSync(androidManifestPath, manifestContent, 'utf8');
340
- console.log("💾 AndroidManifest.xml updated successfully.");
341
- } else {
342
- console.log("✅ No changes needed. AndroidManifest.xml is up to date.");
343
- }
344
-
345
- } else {
346
- console.error("❌ AndroidManifest.xml not found at the specified path.");
347
- }
348
- } else {
349
- console.log("\x1b[33m%s\x1b[0m", "⚠️ No AdMob found, so AD_ID permission and meta-data were not added");
350
- }
351
-
352
-
353
-
354
-
355
-
356
-
357
- }
358
-
359
-
360
-
361
-
362
-
363
-
364
-
365
-
366
-
367
-
368
-
369
- checkCommonFileStoreId();
370
-
371
-
372
-
373
- if (_isADMOB_ENABLED)
374
- {
375
- checkIsTestingInAdmob();
376
- checkIsAdsDisableByReturnStatement()
377
-
378
- await addPermission_AD_ID()
379
- }
380
-
381
-
382
-
383
-
384
-
385
-
386
- const { playstore, samsung, amazon } = (_isADMOB_ENABLED && admobConfig.IAP) ? admobConfig.IAP : { playstore: false, samsung: false, amazon: false };
387
- console.log(`ℹ️ IAP Configurations - PlayStore: ${playstore}, Samsung: ${samsung}, Amazon: ${amazon}`);
388
-
389
-
390
-
391
-
392
-
393
-
394
- // Get the store ID from the command line arguments
395
- /* const storeIdArg = ["2","3"]//process.argv[2]; // Get the store ID from the command line
396
- const storeIds = storeIdArg ? storeIdArg : Object.keys(storeNames);//["1", "2", "7"]; // If a specific ID is provided, use it; otherwise, use all store IDs
397
-
398
- debugger; */
399
-
400
- let storeIdArg = process.argv[2]; // command-line argument
401
- let storeIds;
402
-
403
- if (storeIdArg) {
404
- try {
405
- // Try parsing as JSON array first
406
- const parsed = JSON.parse(storeIdArg);
407
- if (Array.isArray(parsed)) {
408
- storeIds = parsed.map(String); // ensure strings
409
- } else {
410
- storeIds = [parsed.toString()];
411
- }
412
- } catch (e) {
413
- // Not JSON, assume comma-separated string
414
- storeIds = storeIdArg.split(",").map(s => s.trim());
415
- }
416
- } else {
417
- // No argument, use all store IDs
418
- storeIds = Object.keys(storeNames);
419
- }
420
-
421
-
422
- // Store the original minSdkVersion globally
423
- let originalMinSdkVersion;
424
-
425
- // Remove any existing AAB files before starting the build process
426
- const aabDirectory = join("android", "app", "build", "outputs", "bundle", "release");
427
- if (existsSync(aabDirectory)) {
428
- const files = readdirSync(aabDirectory).filter(file => file.endsWith('.aab'));
429
- files.forEach(file => {
430
- const filePath = join(aabDirectory, file);
431
- unlinkSync(filePath);
432
- console.log(`ℹ️ Deleted existing AAB file: ${file}`);
433
- });
434
- }
435
-
436
- const aabOutputDir = join("AAB");
437
- if (!existsSync(aabOutputDir)) {
438
- mkdirSync(aabOutputDir);
439
- console.log(`Created directory: ${aabOutputDir}`);
440
- }
441
-
442
- if (existsSync(aabOutputDir)) {
443
- const files = readdirSync(aabOutputDir).filter(
444
- file => file.endsWith('.aab') || file.endsWith('.apk')
445
- );
446
-
447
- files.forEach(file => {
448
- const filePath = join(aabOutputDir, file);
449
- unlinkSync(filePath);
450
- console.log(`Deleted existing build file: ${file}`);
451
- });
452
- }
453
-
454
-
455
- // Extract version code and version name from build.gradle
456
- const gradleFilePath = join("android", "app", "build.gradle");
457
- const gradleContent = readFileSync(gradleFilePath, 'utf8');
458
-
459
- const versionCodeMatch = gradleContent.match(/versionCode\s+(\d+)/);
460
- const versionNameMatch = gradleContent.match(/versionName\s+"([^"]+)"/);
461
-
462
- const versionCode = versionCodeMatch ? versionCodeMatch[1] : '';
463
- const versionName = versionNameMatch ? versionNameMatch[1] : '';
464
-
465
- // Display the current versionCode and versionName
466
- console.log(`Current versionCode: ${versionCode}`);
467
- console.log(`Current versionName: ${versionName}`);
468
-
469
- // Create an interface for user input
470
- const rl = createInterface({
471
- input: process.stdin,
472
- output: process.stdout
473
- });
474
-
475
- // Ask for new versionCode
476
- rl.question('Enter new versionCode (press enter to keep current): ', (newVersionCode) => {
477
- const finalVersionCode = newVersionCode || versionCode; // Use existing if no input
478
-
479
- // Ask for new versionName
480
- rl.question('Enter new versionName (press enter to keep current): ', (newVersionName) => {
481
- const finalVersionName = newVersionName || versionName; // Use existing if no input
482
-
483
- // Log the final version details
484
- console.log(`📦 Final versionCode: ${finalVersionCode}`);
485
- console.log(`📝 Final versionName: ${finalVersionName}`);
486
-
487
-
488
-
489
- // Update build.gradle with the new version details
490
- let updatedGradleContent = gradleContent
491
- .replace(/versionCode\s+\d+/, `versionCode ${finalVersionCode}`)
492
- .replace(/versionName\s+"[^"]+"/, `versionName "${finalVersionName}"`);
493
-
494
- // Check if resConfigs "en" already exists
495
- const resConfigsLine = ' resConfigs "en"';
496
- if (!updatedGradleContent.includes(resConfigsLine)) {
497
- // Add resConfigs "en" below versionName
498
- updatedGradleContent = updatedGradleContent.replace(/versionName\s+"[^"]+"/, `versionName "${finalVersionName}"\n${resConfigsLine}`);
499
- } else {
500
- console.log('ℹ️ resConfigs "en" already exists in build.gradle.');
501
- }
502
-
503
-
504
-
505
- // List of package IDs for which minify should be false
506
-
507
-
508
- // Determine desired minify value
509
- const desiredMinify = !_isffmpegUsed;
510
-
511
- // Check if minifyEnabled is already present
512
- if (/minifyEnabled\s+(true|false)/.test(updatedGradleContent)) {
513
- // Replace existing value with desired
514
- updatedGradleContent = updatedGradleContent.replace(/minifyEnabled\s+(true|false)/, `minifyEnabled ${desiredMinify}`);
515
- console.log(`Replaced minifyEnabled with ${desiredMinify}.`);
516
- } else if (/buildTypes\s*{[\s\S]*?release\s*{/.test(updatedGradleContent)) {
517
- // Insert minifyEnabled if not present
518
- updatedGradleContent = updatedGradleContent.replace(
519
- /(buildTypes\s*{[\s\S]*?release\s*{)/,
520
- `$1\n minifyEnabled ${desiredMinify}`
521
- );
522
- console.log(`✅ Inserted minifyEnabled ${desiredMinify} into release block.`);
523
- } else {
524
- console.log('⚠️ Warning: buildTypes > release block not found. minifyEnabled was not added.');
525
- }
526
-
527
-
528
-
529
-
530
-
531
-
532
- // Write the updated gradle content back to build.gradle
533
- writeFileSync(gradleFilePath, updatedGradleContent, 'utf8');
534
- console.log(`✅ Updated build.gradle with versionCode: ${finalVersionCode}, versionName: ${finalVersionName}, resConfigs "en" and "minifyEnabled true"`);
535
-
536
- storeIds.forEach((id) => {
537
- console.log(`ℹ️ Building for Store ID ${id}`);
538
-
539
- // Set the environment variable for store ID
540
- process.env.VITE_STORE_ID = id;
541
-
542
- // Conditionally set the new file name
543
- let newFileName;
544
- let storeName = storeNames[id];
545
-
546
- debugger;
547
-
548
-
549
- managePackages(storeName);
550
-
551
- if (storeName === "SamsungStore") {
552
- // For SamsungStore, rename to versionCode value only
553
- //newFileName = `${finalVersionCode}.aab`;
554
- newFileName=`${_appUniqueId}_${_appName.replaceAll(" ","_")}-${(storeName.toUpperCase()).replace("STORE","")}-b${finalVersionCode}-v${finalVersionName.replace(/\./g,'_')}.aab`
555
- } else {
556
- // For other stores, use the standard naming format
557
- //newFileName = `app-release-signed-${storeName}-b${finalVersionCode}-v${finalVersionName}.aab`;
558
-
559
- newFileName=`${_appUniqueId}_${_appName.replaceAll(" ","_")}-${(storeName.toUpperCase()).replace("STORE","")}-b${finalVersionCode}-v${finalVersionName}.aab`
560
-
561
-
562
- /*
563
- const _appUniqueId = config.android?.APP_UNIQUE_ID;
564
- const _appName=config.appName;
565
- const _appPackageId=config.appId;
566
- */
567
-
568
- }
569
-
570
- //storeName="amazon"
571
- const checkFullPath = join("AAB", newFileName); // Update to point to the new AAB directory
572
-
573
- // Modify minSdkVersion in variables.gradle for SamsungStore
574
- const variablesGradleFilePath = join("android", "variables.gradle");
575
- let variablesGradleContent = readFileSync(variablesGradleFilePath, 'utf8');
576
-
577
- // Extract the current minSdkVersion
578
- const minSdkVersionMatch = variablesGradleContent.match(/minSdkVersion\s*=\s*(\d+)/);
579
- const currentMinSdkVersion = minSdkVersionMatch ? parseInt(minSdkVersionMatch[1], 10) : null;
580
-
581
- // Store the original minSdkVersion (only on the first iteration)
582
- if (!originalMinSdkVersion) {
583
- originalMinSdkVersion = currentMinSdkVersion;
584
- }
585
- try {
586
- // Modify the minSdkVersion based on the store
587
-
588
-
589
- if(currentMinSdkVersion==23 || currentMinSdkVersion==24)
590
- {
591
- if (storeName === "SamsungStore" || storeName === "PlayStore" ||
592
- //_appPackageId=="vfx.green.editor" || _appPackageId=="audio.music.sound.editor" || _appPackageId=="video.to.gif.maker"
593
-
594
- //ffmpegUsedPackages.includes(_appPackageId)
595
- _isffmpegUsed
596
- ) {
597
- if (currentMinSdkVersion !== 24) {
598
- variablesGradleContent = variablesGradleContent.replace(/minSdkVersion\s*=\s*\d+/, 'minSdkVersion = 24');
599
- console.log('minSdkVersion updated to 24 for SamsungStore & PlayStore');
600
- writeFileSync(variablesGradleFilePath, variablesGradleContent);
601
- }
602
- } else {
603
- // For PlayStore and AmazonStore, ensure minSdkVersion is originalMinSdkVersion
604
- //if (currentMinSdkVersion !== originalMinSdkVersion) {
605
- variablesGradleContent = variablesGradleContent.replace(/minSdkVersion\s*=\s*\d+/, `minSdkVersion = ${amazonMinSdkVersion}`);
606
- console.log(`minSdkVersion reverted to ${amazonMinSdkVersion} for ${storeName}`);
607
- writeFileSync(variablesGradleFilePath, variablesGradleContent);
608
- //}
609
- }
610
- }
611
-
612
-
613
- // Run the Node.js script to modify plugin.xml
614
- if (isAdmobFound) {
615
- execSync('node buildCodeplay/modify-plugin-xml.js', { stdio: 'inherit' });
616
- } else {
617
- console.log("\x1b[33m%s\x1b[0m", "Seems to Pro Version [No ads found]");
618
- }
619
-
620
- // Run the Vite build
621
- execSync(`npm run build:storeid${id}`, { stdio: 'inherit' });
622
-
623
-
624
-
625
- // Copy the built files to the appropriate folder
626
- const src = join("www", "*");
627
- const dest = join("android", "app", "src", "main", "assets", "public");
628
-
629
- // Use 'xcopy' command for Windows
630
- execSync(`xcopy ${src} ${dest} /E /I /Y`, { stdio: 'inherit' });
631
-
632
- // Build Android AAB file
633
- //child_process.execSync('cd android && ./gradlew bundleRelease', { stdio: 'inherit' });
634
-
635
-
636
- // Build Android AAB or APK file based on store
637
- console.log(`🏗️ Building release for ${storeName}...`);
638
-
639
- let buildType = "AAB";
640
- if (["VivoStore", "OppoStore", "MiStore"].includes(storeName)) {
641
- buildType = "APK";
642
- }
643
-
644
- execSync('npx cap sync android', { stdio: 'inherit' });
645
- execSync(`npx cap build android --androidreleasetype=${buildType}`, { stdio: 'inherit' });
646
-
647
- // Determine output paths
648
- let oldFilePath;
649
- let newExt = buildType === "APK" ? "apk" : "aab";
650
-
651
- if (buildType === "APK") {
652
- oldFilePath = join("android", "app", "build", "outputs", "apk", "release", "app-release-signed.apk");
653
- } else {
654
- oldFilePath = join("android", "app", "build", "outputs", "bundle", "release", "app-release-signed.aab");
655
- }
656
-
657
- const checkFullPath = join("AAB", newFileName.replace(/\.aab$/, `.${newExt}`));
658
-
659
- // Rename the output file
660
- if (existsSync(oldFilePath)) {
661
- renameSync(oldFilePath, checkFullPath);
662
- console.log(`✅ Renamed output ${newExt.toUpperCase()} file to: ${path.basename(checkFullPath)}`);
663
- } else {
664
- console.error(`❌ ${newExt.toUpperCase()} file not found after build.`);
665
- }
666
-
667
- } catch (error) {
668
- console.error(`❌ Error during build for Store ID ${id}:`, error);
669
- process.exit(1);
670
- }
671
- });
672
-
673
- rl.close(); // Close the readline interface after all operations
674
- });
675
- });
676
-
677
-
678
-
679
-
680
-
681
- function managePackages(store) {
682
- console.log(`IAP Configurations - PlayStore: ${playstore}, Samsung: ${samsung}, Amazon: ${amazon}`);
683
-
684
- let install = "";
685
- let uninstall = "";
686
-
687
-
688
-
689
- let manifestContent = readFileSync(androidManifestPath, 'utf-8');
690
-
691
- const permissionsToRemove = [
692
- 'com.android.vending.BILLING',
693
- 'com.samsung.android.iap.permission.BILLING'
694
- ];
695
-
696
-
697
- permissionsToRemove.forEach(permission => {
698
- const permissionRegex = new RegExp(`^\\s*<uses-permission\\s+android:name="${permission}"\\s*/?>\\s*[\r\n]?`, 'm');
699
- if (permissionRegex.test(manifestContent)) {
700
- manifestContent = manifestContent.replace(permissionRegex, '');
701
- console.log(`✅ Removed <uses-permission android:name="${permission}" /> from AndroidManifest.xml`);
702
- }
703
- });
704
-
705
- // Write the updated content back to the file
706
- writeFileSync(androidManifestPath, manifestContent, 'utf-8');
707
-
708
-
709
-
710
- if ((playstore && store === "PlayStore") || (amazon && store === "AmazonStore")) {
711
- install = '@revenuecat/purchases-capacitor';
712
- uninstall = 'cordova-plugin-samsungiap';
713
-
714
- // Update AndroidManifest.xml for PlayStore
715
- if(playstore)
716
- updateAndroidManifest(store,
717
- '<uses-permission android:name="com.android.vending.BILLING" />');
718
-
719
- } else if (samsung && store === "SamsungStore") {
720
- install = 'cordova-plugin-samsungiap';
721
- uninstall = '@revenuecat/purchases-capacitor';
722
-
723
- // Update AndroidManifest.xml for SamsungStore
724
- updateAndroidManifest(store,
725
- '<uses-permission android:name="com.samsung.android.iap.permission.BILLING" />');
726
-
727
- } else {
728
- console.log("No valid store specified or no configurations found. Both plugins will be uninstalled.");
729
- try {
730
- execSync(`npm uninstall cordova-plugin-samsungiap`, { stdio: 'inherit' });
731
- execSync(`npm uninstall @revenuecat/purchases-capacitor`, { stdio: 'inherit' });
732
- console.log(`✅ Both plugins uninstalled successfully.`);
733
- } catch (err) {
734
- console.error("❌ Error uninstalling plugins:", err);
735
- }
736
- return;
737
- }
738
-
739
- console.log(`⚠️ Installing ${install} and uninstalling ${uninstall} for ${store}...`);
740
- try {
741
- if (install) {
742
- execSync(`npm install ${install}`, { stdio: 'inherit' });
743
- }
744
- if (uninstall) {
745
- execSync(`npm uninstall ${uninstall}`, { stdio: 'inherit' });
746
- }
747
- console.log(`✅ ${install} installed and ${uninstall} uninstalled successfully.`);
748
- } catch (err) {
749
- console.error(`❌ Error managing packages for ${store}:`, err);
750
- }
751
- }
752
-
753
-
754
-
755
- function updateAndroidManifest(store, addPermission) {
756
- try {
757
- if (!existsSync(androidManifestPath)) {
758
- console.error("❌ AndroidManifest.xml file not found!");
759
- return;
760
- }
761
-
762
- // Read the content of the AndroidManifest.xml
763
- let manifestContent = readFileSync(androidManifestPath, 'utf-8');
764
-
765
- // Normalize line endings to `\n` for consistent processing
766
- manifestContent = manifestContent.replace(/\r\n/g, '\n');
767
-
768
- // Check if the permission is already present
769
- if (manifestContent.includes(addPermission.trim())) {
770
- console.log(`${addPermission} is already in the AndroidManifest.xml. Skipping addition.`);
771
- return; // Skip if the permission is already present
772
- }
773
-
774
- // Insert the permission before the closing </manifest> tag
775
- const closingTag = '</manifest>';
776
- const formattedPermission = ` ${addPermission.trim()}\n`;
777
- if (manifestContent.includes(closingTag)) {
778
- manifestContent = manifestContent.replace(
779
- closingTag,
780
- `${formattedPermission}${closingTag}`
781
- );
782
- console.log(`✅ Added ${addPermission} before </manifest> tag.`);
783
- } else {
784
- console.warn(`⚠️ </manifest> tag not found. Adding ${addPermission} at the end of the file.`);
785
- manifestContent += `\n${formattedPermission}`;
786
- }
787
-
788
- // Normalize line endings back to `\r\n` and write the updated content
789
- manifestContent = manifestContent.replace(/\n/g, '\r\n');
790
- writeFileSync(androidManifestPath, manifestContent, 'utf-8');
791
- console.log(`✅ AndroidManifest.xml updated successfully for ${store}`);
792
- } catch (err) {
793
- console.error(`❌ Error updating AndroidManifest.xml for ${store}:`, err);
794
- }
795
- }
796
-
797
-
798
-
799
- /* function updateAndroidManifest1(store, addPermission) {
800
- try {
801
- if (!fs.existsSync(androidManifestPath)) {
802
- console.error("AndroidManifest.xml file not found!");
803
- return;
804
- }
805
-
806
- let manifestContent = fs.readFileSync(androidManifestPath, 'utf-8');
807
-
808
-
809
-
810
- // Add the required permission if not already present
811
- if (!manifestContent.includes(addPermission)) {
812
- const manifestLines = manifestContent.split('\n');
813
- const insertIndex = manifestLines.findIndex(line => line.trim().startsWith('<application'));
814
- if (insertIndex > -1) {
815
- manifestLines.splice(insertIndex, 0, ` ${addPermission}`);
816
- manifestContent = manifestLines.join('\n');
817
- console.log(`Added ${addPermission} to AndroidManifest.xml`);
818
- }
819
- }
820
-
821
- // Write the updated content back to the file
822
- fs.writeFileSync(androidManifestPath, manifestContent, 'utf-8');
823
- console.log(`AndroidManifest.xml updated successfully for ${store}`);
824
- } catch (err) {
825
- console.error(`Error updating AndroidManifest.xml for ${store}:`, err);
826
- }
827
- } */
828
-
829
-
830
-
831
-
832
-
833
-
834
-
835
-
1
+ /*
2
+ Can be run like
3
+ node finalrelease
4
+ node finalrelease 1
5
+ node finalrelease "1,2,7"
6
+ */
7
+
8
+ import { existsSync, readFileSync, readdirSync, promises, constants, writeFileSync, unlinkSync, mkdirSync, renameSync } from 'fs';
9
+ import { join, resolve } from 'path';
10
+ import { execSync } from 'child_process';
11
+ import { createInterface } from 'readline';
12
+
13
+ import { fileURLToPath } from 'url';
14
+ import { dirname } from 'path';
15
+
16
+ /* const path = require('path');
17
+ const fs = require('fs'); */
18
+
19
+ import fs from 'fs';
20
+ import path from 'path';
21
+
22
+ // Define a mapping between store IDs and store names
23
+ /* const storeNames = {
24
+ "1": "PlayStore",
25
+ "2": "SamsungStore",
26
+ "7": "AmazonStore"
27
+ }; */
28
+
29
+ const storeNames = {
30
+ "1": "PlayStore",
31
+ "2": "SamsungStore",
32
+ "7": "AmazonStore",
33
+
34
+ "3": "MiStore",
35
+ "4": "HuaweiStore",
36
+ "5": "OppoStore",
37
+ //"6": "iOSStore",
38
+ "8": "VivoStore"
39
+ };
40
+
41
+
42
+ //1=> PlayStore 2=> SamsungStore 3=>MiStore 4=>HuaweiStore 5=>OppoStore 6=>iOSStore 7=> AmazonStore 8=> VivoStore
43
+
44
+
45
+
46
+
47
+
48
+
49
+
50
+ let isAdmobFound = false;
51
+
52
+ const amazonMinSdkVersion=23;
53
+
54
+ const androidManifestPath = join("android", "app", "src", "main", "AndroidManifest.xml");
55
+
56
+
57
+ const configPath = path.join(process.cwd(), 'capacitor.config.json');
58
+ const config = JSON.parse(fs.readFileSync(configPath, 'utf8'));
59
+
60
+ const _appUniqueId = config.android?.APP_UNIQUE_ID;
61
+ const _appName=config.appName;
62
+ const _appPackageId=config.appId;
63
+
64
+
65
+
66
+ const __filename = fileURLToPath(import.meta.url);
67
+ const __dirname = dirname(__filename);
68
+
69
+ function fileExists(filePath) {
70
+ return existsSync(filePath);
71
+ }
72
+
73
+
74
+ const ffmpegPluginXmlPath = path.join(__dirname, 'node_modules', 'codeplay-fmpg', 'plugin.xml');
75
+ const capacitorConfigPath = join(process.cwd(), 'capacitor.config.json');
76
+
77
+
78
+
79
+ function isFfmpegUsed() {
80
+ try {
81
+ const xmlContent = fs.readFileSync(ffmpegPluginXmlPath, 'utf8');
82
+ // Simple check: if plugin.xml exists and contains <plugin> tag (or any FFmpeg-specific tag)
83
+ return /<plugin\b/.test(xmlContent);
84
+ } catch (err) {
85
+ console.log('⚠️ FFmpeg plugin not found:', err.message);
86
+ return false;
87
+ }
88
+ }
89
+
90
+ const _isffmpegUsed = isFfmpegUsed();
91
+
92
+ function getAdMobConfig() {
93
+ if (!fileExists(capacitorConfigPath)) {
94
+ throw new Error('❌ capacitor.config.json not found. Ensure this is a Capacitor project.');
95
+ }
96
+
97
+ const config = JSON.parse(readFileSync(capacitorConfigPath, 'utf8'));
98
+ const admobConfig = config.plugins?.AdMob;
99
+
100
+ //_appPackageId=config.appId;
101
+
102
+ if (!admobConfig) {
103
+ throw new Error('❌ AdMob configuration is missing in capacitor.config.json.');
104
+ }
105
+
106
+ // Default to true if ADMOB_ENABLED is not specified
107
+ const isEnabled = admobConfig.ADMOB_ENABLED !== false;
108
+
109
+ if (!isEnabled) {
110
+ return { ADMOB_ENABLED: false }; // Skip further validation
111
+ }
112
+
113
+ if (!admobConfig.APP_ID_ANDROID || !admobConfig.APP_ID_IOS) {
114
+ throw new Error(' ❌ AdMob configuration is incomplete. Ensure APP_ID_ANDROID and APP_ID_IOS are defined.');
115
+ }
116
+
117
+ return {
118
+ ADMOB_ENABLED: true,
119
+ APP_ID_ANDROID: admobConfig.APP_ID_ANDROID,
120
+ APP_ID_IOS: admobConfig.APP_ID_IOS,
121
+ USE_LITE_ADS: admobConfig.USE_LITE_ADS === "lite",
122
+ };
123
+ }
124
+
125
+
126
+
127
+ const _capacitorConfig = getAdMobConfig();
128
+
129
+ const _isADMOB_ENABLED=_capacitorConfig.ADMOB_ENABLED;
130
+
131
+ // Proceed only if ADMOB_ENABLED is true
132
+ //if (_capacitorConfig.ADMOB_ENABLED)
133
+
134
+
135
+ const admobConfigPath = join('src', 'js','Ads', 'admob-ad-configuration.json');
136
+ let admobConfig;
137
+
138
+ if (_isADMOB_ENABLED)
139
+ {
140
+ try
141
+ {
142
+ admobConfig = JSON.parse(readFileSync(admobConfigPath, 'utf8'));
143
+ }
144
+ catch (err)
145
+ {
146
+ console.error("❌ Failed to read admob-ad-configuration.json", err);
147
+ process.exit(1);
148
+ }
149
+ }
150
+
151
+
152
+
153
+
154
+
155
+
156
+
157
+
158
+
159
+
160
+
161
+
162
+
163
+
164
+
165
+
166
+
167
+
168
+
169
+
170
+
171
+
172
+ const checkCommonFileStoreId=()=>{
173
+ const possibleConfigFiles = ['vite.config.mjs', 'vite.config.js'];
174
+ let viteConfigPath;
175
+ for (const configFile of possibleConfigFiles) {
176
+ const fullPath = resolve( configFile);
177
+ if (existsSync(fullPath)) {
178
+ viteConfigPath = fullPath;
179
+ break;
180
+ }
181
+ }
182
+
183
+ if (!viteConfigPath) {
184
+ console.error('❌ Error: No vite.config.mjs or vite.config.js file found.');
185
+ process.exit(1);
186
+ }
187
+
188
+ try {
189
+ // Read vite config file
190
+ const viteConfigContent = readFileSync(viteConfigPath, 'utf-8');
191
+
192
+ // Extract @common alias path
193
+ const aliasPattern = /'@common':\s*path\.resolve\(__dirname,\s*'(.+?)'\)/;
194
+ const match = viteConfigContent.match(aliasPattern);
195
+
196
+ if (!match) {
197
+ console.error(`❌ Error: @common alias not found in ${viteConfigPath}`);
198
+ process.exit(1);
199
+ }
200
+
201
+ const commonFilePath = match[1];
202
+ const resolvedCommonPath = resolve(__dirname, commonFilePath);
203
+
204
+ // Read the common file content
205
+ if (!existsSync(resolvedCommonPath)) {
206
+ console.error(`❌ Error: Resolved common file does not exist: ${resolvedCommonPath}`);
207
+ process.exit(1);
208
+ }
209
+
210
+ const commonFileContent = readFileSync(resolvedCommonPath, 'utf-8');
211
+
212
+ // Check for the _storeid export line
213
+ const storeIdPattern = /export\s+let\s+_storeid\s*=\s*import\.meta\.env\.VITE_STORE_ID\s*\|\|\s*1\s*;/;
214
+ if (!storeIdPattern.test(commonFileContent)) {
215
+ console.error(`❌ Error: _storeid value is wrong in ${commonFilePath}`);
216
+ process.exit(1);
217
+ }
218
+
219
+ console.log(commonFilePath,'Success - No problem found');
220
+ } catch (error) {
221
+ console.error('❌ Error:', error);
222
+ process.exit(1);
223
+ }
224
+
225
+ }
226
+
227
+ const checkIsTestingInAdmob=()=>{
228
+
229
+ if (admobConfig.config && admobConfig.config.isTesting === true) {
230
+ console.error(`❌ Problem found while generating the AAB file. Please change "isTesting: true" to "isTesting: false" in the "admob-ad-configuration.json" file.`);
231
+ process.exit(1); // Exit with an error code to halt the process
232
+ } else {
233
+ console.log('✅ No problem found. "isTesting" is either already false or not defined.');
234
+ }
235
+ }
236
+
237
+ const checkIsAdsDisableByReturnStatement=()=>{
238
+
239
+
240
+ const adsFolder = join('src', 'js', 'Ads');
241
+ const filePattern = /^admob-emi-(\d+\.)+\d+\.js$/;
242
+
243
+ // Step 1: Find the admob file
244
+ const files = readdirSync(adsFolder);
245
+ const admobFile = files.find(f => filePattern.test(f));
246
+
247
+ if (!admobFile) {
248
+ console.log('❌ No Admob file found.');
249
+ process.exit(1);
250
+ }
251
+
252
+ const filePath = join(adsFolder, admobFile);
253
+ const content = readFileSync(filePath, 'utf-8');
254
+
255
+ // Step 2: Extract the adsOnDeviceReady function body
256
+ const functionRegex = /async\s+function\s+adsOnDeviceReady\s*\([^)]*\)\s*{([\s\S]*?)^}/m;
257
+ const match = content.match(functionRegex);
258
+
259
+ if (!match) {
260
+ console.log(`❌ Function 'adsOnDeviceReady' not found in file: ${admobFile}`);
261
+ process.exit(1);
262
+ }
263
+
264
+ const body = match[1];
265
+ const lines = body.split('\n').map(line => line.trim());
266
+
267
+ // Step 3: Skip blank lines and comments, get the first real code line
268
+ let firstCodeLine = '';
269
+ for (const line of lines) {
270
+ if (line === '' || line.startsWith('//')) continue;
271
+ firstCodeLine = line;
272
+ break;
273
+ }
274
+
275
+ // Step 4: Block if it's any of the unwanted returns
276
+ const badReturnPattern = /^return\s*(true|false)?\s*;?$/;
277
+
278
+ if (badReturnPattern.test(firstCodeLine)) {
279
+ console.log(`❌ BLOCKED in file '${admobFile}': First active line in 'adsOnDeviceReady' is '${firstCodeLine}'`);
280
+ process.exit(2);
281
+ } else {
282
+ console.log(`✅ Safe: No early return (true/false) found in 'adsOnDeviceReady' of file '${admobFile}'.`);
283
+ }
284
+ }
285
+
286
+
287
+
288
+ const addPermission_AD_ID=async()=>{
289
+
290
+ const admobPluginXmlPath = join('node_modules', 'emi-indo-cordova-plugin-admob', 'plugin.xml');
291
+
292
+
293
+ //let isAdmobFound = false;
294
+ try {
295
+ await promises.access(admobPluginXmlPath, constants.F_OK);
296
+ isAdmobFound = true;
297
+ } catch (err) {
298
+ isAdmobFound = false;
299
+ }
300
+
301
+
302
+
303
+ if (isAdmobFound) {
304
+ if (existsSync(androidManifestPath)) {
305
+ let manifestContent = readFileSync(androidManifestPath, 'utf8');
306
+ let modified = false;
307
+
308
+ // --- Step 1: Ensure AD_ID permission exists ---
309
+ const adIdPermission = '<uses-permission android:name="com.google.android.gms.permission.AD_ID" />';
310
+ if (!manifestContent.includes(adIdPermission)) {
311
+ console.log("📄 AD_ID permission not found. Adding to AndroidManifest.xml.");
312
+ manifestContent = manifestContent.replace('</manifest>', ` ${adIdPermission}\n</manifest>`);
313
+ console.log("✅ AD_ID permission added successfully.");
314
+ modified = true;
315
+ } else {
316
+ console.log("ℹ️ AD_ID permission already exists in AndroidManifest.xml.");
317
+ }
318
+
319
+ // --- Step 2: Ensure OPTIMIZE_AD_LOADING meta-data exists ---
320
+ const optimizeAdMeta = `<meta-data android:name="com.google.android.gms.ads.flag.OPTIMIZE_AD_LOADING" android:value="true" />`;
321
+
322
+ if (!manifestContent.includes(optimizeAdMeta)) {
323
+ console.log("📄 OPTIMIZE_AD_LOADING meta-data not found. Adding to AndroidManifest.xml.");
324
+
325
+ const appTagPattern = /<application[^>]*>/;
326
+ if (appTagPattern.test(manifestContent)) {
327
+ manifestContent = manifestContent.replace(appTagPattern, match => `${match}\n ${optimizeAdMeta}`);
328
+ console.log("✅ OPTIMIZE_AD_LOADING meta-data added successfully.");
329
+ modified = true;
330
+ } else {
331
+ console.error("❌ <application> tag not found in AndroidManifest.xml.");
332
+ }
333
+ } else {
334
+ console.log("ℹ️ OPTIMIZE_AD_LOADING meta-data already exists in AndroidManifest.xml.");
335
+ }
336
+
337
+ // --- Step 3: Write only if modified ---
338
+ if (modified) {
339
+ writeFileSync(androidManifestPath, manifestContent, 'utf8');
340
+ console.log("💾 AndroidManifest.xml updated successfully.");
341
+ } else {
342
+ console.log("✅ No changes needed. AndroidManifest.xml is up to date.");
343
+ }
344
+
345
+ } else {
346
+ console.error("❌ AndroidManifest.xml not found at the specified path.");
347
+ }
348
+ } else {
349
+ console.log("\x1b[33m%s\x1b[0m", "⚠️ No AdMob found, so AD_ID permission and meta-data were not added");
350
+ }
351
+
352
+
353
+
354
+
355
+
356
+
357
+ }
358
+
359
+
360
+
361
+
362
+
363
+
364
+
365
+
366
+
367
+
368
+
369
+ checkCommonFileStoreId();
370
+
371
+
372
+
373
+ if (_isADMOB_ENABLED)
374
+ {
375
+ checkIsTestingInAdmob();
376
+ checkIsAdsDisableByReturnStatement()
377
+
378
+ await addPermission_AD_ID()
379
+ }
380
+
381
+
382
+
383
+
384
+
385
+
386
+ const { playstore, samsung, amazon } = (_isADMOB_ENABLED && admobConfig.IAP) ? admobConfig.IAP : { playstore: false, samsung: false, amazon: false };
387
+ console.log(`ℹ️ IAP Configurations - PlayStore: ${playstore}, Samsung: ${samsung}, Amazon: ${amazon}`);
388
+
389
+
390
+
391
+
392
+
393
+
394
+ // Get the store ID from the command line arguments
395
+ /* const storeIdArg = ["2","3"]//process.argv[2]; // Get the store ID from the command line
396
+ const storeIds = storeIdArg ? storeIdArg : Object.keys(storeNames);//["1", "2", "7"]; // If a specific ID is provided, use it; otherwise, use all store IDs
397
+
398
+ debugger; */
399
+
400
+ let storeIdArg = process.argv[2]; // command-line argument
401
+ let storeIds;
402
+
403
+ if (storeIdArg) {
404
+ try {
405
+ // Try parsing as JSON array first
406
+ const parsed = JSON.parse(storeIdArg);
407
+ if (Array.isArray(parsed)) {
408
+ storeIds = parsed.map(String); // ensure strings
409
+ } else {
410
+ storeIds = [parsed.toString()];
411
+ }
412
+ } catch (e) {
413
+ // Not JSON, assume comma-separated string
414
+ storeIds = storeIdArg.split(",").map(s => s.trim());
415
+ }
416
+ } else {
417
+ // No argument, use all store IDs
418
+ storeIds = Object.keys(storeNames);
419
+ }
420
+
421
+
422
+ // Store the original minSdkVersion globally
423
+ let originalMinSdkVersion;
424
+
425
+ // Remove any existing AAB files before starting the build process
426
+ const aabDirectory = join("android", "app", "build", "outputs", "bundle", "release");
427
+ if (existsSync(aabDirectory)) {
428
+ const files = readdirSync(aabDirectory).filter(file => file.endsWith('.aab'));
429
+ files.forEach(file => {
430
+ const filePath = join(aabDirectory, file);
431
+ unlinkSync(filePath);
432
+ console.log(`ℹ️ Deleted existing AAB file: ${file}`);
433
+ });
434
+ }
435
+
436
+ const aabOutputDir = join("AAB");
437
+ if (!existsSync(aabOutputDir)) {
438
+ mkdirSync(aabOutputDir);
439
+ console.log(`Created directory: ${aabOutputDir}`);
440
+ }
441
+
442
+ if (existsSync(aabOutputDir)) {
443
+ const files = readdirSync(aabOutputDir).filter(
444
+ file => file.endsWith('.aab') || file.endsWith('.apk')
445
+ );
446
+
447
+ files.forEach(file => {
448
+ const filePath = join(aabOutputDir, file);
449
+ unlinkSync(filePath);
450
+ console.log(`Deleted existing build file: ${file}`);
451
+ });
452
+ }
453
+
454
+
455
+ // Extract version code and version name from build.gradle
456
+ const gradleFilePath = join("android", "app", "build.gradle");
457
+ const gradleContent = readFileSync(gradleFilePath, 'utf8');
458
+
459
+ const versionCodeMatch = gradleContent.match(/versionCode\s+(\d+)/);
460
+ const versionNameMatch = gradleContent.match(/versionName\s+"([^"]+)"/);
461
+
462
+ const versionCode = versionCodeMatch ? versionCodeMatch[1] : '';
463
+ const versionName = versionNameMatch ? versionNameMatch[1] : '';
464
+
465
+ // Display the current versionCode and versionName
466
+ console.log(`Current versionCode: ${versionCode}`);
467
+ console.log(`Current versionName: ${versionName}`);
468
+
469
+ // Create an interface for user input
470
+ const rl = createInterface({
471
+ input: process.stdin,
472
+ output: process.stdout
473
+ });
474
+
475
+ // Ask for new versionCode
476
+ rl.question('Enter new versionCode (press enter to keep current): ', (newVersionCode) => {
477
+ const finalVersionCode = newVersionCode || versionCode; // Use existing if no input
478
+
479
+ // Ask for new versionName
480
+ rl.question('Enter new versionName (press enter to keep current): ', (newVersionName) => {
481
+ const finalVersionName = newVersionName || versionName; // Use existing if no input
482
+
483
+ // Log the final version details
484
+ console.log(`📦 Final versionCode: ${finalVersionCode}`);
485
+ console.log(`📝 Final versionName: ${finalVersionName}`);
486
+
487
+
488
+
489
+ // Update build.gradle with the new version details
490
+ let updatedGradleContent = gradleContent
491
+ .replace(/versionCode\s+\d+/, `versionCode ${finalVersionCode}`)
492
+ .replace(/versionName\s+"[^"]+"/, `versionName "${finalVersionName}"`);
493
+
494
+ // Check if resConfigs "en" already exists
495
+ const resConfigsLine = ' resConfigs "en"';
496
+ if (!updatedGradleContent.includes(resConfigsLine)) {
497
+ // Add resConfigs "en" below versionName
498
+ updatedGradleContent = updatedGradleContent.replace(/versionName\s+"[^"]+"/, `versionName "${finalVersionName}"\n${resConfigsLine}`);
499
+ } else {
500
+ console.log('ℹ️ resConfigs "en" already exists in build.gradle.');
501
+ }
502
+
503
+
504
+
505
+ // List of package IDs for which minify should be false
506
+
507
+
508
+ // Determine desired minify value
509
+ const desiredMinify = !_isffmpegUsed;
510
+
511
+ // Check if minifyEnabled is already present
512
+ if (/minifyEnabled\s+(true|false)/.test(updatedGradleContent)) {
513
+ // Replace existing value with desired
514
+ updatedGradleContent = updatedGradleContent.replace(/minifyEnabled\s+(true|false)/, `minifyEnabled ${desiredMinify}`);
515
+ console.log(`Replaced minifyEnabled with ${desiredMinify}.`);
516
+ } else if (/buildTypes\s*{[\s\S]*?release\s*{/.test(updatedGradleContent)) {
517
+ // Insert minifyEnabled if not present
518
+ updatedGradleContent = updatedGradleContent.replace(
519
+ /(buildTypes\s*{[\s\S]*?release\s*{)/,
520
+ `$1\n minifyEnabled ${desiredMinify}`
521
+ );
522
+ console.log(`✅ Inserted minifyEnabled ${desiredMinify} into release block.`);
523
+ } else {
524
+ console.log('⚠️ Warning: buildTypes > release block not found. minifyEnabled was not added.');
525
+ }
526
+
527
+
528
+
529
+
530
+
531
+
532
+ // Write the updated gradle content back to build.gradle
533
+ writeFileSync(gradleFilePath, updatedGradleContent, 'utf8');
534
+ console.log(`✅ Updated build.gradle with versionCode: ${finalVersionCode}, versionName: ${finalVersionName}, resConfigs "en" and "minifyEnabled true"`);
535
+
536
+ storeIds.forEach((id) => {
537
+ console.log(`ℹ️ Building for Store ID ${id}`);
538
+
539
+ // Set the environment variable for store ID
540
+ process.env.VITE_STORE_ID = id;
541
+
542
+ // Conditionally set the new file name
543
+ let newFileName;
544
+ let storeName = storeNames[id];
545
+
546
+ debugger;
547
+
548
+
549
+ managePackages(storeName);
550
+
551
+ if (storeName === "SamsungStore") {
552
+ // For SamsungStore, rename to versionCode value only
553
+ //newFileName = `${finalVersionCode}.aab`;
554
+ newFileName=`${_appUniqueId}_${_appName.replaceAll(" ","_")}-${(storeName.toUpperCase()).replace("STORE","")}-b${finalVersionCode}-v${finalVersionName.replace(/\./g,'_')}.aab`
555
+ } else {
556
+ // For other stores, use the standard naming format
557
+ //newFileName = `app-release-signed-${storeName}-b${finalVersionCode}-v${finalVersionName}.aab`;
558
+
559
+ newFileName=`${_appUniqueId}_${_appName.replaceAll(" ","_")}-${(storeName.toUpperCase()).replace("STORE","")}-b${finalVersionCode}-v${finalVersionName}.aab`
560
+
561
+
562
+ /*
563
+ const _appUniqueId = config.android?.APP_UNIQUE_ID;
564
+ const _appName=config.appName;
565
+ const _appPackageId=config.appId;
566
+ */
567
+
568
+ }
569
+
570
+ //storeName="amazon"
571
+ const checkFullPath = join("AAB", newFileName); // Update to point to the new AAB directory
572
+
573
+ // Modify minSdkVersion in variables.gradle for SamsungStore
574
+ const variablesGradleFilePath = join("android", "variables.gradle");
575
+ let variablesGradleContent = readFileSync(variablesGradleFilePath, 'utf8');
576
+
577
+ // Extract the current minSdkVersion
578
+ const minSdkVersionMatch = variablesGradleContent.match(/minSdkVersion\s*=\s*(\d+)/);
579
+ const currentMinSdkVersion = minSdkVersionMatch ? parseInt(minSdkVersionMatch[1], 10) : null;
580
+
581
+ // Store the original minSdkVersion (only on the first iteration)
582
+ if (!originalMinSdkVersion) {
583
+ originalMinSdkVersion = currentMinSdkVersion;
584
+ }
585
+ try {
586
+ // Modify the minSdkVersion based on the store
587
+
588
+
589
+ if(currentMinSdkVersion==23 || currentMinSdkVersion==24)
590
+ {
591
+ if (storeName === "SamsungStore" || storeName === "PlayStore" ||
592
+ //_appPackageId=="vfx.green.editor" || _appPackageId=="audio.music.sound.editor" || _appPackageId=="video.to.gif.maker"
593
+
594
+ //ffmpegUsedPackages.includes(_appPackageId)
595
+ _isffmpegUsed
596
+ ) {
597
+ if (currentMinSdkVersion !== 24) {
598
+ variablesGradleContent = variablesGradleContent.replace(/minSdkVersion\s*=\s*\d+/, 'minSdkVersion = 24');
599
+ console.log('minSdkVersion updated to 24 for SamsungStore & PlayStore');
600
+ writeFileSync(variablesGradleFilePath, variablesGradleContent);
601
+ }
602
+ } else {
603
+ // For PlayStore and AmazonStore, ensure minSdkVersion is originalMinSdkVersion
604
+ //if (currentMinSdkVersion !== originalMinSdkVersion) {
605
+ variablesGradleContent = variablesGradleContent.replace(/minSdkVersion\s*=\s*\d+/, `minSdkVersion = ${amazonMinSdkVersion}`);
606
+ console.log(`minSdkVersion reverted to ${amazonMinSdkVersion} for ${storeName}`);
607
+ writeFileSync(variablesGradleFilePath, variablesGradleContent);
608
+ //}
609
+ }
610
+ }
611
+
612
+
613
+ // Run the Node.js script to modify plugin.xml
614
+ if (isAdmobFound) {
615
+ execSync('node buildCodeplay/modify-plugin-xml.js', { stdio: 'inherit' });
616
+ } else {
617
+ console.log("\x1b[33m%s\x1b[0m", "Seems to Pro Version [No ads found]");
618
+ }
619
+
620
+ // Run the Vite build
621
+ execSync(`npm run build:storeid${id}`, { stdio: 'inherit' });
622
+
623
+
624
+
625
+ // Copy the built files to the appropriate folder
626
+ const src = join("www", "*");
627
+ const dest = join("android", "app", "src", "main", "assets", "public");
628
+
629
+ // Use 'xcopy' command for Windows
630
+ execSync(`xcopy ${src} ${dest} /E /I /Y`, { stdio: 'inherit' });
631
+
632
+ // Build Android AAB file
633
+ //child_process.execSync('cd android && ./gradlew bundleRelease', { stdio: 'inherit' });
634
+
635
+
636
+ // Build Android AAB or APK file based on store
637
+ console.log(`🏗️ Building release for ${storeName}...`);
638
+
639
+ let buildType = "AAB";
640
+ if (["VivoStore", "OppoStore", "MiStore"].includes(storeName)) {
641
+ buildType = "APK";
642
+ }
643
+
644
+ execSync('npx cap sync android', { stdio: 'inherit' });
645
+ execSync(`npx cap build android --androidreleasetype=${buildType}`, { stdio: 'inherit' });
646
+
647
+ // Determine output paths
648
+ let oldFilePath;
649
+ let newExt = buildType === "APK" ? "apk" : "aab";
650
+
651
+ if (buildType === "APK") {
652
+ oldFilePath = join("android", "app", "build", "outputs", "apk", "release", "app-release-signed.apk");
653
+ } else {
654
+ oldFilePath = join("android", "app", "build", "outputs", "bundle", "release", "app-release-signed.aab");
655
+ }
656
+
657
+ const checkFullPath = join("AAB", newFileName.replace(/\.aab$/, `.${newExt}`));
658
+
659
+ // Rename the output file
660
+ if (existsSync(oldFilePath)) {
661
+ renameSync(oldFilePath, checkFullPath);
662
+ console.log(`✅ Renamed output ${newExt.toUpperCase()} file to: ${path.basename(checkFullPath)}`);
663
+ } else {
664
+ console.error(`❌ ${newExt.toUpperCase()} file not found after build.`);
665
+ }
666
+
667
+ } catch (error) {
668
+ console.error(`❌ Error during build for Store ID ${id}:`, error);
669
+ process.exit(1);
670
+ }
671
+ });
672
+
673
+ rl.close(); // Close the readline interface after all operations
674
+ });
675
+ });
676
+
677
+
678
+
679
+
680
+
681
+ function managePackages(store) {
682
+ console.log(`IAP Configurations - PlayStore: ${playstore}, Samsung: ${samsung}, Amazon: ${amazon}`);
683
+
684
+ let install = "";
685
+ let uninstall = "";
686
+
687
+
688
+
689
+ let manifestContent = readFileSync(androidManifestPath, 'utf-8');
690
+
691
+ const permissionsToRemove = [
692
+ 'com.android.vending.BILLING',
693
+ 'com.samsung.android.iap.permission.BILLING'
694
+ ];
695
+
696
+
697
+ permissionsToRemove.forEach(permission => {
698
+ const permissionRegex = new RegExp(`^\\s*<uses-permission\\s+android:name="${permission}"\\s*/?>\\s*[\r\n]?`, 'm');
699
+ if (permissionRegex.test(manifestContent)) {
700
+ manifestContent = manifestContent.replace(permissionRegex, '');
701
+ console.log(`✅ Removed <uses-permission android:name="${permission}" /> from AndroidManifest.xml`);
702
+ }
703
+ });
704
+
705
+ // Write the updated content back to the file
706
+ writeFileSync(androidManifestPath, manifestContent, 'utf-8');
707
+
708
+
709
+
710
+ if ((playstore && store === "PlayStore") || (amazon && store === "AmazonStore")) {
711
+ install = '@revenuecat/purchases-capacitor';
712
+ uninstall = 'cordova-plugin-samsungiap';
713
+
714
+ // Update AndroidManifest.xml for PlayStore
715
+ if(playstore)
716
+ updateAndroidManifest(store,
717
+ '<uses-permission android:name="com.android.vending.BILLING" />');
718
+
719
+ } else if (samsung && store === "SamsungStore") {
720
+ install = 'cordova-plugin-samsungiap';
721
+ uninstall = '@revenuecat/purchases-capacitor';
722
+
723
+ // Update AndroidManifest.xml for SamsungStore
724
+ updateAndroidManifest(store,
725
+ '<uses-permission android:name="com.samsung.android.iap.permission.BILLING" />');
726
+
727
+ } else {
728
+ console.log("No valid store specified or no configurations found. Both plugins will be uninstalled.");
729
+ try {
730
+ execSync(`npm uninstall cordova-plugin-samsungiap`, { stdio: 'inherit' });
731
+ execSync(`npm uninstall @revenuecat/purchases-capacitor`, { stdio: 'inherit' });
732
+ console.log(`✅ Both plugins uninstalled successfully.`);
733
+ } catch (err) {
734
+ console.error("❌ Error uninstalling plugins:", err);
735
+ }
736
+ return;
737
+ }
738
+
739
+ console.log(`⚠️ Installing ${install} and uninstalling ${uninstall} for ${store}...`);
740
+ try {
741
+ if (install) {
742
+ execSync(`npm install ${install}`, { stdio: 'inherit' });
743
+ }
744
+ if (uninstall) {
745
+ execSync(`npm uninstall ${uninstall}`, { stdio: 'inherit' });
746
+ }
747
+ console.log(`✅ ${install} installed and ${uninstall} uninstalled successfully.`);
748
+ } catch (err) {
749
+ console.error(`❌ Error managing packages for ${store}:`, err);
750
+ }
751
+ }
752
+
753
+
754
+
755
+ function updateAndroidManifest(store, addPermission) {
756
+ try {
757
+ if (!existsSync(androidManifestPath)) {
758
+ console.error("❌ AndroidManifest.xml file not found!");
759
+ return;
760
+ }
761
+
762
+ // Read the content of the AndroidManifest.xml
763
+ let manifestContent = readFileSync(androidManifestPath, 'utf-8');
764
+
765
+ // Normalize line endings to `\n` for consistent processing
766
+ manifestContent = manifestContent.replace(/\r\n/g, '\n');
767
+
768
+ // Check if the permission is already present
769
+ if (manifestContent.includes(addPermission.trim())) {
770
+ console.log(`${addPermission} is already in the AndroidManifest.xml. Skipping addition.`);
771
+ return; // Skip if the permission is already present
772
+ }
773
+
774
+ // Insert the permission before the closing </manifest> tag
775
+ const closingTag = '</manifest>';
776
+ const formattedPermission = ` ${addPermission.trim()}\n`;
777
+ if (manifestContent.includes(closingTag)) {
778
+ manifestContent = manifestContent.replace(
779
+ closingTag,
780
+ `${formattedPermission}${closingTag}`
781
+ );
782
+ console.log(`✅ Added ${addPermission} before </manifest> tag.`);
783
+ } else {
784
+ console.warn(`⚠️ </manifest> tag not found. Adding ${addPermission} at the end of the file.`);
785
+ manifestContent += `\n${formattedPermission}`;
786
+ }
787
+
788
+ // Normalize line endings back to `\r\n` and write the updated content
789
+ manifestContent = manifestContent.replace(/\n/g, '\r\n');
790
+ writeFileSync(androidManifestPath, manifestContent, 'utf-8');
791
+ console.log(`✅ AndroidManifest.xml updated successfully for ${store}`);
792
+ } catch (err) {
793
+ console.error(`❌ Error updating AndroidManifest.xml for ${store}:`, err);
794
+ }
795
+ }
796
+
797
+
798
+
799
+ /* function updateAndroidManifest1(store, addPermission) {
800
+ try {
801
+ if (!fs.existsSync(androidManifestPath)) {
802
+ console.error("AndroidManifest.xml file not found!");
803
+ return;
804
+ }
805
+
806
+ let manifestContent = fs.readFileSync(androidManifestPath, 'utf-8');
807
+
808
+
809
+
810
+ // Add the required permission if not already present
811
+ if (!manifestContent.includes(addPermission)) {
812
+ const manifestLines = manifestContent.split('\n');
813
+ const insertIndex = manifestLines.findIndex(line => line.trim().startsWith('<application'));
814
+ if (insertIndex > -1) {
815
+ manifestLines.splice(insertIndex, 0, ` ${addPermission}`);
816
+ manifestContent = manifestLines.join('\n');
817
+ console.log(`Added ${addPermission} to AndroidManifest.xml`);
818
+ }
819
+ }
820
+
821
+ // Write the updated content back to the file
822
+ fs.writeFileSync(androidManifestPath, manifestContent, 'utf-8');
823
+ console.log(`AndroidManifest.xml updated successfully for ${store}`);
824
+ } catch (err) {
825
+ console.error(`Error updating AndroidManifest.xml for ${store}:`, err);
826
+ }
827
+ } */
828
+
829
+
830
+
831
+
832
+
833
+
834
+
835
+