-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathnitrogen-patch.js
More file actions
110 lines (98 loc) · 2.58 KB
/
nitrogen-patch.js
File metadata and controls
110 lines (98 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/**
* Recursively patches all generated Nitro files (Android & iOS):
*
* ANDROID
* - Replaces 'com.margelo.nitro.rngooglemapsplus' -> 'com.rngooglemapsplus'
* - Replaces 'com/margelo/nitro/rngooglemapsplus' -> 'com/rngooglemapsplus'
* - Removes 'margelo/nitro/' in RNGoogleMapsPlusOnLoad.cpp
* iOS
*/
import { readFile, readdir, writeFile } from 'node:fs/promises';
import { copyFile, mkdir } from 'node:fs/promises';
import path from 'node:path';
const ROOT_ANDROID = path.join(
process.cwd(),
'nitrogen',
'generated',
'android'
);
const SRC_JSON_DIR = path.join(
process.cwd(),
'nitrogen',
'generated',
'shared',
'json'
);
const DEST_JSON_DIR = path.join(
process.cwd(),
'lib',
'nitrogen',
'generated',
'shared',
'json'
);
const ANDROID_ONLOAD_FILE = path.join(
ROOT_ANDROID,
'RNGoogleMapsPlusOnLoad.cpp'
);
const REPLACEMENTS = [
{
regex: /com\.margelo\.nitro\.rngooglemapsplus/g,
replacement: 'com.rngooglemapsplus',
},
{
regex: /com\/margelo\/nitro\/rngooglemapsplus/g,
replacement: 'com/rngooglemapsplus',
},
];
async function processFile(filePath) {
let content = await readFile(filePath, 'utf8');
let updated = content;
for (const { regex, replacement } of REPLACEMENTS) {
updated = updated.replace(regex, replacement);
}
if (path.resolve(filePath) === path.resolve(ANDROID_ONLOAD_FILE)) {
updated = updated.replace(/margelo\/nitro\//g, '');
}
if (updated !== content) {
await writeFile(filePath, updated, 'utf8');
console.log(`Updated: ${filePath}`);
}
}
async function start(dir) {
const entries = await readdir(dir, { withFileTypes: true });
for (const entry of entries) {
const fullPath = path.join(dir, entry.name);
if (entry.isDirectory()) {
await start(fullPath);
} else if (entry.isFile()) {
await processFile(fullPath);
}
}
}
async function copyJsonFiles() {
try {
await mkdir(DEST_JSON_DIR, { recursive: true });
const files = await readdir(SRC_JSON_DIR);
for (const file of files) {
if (file.endsWith('.json')) {
const src = path.join(SRC_JSON_DIR, file);
const dest = path.join(DEST_JSON_DIR, file);
await copyFile(src, dest);
console.log(`Copied JSON: ${file}`);
}
}
} catch (err) {
console.warn('Failed to copy JSON view configs:', err.message);
}
}
(async () => {
try {
await copyJsonFiles();
await start(ROOT_ANDROID);
console.log('All Nitrogen files patched successfully.');
} catch (err) {
console.error('Error while processing files:', err);
process.exit(1);
}
})();