codeplay-common 4.11.24 → 4.11.25

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.
@@ -0,0 +1,56 @@
1
+ // Version 1.0: keep native app lifecycle callbacks out of the UMP/ATT flow.
2
+ const fs = require('node:fs');
3
+ const path = require('node:path');
4
+
5
+ // Only remove the known block generated by older builds. Any other native
6
+ // request needs an explicit correction rather than deleting custom app code.
7
+ const legacyRequest = [
8
+ 'if ATTrackingManager.trackingAuthorizationStatus == .notDetermined &&',
9
+ '!isTrackingAuthorizationRequestScheduled {',
10
+ 'isTrackingAuthorizationRequestScheduled = true',
11
+ 'DispatchQueue.main.asyncAfter(deadline: .now() + 1) { [weak self] in',
12
+ 'self?.isTrackingAuthorizationRequestScheduled = false',
13
+ 'guard UIApplication.shared.applicationState == .active,',
14
+ 'ATTrackingManager.trackingAuthorizationStatus == .notDetermined else { return }',
15
+ 'ATTrackingManager.requestTrackingAuthorization { _ in }',
16
+ '}',
17
+ '}',
18
+ ].map(line => line.replace(/[.*+?^${}()|[\]\\]/g, '\\$&').replace(/ +/g, '\\s+')).join('\\s*');
19
+
20
+ function nativeSources(directory) {
21
+ if (!fs.existsSync(directory)) return [];
22
+ return fs.readdirSync(directory, { withFileTypes: true }).flatMap(entry => {
23
+ const file = path.join(directory, entry.name);
24
+ if (entry.isDirectory()) return nativeSources(file);
25
+ return entry.isFile() && /\.(?:swift|m|mm)$/.test(entry.name) ? [file] : [];
26
+ });
27
+ }
28
+
29
+ function prepareIOSAdConsent(projectRoot, log = console.log) {
30
+ const appDirectory = path.join(projectRoot, 'ios', 'App', 'App');
31
+ const updates = [];
32
+ for (const file of nativeSources(appDirectory)) {
33
+ const original = fs.readFileSync(file, 'utf8');
34
+ let content = original;
35
+ if (path.basename(file) === 'SceneDelegate.swift') {
36
+ content = content.replace(new RegExp(legacyRequest, 'g'), '');
37
+ content = content.replace(/^[ \t]*private var isTrackingAuthorizationRequestScheduled = false\r?\n/gm, '');
38
+ if (!content.includes('ATTrackingManager')) {
39
+ content = content.replace(/^import AppTrackingTransparency\r?\n/gm, '');
40
+ }
41
+ }
42
+ // Check AppDelegate and other app-owned startup code even when this app
43
+ // has no SceneDelegate. The Capacitor plugin remains the single ATT owner.
44
+ const executable = content.replace(/\/\*[\s\S]*?\*\//g, '').replace(/\/\/[^\r\n]*/g, '');
45
+ if (/\bATTrackingManager\s*(?:\.\s*|\s+)requestTrackingAuthorization(?:WithCompletionHandler)?\b/.test(executable)) {
46
+ throw new Error(`${path.relative(projectRoot, file)} requests ATT outside the regional consent flow. Remove the native startup request; @admob requests ATT after UMP completes.`);
47
+ }
48
+ if (content !== original) updates.push({ file, content });
49
+ }
50
+ // Validate every source before changing any file.
51
+ for (const { file, content } of updates) fs.writeFileSync(file, content, 'utf8');
52
+ log('iOS native startup checked; the ad consent flow controls ATT after UMP.');
53
+ return updates.map(({ file }) => path.relative(projectRoot, file));
54
+ }
55
+
56
+ module.exports = { prepareIOSAdConsent };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "codeplay-common",
3
- "version": "4.11.24",
3
+ "version": "4.11.25",
4
4
  "description": "Common build scripts and files",
5
5
  "scripts": {
6
6
  "postinstall": "node scripts/sync-files.js",
@@ -0,0 +1,99 @@
1
+ const { test } = require('node:test');
2
+ const assert = require('node:assert/strict');
3
+ const fs = require('node:fs');
4
+ const path = require('node:path');
5
+ const { prepareIOSAdConsent } = require('../files/buildCodeplay/ios-ad-consent.cjs');
6
+ const { createRequire } = require('node:module');
7
+ const vm = require('node:vm');
8
+ const { install } = require('./sync-files.js');
9
+
10
+ function fixture(t, sources) {
11
+ const tempRoot = path.resolve(__dirname, '../agent-temp/ios-ad-consent-tests');
12
+ fs.mkdirSync(tempRoot, { recursive: true });
13
+ const root = fs.mkdtempSync(path.join(tempRoot, 'case-'));
14
+ t.after(() => fs.rmSync(root, { recursive: true, force: true }));
15
+ for (const [name, content] of Object.entries(sources)) {
16
+ const file = path.join(root, 'ios/App/App', name);
17
+ fs.mkdirSync(path.dirname(file), { recursive: true });
18
+ fs.writeFileSync(file, content);
19
+ }
20
+ return { root, read: name => fs.readFileSync(path.join(root, 'ios/App/App', name), 'utf8') };
21
+ }
22
+
23
+ const legacyScene = `import UIKit
24
+ import AppTrackingTransparency
25
+ class SceneDelegate {
26
+ private var isTrackingAuthorizationRequestScheduled = false
27
+ func sceneDidBecomeActive() {
28
+ if ATTrackingManager.trackingAuthorizationStatus == .notDetermined &&
29
+ !isTrackingAuthorizationRequestScheduled {
30
+ isTrackingAuthorizationRequestScheduled = true
31
+
32
+ DispatchQueue.main.asyncAfter(deadline: .now() + 1) { [weak self] in
33
+ self?.isTrackingAuthorizationRequestScheduled = false
34
+
35
+ guard UIApplication.shared.applicationState == .active,
36
+ ATTrackingManager.trackingAuthorizationStatus == .notDetermined else { return }
37
+
38
+ ATTrackingManager.requestTrackingAuthorization { _ in }
39
+ }
40
+ }
41
+ keepOtherStartupCode()
42
+ }
43
+ }
44
+ `;
45
+
46
+ test('removes the known scene startup request with LF, CRLF and changed indentation', t => {
47
+ for (const source of [legacyScene, legacyScene.replace(/\n/g, '\r\n'), legacyScene.replace(/ /g, '\t')]) {
48
+ const f = fixture(t, { 'SceneDelegate.swift': source });
49
+ assert.equal(prepareIOSAdConsent(f.root, () => {}).length, 1);
50
+ assert.doesNotMatch(f.read('SceneDelegate.swift'), /ATTrackingManager|AppTrackingTransparency|isTrackingAuthorizationRequestScheduled/);
51
+ assert.match(f.read('SceneDelegate.swift'), /keepOtherStartupCode\(\)/);
52
+ assert.deepEqual(prepareIOSAdConsent(f.root, () => {}), [], 'Migration is idempotent');
53
+ }
54
+ });
55
+
56
+ test('rejects an AppDelegate ATT request even without a SceneDelegate', t => {
57
+ const source = 'ATTrackingManager\n .requestTrackingAuthorization { _ in }';
58
+ const f = fixture(t, { 'AppDelegate.swift': source });
59
+ assert.throws(() => prepareIOSAdConsent(f.root, () => {}), /AppDelegate.swift requests ATT/);
60
+ assert.equal(f.read('AppDelegate.swift'), source);
61
+ });
62
+
63
+ test('checks nested app code and validates everything before migrating the scene', t => {
64
+ const f = fixture(t, {
65
+ 'SceneDelegate.swift': legacyScene,
66
+ 'Startup/Permissions.m': '[ATTrackingManager requestTrackingAuthorizationWithCompletionHandler:handler];',
67
+ });
68
+ assert.throws(() => prepareIOSAdConsent(f.root, () => {}), /Permissions.m requests ATT/);
69
+ assert.equal(f.read('SceneDelegate.swift'), legacyScene, 'No partial edits when validation fails');
70
+ });
71
+
72
+ test('preserves custom native code and ignores commented ATT examples', t => {
73
+ const source = '// ATTrackingManager.requestTrackingAuthorization { _ in }\n/* ATTrackingManager.requestTrackingAuthorization */\nkeepOtherStartupCode()';
74
+ const f = fixture(t, { 'AppDelegate.swift': source });
75
+ assert.deepEqual(prepareIOSAdConsent(f.root, () => {}), []);
76
+ assert.equal(f.read('AppDelegate.swift'), source);
77
+ });
78
+
79
+ test('installer ships the guard and generated before-build invokes it from the consuming app', t => {
80
+ const f = fixture(t, { 'SceneDelegate.swift': legacyScene });
81
+ fs.writeFileSync(path.join(f.root, 'package.json'), JSON.stringify({ name: 'consent-fixture', scripts: {} }));
82
+ fs.writeFileSync(path.join(f.root, 'capacitor.config.json'), JSON.stringify({ appId: 'fixture.app' }));
83
+ install({ projectRoot: f.root, copyAssets: false });
84
+ const beforeBuild = path.join(f.root, 'buildCodeplay/codeplayBeforeBuild.js');
85
+ const source = fs.readFileSync(beforeBuild, 'utf8').replace(/\r\n/g, '\n');
86
+ const start = source.indexOf('function updateIOSStartupForAdConsent() {');
87
+ const end = source.indexOf('\n\n\ntry {', start);
88
+ assert.ok(start >= 0 && end > start);
89
+ assert.ok(fs.existsSync(path.join(f.root, 'buildCodeplay/ios-ad-consent.cjs')));
90
+ vm.runInNewContext(source.slice(start, end) + '\nupdateIOSStartupForAdConsent();', {
91
+ require: createRequire(beforeBuild), process: { cwd: () => f.root },
92
+ });
93
+ assert.doesNotMatch(f.read('SceneDelegate.swift'), /requestTrackingAuthorization/);
94
+ assert.match(f.read('SceneDelegate.swift'), /keepOtherStartupCode\(\)/);
95
+ fs.writeFileSync(path.join(f.root, 'ios/App/App/AppDelegate.swift'), 'ATTrackingManager.requestTrackingAuthorization { _ in }');
96
+ assert.throws(() => vm.runInNewContext(source.slice(start, end) + '\nupdateIOSStartupForAdConsent();', {
97
+ require: createRequire(beforeBuild), process: { cwd: () => f.root },
98
+ }), /AppDelegate.swift requests ATT/);
99
+ });