codeplay-common 3.2.1 → 3.2.3

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,2318 +1,2528 @@
1
- const fs = require('fs');
2
- const path = require('path');
3
- const plist = require('plist');
4
-
5
-
6
-
7
- const { execSync } = require("child_process");
8
-
9
-
10
-
11
-
12
- const { readFileSync } = require("fs");
13
-
14
- const ENABLE_AUTO_UPDATE = true;
15
- const USE_LIVE_SERVER_VERSION = true;
16
-
17
- const configPath = path.join(process.cwd(), 'capacitor.config.json');
18
- const updateLogFile = path.join(process.cwd(), "", "plugin-update-log.txt");
19
-
20
- // Expected plugin list with minimum versions
21
- /* const requiredPlugins = [
22
-
23
- { pattern: /backbutton-(\d+\.\d+)\.js$/, minVersion: '2.0', required: true, baseDir: 'js',mandatoryUpdate: true},
24
-
25
-
26
- { pattern: /common-(\d+\.\d+)(?:-beta-(\d+))?\.js$/, minVersion: '6.0', required: true, baseDir: 'js',mandatoryUpdate: true },
27
-
28
- { pattern: /localization_settings-(\d+\.\d+)\.js$/, minVersion: '1.1', required: true, baseDir: 'js',mandatoryUpdate: false },
29
- { pattern: /localization-(\d+\.\d+)\.js$/, minVersion: '1.5', required: true, baseDir: 'js',mandatoryUpdate: true },
30
- { pattern: /localNotification-(\d+\.\d+)\.js$/, minVersion: '2.2', required: true, baseDir: 'js',mandatoryUpdate: false },
31
- { pattern: /localNotification_AppSettings-(\d+\.\d+)\.js$/, minVersion: '1.0', required: true, baseDir: 'js',mandatoryUpdate: false },
32
- { pattern: /onesignal-(\d+\.\d+)\.js$/, minVersion: '2.3', required: true, baseDir: 'js',mandatoryUpdate: false },
33
- { pattern: /saveToGalleryAndSaveAnyFile-(\d+\.\d+)(-ios)?\.js$/, minVersion: '3.1', required: true, baseDir: 'js',mandatoryUpdate: true },
34
- { pattern: /Ads[\/\\]admob-emi-(\d+\.\d+)\.js$/, minVersion: '3.7', required: true, baseDir: 'js',mandatoryUpdate: true },
35
-
36
- // New added plugins
37
- { pattern: /video-player-(\d+\.\d+)\.js$/, minVersion: '1.5', required: true, baseDir: 'js',mandatoryUpdate: false },
38
- { pattern: /image-cropper-(\d+\.\d+)\.js$/, minVersion: '1.1', required: true, baseDir: 'js',mandatoryUpdate: false },
39
- { pattern: /common-(\d+\.\d+)\.less$/, minVersion: '1.6', required: true, baseDir: 'assets/css',mandatoryUpdate: false },
40
-
41
-
42
- // New folders
43
- { pattern: /IAP-(\d+\.\d+)$/, minVersion: '2.8', isFolder: true , required: true, baseDir: 'js/Ads',mandatoryUpdate: true },
44
- { pattern: /editor-(\d+\.\d+)$/, minVersion: '1.9', isFolder: true, required: true, baseDir: 'js',mandatoryUpdate: true },
45
- { pattern: /ffmpeg-(\d+\.\d+)$/, minVersion: '1.6', isFolder: true, required: true, baseDir: 'js',mandatoryUpdate: true },
46
- { pattern: /theme-(\d+\.\d+)$/, minVersion: '3.3', isFolder: true , required: true, baseDir: 'theme',mandatoryUpdate: true },
47
-
48
-
49
- { pattern: /certificatejs-(\d+\.\d+)$/, minVersion: '1.6', isFolder: true , required: true, baseDir: 'certificate',mandatoryUpdate: true }
50
-
51
- ]; */
52
-
53
-
54
- function requireOrInstall(packageName) {
55
- try {
56
- return require(packageName);
57
- } catch (err) {
58
-
59
- console.log(`📦 "${packageName}" not found. Installing automatically...`);
60
-
61
- try {
62
- execSync(`npm install ${packageName}`, { stdio: "inherit" });
63
- console.log(`✅ "${packageName}" installed successfully.`);
64
- } catch (installErr) {
65
- console.error(`❌ Failed to install "${packageName}".`);
66
- process.exit(1);
67
- }
68
-
69
- // Try loading again
70
- return require(packageName);
71
- }
72
- }
73
-
74
- const AdmZip = requireOrInstall("adm-zip");
75
-
76
-
77
-
78
- const pkg = require(path.join(process.cwd(), 'node_modules', 'codeplay-common', 'package.json'));
79
-
80
- const pluginName = pkg.name;
81
- const pluginVersion = pkg.version;
82
-
83
- let updateLogs = [];
84
-
85
- function writeUpdateLine(message) {
86
- updateLogs.push(message);
87
- }
88
-
89
- const MAX_LOG_BLOCKS = 50;
90
-
91
- function saveUpdateLogs() {
92
-
93
- if (updateLogs.length === 0) return;
94
-
95
- const logDir = path.dirname(updateLogFile);
96
-
97
- if (!fs.existsSync(logDir)) {
98
- fs.mkdirSync(logDir, { recursive: true });
99
- }
100
-
101
- const now = new Date();
102
-
103
- const formattedTime = now.toLocaleString('en-GB', {
104
- day: '2-digit',
105
- month: '2-digit',
106
- year: 'numeric',
107
- hour: '2-digit',
108
- minute: '2-digit',
109
- hour12: true
110
- }).replace(',', '').replace(/\//g, '-');
111
-
112
- let newBlock = `${pluginName}: ${pluginVersion}\n[${formattedTime}]\n`;
113
-
114
- updateLogs.forEach(line => {
115
- newBlock += `${line}\n`;
116
- });
117
-
118
- newBlock += "\n";
119
-
120
- let existingLog = "";
121
-
122
- if (fs.existsSync(updateLogFile)) {
123
- existingLog = fs.readFileSync(updateLogFile, "utf8");
124
- }
125
-
126
- let combinedLog = newBlock + existingLog;
127
-
128
- // Split blocks by plugin header
129
- const blocks = combinedLog.split(/\n(?=codeplay-common:)/);
130
-
131
- // Keep only latest 50
132
- const trimmed = blocks.slice(0, MAX_LOG_BLOCKS).join("\n");
133
-
134
- fs.writeFileSync(updateLogFile, trimmed);
135
-
136
- }
137
-
138
-
139
-
140
-
141
-
142
- const versionsFile = path.join(__dirname, "versions.json");
143
-
144
- function loadRequiredPlugins() {
145
-
146
- if (!fs.existsSync(versionsFile)) {
147
- console.error("❌ versions.json not found");
148
- process.exit(1);
149
- }
150
-
151
- const json = JSON.parse(fs.readFileSync(versionsFile, "utf8"));
152
-
153
- return json.plugins.map(p => ({
154
- ...p,
155
- pattern: new RegExp(p.pattern)
156
- }));
157
-
158
- }
159
-
160
- let requiredPlugins = loadRequiredPlugins();
161
-
162
-
163
-
164
-
165
-
166
-
167
- async function downloadAndExtractZip(url, destFolder) {
168
-
169
- const zipPath = destFolder + ".zip";
170
-
171
- await downloadFile(url, zipPath);
172
-
173
- const zip = new AdmZip(zipPath);
174
- zip.extractAllTo(destFolder, true);
175
-
176
- fs.unlinkSync(zipPath);
177
-
178
- }
179
-
180
-
181
-
182
-
183
-
184
-
185
-
186
- //Check codeplay-common latest version installed or not Start
187
- //const { execSync } = require('child_process');
188
-
189
-
190
- function getInstalledVersion(packageName) {
191
- try {
192
- const packageJsonPath = path.join(process.cwd(), 'node_modules', packageName, 'package.json');
193
- if (fs.existsSync(packageJsonPath)) {
194
- const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
195
- return packageJson.version;
196
- }
197
- } catch (error) {
198
- return null;
199
- }
200
- return null;
201
- }
202
-
203
- function getLatestVersion(packageName) {
204
- try {
205
- return execSync(`npm view ${packageName} version`).toString().trim();
206
- } catch (error) {
207
- console.error(`Failed to fetch latest version for ${packageName}`);
208
- return null;
209
- }
210
- }
211
-
212
- function checkPackageVersion() {
213
- const packageName = 'codeplay-common';
214
- const installedVersion = getInstalledVersion(packageName);
215
- const latestVersion = getLatestVersion(packageName);
216
-
217
- if (!installedVersion) {
218
- console.error(`${packageName} is not installed. Please install it using "npm install ${packageName}".`);
219
- process.exit(1);
220
- }
221
-
222
- if (installedVersion !== latestVersion) {
223
- console.error(`\x1b[31m${packageName} is outdated (installed: ${installedVersion}, latest: ${latestVersion}). Please update it.\x1b[0m\n\x1b[33mUse 'npm uninstall codeplay-common ; npm i codeplay-common'\x1b[0m`);
224
- process.exit(1);
225
- }
226
-
227
- console.log(`${packageName} is up to date (version ${installedVersion}).`);
228
- }
229
-
230
- // Run package version check before executing the main script
231
- try {
232
- checkPackageVersion();
233
- } catch (error) {
234
- console.error(error.message);
235
- process.exit(1);
236
- }
237
-
238
- //Check codeplay-common latest version installed or not END
239
-
240
-
241
-
242
- function compareWithBeta(installedVersion, minVersion, isBeta) {
243
- const baseCompare = compareVersions(installedVersion, minVersion);
244
-
245
- if (!isBeta) {
246
- // Stable version → normal compare
247
- return baseCompare;
248
- }
249
-
250
- // Beta version logic
251
- if (baseCompare > 0) return 1; // 5.3-beta > 5.2
252
- if (baseCompare < 0) return -1; // 5.1-beta < 5.2
253
-
254
- // Same version but beta LOWER than stable
255
- return -1; // 5.2-beta < 5.2
256
- }
257
-
258
-
259
-
260
-
261
- const checkAppUniqueId=()=>{
262
-
263
- const config = JSON.parse(fs.readFileSync(configPath, 'utf8'));
264
-
265
- const appUniqueId = config.android?.APP_UNIQUE_ID;
266
- const RESIZEABLE_ACTIVITY = config.android?.RESIZEABLE_ACTIVITY;
267
- const orientation = config.android?.ORIENTATION;
268
-
269
-
270
- let logErrorMessage="";
271
-
272
- // 1️⃣ Check if it’s missing
273
- if (RESIZEABLE_ACTIVITY === undefined) {
274
- logErrorMessage+='❌ Missing android.RESIZEABLE_ACTIVITY option in capacitor.config.json.\n';
275
- }
276
-
277
- // 2️⃣ Check if it’s not boolean (true/false only)
278
- else if (typeof RESIZEABLE_ACTIVITY !== 'boolean') {
279
- logErrorMessage+='❌ Invalid android.RESIZEABLE_ACTIVITY value. Please use only true or false (without quotes).\n';
280
- }
281
-
282
-
283
-
284
- if (!orientation) {
285
- logErrorMessage+='❌ Missing android.ORIENTATION option in capacitor.config.json.\n';
286
- }
287
-
288
- else if(orientation!="portrait" && orientation!="landscape" && orientation!="auto")
289
- {
290
- logErrorMessage+='❌ Spelling mistake in android.ORIENTATION option in capacitor.config.json. Please use only ["portrait" "landscape" "auto"]\n';
291
- }
292
-
293
-
294
- if (!appUniqueId) {
295
- logErrorMessage+='❌ APP_UNIQUE_ID is missing in capacitor.config.json.';
296
- }
297
-
298
- else if (!Number.isInteger(appUniqueId)) {
299
- logErrorMessage+='❌ APP_UNIQUE_ID must be an integer. Example: 1, 2, 3, etc.';
300
- }
301
-
302
-
303
-
304
- if(logErrorMessage!="")
305
- {
306
- console.error(logErrorMessage);
307
- process.exit(1)
308
- }
309
-
310
-
311
- console.log(`✅ APP_UNIQUE_ID is valid: ${appUniqueId}`);
312
-
313
- }
314
-
315
- checkAppUniqueId();
316
-
317
-
318
-
319
-
320
-
321
-
322
-
323
- //@Codemirror check and install/uninstall the packages START
324
- //const fs = require("fs");
325
- //const path = require("path");
326
- //const { execSync } = require("child_process");
327
-
328
- const jsDir = path.join(__dirname, "..", "src", "js");
329
-
330
- // 🔍 Detect OLD structure
331
- const oldEditorDirs = fs.readdirSync(jsDir)
332
- .filter(name => /^editor-\d+\.\d+$/.test(name));
333
-
334
- // 📁 New structure path (optional, not mandatory)
335
- const newEditorBaseDir = path.join(jsDir, "editor");
336
-
337
- // ======================================================
338
- // CASE 1: OLD STRUCTURE FOUND → STOP
339
- // ======================================================
340
- if (oldEditorDirs.length > 0) {
341
-
342
- console.error(`
343
- ❌ OLD EDITOR STRUCTURE DETECTED
344
-
345
- You are using outdated folder structure:
346
- src/js/editor-x.x/
347
-
348
- 🚨 This is no longer supported.
349
-
350
- 📦 Found folders:
351
- ${oldEditorDirs.map(d => " - " + d).join("\n")}
352
-
353
- 👉 Please move them manually:
354
-
355
- src/js/editor-1.6
356
-
357
- src/js/editor/editor-1.6
358
-
359
- ⚠️ Also ensure:
360
- src/js/editor/configuration.json exists
361
-
362
- ❌ Build stopped.
363
- `);
364
-
365
- process.exit(1);
366
- }
367
-
368
- // ======================================================
369
- // ✅ CASE 2: NEW STRUCTURE EXISTS → VALIDATE
370
- // ======================================================
371
- if (fs.existsSync(newEditorBaseDir)) {
372
-
373
- // 🔍 Find editor-x.x inside new folder
374
- const editorDirs = fs.readdirSync(newEditorBaseDir)
375
- .filter(name => /^editor-\d+\.\d+$/.test(name));
376
-
377
- // 👉 If editor folder exists but no versions → skip safely
378
- if (editorDirs.length === 0) {
379
- console.log("ℹ️ No editor-x.x folders found inside src/js/editor/");
380
- return;
381
- }
382
-
383
- // ======================================================
384
- // Validate configuration.json (NEW RULE)
385
- // ======================================================
386
-
387
- const editorConfigPath = path.join(newEditorBaseDir, "configuration.json");
388
-
389
- // ❌ Missing config
390
- if (!fs.existsSync(editorConfigPath)) {
391
- console.error(`
392
- ❌ MISSING EDITOR CONFIGURATION FILE
393
-
394
- Required:
395
- src/js/editor/configuration.json
396
-
397
- ❌ Build stopped.
398
- `);
399
- process.exit(1);
400
- }
401
-
402
- // Check wrong placement
403
- const invalidConfigs = [];
404
-
405
- editorDirs.forEach(dir => {
406
- const wrongPath = path.join(newEditorBaseDir, dir, "configuration.json");
407
- if (fs.existsSync(wrongPath)) {
408
- invalidConfigs.push(`src/js/editor/${dir}/configuration.json`);
409
- }
410
- });
411
-
412
- if (invalidConfigs.length > 0) {
413
- console.error(`
414
- INVALID CONFIGURATION LOCATION
415
-
416
- 🚫 configuration.json must NOT be inside version folders.
417
-
418
- Found:
419
- ${invalidConfigs.map(p => " - " + p).join("\n")}
420
-
421
- ✅ Correct:
422
- src/js/editor/configuration.json
423
-
424
- ❌ Build stopped.
425
- `);
426
- process.exit(1);
427
- }
428
-
429
- console.log("✅ Editor structure validated.");
430
-
431
- // ======================================================
432
- // 🚀 Continue execution (run.js)
433
- // ======================================================
434
-
435
- const latestEditorDir = editorDirs.sort((a, b) => {
436
- const vA = parseFloat(a.split('-')[1]);
437
- const vB = parseFloat(b.split('-')[1]);
438
- return vB - vA;
439
- })[0];
440
-
441
- const runJsPath = path.join(newEditorBaseDir, latestEditorDir, "run.js");
442
-
443
- if (!fs.existsSync(runJsPath)) {
444
- console.error(`❌ run.js not found in ${latestEditorDir}`);
445
- process.exit(1);
446
- }
447
-
448
- console.log(`🚀 Executing ${runJsPath}...`);
449
- execSync(`node "${runJsPath}"`, { stdio: "inherit" });
450
- }
451
-
452
- // ======================================================
453
- // CASE 3: NOTHING EXISTS → DO NOTHING
454
- // ======================================================
455
- else {
456
- console.log("ℹ️ Editor not used in this project. Skipping...");
457
- }
458
-
459
- //@Codemirror check and install/uninstall the packages END
460
-
461
-
462
-
463
-
464
-
465
-
466
-
467
-
468
- //editor-x.x import old style check and stop execution START
469
-
470
-
471
-
472
-
473
-
474
-
475
-
476
-
477
- const ROOT_DIR = path.join(__dirname, "..", "src");
478
-
479
- // Match: editor/editor-2.3, editor/editor-2.3.1, etc.
480
- const FORBIDDEN_REGEX = /editor\/editor-\d+(\.\d+)+/;
481
-
482
- let hasError = false;
483
-
484
- const ERROR_MESSAGE = `const ERROR_MESSAGE = ❌ Invalid import detected!
485
-
486
- You are using a direct version-based path like: editor/editor-x.x/editor.js
487
-
488
- 🚫 This is NOT allowed.
489
-
490
- 👉 Please use the proper alias or updated import method.
491
- Example: import { ... } from '@editor'
492
-
493
- ⚠️ Do not use version-based paths in imports.
494
- 👉 Please add this manually in vite.config.js:
495
-
496
- alias: {
497
- '@editor': path.resolve(__dirname, './src/js/editor/editor-x.x')
498
- }`
499
-
500
- function scanDir(dir) {
501
- const files = fs.readdirSync(dir);
502
-
503
- for (const file of files) {
504
- const fullPath = path.join(dir, file);
505
- const stat = fs.statSync(fullPath);
506
-
507
- if (stat.isDirectory()) {
508
- scanDir(fullPath);
509
- } else if (file.endsWith(".js") || file.endsWith(".ts") || file.endsWith(".f7")) {
510
- const content = fs.readFileSync(fullPath, "utf-8");
511
-
512
- const lines = content.split("\n");
513
-
514
- lines.forEach((line, index) => {
515
- if (FORBIDDEN_REGEX.test(line)) {
516
- console.error(
517
- `❌ Forbidden import found:\nFile: ${fullPath}\nLine: ${index + 1}\nCode: ${line.trim()}\n`,
518
- ERROR_MESSAGE
519
- );
520
- hasError = true;
521
- }
522
- });
523
- }
524
- }
525
- }
526
-
527
- // Run scan
528
- scanDir(ROOT_DIR);
529
-
530
- // Throw error (exit process)
531
- if (hasError) {
532
- console.error("🚫 Build failed due to forbidden editor imports.");
533
- process.exit(1);
534
- } else {
535
- console.log("✅ No forbidden imports found.");
536
- }
537
-
538
-
539
-
540
-
541
-
542
-
543
- //editor-x.x import old style check and stop execution START
544
-
545
-
546
-
547
-
548
-
549
-
550
-
551
-
552
-
553
-
554
- // saveToGalleryAndSaveAnyFile-x.x-ios.js file check for android and return error if exists START
555
-
556
- const os = require('os');
557
-
558
- const saveToGalleryAndSaveFileCheck_iOS = () => {
559
-
560
- // List of paths to scan
561
- const SCAN_PATHS = [
562
- path.resolve(__dirname, '../src/certificate'),
563
- path.resolve(__dirname, '../src/pages'),
564
- path.resolve(__dirname, '../src/js'),
565
- path.resolve(__dirname, '../src/app.f7')
566
- ];
567
-
568
- // Directory to exclude
569
- const EXCLUDED_DIR = path.resolve(__dirname, '../src/js/Ads');
570
-
571
- const ANDROID_MANIFEST_PATH = path.resolve(__dirname, '../android/app/src/main/AndroidManifest.xml');
572
-
573
-
574
- // Match iOS-specific imports (e.g., saveToGalleryAndSaveAnyFile-2.5-ios.js) not in comments
575
- const IOS_FILE_REGEX = /^(?!\s*\/\/).*['"](?:.*\/)?saveToGalleryAndSaveAnyFile-\d+(\.\d+)*-ios\.js['"]/m;
576
-
577
- // Match Android-specific imports (e.g., saveToGalleryAndSaveAnyFile-2.5.js) not in comments
578
- const ANDROID_FILE_REGEX = /^(?!\s*\/\/).*['"](?:.*\/)?saveToGalleryAndSaveAnyFile-\d+(\.\d+)*\.js['"]/m;
579
-
580
-
581
-
582
-
583
-
584
- const ALLOWED_EXTENSIONS = ['.js', '.f7'];
585
- const isMac = os.platform() === 'darwin';
586
-
587
- let iosImportFound = false;
588
- let androidImportFound = false;
589
-
590
- // Files to skip completely (full or partial match)
591
- const SKIP_FILES = [
592
- 'pdf-3.11.174.min.js',
593
- 'pdf.worker-3.11.174.min.js'
594
- ,'index.browser.js'
595
- ];
596
-
597
-
598
- function scanDirectory(dir) {
599
-
600
- /*
601
- //######################### DO NOT DELETE THIS - START [Appid base validation] #####################################
602
- const config = JSON.parse(fs.readFileSync(configPath, 'utf8'));
603
- const appUniqueId = config.android?.APP_UNIQUE_ID;
604
- if (appUniqueId == "206") return;
605
- //######################### DO NOT DELETE THIS - END [Appid base validation] #####################################
606
- */
607
-
608
- const stat = fs.statSync(dir);
609
-
610
- if (stat.isFile()) {
611
-
612
- // 🔥 Skip files in SKIP_FILES array
613
- const baseName = path.basename(dir);
614
- if (SKIP_FILES.includes(baseName)) {
615
- // Just skip silently
616
- return;
617
- }
618
-
619
- // Only scan allowed file extensions
620
- if (ALLOWED_EXTENSIONS.some(ext => dir.endsWith(ext))) {
621
- process.stdout.write(`\r🔍 Scanning: ${dir} `);
622
-
623
- const content = fs.readFileSync(dir, 'utf8');
624
-
625
- if (IOS_FILE_REGEX.test(content)) {
626
- iosImportFound = true;
627
- if (!isMac) {
628
- console.error(`\n❌ ERROR: iOS-specific import detected in: ${dir}`);
629
- console.error(`🚫 STOPPED: This file should not be imported in Android/Windows/Linux builds.\n`);
630
- process.exit(1);
631
- }
632
- }
633
- else if (ANDROID_FILE_REGEX.test(content) && !content.includes('-ios.js')) {
634
- androidImportFound = true;
635
- }
636
- }
637
- }
638
- else if (stat.isDirectory()) {
639
- if (dir === EXCLUDED_DIR || path.basename(dir) === 'node_modules') return;
640
-
641
- const entries = fs.readdirSync(dir, { withFileTypes: true });
642
- for (let entry of entries) {
643
- scanDirectory(path.join(dir, entry.name));
644
- }
645
- }
646
- }
647
-
648
-
649
- // Run scan on all specified paths
650
- for (let scanPath of SCAN_PATHS) {
651
- if (fs.existsSync(scanPath)) {
652
- scanDirectory(scanPath);
653
- }
654
- }
655
-
656
-
657
-
658
- /* // Check src folder
659
- if (!fs.existsSync(ROOT_DIR)) {
660
- console.warn(`⚠️ Warning: 'src' directory not found at: ${ROOT_DIR}`);
661
- return;
662
- } */
663
-
664
- //scanDirectory(ROOT_DIR);
665
-
666
- // iOS Checks
667
- if (isMac && !iosImportFound) {
668
- console.warn(`⚠️ WARNING: You're on macOS but no iOS version (saveToGalleryAndSaveAnyFile-x.x-ios.js) found.`);
669
- process.exit(1);
670
- } else if (isMac && iosImportFound) {
671
- console.log('✅ iOS version detected for macOS build.');
672
- } else if (!iosImportFound) {
673
- console.log('✅ No iOS-specific imports detected for non-macOS.');
674
- }
675
-
676
- // Android Checks
677
- if (androidImportFound) {
678
- console.log("📱 Android version of saveToGalleryAndSaveAnyFile detected. Checking AndroidManifest.xml...");
679
-
680
- if (!fs.existsSync(ANDROID_MANIFEST_PATH)) {
681
- console.error("❌ AndroidManifest.xml not found. Cannot add requestLegacyExternalStorage attribute.");
682
- return;
683
- }
684
-
685
- let manifestContent = fs.readFileSync(ANDROID_MANIFEST_PATH, 'utf8');
686
-
687
- if (!manifestContent.includes('android:requestLegacyExternalStorage="true"')) {
688
- console.log("Adding android:requestLegacyExternalStorage=\"true\" to <application> tag...");
689
-
690
- manifestContent = manifestContent.replace(
691
- /<application([^>]*)>/,
692
- (match, attrs) => {
693
- if (attrs.includes('android:requestLegacyExternalStorage')) return match;
694
- return `<application${attrs} android:requestLegacyExternalStorage="true">`;
695
- }
696
- );
697
-
698
- fs.writeFileSync(ANDROID_MANIFEST_PATH, manifestContent, 'utf8');
699
- console.log(" android:requestLegacyExternalStorage=\"true\" added successfully.");
700
- } else {
701
- console.log("ℹ️ android:requestLegacyExternalStorage already exists in AndroidManifest.xml.");
702
- }
703
- } else {
704
- console.log("✅ No Android saveToGalleryAndSaveAnyFile imports detected.");
705
- }
706
- };
707
-
708
- saveToGalleryAndSaveFileCheck_iOS();
709
- // saveToGalleryAndSaveAnyFile-x.x-ios.js file check for android and return error if exists END
710
-
711
-
712
-
713
-
714
-
715
-
716
-
717
-
718
-
719
-
720
-
721
-
722
-
723
- /*
724
- // Clean up AppleDouble files (._*) created by macOS START
725
- if (process.platform === 'darwin') {
726
- try {
727
- console.log('🧹 Cleaning up AppleDouble files (._*)...');
728
- execSync(`find . -name '._*' -delete`);
729
- console.log('✅ AppleDouble files removed.');
730
- } catch (err) {
731
- console.warn('⚠️ Failed to remove AppleDouble files:', err.message);
732
- }
733
- } else {
734
- console.log('ℹ️ Skipping AppleDouble cleanup — not a macOS machine.');
735
- }
736
-
737
- // Clean up AppleDouble files (._*) created by macOS END
738
- */
739
-
740
-
741
-
742
-
743
-
744
-
745
- //In routes.js file check static import START
746
-
747
- const routesPath = path.join(process.cwd(), 'src', 'js', 'routes.js');
748
- const routesContent = fs.readFileSync(routesPath, 'utf-8');
749
-
750
- let inBlockComment = false;
751
- const lines = routesContent.split('\n');
752
-
753
- const allowedImport = `import HomePage from '../pages/home.f7';`;
754
- const badImportRegex = /^[ \t]*import\s+[\w{}*,\s]*\s+from\s+['"].+\.f7['"]\s*;/;
755
- const badImports = [];
756
-
757
- lines.forEach((line, index) => {
758
- const trimmed = line.trim();
759
-
760
- // Handle block comment start and end
761
- if (trimmed.startsWith('/*')) inBlockComment = true;
762
- if (inBlockComment && trimmed.endsWith('*/')) {
763
- inBlockComment = false;
764
- return;
765
- }
766
-
767
- // Skip if inside block comment or line comment
768
- if (inBlockComment || trimmed.startsWith('//')) return;
769
-
770
- // Match static .f7 import
771
- if (badImportRegex.test(trimmed) && trimmed !== allowedImport) {
772
- badImports.push({ line: trimmed, number: index + 1 });
773
- }
774
- });
775
-
776
- if (badImports.length > 0) {
777
- console.error('\n❌ ERROR: Detected disallowed static imports of .f7 files in routes.js\n');
778
- console.error(`⚠️ Only this static import is allowed:\n ${allowedImport}\n`);
779
- console.error(`🔧 Please convert other imports to async dynamic imports like this:\n`);
780
- console.error(`
781
-
782
- import HomePage from '../pages/home.f7';
783
-
784
- const routes = [
785
- {
786
- path: '/',
787
- component:HomePage,
788
- },
789
- {
790
- path: '/ProfilePage/',
791
- async async({ resolve }) {
792
- const page = await import('../pages/profile.f7');
793
- resolve({ component: page.default });
794
- },
795
- }]
796
- `);
797
-
798
- badImports.forEach(({ line, number }) => {
799
- console.error(`${number}: ${line}`);
800
- });
801
-
802
- process.exit(1);
803
- } else {
804
- console.log('✅ routes.js passed the .f7 import check.');
805
- }
806
-
807
- //In routes.js file check static import END
808
-
809
-
810
-
811
-
812
-
813
-
814
-
815
-
816
-
817
-
818
-
819
-
820
-
821
- // Check and change the "BridgeWebViewClient.java" file START
822
- /*
823
- For crash issue due to low memory problem, we need to modify the onRenderProcessGone method in BridgeWebViewClient.java.
824
- */
825
-
826
-
827
- const bridgeWebViewClientFilePath = path.join(process.cwd(), 'node_modules', '@capacitor/android/capacitor/src/main/java/com/getcapacitor', 'BridgeWebViewClient.java');
828
-
829
- // Read the file
830
- if (!fs.existsSync(bridgeWebViewClientFilePath)) {
831
- console.error('❌ Error: BridgeWebViewClient.java not found.');
832
- process.exit(1);
833
- }
834
-
835
- let fileContent = fs.readFileSync(bridgeWebViewClientFilePath, 'utf8');
836
-
837
- // Define old and new code
838
- const oldCodeStart = `@Override
839
- public boolean onRenderProcessGone(WebView view, RenderProcessGoneDetail detail) {
840
- super.onRenderProcessGone(view, detail);
841
- boolean result = false;
842
-
843
- List<WebViewListener> webViewListeners = bridge.getWebViewListeners();
844
- if (webViewListeners != null) {
845
- for (WebViewListener listener : bridge.getWebViewListeners()) {
846
- result = listener.onRenderProcessGone(view, detail) || result;
847
- }
848
- }
849
-
850
- return result;
851
- }`;
852
-
853
- const newCode = `@Override
854
- public boolean onRenderProcessGone(WebView view, RenderProcessGoneDetail detail) {
855
- super.onRenderProcessGone(view, detail);
856
-
857
- boolean result = false;
858
-
859
- List<WebViewListener> webViewListeners = bridge.getWebViewListeners();
860
- if (webViewListeners != null) {
861
- for (WebViewListener listener : bridge.getWebViewListeners()) {
862
- result = listener.onRenderProcessGone(view, detail) || result;
863
- }
864
- }
865
-
866
- if (!result) {
867
- // If no one handled it, handle it ourselves!
868
-
869
- /*if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
870
- if (detail.didCrash()) {
871
- //Log.e("CapacitorWebView", "WebView crashed internally!");
872
- } else {
873
- //Log.w("CapacitorWebView", "WebView was killed by system (low memory) internally!");
874
- }
875
- }*/
876
-
877
- view.post(() -> {
878
- Toast.makeText(view.getContext(), "Reloading due to low memory issue", Toast.LENGTH_SHORT).show();
879
- });
880
-
881
- view.reload(); // Safely reload WebView
882
-
883
- return true; // We handled it
884
- }
885
-
886
- return result;
887
- }`;
888
-
889
- // Step 1: Update method if needed
890
- let updated = false;
891
-
892
- if (fileContent.includes(oldCodeStart)) {
893
- console.log('✅ Found old onRenderProcessGone method. Replacing it...');
894
- fileContent = fileContent.replace(oldCodeStart, newCode);
895
- updated = true;
896
- } else if (fileContent.includes(newCode)) {
897
- console.log('ℹ️ Method already updated. No changes needed in "BridgeWebViewClient.java".');
898
- } else {
899
- console.error('❌ Error: Neither old nor new code found. Unexpected content.');
900
- process.exit(1);
901
- }
902
-
903
- // Step 2: Check and add import if missing
904
- const importToast = 'import android.widget.Toast;';
905
- if (!fileContent.includes(importToast)) {
906
- console.log('✅ Adding missing import for Toast...');
907
- const importRegex = /import\s+[^;]+;/g;
908
- const matches = [...fileContent.matchAll(importRegex)];
909
-
910
- if (matches.length > 0) {
911
- const lastImport = matches[matches.length - 1];
912
- const insertPosition = lastImport.index + lastImport[0].length;
913
- fileContent = fileContent.slice(0, insertPosition) + `\n${importToast}` + fileContent.slice(insertPosition);
914
- updated = true;
915
- } else {
916
- console.error('❌ Error: No import section found in file.');
917
- process.exit(1);
918
- }
919
- } else {
920
- console.log('ℹ️ Import for Toast already exists. No changes needed.');
921
- }
922
-
923
- // Step 3: Save if updated
924
- if (updated) {
925
- fs.writeFileSync(bridgeWebViewClientFilePath, fileContent, 'utf8');
926
- console.log('✅ File updated successfully.');
927
- } else {
928
- console.log('ℹ️ No changes needed.');
929
- }
930
-
931
-
932
-
933
-
934
- // Check and change the "BridgeWebViewClient.java" file END
935
-
936
-
937
-
938
-
939
-
940
-
941
-
942
-
943
- /*
944
- // To resolve the kotlin version issue, we need to update the kotlin version in the build.gradle file START
945
-
946
- // Build the path dynamically like you requested
947
- const gradlePath = path.join(
948
- process.cwd(),
949
- 'android',
950
- 'build.gradle'
951
- );
952
-
953
- // Read the existing build.gradle
954
- let gradleContent = fs.readFileSync(gradlePath, 'utf8');
955
-
956
- // Add `ext.kotlin_version` if it's not already there
957
- if (!gradleContent.includes('ext.kotlin_version')) {
958
- gradleContent = gradleContent.replace(
959
- /buildscript\s*{/,
960
- `buildscript {\n ext.kotlin_version = '2.1.0'`
961
- );
962
- }
963
-
964
- // Add Kotlin classpath if it's not already there
965
- if (!gradleContent.includes('org.jetbrains.kotlin:kotlin-gradle-plugin')) {
966
- gradleContent = gradleContent.replace(
967
- /dependencies\s*{([\s\S]*?)classpath 'com.android.tools.build:gradle:8.7.2'/,
968
- `dependencies {\n classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version")\n$1classpath 'com.android.tools.build:gradle:8.7.2'`
969
- );
970
- }
971
-
972
- // Write back the modified content
973
- fs.writeFileSync(gradlePath, gradleContent, 'utf8');
974
-
975
- console.log('✅ Kotlin version updated in build.gradle.');
976
-
977
- // To resolve the kotlin version issue, we need to update the kotlin version in the build.gradle file END
978
- */
979
-
980
-
981
-
982
-
983
-
984
-
985
-
986
-
987
- let _admobConfig;
988
-
989
-
990
-
991
- const androidPlatformPath = path.join(process.cwd(), 'android');
992
- const iosPlatformPath = path.join(process.cwd(), 'ios');
993
- const pluginPath = path.join(process.cwd(), 'node_modules', 'emi-indo-cordova-plugin-admob', 'plugin.xml');
994
- const infoPlistPath = path.join(process.cwd(), 'ios', 'App', 'App', 'Info.plist');
995
- const resourcesPath = path.join(process.cwd(), 'resources', 'res');
996
- const androidResPath = path.join(process.cwd(), 'android', 'app', 'src', 'main', 'res');
997
- const localNotificationsPluginPath = path.join(process.cwd(), 'node_modules', '@capacitor', 'local-notifications');
998
-
999
- function fileExists(filePath) {
1000
- return fs.existsSync(filePath);
1001
- }
1002
-
1003
- function copyFolderSync(source, target) {
1004
- if (!fs.existsSync(target)) {
1005
- fs.mkdirSync(target, { recursive: true });
1006
- }
1007
-
1008
- fs.readdirSync(source).forEach(file => {
1009
- const sourceFile = path.join(source, file);
1010
- const targetFile = path.join(target, file);
1011
-
1012
- if (fs.lstatSync(sourceFile).isDirectory()) {
1013
- copyFolderSync(sourceFile, targetFile);
1014
- } else {
1015
- fs.copyFileSync(sourceFile, targetFile);
1016
- }
1017
- });
1018
- }
1019
-
1020
- function checkAndCopyResources() {
1021
- if (fileExists(resourcesPath)) {
1022
- copyFolderSync(resourcesPath, androidResPath);
1023
- console.log('✅ Successfully copied resources/res to android/app/src/main/res.');
1024
- } else {
1025
- console.log('resources/res folder not found.');
1026
-
1027
- if (fileExists(localNotificationsPluginPath)) {
1028
- throw new Error('❌ resources/res is required for @capacitor/local-notifications. Stopping execution.');
1029
- }
1030
- }
1031
- }
1032
-
1033
-
1034
-
1035
-
1036
-
1037
-
1038
-
1039
-
1040
- function getAdMobConfig() {
1041
- if (!fileExists(configPath)) {
1042
- throw new Error('❌ capacitor.config.json not found. Ensure this is a Capacitor project.');
1043
- }
1044
-
1045
- const config = JSON.parse(fs.readFileSync(configPath, 'utf8'));
1046
- const admobConfig = config.plugins?.AdMob;
1047
-
1048
- if (!admobConfig) {
1049
- throw new Error('❌ AdMob configuration is missing in capacitor.config.json.');
1050
- }
1051
-
1052
- // Default to true if ADMOB_ENABLED is not specified
1053
- const isEnabled = admobConfig.ADMOB_ENABLED !== false;
1054
-
1055
- if (!isEnabled) {
1056
- return { ADMOB_ENABLED: false }; // Skip further validation
1057
- }
1058
-
1059
- if (!admobConfig.APP_ID_ANDROID || !admobConfig.APP_ID_IOS) {
1060
- throw new Error(' AdMob configuration is incomplete. Ensure APP_ID_ANDROID and APP_ID_IOS are defined.');
1061
- }
1062
-
1063
- return {
1064
- ADMOB_ENABLED: true,
1065
- APP_ID_ANDROID: admobConfig.APP_ID_ANDROID,
1066
- APP_ID_IOS: admobConfig.APP_ID_IOS,
1067
- USE_LITE_ADS: admobConfig.USE_LITE_ADS === "lite",
1068
- };
1069
- }
1070
-
1071
- function validateAndroidBuildOptions() {
1072
-
1073
-
1074
- if (!fileExists(configPath)) {
1075
- console.log('❌ capacitor.config.json not found. Ensure this is a Capacitor project.');
1076
- process.exit(1);
1077
- }
1078
-
1079
- const config = JSON.parse(fs.readFileSync(configPath, 'utf8'));
1080
-
1081
- const targetAppId=config.appId
1082
-
1083
- const buildOptions = config.android?.buildOptions;
1084
-
1085
- if (!buildOptions) {
1086
- console.log('❌ Missing android.buildOptions in capacitor.config.json.');
1087
- process.exit(1);
1088
- }
1089
-
1090
- const requiredProps = [
1091
- 'keystorePath',
1092
- 'keystorePassword',
1093
- 'keystoreAlias',
1094
- 'keystoreAliasPassword',
1095
- 'releaseType',
1096
- 'signingType'
1097
- ];
1098
-
1099
- const missing = requiredProps.filter(prop => !buildOptions[prop]);
1100
-
1101
- if (missing.length > 0) {
1102
- console.log('❌ Missing properties android.buildOptions in capacitor.config.json.');
1103
- process.exit(1);
1104
- }
1105
-
1106
-
1107
- const keystorePath=buildOptions.keystorePath
1108
- const keyFileName = path.basename(keystorePath);
1109
-
1110
-
1111
-
1112
- const keystoreMap = {
1113
- "gameskey.jks": [
1114
- "com.cube.blaster",
1115
- ],
1116
- "htmleditorkeystoke.jks": [
1117
- "com.HTML.AngularJS.Codeplay",
1118
- "com.html.codeplay.pro",
1119
- "com.bootstrap.code.play",
1120
- "com.kids.learning.master",
1121
- "com.Simple.Barcode.Scanner",
1122
- "com.FAShtmlcssjs.editor"
1123
- ]
1124
- };
1125
-
1126
- // find which keystore is required for the given targetAppId
1127
- let requiredKey = "newappskey.jks"; // default
1128
- for (const [keyFile, appIds] of Object.entries(keystoreMap)) {
1129
- if (appIds.includes(targetAppId)) {
1130
- requiredKey = keyFile;
1131
- break;
1132
- }
1133
- }
1134
-
1135
- // validate
1136
- if (keyFileName !== requiredKey) {
1137
- console.log(`❌ The keystore path is mismatched. Expected ${requiredKey} for ${targetAppId}, but got ${keyFileName}`);
1138
- process.exit(1);
1139
- }
1140
-
1141
-
1142
-
1143
-
1144
-
1145
- // optionally return them
1146
- //return buildOptions;
1147
- }
1148
-
1149
- function updatePluginXml(admobConfig) {
1150
- if (!fileExists(pluginPath)) {
1151
- console.error(' ❌ plugin.xml not found. Ensure the plugin is installed.');
1152
- return;
1153
- }
1154
-
1155
- let pluginContent = fs.readFileSync(pluginPath, 'utf8');
1156
-
1157
- pluginContent = pluginContent
1158
- .replace(/<preference name="APP_ID_ANDROID" default=".*?" \/>/, `<preference name="APP_ID_ANDROID" default="${admobConfig.APP_ID_ANDROID}" />`)
1159
- .replace(/<preference name="APP_ID_IOS" default=".*?" \/>/, `<preference name="APP_ID_IOS" default="${admobConfig.APP_ID_IOS}" />`);
1160
-
1161
- fs.writeFileSync(pluginPath, pluginContent, 'utf8');
1162
- console.log('✅ AdMob IDs successfully updated in plugin.xml');
1163
- }
1164
-
1165
- function updateInfoPlist(admobConfig) {
1166
- if (!fileExists(infoPlistPath)) {
1167
- console.error(' ❌ Info.plist not found. Ensure you have built the iOS project.');
1168
- return;
1169
- }
1170
-
1171
- const plistContent = fs.readFileSync(infoPlistPath, 'utf8');
1172
- const plistData = plist.parse(plistContent);
1173
-
1174
- plistData.GADApplicationIdentifier = admobConfig.APP_ID_IOS;
1175
- plistData.NSUserTrackingUsageDescription = 'This identifier will be used to deliver personalized ads to you.';
1176
- plistData.GADDelayAppMeasurementInit = true;
1177
-
1178
- const updatedPlistContent = plist.build(plistData);
1179
- fs.writeFileSync(infoPlistPath, updatedPlistContent, 'utf8');
1180
- console.log('AdMob IDs and additional configurations successfully updated in Info.plist');
1181
- }
1182
-
1183
-
1184
- try {
1185
- if (!fileExists(configPath)) {
1186
- throw new Error(' ❌ capacitor.config.json not found. Skipping setup.');
1187
- }
1188
-
1189
- if (!fileExists(androidPlatformPath) && !fileExists(iosPlatformPath)) {
1190
- throw new Error('Neither Android nor iOS platforms are found. Ensure platforms are added to your Capacitor project.');
1191
- }
1192
-
1193
- checkAndCopyResources();
1194
-
1195
-
1196
-
1197
- _admobConfig = getAdMobConfig();
1198
-
1199
-
1200
-
1201
-
1202
-
1203
- // Proceed only if ADMOB_ENABLED is true
1204
- if (_admobConfig.ADMOB_ENABLED) {
1205
- if (fileExists(androidPlatformPath)) {
1206
- updatePluginXml(_admobConfig);
1207
- }
1208
-
1209
- if (fileExists(iosPlatformPath)) {
1210
- updateInfoPlist(_admobConfig);
1211
- }
1212
- }
1213
-
1214
-
1215
- } catch (error) {
1216
- console.error(error.message);
1217
- process.exit(1); // Stop execution if there's a critical error
1218
- }
1219
-
1220
-
1221
-
1222
- validateAndroidBuildOptions();
1223
-
1224
-
1225
-
1226
-
1227
-
1228
-
1229
- // Check all the codeplays plugins version START
1230
-
1231
-
1232
- const readline = require('readline');
1233
-
1234
-
1235
- //const srcDir = path.join(__dirname, 'src');
1236
- const srcDir = path.join(process.cwd(), 'src');
1237
- let outdatedPlugins = [];
1238
-
1239
- function parseVersion(ver) {
1240
- return ver.split('.').map(n => parseInt(n, 10));
1241
- }
1242
-
1243
- function compareVersions(v1, v2) {
1244
- const [a1, b1] = parseVersion(v1);
1245
- const [a2, b2] = parseVersion(v2);
1246
- if (a1 !== a2) return a1 - a2;
1247
- return b1 - b2;
1248
- }
1249
-
1250
- function walkSync(dir, filelist = []) {
1251
- fs.readdirSync(dir).forEach(file => {
1252
- const fullPath = path.join(dir, file);
1253
- const stat = fs.statSync(fullPath);
1254
- if (stat.isDirectory()) {
1255
- walkSync(fullPath, filelist);
1256
- } else {
1257
- filelist.push(fullPath);
1258
- }
1259
- });
1260
- return filelist;
1261
- }
1262
-
1263
-
1264
-
1265
- function getSearchRoot(plugin) {
1266
- return path.join(srcDir, plugin.baseDir || 'js');
1267
- }
1268
-
1269
-
1270
-
1271
-
1272
-
1273
-
1274
-
1275
-
1276
-
1277
-
1278
-
1279
-
1280
-
1281
-
1282
-
1283
-
1284
-
1285
-
1286
-
1287
-
1288
- /*############################################## AUTO DOWNLOAD FROM SERVER START #####################################*/
1289
-
1290
- // ============================================================
1291
- // 🔥 AUTO PLUGIN UPDATE SYSTEM (MANDATORY UPDATES)
1292
- // ============================================================
1293
-
1294
- const https = require("https");
1295
-
1296
- /**
1297
- * Check if file exists on server using HEAD request
1298
- */
1299
- function urlExists(url) {
1300
- return new Promise(resolve => {
1301
- const req = https.request(url, { method: "HEAD" }, res => {
1302
- resolve(res.statusCode === 200);
1303
- });
1304
-
1305
- req.on("error", () => resolve(false));
1306
- req.end();
1307
- });
1308
- }
1309
-
1310
- /**
1311
- * Download file from server
1312
- */
1313
- function downloadFile(url, dest) {
1314
- return new Promise((resolve, reject) => {
1315
- const file = fs.createWriteStream(dest);
1316
-
1317
- https.get(url, response => {
1318
- if (response.statusCode !== 200) {
1319
- reject("Download failed");
1320
- return;
1321
- }
1322
-
1323
- response.pipe(file);
1324
-
1325
- file.on("finish", () => {
1326
- file.close(resolve);
1327
- });
1328
- }).on("error", err => {
1329
- fs.unlink(dest, () => {});
1330
- reject(err);
1331
- });
1332
- });
1333
- }
1334
-
1335
- /**
1336
- * Update imports across src folder
1337
- * Replaces old filename → new filename
1338
- */
1339
- /**
1340
- * Update imports across project
1341
- * Replaces old filename → new filename
1342
- */
1343
-
1344
- const VITE_ALIAS_ONLY = [
1345
- "common",
1346
- "admob-emi",
1347
- "localization",
1348
- "theme",
1349
- "certificatejs",
1350
- "ffmpeg"
1351
- ];
1352
-
1353
- function updateImports(oldName, newName) {
1354
-
1355
- const projectRoot = process.cwd();
1356
-
1357
- const filesToScan = [
1358
- path.join(projectRoot, "vite.config.js"),
1359
- path.join(projectRoot, "vite.config.mjs")
1360
- ];
1361
-
1362
- const srcDir = path.join(projectRoot, "src");
1363
-
1364
- // scan vite config
1365
- filesToScan.forEach(file => {
1366
-
1367
- if (!fs.existsSync(file)) return;
1368
-
1369
- let content = fs.readFileSync(file, "utf8");
1370
-
1371
- if (content.includes(oldName)) {
1372
-
1373
- content = content.split(oldName).join(newName);
1374
-
1375
- fs.writeFileSync(file, content);
1376
-
1377
- console.log(`✏️ Updated alias in ${path.basename(file)}`);
1378
- }
1379
-
1380
- });
1381
-
1382
- // scan src files
1383
- function walk(dir) {
1384
-
1385
- fs.readdirSync(dir).forEach(file => {
1386
-
1387
- if (["node_modules","android","ios","dist",".git"].includes(file))
1388
- return;
1389
-
1390
- const full = path.join(dir, file);
1391
- const stat = fs.statSync(full);
1392
-
1393
- if (stat.isDirectory()) {
1394
- walk(full);
1395
- }
1396
-
1397
- else if (
1398
- (full.endsWith(".js") ||
1399
- full.endsWith(".f7") ||
1400
- full.endsWith(".mjs")) &&
1401
- !full.endsWith(".min.js")
1402
- ) {
1403
-
1404
- let content = fs.readFileSync(full, "utf8");
1405
-
1406
- if (content.includes(oldName)) {
1407
-
1408
- content = content.split(oldName).join(newName);
1409
-
1410
- fs.writeFileSync(full, content);
1411
-
1412
- console.log(`✏️ Updated import in ${path.relative(projectRoot, full)}`);
1413
- }
1414
-
1415
- }
1416
-
1417
- });
1418
-
1419
- }
1420
-
1421
- if (fs.existsSync(srcDir)) {
1422
- walk(srcDir);
1423
- }
1424
-
1425
- }
1426
-
1427
-
1428
- /**
1429
- * Auto-update a plugin file
1430
- * Returns TRUE if success
1431
- * Returns FALSE if fallback to manual needed
1432
- */
1433
-
1434
-
1435
- let _serverVersions = null;
1436
- async function fetchVersions() {
1437
-
1438
- if (_serverVersions) return _serverVersions;
1439
-
1440
- return new Promise((resolve) => {
1441
-
1442
- https.get(
1443
- "https://htmlcodeplay.com/code-play-plugin/versions.json",
1444
- { timeout: 5000 },
1445
- res => {
1446
-
1447
- let data = "";
1448
-
1449
- res.on("data", chunk => data += chunk);
1450
-
1451
- res.on("end", () => {
1452
-
1453
- try {
1454
-
1455
- _serverVersions = JSON.parse(data);
1456
-
1457
- resolve(_serverVersions);
1458
-
1459
- } catch {
1460
-
1461
- resolve(null);
1462
-
1463
- }
1464
-
1465
- });
1466
-
1467
- }
1468
-
1469
- ).on("error", () => resolve(null));
1470
-
1471
- });
1472
-
1473
- }
1474
-
1475
-
1476
-
1477
- async function autoUpdatePlugin(pluginDef, pluginInfo) {
1478
-
1479
- const versions = await fetchVersions();
1480
-
1481
- if (!versions) {
1482
- console.log("⚠️ versions.json not reachable");
1483
- return false;
1484
- }
1485
-
1486
- const oldFullPath = path.join(srcDir, pluginInfo.name);
1487
- const oldFileName = path.basename(oldFullPath);
1488
-
1489
- //const oldFileName = path.basename(oldFullPath);
1490
- const oldVersionFile = oldFileName;
1491
-
1492
-
1493
- const ext = path.extname(oldFileName); // .js or .less
1494
- //const baseName = oldFileName.replace(/-\d+\.\d+.*$/, "");
1495
- const baseName = oldFileName.replace(/-\d+\.\d+.*$/, '').replace(/\.(js|less)$/, '');
1496
-
1497
- // version lookup key
1498
- let pluginKey = baseName;
1499
-
1500
-
1501
- // Only common plugin has js and less variants
1502
- if (baseName === "common") {
1503
- if (ext === ".js") pluginKey = "common-js";
1504
- if (ext === ".less") pluginKey = "common-less";
1505
- }
1506
-
1507
- const latestVersion = versions[pluginKey];
1508
-
1509
- if (!latestVersion) {
1510
- console.log(`❌ No version entry for ${baseName}`);
1511
- return false;
1512
- }
1513
-
1514
- // ===============================
1515
- // FOLDER PLUGIN UPDATE
1516
- // ===============================
1517
- if (pluginDef.isFolder) {
1518
-
1519
- const zipName = `${baseName}-${latestVersion}.zip`;
1520
- const url = `https://htmlcodeplay.com/code-play-plugin/${zipName}`;
1521
-
1522
- const destRoot = path.join(srcDir, pluginDef.destDir || pluginDef.baseDir || '');
1523
- const oldPath = path.join(destRoot, pluginInfo.name);
1524
- const newPath = path.join(destRoot, `${baseName}-${latestVersion}`);
1525
-
1526
- if (!(await urlExists(url))) return false;
1527
-
1528
- fs.rmSync(oldPath, { recursive: true, force: true });
1529
-
1530
- await downloadAndExtractZip(url, newPath);
1531
-
1532
- updateImports(pluginInfo.name, `${baseName}-${latestVersion}`);
1533
-
1534
- // ✅ ADD THIS
1535
- writeUpdateLine(`${pluginInfo.name} -> ${baseName}-${latestVersion}`);
1536
-
1537
- console.log(`✅ Folder updated → ${baseName}-${latestVersion}`);
1538
-
1539
- return true;
1540
- }
1541
- // ===============================
1542
- // FILE PLUGIN UPDATE
1543
- // ===============================
1544
-
1545
- const pluginDir = path.dirname(oldFullPath);
1546
-
1547
- // Only this plugin has ios variant
1548
- const IOS_VARIANT_PLUGINS = [
1549
- "saveToGalleryAndSaveAnyFile"
1550
- ];
1551
-
1552
- /* let variants = [
1553
- `${baseName}-${latestVersion}.js`
1554
- ]; */
1555
-
1556
- //const ext = path.extname(oldFileName);
1557
- const variants = [`${baseName.replace(ext,'')}-${latestVersion}${ext}`];
1558
-
1559
-
1560
- if (IOS_VARIANT_PLUGINS.includes(baseName)) {
1561
- variants.push(`${baseName}-${latestVersion}-ios.js`);
1562
- }
1563
-
1564
- let downloaded = [];
1565
-
1566
- // Download files
1567
- for (const fileName of variants) {
1568
-
1569
- const url = `https://htmlcodeplay.com/code-play-plugin/${fileName}`;
1570
-
1571
- console.log(`🔍 Checking latest: ${fileName}`);
1572
-
1573
- if (await urlExists(url)) {
1574
-
1575
- const destPath = path.join(pluginDir, fileName);
1576
-
1577
- await downloadFile(url, destPath);
1578
-
1579
- downloaded.push(fileName);
1580
-
1581
- console.log(`⬇ Downloaded → ${fileName}`);
1582
-
1583
- //writeUpdateLine(`Downloaded: ${fileName}`);
1584
-
1585
- }
1586
- }
1587
-
1588
- if (downloaded.length === 0) {
1589
- console.log(`❌ No files downloaded for ${baseName}`);
1590
- return false;
1591
- }
1592
-
1593
- // Remove ONLY versioned files (safe)
1594
- //const versionPattern = new RegExp(`^${baseName}-\\d+\\.\\d+(-ios)?\\.js$`);
1595
- const versionPattern = new RegExp(`^${baseName}-\\d+\\.\\d+(-ios)?\\${ext}$`);
1596
-
1597
- const existingFiles = fs.readdirSync(pluginDir);
1598
-
1599
- existingFiles.forEach(file => {
1600
-
1601
- if (
1602
- versionPattern.test(file) &&
1603
- !downloaded.includes(file)
1604
- ) {
1605
-
1606
- const oldPath = path.join(pluginDir, file);
1607
-
1608
- fs.unlinkSync(oldPath);
1609
-
1610
- console.log(`🗑 Removed old file → ${file}`);
1611
- //writeUpdateLine(`Removed old file: ${file}`);
1612
- }
1613
-
1614
- });
1615
-
1616
- //const newFileName = `${baseName}-${latestVersion}.js`;
1617
- const newFileName = `${baseName}-${latestVersion}${ext}`;
1618
-
1619
- writeUpdateLine(`${oldVersionFile} -> ${newFileName}`);
1620
-
1621
- updateImports(oldFileName, newFileName);
1622
- //updateImports(baseName, `${baseName}-${latestVersion}.js`);
1623
- //updateImports(pluginInfo.name, `${baseName}-${latestVersion}`);
1624
-
1625
- //console.log(`✅ Updated → ${newFileName}`);
1626
- //console.log(`✅ Updated → ${baseName}-${latestVersion}`);
1627
-
1628
- return true;
1629
- }
1630
-
1631
-
1632
-
1633
-
1634
- /*############################################## AUTO DOWNLOAD FROM SERVER END #####################################*/
1635
-
1636
-
1637
-
1638
-
1639
-
1640
-
1641
-
1642
-
1643
-
1644
-
1645
-
1646
-
1647
-
1648
-
1649
-
1650
-
1651
-
1652
-
1653
- async function loadPluginVersions() {
1654
-
1655
- if (!USE_LIVE_SERVER_VERSION) {
1656
- console.log("ℹ️ Using local plugin versions (offline mode).");
1657
- return;
1658
- }
1659
-
1660
- console.log("🌐 Fetching plugin versions from server...");
1661
-
1662
- const versions = await fetchVersions();
1663
-
1664
- if (!versions || typeof versions !== "object") {
1665
- console.log("⚠️ Server unavailable or invalid versions.json. Falling back to local versions.");
1666
- return;
1667
- }
1668
-
1669
- requiredPlugins.forEach(plugin => {
1670
-
1671
- if (!plugin.name) return;
1672
-
1673
- if (versions[plugin.name]) {
1674
- plugin.minVersion = versions[plugin.name];
1675
- }
1676
-
1677
- });
1678
-
1679
- console.log("✅ Plugin versions loaded from server.");
1680
-
1681
- }
1682
-
1683
-
1684
-
1685
- let hasMandatoryUpdate = false;
1686
- function checkPlugins() {
1687
- return new Promise(async (resolve, reject) => {
1688
- const files = walkSync(srcDir);
1689
- const outdatedPlugins = [];
1690
- let hasMandatoryUpdate = false;
1691
-
1692
- for (const plugin of requiredPlugins) {
1693
- const searchRoot = getSearchRoot(plugin);
1694
-
1695
- // ---------- Folder plugins ----------
1696
- if (plugin.isFolder) {
1697
- if (!fs.existsSync(searchRoot)) continue;
1698
-
1699
- const subDirs = fs.readdirSync(searchRoot)
1700
- .map(name => path.join(searchRoot, name))
1701
- .filter(p => fs.statSync(p).isDirectory());
1702
-
1703
- for (const dir of subDirs) {
1704
- const relativePath = path.relative(searchRoot, dir).replace(/\\/g, '/');
1705
- const match = plugin.pattern.exec(relativePath);
1706
-
1707
- if (match) {
1708
- const currentVersion = match[1];
1709
-
1710
- if (compareVersions(currentVersion, plugin.minVersion) < 0) {
1711
- outdatedPlugins.push({
1712
- name: relativePath,
1713
- currentVersion,
1714
- requiredVersion: plugin.minVersion,
1715
- mandatoryUpdate: plugin.mandatoryUpdate === true
1716
- });
1717
-
1718
- if (plugin.mandatoryUpdate) {
1719
- hasMandatoryUpdate = true;
1720
- }
1721
- }
1722
- }
1723
- }
1724
- continue;
1725
- }
1726
-
1727
- // ---------- File plugins ----------
1728
- const matchedFile = files.find(file =>
1729
- file.startsWith(searchRoot) && plugin.pattern.test(file)
1730
- );
1731
-
1732
- if (matchedFile) {
1733
- const match = plugin.pattern.exec(matchedFile);
1734
- if (match) {
1735
- const currentVersion = match[1];
1736
- const isBeta = !!match[2];
1737
-
1738
- const cmp = plugin.pattern.source.includes('beta')
1739
- ? compareWithBeta(currentVersion, plugin.minVersion, isBeta)
1740
- : compareVersions(currentVersion, plugin.minVersion);
1741
-
1742
- if (cmp < 0) {
1743
- outdatedPlugins.push({
1744
- name: path.relative(srcDir, matchedFile),
1745
- currentVersion: isBeta ? `${currentVersion}-beta` : currentVersion,
1746
- requiredVersion: plugin.minVersion,
1747
- mandatoryUpdate: plugin.mandatoryUpdate === true
1748
- });
1749
-
1750
- if (plugin.mandatoryUpdate) {
1751
- hasMandatoryUpdate = true;
1752
- }
1753
- }
1754
- }
1755
- }
1756
- }
1757
-
1758
- // ---------- Result handling ----------
1759
- if (outdatedPlugins.length > 0) {
1760
- console.log('\n❗ The following plugins are outdated:\n');
1761
-
1762
- outdatedPlugins.forEach( p => {
1763
- const tag = p.mandatoryUpdate ? '🔥 MANDATORY' : '';
1764
- console.log(
1765
- ` ⚠️ - ${p.name} (Current: ${p.currentVersion}, Required: ${p.requiredVersion}) ${tag}`
1766
- );
1767
- });
1768
-
1769
- // 🚨 Mandatory update → stop build
1770
- /* if (hasMandatoryUpdate) {
1771
- console.log('\n🚫 One or more plugins require a mandatory update.');
1772
- console.log('❌ Build cancelled. Please update mandatory plugins and try again.');
1773
- process.exit(1);
1774
- } */
1775
-
1776
-
1777
-
1778
-
1779
-
1780
- if (hasMandatoryUpdate) {
1781
-
1782
- //--------------------------------------------------
1783
- // 🚫 AUTO UPDATE DISABLED
1784
- //--------------------------------------------------
1785
- if (!ENABLE_AUTO_UPDATE) {
1786
- console.log("\n🚫 Auto-update disabled.");
1787
- console.log("❌ Manual update required.");
1788
- process.exit(1);
1789
- }
1790
-
1791
- //--------------------------------------------------
1792
- // 🔥 AUTO UPDATE ENABLED
1793
- //--------------------------------------------------
1794
- console.log("\n🔥 Mandatory plugins outdated. Trying auto-update...\n");
1795
-
1796
-
1797
-
1798
- let autoFailed = false;
1799
-
1800
- for (const p of outdatedPlugins.filter(x => x.mandatoryUpdate)) {
1801
-
1802
- const pluginDef = requiredPlugins.find(def =>
1803
- def.pattern.test(p.name)
1804
- );
1805
-
1806
- if (!pluginDef) continue;
1807
-
1808
- const success = await autoUpdatePlugin(
1809
- pluginDef,
1810
- p
1811
- );
1812
-
1813
- if (!success) {
1814
- autoFailed = true;
1815
-
1816
- const pluginDef = requiredPlugins.find(def =>
1817
- def.pattern.test(p.name)
1818
- );
1819
-
1820
- console.log(`❌ Manual update required for ${p.name}`);
1821
-
1822
- if (pluginDef) {
1823
- console.log(`👉 Required minimum version: ${pluginDef.minVersion}`);
1824
- }
1825
- }
1826
- }
1827
-
1828
- // 🚨 Fallback to manual if any failed
1829
- if (autoFailed) {
1830
- console.log('\n🚫 One or more plugins require manual update.');
1831
- console.log('❌ Build cancelled. Please update mandatory plugins.');
1832
- process.exit(1);
1833
- }
1834
-
1835
- console.log('\n🎉 All mandatory plugins auto-updated! Rechecking plugins...\n');
1836
-
1837
- // Re-run plugin check so outdated list becomes empty
1838
- await checkPlugins();
1839
- return;
1840
- }
1841
-
1842
-
1843
-
1844
-
1845
-
1846
-
1847
- // Optional updates → ask user
1848
- const rl = readline.createInterface({
1849
- input: process.stdin,
1850
- output: process.stdout
1851
- });
1852
-
1853
- rl.question(
1854
- '\nAre you sure you want to continue without updating these plugins? (y/n): ',
1855
- answer => {
1856
- rl.close();
1857
-
1858
- if (answer.toLowerCase() !== 'y') {
1859
- console.log('\n❌ Build cancelled due to outdated plugins.');
1860
- process.exit(1);
1861
- } else {
1862
- console.log('\n✅ Continuing build...');
1863
- resolve();
1864
- }
1865
- }
1866
- );
1867
- } else {
1868
- console.log('✅ All plugin versions are up to date.');
1869
- saveUpdateLogs();
1870
- resolve();
1871
- }
1872
- });
1873
- }
1874
-
1875
-
1876
-
1877
-
1878
-
1879
-
1880
-
1881
-
1882
- // Check all the codeplays plugins version START
1883
-
1884
-
1885
-
1886
-
1887
- // ====================================================================
1888
- // AUTO-ADD esbuild.drop: ['console','debugger'] to vite.config.js / mjs
1889
- // ====================================================================
1890
-
1891
-
1892
-
1893
- const checkAndupdateDropInViteConfig = () => {
1894
-
1895
- const possibleFiles = [
1896
- "vite.config.js",
1897
- "vite.config.mjs"
1898
- ];
1899
-
1900
- // Detect existing config file
1901
- const viteConfigPath = possibleFiles
1902
- .map(file => path.join(process.cwd(), file))
1903
- .find(filePath => fs.existsSync(filePath));
1904
-
1905
- if (!viteConfigPath) {
1906
- console.warn("⚠️ No vite config found. Skipping.");
1907
- return;
1908
- }
1909
-
1910
- //console.log("📄 Using:", viteConfigPath.split("/").pop());
1911
-
1912
- let viteContent = fs.readFileSync(viteConfigPath, "utf8");
1913
-
1914
- // Skip if already exists
1915
- if (/drop\s*:\s*\[.*['"]console['"].*\]/.test(viteContent)) {
1916
- console.log("ℹ️ vite.config.(m)js already Updated. Skipping...");
1917
- return;
1918
- }
1919
-
1920
- console.log("🔧 Adding esbuild.drop ...");
1921
-
1922
- // If esbuild block exists
1923
- if (/esbuild\s*:\s*{/.test(viteContent)) {
1924
- viteContent = viteContent.replace(
1925
- /esbuild\s*:\s*{([\s\S]*?)(^ {0,8})}/m,
1926
- (full, inner, indent) => {
1927
-
1928
- let lines = inner
1929
- .split("\n")
1930
- .map(l => l.trim())
1931
- .filter(Boolean);
1932
-
1933
- // Fix last comma
1934
- if (lines.length > 0) {
1935
- lines[lines.length - 1] =
1936
- lines[lines.length - 1].replace(/,+$/, "") + ",";
1937
- }
1938
-
1939
- // Re-indent
1940
- lines = lines.map(l => indent + " " + l);
1941
-
1942
- // Add drop
1943
- lines.push(`${indent} drop: ['console','debugger'],`);
1944
-
1945
- return `esbuild: {\n${lines.join("\n")}\n${indent}}`;
1946
- }
1947
- );
1948
- }
1949
-
1950
- // If esbuild missing
1951
- else {
1952
- viteContent = viteContent.replace(
1953
- /export default defineConfig\s*\(\s*{/,
1954
- m => `${m}\n esbuild: {\n drop: ['console','debugger'],\n },`
1955
- );
1956
- }
1957
-
1958
- fs.writeFileSync(viteConfigPath, viteContent, "utf8");
1959
- console.log("✅ vite.config.(m)js Updated successfully.");
1960
- };
1961
-
1962
-
1963
-
1964
-
1965
-
1966
-
1967
-
1968
-
1969
-
1970
-
1971
- const compareVersion = (v1, v2) => {
1972
- const a = v1.split(".").map(Number);
1973
- const b = v2.split(".").map(Number);
1974
-
1975
- for (let i = 0; i < Math.max(a.length, b.length); i++) {
1976
- const num1 = a[i] || 0;
1977
- const num2 = b[i] || 0;
1978
- if (num1 > num2) return 1;
1979
- if (num1 < num2) return -1;
1980
- }
1981
- return 0;
1982
- };
1983
-
1984
-
1985
-
1986
-
1987
- const admobConfigPath = path.join('src', 'js','Ads', 'admob-ad-configuration.json');
1988
-
1989
- const checkAdmobConfigurationProperty=()=>{
1990
-
1991
-
1992
- if (!_admobConfig.ADMOB_ENABLED)
1993
- {
1994
- console.log("ℹ️ Admob is not enabled so 'admob-ad-configuration.json' checking is skipping...");
1995
- return;
1996
- }
1997
-
1998
-
1999
- const REQUIRED_CONFIG_KEYS = [
2000
- "isKidsApp",
2001
- "isTesting",
2002
- "isConsoleLogEnabled",
2003
- "bannerEnabled",
2004
- "interstitialEnabled",
2005
- "appOpenEnabled",
2006
- "rewardVideoEnabled",
2007
- "rewardInterstitialEnabled",
2008
- "collapsibleEnabled",
2009
- "isLandScape",
2010
- "isOverlappingEnable",
2011
- "bannerTypeAndroid",
2012
- "bannerTypeiOS",
2013
- "bannerTopSpaceColor",
2014
- "interstitialLoadScreenTextColor",
2015
- "interstitialLoadScreenBackgroundColor",
2016
- "beforeBannerSpace",
2017
- "whenShow",
2018
- "minimumClick",
2019
- "interstitialTimeOut",
2020
- "interstitialFirstTimeOut",
2021
- "appOpenAdsTimeOut",
2022
- "maxRetryCount",
2023
- "retrySecondsAr",
2024
- "appOpenPerSession",
2025
- "interstitialPerSession",
2026
- "appOpenFirstTimeOut"
2027
- ];
2028
-
2029
-
2030
-
2031
-
2032
-
2033
- let admobConfigInJson;
2034
-
2035
- try {
2036
- admobConfigInJson = JSON.parse(readFileSync(admobConfigPath, "utf8"));
2037
- } catch (err) {
2038
- console.error("❌ Failed to read admob-ad-configuration.json", err);
2039
- process.exit(1);
2040
- }
2041
-
2042
- // Validate config object exists
2043
- if (!admobConfigInJson.config) {
2044
- console.error('❌ "config" object is missing in admob-ad-configuration.json');
2045
- process.exit(1);
2046
- }
2047
-
2048
-
2049
- const admobConfigMinVersion="1.5"
2050
-
2051
- if (compareVersion(admobConfigInJson.VERSION, admobConfigMinVersion) < 0) {
2052
- console.error(`❌ Please use at-least version ${admobConfigMinVersion} in "src/js/Ads/admob-ad-configuration.json"`);
2053
- process.exit(1);
2054
- }
2055
-
2056
-
2057
- const config = admobConfigInJson.config;
2058
-
2059
- // Find missing properties
2060
- const missingKeys = REQUIRED_CONFIG_KEYS.filter(
2061
- key => !(key in config)
2062
- );
2063
-
2064
-
2065
-
2066
- if (missingKeys.length > 0) {
2067
- console.error(" Missing required configuration keys. Please check it in 'src/js/Ads/admob-ad-configuration.json'");
2068
-
2069
- missingKeys.forEach(k => console.error(" - " + k));
2070
- process.exit(1);
2071
- }
2072
-
2073
-
2074
- console.log('✅ All keys exist. in "admob-ad-configuration.json file" Configuration looks good.');
2075
- }
2076
-
2077
-
2078
-
2079
- function ensureGitignoreEntry(entry) {
2080
- const gitignorePath = path.join(process.cwd(), '.gitignore');
2081
-
2082
- // If .gitignore doesn't exist, create it
2083
- if (!fs.existsSync(gitignorePath)) {
2084
- fs.writeFileSync(gitignorePath, `${entry}\n`, 'utf8');
2085
- console.log(`✅ .gitignore created and added: ${entry}`);
2086
- return;
2087
- }
2088
-
2089
- const content = fs.readFileSync(gitignorePath, 'utf8');
2090
-
2091
- // Normalize lines (trim + remove trailing slashes for comparison)
2092
- const lines = content
2093
- .split(/\r?\n/)
2094
- .map(l => l.trim());
2095
-
2096
- const normalizedEntry = entry.replace(/\/$/, '');
2097
-
2098
- const exists = lines.some(
2099
- line => line.replace(/\/$/, '') === normalizedEntry
2100
- );
2101
-
2102
- if (exists) {
2103
- console.log(`ℹ️ .gitignore already contains: ${entry}`);
2104
- return;
2105
- }
2106
-
2107
- // Ensure file ends with newline
2108
- const separator = content.endsWith('\n') ? '' : '\n';
2109
-
2110
- fs.appendFileSync(gitignorePath, `${separator}${entry}\n`, 'utf8');
2111
- console.log(`✅ Added to .gitignore: ${entry}`);
2112
- }
2113
-
2114
-
2115
- ensureGitignoreEntry('buildCodeplay/');
2116
-
2117
-
2118
- // Run the validation
2119
- (async () => {
2120
-
2121
- await loadPluginVersions(); // 🔥 NEW
2122
-
2123
- await checkPlugins();
2124
- checkAndupdateDropInViteConfig();
2125
- checkAdmobConfigurationProperty()
2126
- })();
2127
-
2128
-
2129
- // ======================================================
2130
- // Validate theme folder location (src/js/theme is NOT allowed)
2131
- // ======================================================
2132
-
2133
- function validateThemeFolderLocation() {
2134
- const oldThemePath = path.join(process.cwd(), 'src', 'js', 'theme');
2135
- const newThemePath = path.join(process.cwd(), 'src', 'theme');
2136
-
2137
- // Block old structure
2138
- if (fs.existsSync(oldThemePath)) {
2139
- console.error(
2140
- '\n❌ INVALID PROJECT STRUCTURE DETECTED\n' +
2141
- '--------------------------------------------------\n' +
2142
- 'The "theme" folder must NOT be inside:\n' +
2143
- ' src/js/theme\n\n' +
2144
- '✅ Correct location is:\n' +
2145
- ' src/theme\n\n' +
2146
- '🛑 Please move the folder and re-run the build.\n'
2147
- );
2148
- process.exit(1);
2149
- }
2150
-
2151
- // ⚠️ Optional warning if new theme folder is missing
2152
- if (!fs.existsSync(newThemePath)) {
2153
- console.warn(
2154
- '\n⚠️ WARNING: "src/theme" folder not found.\n' +
2155
- 'If your app uses themes, please ensure it exists.\n'
2156
- );
2157
- } else {
2158
- console.log('✅ Theme folder structure validated (src/theme).');
2159
- }
2160
- }
2161
- validateThemeFolderLocation()
2162
-
2163
-
2164
-
2165
- const validateAndRestoreSignDetails=()=>{
2166
-
2167
- // Read config file
2168
- const config = JSON.parse(fs.readFileSync(configPath, 'utf8'));
2169
-
2170
- // Ensure android and buildOptions exist
2171
- if (!config.android) config.android = {};
2172
- if (!config.android.buildOptions) config.android.buildOptions = {};
2173
-
2174
- // Update only if changed
2175
- let updated = false;
2176
-
2177
- if (config.android.buildOptions.releaseType !== 'AAB') {
2178
- config.android.buildOptions.releaseType = 'AAB';
2179
- updated = true;
2180
- }
2181
-
2182
- if (config.android.buildOptions.signingType !== 'jarsigner') {
2183
- config.android.buildOptions.signingType = 'jarsigner';
2184
- updated = true;
2185
- }
2186
-
2187
- // Write back only if modified
2188
- if (updated) {
2189
- fs.writeFileSync(configPath, JSON.stringify(config, null, 2));
2190
- console.log('capacitor.config.json updated successfully.');
2191
- } else {
2192
- console.log('No changes needed.');
2193
- }
2194
-
2195
- }
2196
-
2197
- validateAndRestoreSignDetails()
2198
-
2199
-
2200
- execSync('node buildCodeplay/fix-onesignal-plugin.js', { stdio: 'inherit' });
2201
-
2202
-
2203
-
2204
-
2205
-
2206
-
2207
-
2208
- //################################## SystemBars.java update for "@capacitor/android": "^8.3.0" plugin START ###############################
2209
-
2210
-
2211
- const filePath = path.join(
2212
- __dirname,
2213
- "../node_modules/@capacitor/android/capacitor/src/main/java/com/getcapacitor/plugin/SystemBars.java"
2214
- );
2215
-
2216
- // 🔍 OLD BLOCK (anchor)
2217
- const OLD_BLOCK = `if (shouldPassthroughInsets) {
2218
- // We need to correct for a possible shown IME
2219
- v.setPadding(0, 0, 0, keyboardVisible ? imeInsets.bottom : 0);
2220
-
2221
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.VANILLA_ICE_CREAM && hasViewportCover && insetHandlingEnabled) {
2222
- Insets safeAreaInsets = calcSafeAreaInsets(insets);
2223
- injectSafeAreaCSS(safeAreaInsets.top, safeAreaInsets.right, safeAreaInsets.bottom, safeAreaInsets.left);
2224
- }
2225
-
2226
- return new WindowInsetsCompat.Builder(insets)
2227
- .setInsets(
2228
- WindowInsetsCompat.Type.systemBars() | WindowInsetsCompat.Type.displayCutout(),
2229
- Insets.of(
2230
- systemBarsInsets.left,
2231
- systemBarsInsets.top,
2232
- systemBarsInsets.right,
2233
- getBottomInset(systemBarsInsets, keyboardVisible)
2234
- )
2235
- )
2236
- .build();
2237
- }`;
2238
-
2239
- // ✅ NEW BLOCK
2240
- const NEW_BLOCK = `if (shouldPassthroughInsets) {
2241
- /* 🔴 ORIGINAL CODE (COMMENTED FOR SAFETY)
2242
- ${OLD_BLOCK.split("\n").map(line => " " + line).join("\n")}
2243
- */
2244
-
2245
- // ✅ NEW LOGIC (CUSTOM FIX)
2246
- v.setPadding(0, 0, 0, 0);
2247
-
2248
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.VANILLA_ICE_CREAM && hasViewportCover && insetHandlingEnabled) {
2249
- Insets safeAreaInsets = calcSafeAreaInsets(insets);
2250
- injectSafeAreaCSS(safeAreaInsets.top, safeAreaInsets.right, safeAreaInsets.bottom, safeAreaInsets.left);
2251
- }
2252
-
2253
- return insets; // WebView handles everything
2254
- }`;
2255
-
2256
- // 🚨 ERROR MESSAGE
2257
- const ERROR_MSG = `
2258
- ❌ Capacitor SystemBars.java structure changed!
2259
-
2260
- Plugin: @capacitor/android
2261
- Path : node_modules\@capacitor\android\capacitor\src\main\java\com\getcapacitor\plugin\SystemBars.java
2262
-
2263
- Check version of "@capacitor/android": in package.json
2264
-
2265
- 👉 Expected code block not found.
2266
-
2267
- This usually means Capacitor updated internally.
2268
-
2269
- Please:
2270
- 1. Open SystemBars.java
2271
- 2. Update patch script
2272
- 3. Re-run build
2273
-
2274
- ⛔ Build stopped.
2275
- `;
2276
-
2277
- function patchFile() {
2278
- if (!fs.existsSync(filePath)) {
2279
- console.error(" SystemBars.java not found!");
2280
- process.exit(1);
2281
- }
2282
-
2283
- let content = fs.readFileSync(filePath, "utf8");
2284
-
2285
- // ✅ Already patched?
2286
- if (content.includes("🔴 ORIGINAL CODE (COMMENTED FOR SAFETY)")) {
2287
- console.log("✅ Already SystemBars.java patched. Skipping...");
2288
- return;
2289
- }
2290
-
2291
- // 🔍 Check old block exists
2292
- if (!content.includes(OLD_BLOCK)) {
2293
- console.error(ERROR_MSG);
2294
- process.exit(1);
2295
- }
2296
-
2297
- // 🔁 Replace
2298
- const updated = content.replace(OLD_BLOCK, NEW_BLOCK);
2299
-
2300
- fs.writeFileSync(filePath, updated, "utf8");
2301
-
2302
- console.log("✅ SystemBars.java patched (comment + new logic)!");
2303
- }
2304
-
2305
- patchFile();
2306
-
2307
- //################################## SystemBars.java update for "@capacitor/android": "^8.3.0" plugin END ###############################
2308
-
2309
-
2310
-
2311
-
2312
- /*
2313
- Release Notes
2314
-
2315
- 5.1
2316
- Kotlin version update is commented. Previously admob is not worked if not update the kotlin version to higher version
2317
-
1
+ const fs = require('fs');
2
+ const path = require('path');
3
+ const plist = require('plist');
4
+
5
+
6
+
7
+ const { execSync } = require("child_process");
8
+
9
+
10
+
11
+
12
+ const { readFileSync } = require("fs");
13
+
14
+ const ENABLE_AUTO_UPDATE = true;
15
+ const USE_LIVE_SERVER_VERSION = true;
16
+
17
+ const configPath = path.join(process.cwd(), 'capacitor.config.json');
18
+ const updateLogFile = path.join(process.cwd(), "", "plugin-update-log.txt");
19
+
20
+ // Expected plugin list with minimum versions
21
+ /* const requiredPlugins = [
22
+
23
+ { pattern: /backbutton-(\d+\.\d+)\.js$/, minVersion: '2.0', required: true, baseDir: 'js',mandatoryUpdate: true},
24
+
25
+
26
+ { pattern: /common-(\d+\.\d+)(?:-beta-(\d+))?\.js$/, minVersion: '6.0', required: true, baseDir: 'js',mandatoryUpdate: true },
27
+
28
+ { pattern: /localization_settings-(\d+\.\d+)\.js$/, minVersion: '1.1', required: true, baseDir: 'js',mandatoryUpdate: false },
29
+ { pattern: /localization-(\d+\.\d+)\.js$/, minVersion: '1.5', required: true, baseDir: 'js',mandatoryUpdate: true },
30
+ { pattern: /localNotification-(\d+\.\d+)\.js$/, minVersion: '2.2', required: true, baseDir: 'js',mandatoryUpdate: false },
31
+ { pattern: /localNotification_AppSettings-(\d+\.\d+)\.js$/, minVersion: '1.0', required: true, baseDir: 'js',mandatoryUpdate: false },
32
+ { pattern: /onesignal-(\d+\.\d+)\.js$/, minVersion: '2.3', required: true, baseDir: 'js',mandatoryUpdate: false },
33
+ { pattern: /saveToGalleryAndSaveAnyFile-(\d+\.\d+)(-ios)?\.js$/, minVersion: '3.1', required: true, baseDir: 'js',mandatoryUpdate: true },
34
+ { pattern: /Ads[\/\\]admob-emi-(\d+\.\d+)\.js$/, minVersion: '3.7', required: true, baseDir: 'js',mandatoryUpdate: true },
35
+
36
+ // New added plugins
37
+ { pattern: /video-player-(\d+\.\d+)\.js$/, minVersion: '1.5', required: true, baseDir: 'js',mandatoryUpdate: false },
38
+ { pattern: /image-cropper-(\d+\.\d+)\.js$/, minVersion: '1.1', required: true, baseDir: 'js',mandatoryUpdate: false },
39
+ { pattern: /common-(\d+\.\d+)\.less$/, minVersion: '1.6', required: true, baseDir: 'assets/css',mandatoryUpdate: false },
40
+
41
+
42
+ // New folders
43
+ { pattern: /IAP-(\d+\.\d+)$/, minVersion: '2.8', isFolder: true , required: true, baseDir: 'js/Ads',mandatoryUpdate: true },
44
+ { pattern: /editor-(\d+\.\d+)$/, minVersion: '1.9', isFolder: true, required: true, baseDir: 'js',mandatoryUpdate: true },
45
+ { pattern: /ffmpeg-(\d+\.\d+)$/, minVersion: '1.6', isFolder: true, required: true, baseDir: 'js',mandatoryUpdate: true },
46
+ { pattern: /theme-(\d+\.\d+)$/, minVersion: '3.3', isFolder: true , required: true, baseDir: 'theme',mandatoryUpdate: true },
47
+
48
+
49
+ { pattern: /certificatejs-(\d+\.\d+)$/, minVersion: '1.6', isFolder: true , required: true, baseDir: 'certificate',mandatoryUpdate: true }
50
+
51
+ ]; */
52
+
53
+
54
+ const ROOT_DIR = path.join(__dirname, "..", "src");
55
+
56
+ function requireOrInstall(packageName) {
57
+ try {
58
+ return require(packageName);
59
+ } catch (err) {
60
+
61
+ console.log(`📦 "${packageName}" not found. Installing automatically...`);
62
+
63
+ try {
64
+ execSync(`npm install ${packageName}`, { stdio: "inherit" });
65
+ console.log(`✅ "${packageName}" installed successfully.`);
66
+ } catch (installErr) {
67
+ console.error(`❌ Failed to install "${packageName}".`);
68
+ process.exit(1);
69
+ }
70
+
71
+ // Try loading again
72
+ return require(packageName);
73
+ }
74
+ }
75
+
76
+ const AdmZip = requireOrInstall("adm-zip");
77
+
78
+
79
+
80
+ const pkg = require(path.join(process.cwd(), 'node_modules', 'codeplay-common', 'package.json'));
81
+
82
+ const pluginName = pkg.name;
83
+ const pluginVersion = pkg.version;
84
+
85
+ let updateLogs = [];
86
+
87
+ function writeUpdateLine(message) {
88
+ updateLogs.push(message);
89
+ }
90
+
91
+ const MAX_LOG_BLOCKS = 50;
92
+
93
+ function saveUpdateLogs() {
94
+
95
+ if (updateLogs.length === 0) return;
96
+
97
+ const logDir = path.dirname(updateLogFile);
98
+
99
+ if (!fs.existsSync(logDir)) {
100
+ fs.mkdirSync(logDir, { recursive: true });
101
+ }
102
+
103
+ const now = new Date();
104
+
105
+ const formattedTime = now.toLocaleString('en-GB', {
106
+ day: '2-digit',
107
+ month: '2-digit',
108
+ year: 'numeric',
109
+ hour: '2-digit',
110
+ minute: '2-digit',
111
+ hour12: true
112
+ }).replace(',', '').replace(/\//g, '-');
113
+
114
+ let newBlock = `${pluginName}: ${pluginVersion}\n[${formattedTime}]\n`;
115
+
116
+ updateLogs.forEach(line => {
117
+ newBlock += `${line}\n`;
118
+ });
119
+
120
+ newBlock += "\n";
121
+
122
+ let existingLog = "";
123
+
124
+ if (fs.existsSync(updateLogFile)) {
125
+ existingLog = fs.readFileSync(updateLogFile, "utf8");
126
+ }
127
+
128
+ let combinedLog = newBlock + existingLog;
129
+
130
+ // Split blocks by plugin header
131
+ const blocks = combinedLog.split(/\n(?=codeplay-common:)/);
132
+
133
+ // Keep only latest 50
134
+ const trimmed = blocks.slice(0, MAX_LOG_BLOCKS).join("\n");
135
+
136
+ fs.writeFileSync(updateLogFile, trimmed);
137
+
138
+ }
139
+
140
+
141
+
142
+
143
+
144
+ const versionsFile = path.join(__dirname, "versions.json");
145
+
146
+ function loadRequiredPlugins() {
147
+
148
+ if (!fs.existsSync(versionsFile)) {
149
+ console.error("❌ versions.json not found");
150
+ process.exit(1);
151
+ }
152
+
153
+ const json = JSON.parse(fs.readFileSync(versionsFile, "utf8"));
154
+
155
+ return json.plugins.map(p => ({
156
+ ...p,
157
+ pattern: new RegExp(p.pattern)
158
+ }));
159
+
160
+ }
161
+
162
+ let requiredPlugins = loadRequiredPlugins();
163
+
164
+
165
+
166
+
167
+
168
+
169
+ async function downloadAndExtractZip(url, destFolder) {
170
+
171
+ const zipPath = destFolder + ".zip";
172
+
173
+ await downloadFile(url, zipPath);
174
+
175
+ const zip = new AdmZip(zipPath);
176
+ zip.extractAllTo(destFolder, true);
177
+
178
+ fs.unlinkSync(zipPath);
179
+
180
+ }
181
+
182
+
183
+
184
+
185
+
186
+
187
+
188
+ //Check codeplay-common latest version installed or not Start
189
+ //const { execSync } = require('child_process');
190
+
191
+
192
+ function getInstalledVersion(packageName) {
193
+ try {
194
+ const packageJsonPath = path.join(process.cwd(), 'node_modules', packageName, 'package.json');
195
+ if (fs.existsSync(packageJsonPath)) {
196
+ const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
197
+ return packageJson.version;
198
+ }
199
+ } catch (error) {
200
+ return null;
201
+ }
202
+ return null;
203
+ }
204
+
205
+ function getLatestVersion(packageName) {
206
+ try {
207
+ return execSync(`npm view ${packageName} version`).toString().trim();
208
+ } catch (error) {
209
+ console.error(`Failed to fetch latest version for ${packageName}`);
210
+ return null;
211
+ }
212
+ }
213
+
214
+ function checkPackageVersion() {
215
+ const packageName = 'codeplay-common';
216
+ const installedVersion = getInstalledVersion(packageName);
217
+ const latestVersion = getLatestVersion(packageName);
218
+
219
+ if (!installedVersion) {
220
+ console.error(`${packageName} is not installed. Please install it using "npm install ${packageName}".`);
221
+ process.exit(1);
222
+ }
223
+
224
+ if (installedVersion !== latestVersion) {
225
+ console.error(`\x1b[31m${packageName} is outdated (installed: ${installedVersion}, latest: ${latestVersion}). Please update it.\x1b[0m\n\x1b[33mUse 'npm uninstall codeplay-common ; npm i codeplay-common'\x1b[0m`);
226
+ process.exit(1);
227
+ }
228
+
229
+ console.log(`${packageName} is up to date (version ${installedVersion}).`);
230
+ }
231
+
232
+ // Run package version check before executing the main script
233
+ try {
234
+ checkPackageVersion();
235
+ } catch (error) {
236
+ console.error(error.message);
237
+ process.exit(1);
238
+ }
239
+
240
+ //Check codeplay-common latest version installed or not END
241
+
242
+
243
+
244
+ function compareWithBeta(installedVersion, minVersion, isBeta) {
245
+ const baseCompare = compareVersions(installedVersion, minVersion);
246
+
247
+ if (!isBeta) {
248
+ // Stable version → normal compare
249
+ return baseCompare;
250
+ }
251
+
252
+ // Beta version logic
253
+ if (baseCompare > 0) return 1; // 5.3-beta > 5.2
254
+ if (baseCompare < 0) return -1; // 5.1-beta < 5.2
255
+
256
+ // Same version but beta → LOWER than stable
257
+ return -1; // 5.2-beta < 5.2
258
+ }
259
+
260
+
261
+
262
+
263
+ const checkAppUniqueId=()=>{
264
+
265
+ const config = JSON.parse(fs.readFileSync(configPath, 'utf8'));
266
+
267
+ const appUniqueId = config.android?.APP_UNIQUE_ID;
268
+ const RESIZEABLE_ACTIVITY = config.android?.RESIZEABLE_ACTIVITY;
269
+ const orientation = config.android?.ORIENTATION;
270
+
271
+
272
+ let logErrorMessage="";
273
+
274
+ // 1️⃣ Check if it’s missing
275
+ if (RESIZEABLE_ACTIVITY === undefined) {
276
+ logErrorMessage+='❌ Missing android.RESIZEABLE_ACTIVITY option in capacitor.config.json.\n';
277
+ }
278
+
279
+ // 2️⃣ Check if it’s not boolean (true/false only)
280
+ else if (typeof RESIZEABLE_ACTIVITY !== 'boolean') {
281
+ logErrorMessage+='❌ Invalid android.RESIZEABLE_ACTIVITY value. Please use only true or false (without quotes).\n';
282
+ }
283
+
284
+
285
+
286
+ if (!orientation) {
287
+ logErrorMessage+='❌ Missing android.ORIENTATION option in capacitor.config.json.\n';
288
+ }
289
+
290
+ else if(orientation!="portrait" && orientation!="landscape" && orientation!="auto")
291
+ {
292
+ logErrorMessage+='❌ Spelling mistake in android.ORIENTATION option in capacitor.config.json. Please use only ["portrait" "landscape" "auto"]\n';
293
+ }
294
+
295
+
296
+ if (!appUniqueId) {
297
+ logErrorMessage+='❌ APP_UNIQUE_ID is missing in capacitor.config.json.';
298
+ }
299
+
300
+ else if (!Number.isInteger(appUniqueId)) {
301
+ logErrorMessage+='❌ APP_UNIQUE_ID must be an integer. Example: 1, 2, 3, etc.';
302
+ }
303
+
304
+
305
+
306
+ if(logErrorMessage!="")
307
+ {
308
+ console.error(logErrorMessage);
309
+ process.exit(1)
310
+ }
311
+
312
+
313
+ console.log(`✅ APP_UNIQUE_ID is valid: ${appUniqueId}`);
314
+
315
+ }
316
+
317
+ checkAppUniqueId();
318
+
319
+
320
+
321
+
322
+
323
+
324
+ // ======================================================
325
+ // 🚫 BLOCK STATIC IMPORT OF showSubscribePopup (ANY VERSION)
326
+ // ======================================================
327
+
328
+ const STATIC_SUBSCRIBE_REGEX = /import\s*{\s*showSubscribePopup\s*}\s*from\s*['"].*\/js\/Ads\/IAP-\d+(\.\d+)*\/IAP-check-And-LoadAd\.js['"]/;
329
+
330
+ // Detect dynamic import (valid)
331
+ const DYNAMIC_SUBSCRIBE_REGEX = /await\s+import\s*\(\s*['"].*\/js\/Ads\/IAP-\d+(\.\d+)*\/IAP-check-And-LoadAd\.js['"]\s*\)/;
332
+
333
+ let subscribeImportError = false;
334
+
335
+ function scanSubscribeImport(dir) {
336
+ const files = fs.readdirSync(dir);
337
+
338
+ for (const file of files) {
339
+ const fullPath = path.join(dir, file);
340
+ const stat = fs.statSync(fullPath);
341
+
342
+ if (stat.isDirectory()) {
343
+ scanSubscribeImport(fullPath);
344
+ }
345
+ else if (file.endsWith(".js") || file.endsWith(".ts") || file.endsWith(".f7")) {
346
+
347
+ const content = fs.readFileSync(fullPath, "utf-8");
348
+ const lines = content.split("\n");
349
+
350
+ lines.forEach((line, index) => {
351
+
352
+ // ❌ STATIC import → ERROR
353
+ if (STATIC_SUBSCRIBE_REGEX.test(line)) {
354
+ console.error(`
355
+ ❌ Forbidden static import detected!
356
+
357
+ File: ${fullPath}
358
+ Line: ${index + 1}
359
+ Code: ${line.trim()}
360
+
361
+ 🚫 DO NOT use static import for showSubscribePopup
362
+
363
+ 👉 Remove:
364
+ import { showSubscribePopup } from './../js/Ads/IAP-x.x/IAP-check-And-LoadAd.js'
365
+
366
+ ✅ Use dynamic import (ANY version allowed):
367
+
368
+ const { showSubscribePopup } = await import('./../js/Ads/IAP-x.x/IAP-check-And-LoadAd.js');
369
+ showSubscribePopup();
370
+ `);
371
+ subscribeImportError = true;
372
+ }
373
+
374
+ // Optional: detect wrong dynamic usage (no await)
375
+ if (
376
+ line.includes("import(") &&
377
+ line.includes("IAP-") &&
378
+ !line.includes("await")
379
+ ) {
380
+ console.warn(`
381
+ ⚠️ Warning: Dynamic import without await
382
+
383
+ File: ${fullPath}
384
+ Line: ${index + 1}
385
+ Code: ${line.trim()}
386
+
387
+ 👉 Always use:
388
+ const { showSubscribePopup } = await import(...)
389
+ `);
390
+ }
391
+
392
+ });
393
+ }
394
+ }
395
+ }
396
+
397
+
398
+ // Run scan
399
+ scanSubscribeImport(ROOT_DIR);
400
+
401
+ // Stop build
402
+ if (subscribeImportError) {
403
+ console.error("🚫 Build failed due to forbidden showSubscribePopup static import.");
404
+ process.exit(1);
405
+ } else {
406
+ console.log("✅ showSubscribePopup import usage is valid.");
407
+ }
408
+
409
+
410
+
411
+
412
+ // ======================================================
413
+ // 🚫 BLOCK STATIC IMPORT OF showSubscribePopup (ANY VERSION) END
414
+ // ======================================================
415
+
416
+
417
+
418
+
419
+
420
+
421
+
422
+
423
+
424
+
425
+
426
+ //@Codemirror check and install/uninstall the packages START
427
+ //const fs = require("fs");
428
+ //const path = require("path");
429
+ //const { execSync } = require("child_process");
430
+
431
+ const jsDir = path.join(__dirname, "..", "src", "js");
432
+
433
+ // 🔍 Detect OLD structure
434
+ const oldEditorDirs = fs.readdirSync(jsDir)
435
+ .filter(name => /^editor-\d+\.\d+$/.test(name));
436
+
437
+ // 📁 New structure path (optional, not mandatory)
438
+ const newEditorBaseDir = path.join(jsDir, "editor");
439
+
440
+ // ======================================================
441
+ // CASE 1: OLD STRUCTURE FOUND → STOP
442
+ // ======================================================
443
+ if (oldEditorDirs.length > 0) {
444
+
445
+ console.error(`
446
+ ❌ OLD EDITOR STRUCTURE DETECTED
447
+
448
+ You are using outdated folder structure:
449
+ src/js/editor-x.x/
450
+
451
+ 🚨 This is no longer supported.
452
+
453
+ 📦 Found folders:
454
+ ${oldEditorDirs.map(d => " - " + d).join("\n")}
455
+
456
+ 👉 Please move them manually:
457
+
458
+ src/js/editor-1.6
459
+
460
+ src/js/editor/editor-1.6
461
+
462
+ ⚠️ Also ensure:
463
+ src/js/editor/configuration.json exists
464
+
465
+ ❌ Build stopped.
466
+ `);
467
+
468
+ process.exit(1);
469
+ }
470
+
471
+ // ======================================================
472
+ // ✅ CASE 2: NEW STRUCTURE EXISTS → VALIDATE
473
+ // ======================================================
474
+ if (fs.existsSync(newEditorBaseDir)) {
475
+
476
+ // 🔍 Find editor-x.x inside new folder
477
+ const editorDirs = fs.readdirSync(newEditorBaseDir)
478
+ .filter(name => /^editor-\d+\.\d+$/.test(name));
479
+
480
+ // 👉 If editor folder exists but no versions → skip safely
481
+ if (editorDirs.length === 0) {
482
+ console.log("ℹ️ No editor-x.x folders found inside src/js/editor/");
483
+ return;
484
+ }
485
+
486
+ // ======================================================
487
+ // ✅ Validate configuration.json (NEW RULE)
488
+ // ======================================================
489
+
490
+ const editorConfigPath = path.join(newEditorBaseDir, "configuration.json");
491
+
492
+ // ❌ Missing config
493
+ if (!fs.existsSync(editorConfigPath)) {
494
+ console.error(`
495
+ ❌ MISSING EDITOR CONFIGURATION FILE
496
+
497
+ Required:
498
+ src/js/editor/configuration.json
499
+
500
+ Build stopped.
501
+ `);
502
+ process.exit(1);
503
+ }
504
+
505
+ // Check wrong placement
506
+ const invalidConfigs = [];
507
+
508
+ editorDirs.forEach(dir => {
509
+ const wrongPath = path.join(newEditorBaseDir, dir, "configuration.json");
510
+ if (fs.existsSync(wrongPath)) {
511
+ invalidConfigs.push(`src/js/editor/${dir}/configuration.json`);
512
+ }
513
+ });
514
+
515
+ if (invalidConfigs.length > 0) {
516
+ console.error(`
517
+ INVALID CONFIGURATION LOCATION
518
+
519
+ 🚫 configuration.json must NOT be inside version folders.
520
+
521
+ Found:
522
+ ${invalidConfigs.map(p => " - " + p).join("\n")}
523
+
524
+ ✅ Correct:
525
+ src/js/editor/configuration.json
526
+
527
+ Build stopped.
528
+ `);
529
+ process.exit(1);
530
+ }
531
+
532
+ console.log(" Editor structure validated.");
533
+
534
+ // ======================================================
535
+ // 🚀 Continue execution (run.js)
536
+ // ======================================================
537
+
538
+ const latestEditorDir = editorDirs.sort((a, b) => {
539
+ const vA = parseFloat(a.split('-')[1]);
540
+ const vB = parseFloat(b.split('-')[1]);
541
+ return vB - vA;
542
+ })[0];
543
+
544
+ const runJsPath = path.join(newEditorBaseDir, latestEditorDir, "run.js");
545
+
546
+ if (!fs.existsSync(runJsPath)) {
547
+ console.error(`❌ run.js not found in ${latestEditorDir}`);
548
+ process.exit(1);
549
+ }
550
+
551
+ console.log(`🚀 Executing ${runJsPath}...`);
552
+ execSync(`node "${runJsPath}"`, { stdio: "inherit" });
553
+ }
554
+
555
+ // ======================================================
556
+ // CASE 3: NOTHING EXISTS → DO NOTHING
557
+ // ======================================================
558
+ else {
559
+ console.log("ℹ️ Editor not used in this project. Skipping...");
560
+ }
561
+
562
+ //@Codemirror check and install/uninstall the packages END
563
+
564
+
565
+
566
+
567
+
568
+
569
+
570
+
571
+
572
+ // saveToGalleryAndSaveAnyFile-x.x-ios.js file check for android and return error if exists START
573
+
574
+ const os = require('os');
575
+
576
+ const saveToGalleryAndSaveFileCheck_iOS = () => {
577
+
578
+ // List of paths to scan
579
+ const SCAN_PATHS = [
580
+ path.resolve(__dirname, '../src/certificate'),
581
+ path.resolve(__dirname, '../src/pages'),
582
+ path.resolve(__dirname, '../src/js'),
583
+ path.resolve(__dirname, '../src/app.f7')
584
+ ];
585
+
586
+ // Directory to exclude
587
+ const EXCLUDED_DIR = path.resolve(__dirname, '../src/js/Ads');
588
+
589
+ const ANDROID_MANIFEST_PATH = path.resolve(__dirname, '../android/app/src/main/AndroidManifest.xml');
590
+
591
+
592
+ // Match iOS-specific imports (e.g., saveToGalleryAndSaveAnyFile-2.5-ios.js) not in comments
593
+ const IOS_FILE_REGEX = /^(?!\s*\/\/).*['"](?:.*\/)?saveToGalleryAndSaveAnyFile-\d+(\.\d+)*-ios\.js['"]/m;
594
+
595
+ // Match Android-specific imports (e.g., saveToGalleryAndSaveAnyFile-2.5.js) not in comments
596
+ const ANDROID_FILE_REGEX = /^(?!\s*\/\/).*['"](?:.*\/)?saveToGalleryAndSaveAnyFile-\d+(\.\d+)*\.js['"]/m;
597
+
598
+
599
+
600
+
601
+
602
+ const ALLOWED_EXTENSIONS = ['.js', '.f7'];
603
+ const isMac = os.platform() === 'darwin';
604
+
605
+ let iosImportFound = false;
606
+ let androidImportFound = false;
607
+
608
+ // Files to skip completely (full or partial match)
609
+ const SKIP_FILES = [
610
+ 'pdf-3.11.174.min.js',
611
+ 'pdf.worker-3.11.174.min.js'
612
+ ,'index.browser.js'
613
+ ];
614
+
615
+
616
+ function scanDirectory(dir) {
617
+
618
+ /*
619
+ //######################### DO NOT DELETE THIS - START [Appid base validation] #####################################
620
+ const config = JSON.parse(fs.readFileSync(configPath, 'utf8'));
621
+ const appUniqueId = config.android?.APP_UNIQUE_ID;
622
+ if (appUniqueId == "206") return;
623
+ //######################### DO NOT DELETE THIS - END [Appid base validation] #####################################
624
+ */
625
+
626
+ const stat = fs.statSync(dir);
627
+
628
+ if (stat.isFile()) {
629
+
630
+ // 🔥 Skip files in SKIP_FILES array
631
+ const baseName = path.basename(dir);
632
+ if (SKIP_FILES.includes(baseName)) {
633
+ // Just skip silently
634
+ return;
635
+ }
636
+
637
+ // Only scan allowed file extensions
638
+ if (ALLOWED_EXTENSIONS.some(ext => dir.endsWith(ext))) {
639
+ process.stdout.write(`\r🔍 Scanning: ${dir} `);
640
+
641
+ const content = fs.readFileSync(dir, 'utf8');
642
+
643
+ if (IOS_FILE_REGEX.test(content)) {
644
+ iosImportFound = true;
645
+ if (!isMac) {
646
+ console.error(`\n❌ ERROR: iOS-specific import detected in: ${dir}`);
647
+ console.error(`🚫 STOPPED: This file should not be imported in Android/Windows/Linux builds.\n`);
648
+ process.exit(1);
649
+ }
650
+ }
651
+ else if (ANDROID_FILE_REGEX.test(content) && !content.includes('-ios.js')) {
652
+ androidImportFound = true;
653
+ }
654
+ }
655
+ }
656
+ else if (stat.isDirectory()) {
657
+ if (dir === EXCLUDED_DIR || path.basename(dir) === 'node_modules') return;
658
+
659
+ const entries = fs.readdirSync(dir, { withFileTypes: true });
660
+ for (let entry of entries) {
661
+ scanDirectory(path.join(dir, entry.name));
662
+ }
663
+ }
664
+ }
665
+
666
+
667
+ // Run scan on all specified paths
668
+ for (let scanPath of SCAN_PATHS) {
669
+ if (fs.existsSync(scanPath)) {
670
+ scanDirectory(scanPath);
671
+ }
672
+ }
673
+
674
+
675
+
676
+ /* // Check src folder
677
+ if (!fs.existsSync(ROOT_DIR)) {
678
+ console.warn(`⚠️ Warning: 'src' directory not found at: ${ROOT_DIR}`);
679
+ return;
680
+ } */
681
+
682
+ //scanDirectory(ROOT_DIR);
683
+
684
+ // iOS Checks
685
+ if (isMac && !iosImportFound) {
686
+ console.warn(`⚠️ WARNING: You're on macOS but no iOS version (saveToGalleryAndSaveAnyFile-x.x-ios.js) found.`);
687
+ process.exit(1);
688
+ } else if (isMac && iosImportFound) {
689
+ console.log('✅ iOS version detected for macOS build.');
690
+ } else if (!iosImportFound) {
691
+ console.log('✅ No iOS-specific imports detected for non-macOS.');
692
+ }
693
+
694
+ // Android Checks
695
+ if (androidImportFound) {
696
+ console.log("📱 Android version of saveToGalleryAndSaveAnyFile detected. Checking AndroidManifest.xml...");
697
+
698
+ if (!fs.existsSync(ANDROID_MANIFEST_PATH)) {
699
+ console.error(" AndroidManifest.xml not found. Cannot add requestLegacyExternalStorage attribute.");
700
+ return;
701
+ }
702
+
703
+ let manifestContent = fs.readFileSync(ANDROID_MANIFEST_PATH, 'utf8');
704
+
705
+ if (!manifestContent.includes('android:requestLegacyExternalStorage="true"')) {
706
+ console.log("Adding android:requestLegacyExternalStorage=\"true\" to <application> tag...");
707
+
708
+ manifestContent = manifestContent.replace(
709
+ /<application([^>]*)>/,
710
+ (match, attrs) => {
711
+ if (attrs.includes('android:requestLegacyExternalStorage')) return match;
712
+ return `<application${attrs} android:requestLegacyExternalStorage="true">`;
713
+ }
714
+ );
715
+
716
+ fs.writeFileSync(ANDROID_MANIFEST_PATH, manifestContent, 'utf8');
717
+ console.log("✅ android:requestLegacyExternalStorage=\"true\" added successfully.");
718
+ } else {
719
+ console.log("ℹ️ android:requestLegacyExternalStorage already exists in AndroidManifest.xml.");
720
+ }
721
+ } else {
722
+ console.log("✅ No Android saveToGalleryAndSaveAnyFile imports detected.");
723
+ }
724
+ };
725
+
726
+ saveToGalleryAndSaveFileCheck_iOS();
727
+ // saveToGalleryAndSaveAnyFile-x.x-ios.js file check for android and return error if exists END
728
+
729
+
730
+
731
+
732
+
733
+
734
+
735
+
736
+
737
+
738
+
739
+
740
+
741
+ /*
742
+ // Clean up AppleDouble files (._*) created by macOS START
743
+ if (process.platform === 'darwin') {
744
+ try {
745
+ console.log('🧹 Cleaning up AppleDouble files (._*)...');
746
+ execSync(`find . -name '._*' -delete`);
747
+ console.log('✅ AppleDouble files removed.');
748
+ } catch (err) {
749
+ console.warn('⚠️ Failed to remove AppleDouble files:', err.message);
750
+ }
751
+ } else {
752
+ console.log('ℹ️ Skipping AppleDouble cleanup — not a macOS machine.');
753
+ }
754
+
755
+ // Clean up AppleDouble files (._*) created by macOS END
756
+ */
757
+
758
+
759
+
760
+
761
+
762
+
763
+ //In routes.js file check static import START
764
+
765
+ const routesPath = path.join(process.cwd(), 'src', 'js', 'routes.js');
766
+ const routesContent = fs.readFileSync(routesPath, 'utf-8');
767
+
768
+ let inBlockComment = false;
769
+ const lines = routesContent.split('\n');
770
+
771
+ const allowedImport = `import HomePage from '../pages/home.f7';`;
772
+ const badImportRegex = /^[ \t]*import\s+[\w{}*,\s]*\s+from\s+['"].+\.f7['"]\s*;/;
773
+ const badImports = [];
774
+
775
+ lines.forEach((line, index) => {
776
+ const trimmed = line.trim();
777
+
778
+ // Handle block comment start and end
779
+ if (trimmed.startsWith('/*')) inBlockComment = true;
780
+ if (inBlockComment && trimmed.endsWith('*/')) {
781
+ inBlockComment = false;
782
+ return;
783
+ }
784
+
785
+ // Skip if inside block comment or line comment
786
+ if (inBlockComment || trimmed.startsWith('//')) return;
787
+
788
+ // Match static .f7 import
789
+ if (badImportRegex.test(trimmed) && trimmed !== allowedImport) {
790
+ badImports.push({ line: trimmed, number: index + 1 });
791
+ }
792
+ });
793
+
794
+ if (badImports.length > 0) {
795
+ console.error('\n❌ ERROR: Detected disallowed static imports of .f7 files in routes.js\n');
796
+ console.error(`⚠️ Only this static import is allowed:\n ${allowedImport}\n`);
797
+ console.error(`🔧 Please convert other imports to async dynamic imports like this:\n`);
798
+ console.error(`
799
+
800
+ import HomePage from '../pages/home.f7';
801
+
802
+ const routes = [
803
+ {
804
+ path: '/',
805
+ component:HomePage,
806
+ },
807
+ {
808
+ path: '/ProfilePage/',
809
+ async async({ resolve }) {
810
+ const page = await import('../pages/profile.f7');
811
+ resolve({ component: page.default });
812
+ },
813
+ }]
814
+ `);
815
+
816
+ badImports.forEach(({ line, number }) => {
817
+ console.error(`${number}: ${line}`);
818
+ });
819
+
820
+ process.exit(1);
821
+ } else {
822
+ console.log('✅ routes.js passed the .f7 import check.');
823
+ }
824
+
825
+ //In routes.js file check static import END
826
+
827
+
828
+
829
+
830
+
831
+
832
+
833
+
834
+
835
+
836
+
837
+
838
+
839
+ // Check and change the "BridgeWebViewClient.java" file START
840
+ /*
841
+ For crash issue due to low memory problem, we need to modify the onRenderProcessGone method in BridgeWebViewClient.java.
842
+ */
843
+
844
+
845
+ const bridgeWebViewClientFilePath = path.join(process.cwd(), 'node_modules', '@capacitor/android/capacitor/src/main/java/com/getcapacitor', 'BridgeWebViewClient.java');
846
+
847
+ // Read the file
848
+ if (!fs.existsSync(bridgeWebViewClientFilePath)) {
849
+ console.error('❌ Error: BridgeWebViewClient.java not found.');
850
+ process.exit(1);
851
+ }
852
+
853
+ let fileContent = fs.readFileSync(bridgeWebViewClientFilePath, 'utf8');
854
+
855
+ // Define old and new code
856
+ const oldCodeStart = `@Override
857
+ public boolean onRenderProcessGone(WebView view, RenderProcessGoneDetail detail) {
858
+ super.onRenderProcessGone(view, detail);
859
+ boolean result = false;
860
+
861
+ List<WebViewListener> webViewListeners = bridge.getWebViewListeners();
862
+ if (webViewListeners != null) {
863
+ for (WebViewListener listener : bridge.getWebViewListeners()) {
864
+ result = listener.onRenderProcessGone(view, detail) || result;
865
+ }
866
+ }
867
+
868
+ return result;
869
+ }`;
870
+
871
+ const newCode = `@Override
872
+ public boolean onRenderProcessGone(WebView view, RenderProcessGoneDetail detail) {
873
+ super.onRenderProcessGone(view, detail);
874
+
875
+ boolean result = false;
876
+
877
+ List<WebViewListener> webViewListeners = bridge.getWebViewListeners();
878
+ if (webViewListeners != null) {
879
+ for (WebViewListener listener : bridge.getWebViewListeners()) {
880
+ result = listener.onRenderProcessGone(view, detail) || result;
881
+ }
882
+ }
883
+
884
+ if (!result) {
885
+ // If no one handled it, handle it ourselves!
886
+
887
+ /*if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
888
+ if (detail.didCrash()) {
889
+ //Log.e("CapacitorWebView", "WebView crashed internally!");
890
+ } else {
891
+ //Log.w("CapacitorWebView", "WebView was killed by system (low memory) internally!");
892
+ }
893
+ }*/
894
+
895
+ view.post(() -> {
896
+ Toast.makeText(view.getContext(), "Reloading due to low memory issue", Toast.LENGTH_SHORT).show();
897
+ });
898
+
899
+ view.reload(); // Safely reload WebView
900
+
901
+ return true; // We handled it
902
+ }
903
+
904
+ return result;
905
+ }`;
906
+
907
+ // Step 1: Update method if needed
908
+ let updated = false;
909
+
910
+ if (fileContent.includes(oldCodeStart)) {
911
+ console.log('✅ Found old onRenderProcessGone method. Replacing it...');
912
+ fileContent = fileContent.replace(oldCodeStart, newCode);
913
+ updated = true;
914
+ } else if (fileContent.includes(newCode)) {
915
+ console.log('ℹ️ Method already updated. No changes needed in "BridgeWebViewClient.java".');
916
+ } else {
917
+ console.error('❌ Error: Neither old nor new code found. Unexpected content.');
918
+ process.exit(1);
919
+ }
920
+
921
+ // Step 2: Check and add import if missing
922
+ const importToast = 'import android.widget.Toast;';
923
+ if (!fileContent.includes(importToast)) {
924
+ console.log('✅ Adding missing import for Toast...');
925
+ const importRegex = /import\s+[^;]+;/g;
926
+ const matches = [...fileContent.matchAll(importRegex)];
927
+
928
+ if (matches.length > 0) {
929
+ const lastImport = matches[matches.length - 1];
930
+ const insertPosition = lastImport.index + lastImport[0].length;
931
+ fileContent = fileContent.slice(0, insertPosition) + `\n${importToast}` + fileContent.slice(insertPosition);
932
+ updated = true;
933
+ } else {
934
+ console.error('❌ Error: No import section found in file.');
935
+ process.exit(1);
936
+ }
937
+ } else {
938
+ console.log('ℹ️ Import for Toast already exists. No changes needed.');
939
+ }
940
+
941
+ // Step 3: Save if updated
942
+ if (updated) {
943
+ fs.writeFileSync(bridgeWebViewClientFilePath, fileContent, 'utf8');
944
+ console.log('✅ File updated successfully.');
945
+ } else {
946
+ console.log('ℹ️ No changes needed.');
947
+ }
948
+
949
+
950
+
951
+
952
+ // Check and change the "BridgeWebViewClient.java" file END
953
+
954
+
955
+
956
+
957
+
958
+
959
+
960
+
961
+ /*
962
+ // To resolve the kotlin version issue, we need to update the kotlin version in the build.gradle file START
963
+
964
+ // Build the path dynamically like you requested
965
+ const gradlePath = path.join(
966
+ process.cwd(),
967
+ 'android',
968
+ 'build.gradle'
969
+ );
970
+
971
+ // Read the existing build.gradle
972
+ let gradleContent = fs.readFileSync(gradlePath, 'utf8');
973
+
974
+ // Add `ext.kotlin_version` if it's not already there
975
+ if (!gradleContent.includes('ext.kotlin_version')) {
976
+ gradleContent = gradleContent.replace(
977
+ /buildscript\s*{/,
978
+ `buildscript {\n ext.kotlin_version = '2.1.0'`
979
+ );
980
+ }
981
+
982
+ // Add Kotlin classpath if it's not already there
983
+ if (!gradleContent.includes('org.jetbrains.kotlin:kotlin-gradle-plugin')) {
984
+ gradleContent = gradleContent.replace(
985
+ /dependencies\s*{([\s\S]*?)classpath 'com.android.tools.build:gradle:8.7.2'/,
986
+ `dependencies {\n classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version")\n$1classpath 'com.android.tools.build:gradle:8.7.2'`
987
+ );
988
+ }
989
+
990
+ // Write back the modified content
991
+ fs.writeFileSync(gradlePath, gradleContent, 'utf8');
992
+
993
+ console.log('✅ Kotlin version updated in build.gradle.');
994
+
995
+ // To resolve the kotlin version issue, we need to update the kotlin version in the build.gradle file END
996
+ */
997
+
998
+
999
+
1000
+
1001
+
1002
+
1003
+
1004
+
1005
+ let _admobConfig;
1006
+
1007
+
1008
+
1009
+ const androidPlatformPath = path.join(process.cwd(), 'android');
1010
+ const iosPlatformPath = path.join(process.cwd(), 'ios');
1011
+ const pluginPath = path.join(process.cwd(), 'node_modules', 'emi-indo-cordova-plugin-admob', 'plugin.xml');
1012
+ const infoPlistPath = path.join(process.cwd(), 'ios', 'App', 'App', 'Info.plist');
1013
+ const resourcesPath = path.join(process.cwd(), 'resources', 'res');
1014
+ const androidResPath = path.join(process.cwd(), 'android', 'app', 'src', 'main', 'res');
1015
+ const localNotificationsPluginPath = path.join(process.cwd(), 'node_modules', '@capacitor', 'local-notifications');
1016
+
1017
+ function fileExists(filePath) {
1018
+ return fs.existsSync(filePath);
1019
+ }
1020
+
1021
+ function copyFolderSync(source, target) {
1022
+ if (!fs.existsSync(target)) {
1023
+ fs.mkdirSync(target, { recursive: true });
1024
+ }
1025
+
1026
+ fs.readdirSync(source).forEach(file => {
1027
+ const sourceFile = path.join(source, file);
1028
+ const targetFile = path.join(target, file);
1029
+
1030
+ if (fs.lstatSync(sourceFile).isDirectory()) {
1031
+ copyFolderSync(sourceFile, targetFile);
1032
+ } else {
1033
+ fs.copyFileSync(sourceFile, targetFile);
1034
+ }
1035
+ });
1036
+ }
1037
+
1038
+ function checkAndCopyResources() {
1039
+ if (fileExists(resourcesPath)) {
1040
+ copyFolderSync(resourcesPath, androidResPath);
1041
+ console.log('✅ Successfully copied resources/res to android/app/src/main/res.');
1042
+ } else {
1043
+ console.log('resources/res folder not found.');
1044
+
1045
+ if (fileExists(localNotificationsPluginPath)) {
1046
+ throw new Error('❌ resources/res is required for @capacitor/local-notifications. Stopping execution.');
1047
+ }
1048
+ }
1049
+ }
1050
+
1051
+
1052
+
1053
+
1054
+
1055
+
1056
+
1057
+
1058
+ function getAdMobConfig() {
1059
+ if (!fileExists(configPath)) {
1060
+ throw new Error('❌ capacitor.config.json not found. Ensure this is a Capacitor project.');
1061
+ }
1062
+
1063
+ const config = JSON.parse(fs.readFileSync(configPath, 'utf8'));
1064
+ const admobConfig = config.plugins?.AdMob;
1065
+
1066
+ if (!admobConfig) {
1067
+ throw new Error('❌ AdMob configuration is missing in capacitor.config.json.');
1068
+ }
1069
+
1070
+ // Default to true if ADMOB_ENABLED is not specified
1071
+ const isEnabled = admobConfig.ADMOB_ENABLED !== false;
1072
+
1073
+ if (!isEnabled) {
1074
+ return { ADMOB_ENABLED: false }; // Skip further validation
1075
+ }
1076
+
1077
+ if (!admobConfig.APP_ID_ANDROID || !admobConfig.APP_ID_IOS) {
1078
+ throw new Error(' ❌ AdMob configuration is incomplete. Ensure APP_ID_ANDROID and APP_ID_IOS are defined.');
1079
+ }
1080
+
1081
+ return {
1082
+ ADMOB_ENABLED: true,
1083
+ APP_ID_ANDROID: admobConfig.APP_ID_ANDROID,
1084
+ APP_ID_IOS: admobConfig.APP_ID_IOS,
1085
+ USE_LITE_ADS: admobConfig.USE_LITE_ADS === "lite",
1086
+ };
1087
+ }
1088
+
1089
+ function validateAndroidBuildOptions() {
1090
+
1091
+
1092
+ if (!fileExists(configPath)) {
1093
+ console.log('❌ capacitor.config.json not found. Ensure this is a Capacitor project.');
1094
+ process.exit(1);
1095
+ }
1096
+
1097
+ const config = JSON.parse(fs.readFileSync(configPath, 'utf8'));
1098
+
1099
+ const targetAppId=config.appId
1100
+
1101
+ const buildOptions = config.android?.buildOptions;
1102
+
1103
+ if (!buildOptions) {
1104
+ console.log('❌ Missing android.buildOptions in capacitor.config.json.');
1105
+ process.exit(1);
1106
+ }
1107
+
1108
+ const requiredProps = [
1109
+ 'keystorePath',
1110
+ 'keystorePassword',
1111
+ 'keystoreAlias',
1112
+ 'keystoreAliasPassword',
1113
+ 'releaseType',
1114
+ 'signingType'
1115
+ ];
1116
+
1117
+ const missing = requiredProps.filter(prop => !buildOptions[prop]);
1118
+
1119
+ if (missing.length > 0) {
1120
+ console.log('❌ Missing properties android.buildOptions in capacitor.config.json.');
1121
+ process.exit(1);
1122
+ }
1123
+
1124
+
1125
+ const keystorePath=buildOptions.keystorePath
1126
+ const keyFileName = path.basename(keystorePath);
1127
+
1128
+
1129
+
1130
+ const keystoreMap = {
1131
+ "gameskey.jks": [
1132
+ "com.cube.blaster",
1133
+ ],
1134
+ "htmleditorkeystoke.jks": [
1135
+ "com.HTML.AngularJS.Codeplay",
1136
+ "com.html.codeplay.pro",
1137
+ "com.bootstrap.code.play",
1138
+ "com.kids.learning.master",
1139
+ "com.Simple.Barcode.Scanner",
1140
+ "com.FAShtmlcssjs.editor"
1141
+ ]
1142
+ };
1143
+
1144
+ // find which keystore is required for the given targetAppId
1145
+ let requiredKey = "newappskey.jks"; // default
1146
+ for (const [keyFile, appIds] of Object.entries(keystoreMap)) {
1147
+ if (appIds.includes(targetAppId)) {
1148
+ requiredKey = keyFile;
1149
+ break;
1150
+ }
1151
+ }
1152
+
1153
+ // validate
1154
+ if (keyFileName !== requiredKey) {
1155
+ console.log(`❌ The keystore path is mismatched. Expected ${requiredKey} for ${targetAppId}, but got ${keyFileName}`);
1156
+ process.exit(1);
1157
+ }
1158
+
1159
+
1160
+
1161
+
1162
+
1163
+ // optionally return them
1164
+ //return buildOptions;
1165
+ }
1166
+
1167
+ function updatePluginXml(admobConfig) {
1168
+ if (!fileExists(pluginPath)) {
1169
+ console.error(' ❌ plugin.xml not found. Ensure the plugin is installed.');
1170
+ return;
1171
+ }
1172
+
1173
+ let pluginContent = fs.readFileSync(pluginPath, 'utf8');
1174
+
1175
+ pluginContent = pluginContent
1176
+ .replace(/<preference name="APP_ID_ANDROID" default=".*?" \/>/, `<preference name="APP_ID_ANDROID" default="${admobConfig.APP_ID_ANDROID}" />`)
1177
+ .replace(/<preference name="APP_ID_IOS" default=".*?" \/>/, `<preference name="APP_ID_IOS" default="${admobConfig.APP_ID_IOS}" />`);
1178
+
1179
+ fs.writeFileSync(pluginPath, pluginContent, 'utf8');
1180
+ console.log('AdMob IDs successfully updated in plugin.xml');
1181
+ }
1182
+
1183
+ function updateInfoPlist(admobConfig) {
1184
+ if (!fileExists(infoPlistPath)) {
1185
+ console.error(' ❌ Info.plist not found. Ensure you have built the iOS project.');
1186
+ return;
1187
+ }
1188
+
1189
+ const plistContent = fs.readFileSync(infoPlistPath, 'utf8');
1190
+ const plistData = plist.parse(plistContent);
1191
+
1192
+ plistData.GADApplicationIdentifier = admobConfig.APP_ID_IOS;
1193
+ plistData.NSUserTrackingUsageDescription = 'This identifier will be used to deliver personalized ads to you.';
1194
+ plistData.GADDelayAppMeasurementInit = true;
1195
+
1196
+ const updatedPlistContent = plist.build(plistData);
1197
+ fs.writeFileSync(infoPlistPath, updatedPlistContent, 'utf8');
1198
+ console.log('AdMob IDs and additional configurations successfully updated in Info.plist');
1199
+ }
1200
+
1201
+
1202
+ try {
1203
+ if (!fileExists(configPath)) {
1204
+ throw new Error(' ❌ capacitor.config.json not found. Skipping setup.');
1205
+ }
1206
+
1207
+ if (!fileExists(androidPlatformPath) && !fileExists(iosPlatformPath)) {
1208
+ throw new Error('Neither Android nor iOS platforms are found. Ensure platforms are added to your Capacitor project.');
1209
+ }
1210
+
1211
+ checkAndCopyResources();
1212
+
1213
+
1214
+
1215
+ _admobConfig = getAdMobConfig();
1216
+
1217
+
1218
+
1219
+
1220
+
1221
+ // Proceed only if ADMOB_ENABLED is true
1222
+ if (_admobConfig.ADMOB_ENABLED) {
1223
+ if (fileExists(androidPlatformPath)) {
1224
+ updatePluginXml(_admobConfig);
1225
+ }
1226
+
1227
+ if (fileExists(iosPlatformPath)) {
1228
+ updateInfoPlist(_admobConfig);
1229
+ }
1230
+ }
1231
+
1232
+
1233
+ } catch (error) {
1234
+ console.error(error.message);
1235
+ process.exit(1); // Stop execution if there's a critical error
1236
+ }
1237
+
1238
+
1239
+
1240
+ validateAndroidBuildOptions();
1241
+
1242
+
1243
+
1244
+
1245
+
1246
+
1247
+ // Check all the codeplays plugins version START
1248
+
1249
+
1250
+ const readline = require('readline');
1251
+
1252
+
1253
+ //const srcDir = path.join(__dirname, 'src');
1254
+ const srcDir = path.join(process.cwd(), 'src');
1255
+ let outdatedPlugins = [];
1256
+
1257
+ function parseVersion(ver) {
1258
+ return ver.split('.').map(n => parseInt(n, 10));
1259
+ }
1260
+
1261
+ function compareVersions(v1, v2) {
1262
+ const [a1, b1] = parseVersion(v1);
1263
+ const [a2, b2] = parseVersion(v2);
1264
+ if (a1 !== a2) return a1 - a2;
1265
+ return b1 - b2;
1266
+ }
1267
+
1268
+ function walkSync(dir, filelist = []) {
1269
+ fs.readdirSync(dir).forEach(file => {
1270
+ const fullPath = path.join(dir, file);
1271
+ const stat = fs.statSync(fullPath);
1272
+ if (stat.isDirectory()) {
1273
+ walkSync(fullPath, filelist);
1274
+ } else {
1275
+ filelist.push(fullPath);
1276
+ }
1277
+ });
1278
+ return filelist;
1279
+ }
1280
+
1281
+
1282
+
1283
+ function getSearchRoot(plugin) {
1284
+ return path.join(srcDir, plugin.baseDir || 'js');
1285
+ }
1286
+
1287
+
1288
+
1289
+
1290
+
1291
+
1292
+
1293
+
1294
+
1295
+
1296
+
1297
+
1298
+
1299
+
1300
+
1301
+
1302
+
1303
+
1304
+
1305
+
1306
+ /*############################################## AUTO DOWNLOAD FROM SERVER START #####################################*/
1307
+
1308
+ // ============================================================
1309
+ // 🔥 AUTO PLUGIN UPDATE SYSTEM (MANDATORY UPDATES)
1310
+ // ============================================================
1311
+
1312
+ const https = require("https");
1313
+
1314
+ /**
1315
+ * Check if file exists on server using HEAD request
1316
+ */
1317
+ function urlExists(url) {
1318
+ return new Promise(resolve => {
1319
+ const req = https.request(url, { method: "HEAD" }, res => {
1320
+ resolve(res.statusCode === 200);
1321
+ });
1322
+
1323
+ req.on("error", () => resolve(false));
1324
+ req.end();
1325
+ });
1326
+ }
1327
+
1328
+ /**
1329
+ * Download file from server
1330
+ */
1331
+ function downloadFile(url, dest) {
1332
+ return new Promise((resolve, reject) => {
1333
+ const file = fs.createWriteStream(dest);
1334
+
1335
+ https.get(url, response => {
1336
+ if (response.statusCode !== 200) {
1337
+ reject("Download failed");
1338
+ return;
1339
+ }
1340
+
1341
+ response.pipe(file);
1342
+
1343
+ file.on("finish", () => {
1344
+ file.close(resolve);
1345
+ });
1346
+ }).on("error", err => {
1347
+ fs.unlink(dest, () => {});
1348
+ reject(err);
1349
+ });
1350
+ });
1351
+ }
1352
+
1353
+ /**
1354
+ * Update imports across src folder
1355
+ * Replaces old filename → new filename
1356
+ */
1357
+ /**
1358
+ * Update imports across project
1359
+ * Replaces old filename → new filename
1360
+ */
1361
+
1362
+ const VITE_ALIAS_ONLY = [
1363
+ "common",
1364
+ "admob-emi",
1365
+ "localization",
1366
+ "theme",
1367
+ "certificatejs",
1368
+ "ffmpeg"
1369
+ ];
1370
+
1371
+ function updateImports(oldName, newName) {
1372
+
1373
+ const projectRoot = process.cwd();
1374
+
1375
+ const filesToScan = [
1376
+ path.join(projectRoot, "vite.config.js"),
1377
+ path.join(projectRoot, "vite.config.mjs")
1378
+ ];
1379
+
1380
+ const srcDir = path.join(projectRoot, "src");
1381
+
1382
+ // scan vite config
1383
+ filesToScan.forEach(file => {
1384
+
1385
+ if (!fs.existsSync(file)) return;
1386
+
1387
+ let content = fs.readFileSync(file, "utf8");
1388
+
1389
+ if (content.includes(oldName)) {
1390
+
1391
+ content = content.split(oldName).join(newName);
1392
+
1393
+ fs.writeFileSync(file, content);
1394
+
1395
+ console.log(`✏️ Updated alias in ${path.basename(file)}`);
1396
+ }
1397
+
1398
+ });
1399
+
1400
+ // scan src files
1401
+ function walk(dir) {
1402
+
1403
+ fs.readdirSync(dir).forEach(file => {
1404
+
1405
+ if (["node_modules","android","ios","dist",".git"].includes(file))
1406
+ return;
1407
+
1408
+ const full = path.join(dir, file);
1409
+ const stat = fs.statSync(full);
1410
+
1411
+ if (stat.isDirectory()) {
1412
+ walk(full);
1413
+ }
1414
+
1415
+ else if (
1416
+ (full.endsWith(".js") ||
1417
+ full.endsWith(".f7") ||
1418
+ full.endsWith(".mjs")) &&
1419
+ !full.endsWith(".min.js")
1420
+ ) {
1421
+
1422
+ let content = fs.readFileSync(full, "utf8");
1423
+
1424
+ if (content.includes(oldName)) {
1425
+
1426
+ content = content.split(oldName).join(newName);
1427
+
1428
+ fs.writeFileSync(full, content);
1429
+
1430
+ console.log(`✏️ Updated import in ${path.relative(projectRoot, full)}`);
1431
+ }
1432
+
1433
+ }
1434
+
1435
+ });
1436
+
1437
+ }
1438
+
1439
+ if (fs.existsSync(srcDir)) {
1440
+ walk(srcDir);
1441
+ }
1442
+
1443
+ }
1444
+
1445
+
1446
+ /**
1447
+ * Auto-update a plugin file
1448
+ * Returns TRUE if success
1449
+ * Returns FALSE if fallback to manual needed
1450
+ */
1451
+
1452
+
1453
+ let _serverVersions = null;
1454
+ async function fetchVersions() {
1455
+
1456
+ if (_serverVersions) return _serverVersions;
1457
+
1458
+ return new Promise((resolve) => {
1459
+
1460
+ https.get(
1461
+ "https://htmlcodeplay.com/code-play-plugin/versions.json",
1462
+ { timeout: 5000 },
1463
+ res => {
1464
+
1465
+ let data = "";
1466
+
1467
+ res.on("data", chunk => data += chunk);
1468
+
1469
+ res.on("end", () => {
1470
+
1471
+ try {
1472
+
1473
+ _serverVersions = JSON.parse(data);
1474
+
1475
+ resolve(_serverVersions);
1476
+
1477
+ } catch {
1478
+
1479
+ resolve(null);
1480
+
1481
+ }
1482
+
1483
+ });
1484
+
1485
+ }
1486
+
1487
+ ).on("error", () => resolve(null));
1488
+
1489
+ });
1490
+
1491
+ }
1492
+
1493
+
1494
+
1495
+ async function autoUpdatePlugin(pluginDef, pluginInfo) {
1496
+
1497
+ const versions = await fetchVersions();
1498
+
1499
+ if (!versions) {
1500
+ console.log("⚠️ versions.json not reachable");
1501
+ return false;
1502
+ }
1503
+
1504
+ const oldFullPath = path.join(srcDir, pluginInfo.name);
1505
+ const oldFileName = path.basename(oldFullPath);
1506
+
1507
+ //const oldFileName = path.basename(oldFullPath);
1508
+ const oldVersionFile = oldFileName;
1509
+
1510
+
1511
+ const ext = path.extname(oldFileName); // .js or .less
1512
+ //const baseName = oldFileName.replace(/-\d+\.\d+.*$/, "");
1513
+ const baseName = oldFileName.replace(/-\d+\.\d+.*$/, '').replace(/\.(js|less)$/, '');
1514
+
1515
+ // version lookup key
1516
+ let pluginKey = baseName;
1517
+
1518
+
1519
+ // Only common plugin has js and less variants
1520
+ if (baseName === "common") {
1521
+ if (ext === ".js") pluginKey = "common-js";
1522
+ if (ext === ".less") pluginKey = "common-less";
1523
+ }
1524
+
1525
+ const latestVersion = versions[pluginKey];
1526
+
1527
+ if (!latestVersion) {
1528
+ console.log(`❌ No version entry for ${baseName}`);
1529
+ return false;
1530
+ }
1531
+
1532
+ // ===============================
1533
+ // FOLDER PLUGIN UPDATE
1534
+ // ===============================
1535
+ if (pluginDef.isFolder) {
1536
+
1537
+ const zipName = `${baseName}-${latestVersion}.zip`;
1538
+ const url = `https://htmlcodeplay.com/code-play-plugin/${zipName}`;
1539
+
1540
+ const destRoot = path.join(srcDir, pluginDef.destDir || pluginDef.baseDir || '');
1541
+ const oldPath = path.join(destRoot, pluginInfo.name);
1542
+ const newPath = path.join(destRoot, `${baseName}-${latestVersion}`);
1543
+
1544
+ if (!(await urlExists(url))) return false;
1545
+
1546
+ fs.rmSync(oldPath, { recursive: true, force: true });
1547
+
1548
+ await downloadAndExtractZip(url, newPath);
1549
+
1550
+ updateImports(pluginInfo.name, `${baseName}-${latestVersion}`);
1551
+
1552
+ // ADD THIS
1553
+ writeUpdateLine(`${pluginInfo.name} -> ${baseName}-${latestVersion}`);
1554
+
1555
+ console.log(`✅ Folder updated → ${baseName}-${latestVersion}`);
1556
+
1557
+ return true;
1558
+ }
1559
+ // ===============================
1560
+ // FILE PLUGIN UPDATE
1561
+ // ===============================
1562
+
1563
+ const pluginDir = path.dirname(oldFullPath);
1564
+
1565
+ // Only this plugin has ios variant
1566
+ const IOS_VARIANT_PLUGINS = [
1567
+ "saveToGalleryAndSaveAnyFile"
1568
+ ];
1569
+
1570
+ /* let variants = [
1571
+ `${baseName}-${latestVersion}.js`
1572
+ ]; */
1573
+
1574
+ //const ext = path.extname(oldFileName);
1575
+ const variants = [`${baseName.replace(ext,'')}-${latestVersion}${ext}`];
1576
+
1577
+
1578
+ if (IOS_VARIANT_PLUGINS.includes(baseName)) {
1579
+ variants.push(`${baseName}-${latestVersion}-ios.js`);
1580
+ }
1581
+
1582
+ let downloaded = [];
1583
+
1584
+ // Download files
1585
+ for (const fileName of variants) {
1586
+
1587
+ const url = `https://htmlcodeplay.com/code-play-plugin/${fileName}`;
1588
+
1589
+ console.log(`🔍 Checking latest: ${fileName}`);
1590
+
1591
+ if (await urlExists(url)) {
1592
+
1593
+ const destPath = path.join(pluginDir, fileName);
1594
+
1595
+ await downloadFile(url, destPath);
1596
+
1597
+ downloaded.push(fileName);
1598
+
1599
+ console.log(`⬇ Downloaded → ${fileName}`);
1600
+
1601
+ //writeUpdateLine(`Downloaded: ${fileName}`);
1602
+
1603
+ }
1604
+ }
1605
+
1606
+ if (downloaded.length === 0) {
1607
+ console.log(`❌ No files downloaded for ${baseName}`);
1608
+ return false;
1609
+ }
1610
+
1611
+ // Remove ONLY versioned files (safe)
1612
+ //const versionPattern = new RegExp(`^${baseName}-\\d+\\.\\d+(-ios)?\\.js$`);
1613
+ const versionPattern = new RegExp(`^${baseName}-\\d+\\.\\d+(-ios)?\\${ext}$`);
1614
+
1615
+ const existingFiles = fs.readdirSync(pluginDir);
1616
+
1617
+ existingFiles.forEach(file => {
1618
+
1619
+ if (
1620
+ versionPattern.test(file) &&
1621
+ !downloaded.includes(file)
1622
+ ) {
1623
+
1624
+ const oldPath = path.join(pluginDir, file);
1625
+
1626
+ fs.unlinkSync(oldPath);
1627
+
1628
+ console.log(`🗑 Removed old file → ${file}`);
1629
+ //writeUpdateLine(`Removed old file: ${file}`);
1630
+ }
1631
+
1632
+ });
1633
+
1634
+ //const newFileName = `${baseName}-${latestVersion}.js`;
1635
+ const newFileName = `${baseName}-${latestVersion}${ext}`;
1636
+
1637
+ writeUpdateLine(`${oldVersionFile} -> ${newFileName}`);
1638
+
1639
+ updateImports(oldFileName, newFileName);
1640
+ //updateImports(baseName, `${baseName}-${latestVersion}.js`);
1641
+ //updateImports(pluginInfo.name, `${baseName}-${latestVersion}`);
1642
+
1643
+ //console.log(`✅ Updated → ${newFileName}`);
1644
+ //console.log(`✅ Updated → ${baseName}-${latestVersion}`);
1645
+
1646
+ return true;
1647
+ }
1648
+
1649
+
1650
+
1651
+
1652
+ /*############################################## AUTO DOWNLOAD FROM SERVER END #####################################*/
1653
+
1654
+
1655
+
1656
+
1657
+
1658
+
1659
+
1660
+
1661
+
1662
+
1663
+
1664
+
1665
+
1666
+
1667
+
1668
+
1669
+
1670
+
1671
+ async function loadPluginVersions() {
1672
+
1673
+ if (!USE_LIVE_SERVER_VERSION) {
1674
+ console.log("ℹ️ Using local plugin versions (offline mode).");
1675
+ return;
1676
+ }
1677
+
1678
+ console.log("🌐 Fetching plugin versions from server...");
1679
+
1680
+ const versions = await fetchVersions();
1681
+
1682
+ if (!versions || typeof versions !== "object") {
1683
+ console.log("⚠️ Server unavailable or invalid versions.json. Falling back to local versions.");
1684
+ return;
1685
+ }
1686
+
1687
+ requiredPlugins.forEach(plugin => {
1688
+
1689
+ if (!plugin.name) return;
1690
+
1691
+ if (versions[plugin.name]) {
1692
+ plugin.minVersion = versions[plugin.name];
1693
+ }
1694
+
1695
+ });
1696
+
1697
+ console.log("✅ Plugin versions loaded from server.");
1698
+
1699
+ }
1700
+
1701
+
1702
+
1703
+ let hasMandatoryUpdate = false;
1704
+ function checkPlugins() {
1705
+ return new Promise(async (resolve, reject) => {
1706
+ const files = walkSync(srcDir);
1707
+ const outdatedPlugins = [];
1708
+ let hasMandatoryUpdate = false;
1709
+
1710
+ for (const plugin of requiredPlugins) {
1711
+ const searchRoot = getSearchRoot(plugin);
1712
+
1713
+ // ---------- Folder plugins ----------
1714
+ if (plugin.isFolder) {
1715
+ if (!fs.existsSync(searchRoot)) continue;
1716
+
1717
+ const subDirs = fs.readdirSync(searchRoot)
1718
+ .map(name => path.join(searchRoot, name))
1719
+ .filter(p => fs.statSync(p).isDirectory());
1720
+
1721
+ for (const dir of subDirs) {
1722
+ const relativePath = path.relative(searchRoot, dir).replace(/\\/g, '/');
1723
+ const match = plugin.pattern.exec(relativePath);
1724
+
1725
+ if (match) {
1726
+ const currentVersion = match[1];
1727
+
1728
+ if (compareVersions(currentVersion, plugin.minVersion) < 0) {
1729
+ outdatedPlugins.push({
1730
+ name: relativePath,
1731
+ currentVersion,
1732
+ requiredVersion: plugin.minVersion,
1733
+ mandatoryUpdate: plugin.mandatoryUpdate === true
1734
+ });
1735
+
1736
+ if (plugin.mandatoryUpdate) {
1737
+ hasMandatoryUpdate = true;
1738
+ }
1739
+ }
1740
+ }
1741
+ }
1742
+ continue;
1743
+ }
1744
+
1745
+ // ---------- File plugins ----------
1746
+ const matchedFile = files.find(file =>
1747
+ file.startsWith(searchRoot) && plugin.pattern.test(file)
1748
+ );
1749
+
1750
+ if (matchedFile) {
1751
+ const match = plugin.pattern.exec(matchedFile);
1752
+ if (match) {
1753
+ const currentVersion = match[1];
1754
+ const isBeta = !!match[2];
1755
+
1756
+ const cmp = plugin.pattern.source.includes('beta')
1757
+ ? compareWithBeta(currentVersion, plugin.minVersion, isBeta)
1758
+ : compareVersions(currentVersion, plugin.minVersion);
1759
+
1760
+ if (cmp < 0) {
1761
+ outdatedPlugins.push({
1762
+ name: path.relative(srcDir, matchedFile),
1763
+ currentVersion: isBeta ? `${currentVersion}-beta` : currentVersion,
1764
+ requiredVersion: plugin.minVersion,
1765
+ mandatoryUpdate: plugin.mandatoryUpdate === true
1766
+ });
1767
+
1768
+ if (plugin.mandatoryUpdate) {
1769
+ hasMandatoryUpdate = true;
1770
+ }
1771
+ }
1772
+ }
1773
+ }
1774
+ }
1775
+
1776
+ // ---------- Result handling ----------
1777
+ if (outdatedPlugins.length > 0) {
1778
+ console.log('\n❗ The following plugins are outdated:\n');
1779
+
1780
+ outdatedPlugins.forEach( p => {
1781
+ const tag = p.mandatoryUpdate ? '🔥 MANDATORY' : '';
1782
+ console.log(
1783
+ ` ⚠️ - ${p.name} (Current: ${p.currentVersion}, Required: ${p.requiredVersion}) ${tag}`
1784
+ );
1785
+ });
1786
+
1787
+ // 🚨 Mandatory update → stop build
1788
+ /* if (hasMandatoryUpdate) {
1789
+ console.log('\n🚫 One or more plugins require a mandatory update.');
1790
+ console.log('❌ Build cancelled. Please update mandatory plugins and try again.');
1791
+ process.exit(1);
1792
+ } */
1793
+
1794
+
1795
+
1796
+
1797
+
1798
+ if (hasMandatoryUpdate) {
1799
+
1800
+ //--------------------------------------------------
1801
+ // 🚫 AUTO UPDATE DISABLED
1802
+ //--------------------------------------------------
1803
+ if (!ENABLE_AUTO_UPDATE) {
1804
+ console.log("\n🚫 Auto-update disabled.");
1805
+ console.log("❌ Manual update required.");
1806
+ process.exit(1);
1807
+ }
1808
+
1809
+ //--------------------------------------------------
1810
+ // 🔥 AUTO UPDATE ENABLED
1811
+ //--------------------------------------------------
1812
+ console.log("\n🔥 Mandatory plugins outdated. Trying auto-update...\n");
1813
+
1814
+
1815
+
1816
+ let autoFailed = false;
1817
+
1818
+ for (const p of outdatedPlugins.filter(x => x.mandatoryUpdate)) {
1819
+
1820
+ const pluginDef = requiredPlugins.find(def =>
1821
+ def.pattern.test(p.name)
1822
+ );
1823
+
1824
+ if (!pluginDef) continue;
1825
+
1826
+ const success = await autoUpdatePlugin(
1827
+ pluginDef,
1828
+ p
1829
+ );
1830
+
1831
+ if (!success) {
1832
+ autoFailed = true;
1833
+
1834
+ const pluginDef = requiredPlugins.find(def =>
1835
+ def.pattern.test(p.name)
1836
+ );
1837
+
1838
+ console.log(`❌ Manual update required for ${p.name}`);
1839
+
1840
+ if (pluginDef) {
1841
+ console.log(`👉 Required minimum version: ${pluginDef.minVersion}`);
1842
+ }
1843
+ }
1844
+ }
1845
+
1846
+ // 🚨 Fallback to manual if any failed
1847
+ if (autoFailed) {
1848
+ console.log('\n🚫 One or more plugins require manual update.');
1849
+ console.log('❌ Build cancelled. Please update mandatory plugins.');
1850
+ process.exit(1);
1851
+ }
1852
+
1853
+ console.log('\n🎉 All mandatory plugins auto-updated! Rechecking plugins...\n');
1854
+
1855
+ // Re-run plugin check so outdated list becomes empty
1856
+ await checkPlugins();
1857
+ return;
1858
+ }
1859
+
1860
+
1861
+
1862
+
1863
+
1864
+
1865
+ // Optional updates → ask user
1866
+ const rl = readline.createInterface({
1867
+ input: process.stdin,
1868
+ output: process.stdout
1869
+ });
1870
+
1871
+ rl.question(
1872
+ '\nAre you sure you want to continue without updating these plugins? (y/n): ',
1873
+ answer => {
1874
+ rl.close();
1875
+
1876
+ if (answer.toLowerCase() !== 'y') {
1877
+ console.log('\n❌ Build cancelled due to outdated plugins.');
1878
+ process.exit(1);
1879
+ } else {
1880
+ console.log('\n✅ Continuing build...');
1881
+ resolve();
1882
+ }
1883
+ }
1884
+ );
1885
+ } else {
1886
+ console.log('✅ All plugin versions are up to date.');
1887
+ saveUpdateLogs();
1888
+ resolve();
1889
+ }
1890
+ });
1891
+ }
1892
+
1893
+
1894
+
1895
+
1896
+
1897
+
1898
+
1899
+
1900
+
1901
+
1902
+ const localizationBaseDir = path.join(__dirname, "..", "src", "js", "localization");
1903
+
1904
+ // ======================================================
1905
+ // 🌐 LOCALIZATION CHECK (FULLY DYNAMIC)
1906
+ // ======================================================
1907
+
1908
+
1909
+ const jsRootDir = path.join(__dirname, "..", "src", "js");
1910
+
1911
+ // 🔍 Detect OLD localization files in root js/
1912
+ const oldLocalizationFiles = fs.readdirSync(jsRootDir)
1913
+ .filter(name =>
1914
+ /^localization-\d+(\.\d+)*\.js$/.test(name) ||
1915
+ /^localization_settings-\d+(\.\d+)*\.js$/.test(name)
1916
+ );
1917
+
1918
+ // ❌ If old structure found → STOP
1919
+ if (oldLocalizationFiles.length > 0) {
1920
+
1921
+ console.error(`
1922
+ OLD LOCALIZATION STRUCTURE DETECTED
1923
+
1924
+ You are using outdated file structure:
1925
+ src/js/localization-x.x.js
1926
+ src/js/localization_settings-x.x.js
1927
+
1928
+ 🚨 This is no longer supported.
1929
+
1930
+ 📦 Found files:
1931
+ ${oldLocalizationFiles.map(f => " - " + f).join("\n")}
1932
+
1933
+ 👉 Please move them to new structure:
1934
+
1935
+ src/js/localization/localization_settings-x.x.js
1936
+ src/js/localization/localization-x.x/
1937
+
1938
+ ⚠️ Example:
1939
+
1940
+ OLD:
1941
+ src/js/localization-x.x.js
1942
+ src/js/localization_settings-x.x.js
1943
+
1944
+ NEW:
1945
+ src/js/localization/localization_settings-1.1.js
1946
+ src/js/localization/localization-x.x/localization-x.x.js
1947
+
1948
+ ❌ Build stopped.
1949
+ `);
1950
+
1951
+ process.exit(1);
1952
+ }
1953
+
1954
+
1955
+
1956
+
1957
+ if (fs.existsSync(localizationBaseDir)) {
1958
+
1959
+ // ❌ Block localization-x.x.js directly inside localization folder
1960
+ const invalidFiles = fs.readdirSync(localizationBaseDir)
1961
+ .filter(name => /^localization-\d+(\.\d+)*\.js$/.test(name));
1962
+
1963
+ if (invalidFiles.length > 0) {
1964
+ console.error(`
1965
+ ❌ INVALID LOCALIZATION FILE LOCATION
1966
+
1967
+ 🚫 localization-x.x.js must NOT be directly inside:
1968
+ src/js/localization/
1969
+
1970
+ 📦 Found:
1971
+ ${invalidFiles.map(f => " - " + f).join("\n")}
1972
+
1973
+ Move it to:
1974
+
1975
+ src/js/localization/localization-x.x/localization-x.x.js
1976
+
1977
+ Build stopped.
1978
+ `);
1979
+ process.exit(1);
1980
+ }
1981
+
1982
+ // 🔍 Find localization-x.x folders
1983
+ const localizationVersions = fs.readdirSync(localizationBaseDir)
1984
+ .filter(name => /^localization-\d+(\.\d+)+$/.test(name));
1985
+ } else {
1986
+ console.log("ℹ️ Localization not used in this project. Skipping...");
1987
+ }
1988
+
1989
+
1990
+
1991
+
1992
+
1993
+
1994
+
1995
+
1996
+
1997
+
1998
+
1999
+
2000
+
2001
+
2002
+
2003
+
2004
+
2005
+
2006
+
2007
+ //editor-x.x import old style check and stop execution START
2008
+
2009
+
2010
+
2011
+ // Match: editor/editor-2.3, editor/editor-2.3.1, etc.
2012
+ const FORBIDDEN_REGEX = /editor\/editor-\d+(\.\d+)+/;
2013
+
2014
+ let hasError = false;
2015
+
2016
+ const ERROR_MESSAGE = `const ERROR_MESSAGE = ❌ Invalid import detected!
2017
+
2018
+ You are using a direct version-based path like: editor/editor-x.x/editor.js
2019
+
2020
+ 🚫 This is NOT allowed.
2021
+
2022
+ 👉 Please use the proper alias or updated import method.
2023
+ Example: import { ... } from '@editor'
2024
+
2025
+ ⚠️ Do not use version-based paths in imports.
2026
+ 👉 Please add this manually in vite.config.js:
2027
+
2028
+ alias: {
2029
+ '@editor': path.resolve(__dirname, './src/js/editor/editor-x.x')
2030
+ }`
2031
+
2032
+ function scanDir(dir) {
2033
+ const files = fs.readdirSync(dir);
2034
+
2035
+ for (const file of files) {
2036
+ const fullPath = path.join(dir, file);
2037
+ const stat = fs.statSync(fullPath);
2038
+
2039
+ if (stat.isDirectory()) {
2040
+ scanDir(fullPath);
2041
+ } else if (file.endsWith(".js") || file.endsWith(".ts") || file.endsWith(".f7")) {
2042
+ const content = fs.readFileSync(fullPath, "utf-8");
2043
+
2044
+ const lines = content.split("\n");
2045
+
2046
+ lines.forEach((line, index) => {
2047
+ if (FORBIDDEN_REGEX.test(line)) {
2048
+ console.error(
2049
+ `❌ Forbidden import found:\nFile: ${fullPath}\nLine: ${index + 1}\nCode: ${line.trim()}\n`,
2050
+ ERROR_MESSAGE
2051
+ );
2052
+ hasError = true;
2053
+ }
2054
+ });
2055
+ }
2056
+ }
2057
+ }
2058
+
2059
+ // Run scan
2060
+ scanDir(ROOT_DIR);
2061
+
2062
+ // Throw error (exit process)
2063
+ if (hasError) {
2064
+ console.error("🚫 Build failed due to forbidden editor imports.");
2065
+ process.exit(1);
2066
+ } else {
2067
+ console.log(" No forbidden imports found.");
2068
+ }
2069
+
2070
+
2071
+
2072
+
2073
+
2074
+
2075
+ //editor-x.x import old style check and stop execution START
2076
+
2077
+
2078
+
2079
+
2080
+
2081
+
2082
+
2083
+
2084
+
2085
+
2086
+ // Check all the codeplays plugins version START
2087
+
2088
+
2089
+
2090
+
2091
+ // ====================================================================
2092
+ // AUTO-ADD esbuild.drop: ['console','debugger'] to vite.config.js / mjs
2093
+ // ====================================================================
2094
+
2095
+
2096
+
2097
+ const checkAndupdateDropInViteConfig = () => {
2098
+
2099
+ const possibleFiles = [
2100
+ "vite.config.js",
2101
+ "vite.config.mjs"
2102
+ ];
2103
+
2104
+ // Detect existing config file
2105
+ const viteConfigPath = possibleFiles
2106
+ .map(file => path.join(process.cwd(), file))
2107
+ .find(filePath => fs.existsSync(filePath));
2108
+
2109
+ if (!viteConfigPath) {
2110
+ console.warn("⚠️ No vite config found. Skipping.");
2111
+ return;
2112
+ }
2113
+
2114
+ //console.log("📄 Using:", viteConfigPath.split("/").pop());
2115
+
2116
+ let viteContent = fs.readFileSync(viteConfigPath, "utf8");
2117
+
2118
+ // Skip if already exists
2119
+ if (/drop\s*:\s*\[.*['"]console['"].*\]/.test(viteContent)) {
2120
+ console.log("ℹ️ vite.config.(m)js already Updated. Skipping...");
2121
+ return;
2122
+ }
2123
+
2124
+ console.log("🔧 Adding esbuild.drop ...");
2125
+
2126
+ // If esbuild block exists
2127
+ if (/esbuild\s*:\s*{/.test(viteContent)) {
2128
+ viteContent = viteContent.replace(
2129
+ /esbuild\s*:\s*{([\s\S]*?)(^ {0,8})}/m,
2130
+ (full, inner, indent) => {
2131
+
2132
+ let lines = inner
2133
+ .split("\n")
2134
+ .map(l => l.trim())
2135
+ .filter(Boolean);
2136
+
2137
+ // Fix last comma
2138
+ if (lines.length > 0) {
2139
+ lines[lines.length - 1] =
2140
+ lines[lines.length - 1].replace(/,+$/, "") + ",";
2141
+ }
2142
+
2143
+ // Re-indent
2144
+ lines = lines.map(l => indent + " " + l);
2145
+
2146
+ // Add drop
2147
+ lines.push(`${indent} drop: ['console','debugger'],`);
2148
+
2149
+ return `esbuild: {\n${lines.join("\n")}\n${indent}}`;
2150
+ }
2151
+ );
2152
+ }
2153
+
2154
+ // If esbuild missing
2155
+ else {
2156
+ viteContent = viteContent.replace(
2157
+ /export default defineConfig\s*\(\s*{/,
2158
+ m => `${m}\n esbuild: {\n drop: ['console','debugger'],\n },`
2159
+ );
2160
+ }
2161
+
2162
+ fs.writeFileSync(viteConfigPath, viteContent, "utf8");
2163
+ console.log("✅ vite.config.(m)js Updated successfully.");
2164
+ };
2165
+
2166
+
2167
+
2168
+
2169
+
2170
+
2171
+
2172
+
2173
+
2174
+
2175
+ const compareVersion = (v1, v2) => {
2176
+ const a = v1.split(".").map(Number);
2177
+ const b = v2.split(".").map(Number);
2178
+
2179
+ for (let i = 0; i < Math.max(a.length, b.length); i++) {
2180
+ const num1 = a[i] || 0;
2181
+ const num2 = b[i] || 0;
2182
+ if (num1 > num2) return 1;
2183
+ if (num1 < num2) return -1;
2184
+ }
2185
+ return 0;
2186
+ };
2187
+
2188
+
2189
+
2190
+
2191
+
2192
+
2193
+
2194
+
2195
+
2196
+
2197
+ const admobConfigPath = path.join('src', 'js','Ads', 'admob-ad-configuration.json');
2198
+
2199
+ const checkAdmobConfigurationProperty=()=>{
2200
+
2201
+
2202
+ if (!_admobConfig.ADMOB_ENABLED)
2203
+ {
2204
+ console.log("ℹ️ Admob is not enabled so 'admob-ad-configuration.json' checking is skipping...");
2205
+ return;
2206
+ }
2207
+
2208
+
2209
+ const REQUIRED_CONFIG_KEYS = [
2210
+ "isKidsApp",
2211
+ "isTesting",
2212
+ "isConsoleLogEnabled",
2213
+ "bannerEnabled",
2214
+ "interstitialEnabled",
2215
+ "appOpenEnabled",
2216
+ "rewardVideoEnabled",
2217
+ "rewardInterstitialEnabled",
2218
+ "collapsibleEnabled",
2219
+ "isLandScape",
2220
+ "isOverlappingEnable",
2221
+ "bannerTypeAndroid",
2222
+ "bannerTypeiOS",
2223
+ "bannerTopSpaceColor",
2224
+ "interstitialLoadScreenTextColor",
2225
+ "interstitialLoadScreenBackgroundColor",
2226
+ "beforeBannerSpace",
2227
+ "whenShow",
2228
+ "minimumClick",
2229
+ "interstitialTimeOut",
2230
+ "interstitialFirstTimeOut",
2231
+ "appOpenAdsTimeOut",
2232
+ "maxRetryCount",
2233
+ "retrySecondsAr",
2234
+ "appOpenPerSession",
2235
+ "interstitialPerSession",
2236
+ "appOpenFirstTimeOut"
2237
+ ];
2238
+
2239
+
2240
+
2241
+
2242
+
2243
+ let admobConfigInJson;
2244
+
2245
+ try {
2246
+ admobConfigInJson = JSON.parse(readFileSync(admobConfigPath, "utf8"));
2247
+ } catch (err) {
2248
+ console.error("❌ Failed to read admob-ad-configuration.json", err);
2249
+ process.exit(1);
2250
+ }
2251
+
2252
+ // ✅ Validate config object exists
2253
+ if (!admobConfigInJson.config) {
2254
+ console.error('❌ "config" object is missing in admob-ad-configuration.json');
2255
+ process.exit(1);
2256
+ }
2257
+
2258
+
2259
+ const admobConfigMinVersion="1.5"
2260
+
2261
+ if (compareVersion(admobConfigInJson.VERSION, admobConfigMinVersion) < 0) {
2262
+ console.error(`❌ Please use at-least version ${admobConfigMinVersion} in "src/js/Ads/admob-ad-configuration.json"`);
2263
+ process.exit(1);
2264
+ }
2265
+
2266
+
2267
+ const config = admobConfigInJson.config;
2268
+
2269
+ // ✅ Find missing properties
2270
+ const missingKeys = REQUIRED_CONFIG_KEYS.filter(
2271
+ key => !(key in config)
2272
+ );
2273
+
2274
+
2275
+
2276
+ if (missingKeys.length > 0) {
2277
+ console.error("❌ Missing required configuration keys. Please check it in 'src/js/Ads/admob-ad-configuration.json'");
2278
+
2279
+ missingKeys.forEach(k => console.error(" - " + k));
2280
+ process.exit(1);
2281
+ }
2282
+
2283
+
2284
+ console.log('✅ All keys exist. in "admob-ad-configuration.json file" Configuration looks good.');
2285
+ }
2286
+
2287
+
2288
+
2289
+ function ensureGitignoreEntry(entry) {
2290
+ const gitignorePath = path.join(process.cwd(), '.gitignore');
2291
+
2292
+ // If .gitignore doesn't exist, create it
2293
+ if (!fs.existsSync(gitignorePath)) {
2294
+ fs.writeFileSync(gitignorePath, `${entry}\n`, 'utf8');
2295
+ console.log(`✅ .gitignore created and added: ${entry}`);
2296
+ return;
2297
+ }
2298
+
2299
+ const content = fs.readFileSync(gitignorePath, 'utf8');
2300
+
2301
+ // Normalize lines (trim + remove trailing slashes for comparison)
2302
+ const lines = content
2303
+ .split(/\r?\n/)
2304
+ .map(l => l.trim());
2305
+
2306
+ const normalizedEntry = entry.replace(/\/$/, '');
2307
+
2308
+ const exists = lines.some(
2309
+ line => line.replace(/\/$/, '') === normalizedEntry
2310
+ );
2311
+
2312
+ if (exists) {
2313
+ console.log(`ℹ️ .gitignore already contains: ${entry}`);
2314
+ return;
2315
+ }
2316
+
2317
+ // Ensure file ends with newline
2318
+ const separator = content.endsWith('\n') ? '' : '\n';
2319
+
2320
+ fs.appendFileSync(gitignorePath, `${separator}${entry}\n`, 'utf8');
2321
+ console.log(`✅ Added to .gitignore: ${entry}`);
2322
+ }
2323
+
2324
+
2325
+ ensureGitignoreEntry('buildCodeplay/');
2326
+
2327
+
2328
+ // Run the validation
2329
+ (async () => {
2330
+
2331
+ await loadPluginVersions(); // 🔥 NEW
2332
+
2333
+ await checkPlugins();
2334
+ checkAndupdateDropInViteConfig();
2335
+ checkAdmobConfigurationProperty()
2336
+ })();
2337
+
2338
+
2339
+ // ======================================================
2340
+ // Validate theme folder location (src/js/theme is NOT allowed)
2341
+ // ======================================================
2342
+
2343
+ function validateThemeFolderLocation() {
2344
+ const oldThemePath = path.join(process.cwd(), 'src', 'js', 'theme');
2345
+ const newThemePath = path.join(process.cwd(), 'src', 'theme');
2346
+
2347
+ // ❌ Block old structure
2348
+ if (fs.existsSync(oldThemePath)) {
2349
+ console.error(
2350
+ '\n❌ INVALID PROJECT STRUCTURE DETECTED\n' +
2351
+ '--------------------------------------------------\n' +
2352
+ 'The "theme" folder must NOT be inside:\n' +
2353
+ ' src/js/theme\n\n' +
2354
+ '✅ Correct location is:\n' +
2355
+ ' src/theme\n\n' +
2356
+ '🛑 Please move the folder and re-run the build.\n'
2357
+ );
2358
+ process.exit(1);
2359
+ }
2360
+
2361
+ // ⚠️ Optional warning if new theme folder is missing
2362
+ if (!fs.existsSync(newThemePath)) {
2363
+ console.warn(
2364
+ '\n⚠️ WARNING: "src/theme" folder not found.\n' +
2365
+ 'If your app uses themes, please ensure it exists.\n'
2366
+ );
2367
+ } else {
2368
+ console.log('✅ Theme folder structure validated (src/theme).');
2369
+ }
2370
+ }
2371
+ validateThemeFolderLocation()
2372
+
2373
+
2374
+
2375
+ const validateAndRestoreSignDetails=()=>{
2376
+
2377
+ // Read config file
2378
+ const config = JSON.parse(fs.readFileSync(configPath, 'utf8'));
2379
+
2380
+ // Ensure android and buildOptions exist
2381
+ if (!config.android) config.android = {};
2382
+ if (!config.android.buildOptions) config.android.buildOptions = {};
2383
+
2384
+ // Update only if changed
2385
+ let updated = false;
2386
+
2387
+ if (config.android.buildOptions.releaseType !== 'AAB') {
2388
+ config.android.buildOptions.releaseType = 'AAB';
2389
+ updated = true;
2390
+ }
2391
+
2392
+ if (config.android.buildOptions.signingType !== 'jarsigner') {
2393
+ config.android.buildOptions.signingType = 'jarsigner';
2394
+ updated = true;
2395
+ }
2396
+
2397
+ // Write back only if modified
2398
+ if (updated) {
2399
+ fs.writeFileSync(configPath, JSON.stringify(config, null, 2));
2400
+ console.log('capacitor.config.json updated successfully.');
2401
+ } else {
2402
+ console.log('No changes needed.');
2403
+ }
2404
+
2405
+ }
2406
+
2407
+ validateAndRestoreSignDetails()
2408
+
2409
+
2410
+ execSync('node buildCodeplay/fix-onesignal-plugin.js', { stdio: 'inherit' });
2411
+
2412
+
2413
+
2414
+
2415
+
2416
+
2417
+
2418
+ //################################## SystemBars.java update for "@capacitor/android": "^8.3.0" plugin START ###############################
2419
+
2420
+
2421
+ const filePath = path.join(
2422
+ __dirname,
2423
+ "../node_modules/@capacitor/android/capacitor/src/main/java/com/getcapacitor/plugin/SystemBars.java"
2424
+ );
2425
+
2426
+ // 🔍 OLD BLOCK (anchor)
2427
+ const OLD_BLOCK = `if (shouldPassthroughInsets) {
2428
+ // We need to correct for a possible shown IME
2429
+ v.setPadding(0, 0, 0, keyboardVisible ? imeInsets.bottom : 0);
2430
+
2431
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.VANILLA_ICE_CREAM && hasViewportCover && insetHandlingEnabled) {
2432
+ Insets safeAreaInsets = calcSafeAreaInsets(insets);
2433
+ injectSafeAreaCSS(safeAreaInsets.top, safeAreaInsets.right, safeAreaInsets.bottom, safeAreaInsets.left);
2434
+ }
2435
+
2436
+ return new WindowInsetsCompat.Builder(insets)
2437
+ .setInsets(
2438
+ WindowInsetsCompat.Type.systemBars() | WindowInsetsCompat.Type.displayCutout(),
2439
+ Insets.of(
2440
+ systemBarsInsets.left,
2441
+ systemBarsInsets.top,
2442
+ systemBarsInsets.right,
2443
+ getBottomInset(systemBarsInsets, keyboardVisible)
2444
+ )
2445
+ )
2446
+ .build();
2447
+ }`;
2448
+
2449
+ // ✅ NEW BLOCK
2450
+ const NEW_BLOCK = `if (shouldPassthroughInsets) {
2451
+ /* 🔴 ORIGINAL CODE (COMMENTED FOR SAFETY)
2452
+ ${OLD_BLOCK.split("\n").map(line => " " + line).join("\n")}
2453
+ */
2454
+
2455
+ // ✅ NEW LOGIC (CUSTOM FIX)
2456
+ v.setPadding(0, 0, 0, 0);
2457
+
2458
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.VANILLA_ICE_CREAM && hasViewportCover && insetHandlingEnabled) {
2459
+ Insets safeAreaInsets = calcSafeAreaInsets(insets);
2460
+ injectSafeAreaCSS(safeAreaInsets.top, safeAreaInsets.right, safeAreaInsets.bottom, safeAreaInsets.left);
2461
+ }
2462
+
2463
+ return insets; // WebView handles everything
2464
+ }`;
2465
+
2466
+ // 🚨 ERROR MESSAGE
2467
+ const ERROR_MSG = `
2468
+ ❌ Capacitor SystemBars.java structure changed!
2469
+
2470
+ Plugin: @capacitor/android
2471
+ Path : node_modules\@capacitor\android\capacitor\src\main\java\com\getcapacitor\plugin\SystemBars.java
2472
+
2473
+ Check version of "@capacitor/android": in package.json
2474
+
2475
+ 👉 Expected code block not found.
2476
+
2477
+ This usually means Capacitor updated internally.
2478
+
2479
+ Please:
2480
+ 1. Open SystemBars.java
2481
+ 2. Update patch script
2482
+ 3. Re-run build
2483
+
2484
+ ⛔ Build stopped.
2485
+ `;
2486
+
2487
+ function patchFile() {
2488
+ if (!fs.existsSync(filePath)) {
2489
+ console.error("❌ SystemBars.java not found!");
2490
+ process.exit(1);
2491
+ }
2492
+
2493
+ let content = fs.readFileSync(filePath, "utf8");
2494
+
2495
+ // ✅ Already patched?
2496
+ if (content.includes("🔴 ORIGINAL CODE (COMMENTED FOR SAFETY)")) {
2497
+ console.log("✅ Already SystemBars.java patched. Skipping...");
2498
+ return;
2499
+ }
2500
+
2501
+ // 🔍 Check old block exists
2502
+ if (!content.includes(OLD_BLOCK)) {
2503
+ console.error(ERROR_MSG);
2504
+ process.exit(1);
2505
+ }
2506
+
2507
+ // 🔁 Replace
2508
+ const updated = content.replace(OLD_BLOCK, NEW_BLOCK);
2509
+
2510
+ fs.writeFileSync(filePath, updated, "utf8");
2511
+
2512
+ console.log("✅ SystemBars.java patched (comment + new logic)!");
2513
+ }
2514
+
2515
+ patchFile();
2516
+
2517
+ //################################## SystemBars.java update for "@capacitor/android": "^8.3.0" plugin END ###############################
2518
+
2519
+
2520
+
2521
+
2522
+ /*
2523
+ Release Notes
2524
+
2525
+ 5.1
2526
+ Kotlin version update is commented. Previously admob is not worked if not update the kotlin version to higher version
2527
+
2318
2528
  */