-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
251 lines (223 loc) · 7.75 KB
/
index.js
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
const fs = require("fs");
const data_icons = require("./data_icons.json");
const path = require("path");
const defaultColor = "@android:color/black";
const drawable_xml_path = "./chrome/android/java/res/drawable/";
const fa_path = "./node_modules/@fortawesome/fontawesome-pro/svgs/";
const chrome_root_path = "./chrome/"; // Specify the path to the image folder
const build_log_path = "build.log"; // Specify the path for the image log file
const gni_path = "./chrome/android/kiwi_java_resources.gni";
var kiwiJavaResources;
var chromeJavaResourcesToRemove;
var kiwi_java_resources;
const regex1 = /kiwi_java_resources\s*=\s*\[([\s\S]*?)\]/;
const regex2 = /chrome_java_resources_to_remove\s*=\s*\[([\s\S]*?)\]/;
function addToKiwiJavaResources(item) {
let formattedItem = item.replace(/\.\//, "").replace(/chrome\/android\//, "");
const pushItem = `"${formattedItem}"`;
if (!kiwiJavaResources.includes(pushItem)) {
kiwiJavaResources.push(pushItem);
fs.appendFileSync(
build_log_path,
`Item '${pushItem}' added to kiwiJavaResources.\n`
);
} else {
fs.appendFileSync(
build_log_path,
`Item '${pushItem}' already exists in kiwiJavaResources.\n`
);
}
}
function addToChromeJavaResourcesToRemove(item) {
let formattedItem = item.replace(/\.\//, "").replace(/chrome\/android\//, "");
const pushItem = `"${formattedItem}"`;
if (!chromeJavaResourcesToRemove.includes(pushItem)) {
chromeJavaResourcesToRemove.push(pushItem);
fs.appendFileSync(
build_log_path,
`Item '${pushItem}' added to chromeJavaResourcesToRemove.\n`
);
} else {
fs.appendFileSync(
build_log_path,
`Item '${pushItem}' already exists in chromeJavaResourcesToRemove.\n`
);
}
}
function removeFromKiwiJavaResources(item) {
let formattedItem = item.replace(/\.\//, "").replace(/chrome\/android\//, "");
const pushItem = `"${formattedItem}"`;
const index = kiwiJavaResources.indexOf(pushItem);
if (index !== -1) {
kiwiJavaResources.splice(index, 1);
fs.appendFileSync(
build_log_path,
`Remove : Item '${pushItem}' removed from kiwiJavaResources.\n`
);
} else {
fs.appendFileSync(
build_log_path,
`Remove : Item '${pushItem}' does not exist in kiwiJavaResources.\n`
);
}
}
function updateContent() {
const string1 = kiwiJavaResources.map((item) => ` ${item}`).join(",\n");
const string2 = chromeJavaResourcesToRemove
.map((item) => ` ${item}`)
.join(",\n");
var updatedContent = kiwi_java_resources.replace(
regex1,
`kiwi_java_resources = [\n${string1}\n]`
);
updatedContent = updatedContent.replace(
regex2,
`chrome_java_resources_to_remove = [\n${string2}\n]`
);
fs.writeFile(gni_path, updatedContent, "utf8", (err) => {
if (err) {
console.error("Error writing file:", err);
return;
}
console.log("File updated successfully!");
});
}
// Helper function to check if a file is an image file
function isImageFile(file) {
const imageExtensions = [".jpg", ".jpeg", ".png", ".gif"]; // Add more extensions if needed
const ext = path.extname(file).toLowerCase();
return imageExtensions.includes(ext);
}
// Helper function to recursively search for images in a folder
function findImagesForIcon(iconName, folderPath) {
let imagePaths = [];
const files = fs.readdirSync(folderPath);
files.forEach((file) => {
const filePath = path.join(folderPath, file);
const stat = fs.statSync(filePath);
if (stat.isDirectory()) {
// Pass the icon name and the full path to the recursive call
imagePaths = imagePaths.concat(findImagesForIcon(iconName, filePath));
} else if (stat.isFile() && isImageFile(file) && file.includes(iconName)) {
imagePaths.push(filePath);
}
});
return imagePaths;
}
let count = 0;
let icount = 0;
function generateXml(icon, svgContent, xmlFilePath) {
const pathRegex = /<path[^>]+d="([^"]+)"/;
const pathMatch = svgContent.match(pathRegex);
const viewBoxRegex = /viewBox="([^"]+)"\s*/;
const viewBoxMatch = svgContent.match(viewBoxRegex);
const viewBoxValues = viewBoxMatch[1].split(" ");
const viewBoxWidth = viewBoxValues[2];
const viewBoxHeight = viewBoxValues[3];
//console.log(`------ ${pathMatch} ------`);
if (!viewBoxMatch || !pathMatch || viewBoxValues.length < 4) {
console.log(`Invalid SVG content for ${icon}`);
return;
}
const rawpath = pathMatch[1];
// Replace occurrences of '-.' with '-0.' in the path data
const path = rawpath.replace(/([^0-9])\./g, "$10.");
let paddedViewBox = 675.84;
let swidth = parseInt((paddedViewBox - viewBoxWidth) / 2);
let sheight = parseInt((paddedViewBox - viewBoxHeight) / 2);
let color = data_icons[icon].color;
if (!color) color = defaultColor;
let dp = data_icons[icon].dp;
if (!dp) dp = "24";
let nopadding = data_icons[icon].nopadding;
if (nopadding) {
paddedViewBox = "512";
swidth = "0";
sheight = "0";
}
const xmlContent = `<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="${dp}dp"
android:height="${dp}dp"
android:viewportWidth="${paddedViewBox}"
android:viewportHeight="${paddedViewBox}">
<group
android:translateX="${swidth}"
android:translateY="${sheight}">
<path
android:pathData="${path}"
android:fillColor="${color}"
android:fillType="nonZero"
/>
</group>
</vector>`;
fs.writeFileSync(xmlFilePath, xmlContent);
icount++;
console.log(`Generated: ${xmlFilePath}`);
addToKiwiJavaResources(xmlFilePath);
// Log the replaced icon names and spotted files
const imagePaths = findImagesForIcon(icon, chrome_root_path);
const spottedFiles = imagePaths.filter((imagePath) =>
imagePath.includes(icon)
);
if (spottedFiles.length > 0) {
fs.appendFileSync(
build_log_path,
`Icon '${icon}' replaced by '${xmlFilePath}', changes:\n`
);
count += spottedFiles.length;
spottedFiles.map((item) => {
removeFromKiwiJavaResources(item);
addToChromeJavaResourcesToRemove(item);
});
fs.appendFileSync(
build_log_path,
`---------------------------------------\n`
);
}
}
try {
kiwi_java_resources = fs.readFileSync(gni_path, "utf8").toString();
console.log(kiwi_java_resources);
// Use regex to find the arrays
const match1 = kiwi_java_resources.match(regex1);
const match2 = kiwi_java_resources.match(regex2);
console.log(kiwi_java_resources);
if (!match1 || !match2) return console.error("Arrays not found in file.");
// Extract the array string and parse it into an actual array
const arrayString1 = match1[1];
const arrayString2 = match2[1];
kiwiJavaResources = arrayString1
.split(/\s*,\s*/)
.map((item) => item.trim())
.filter((item) => item !== "");
chromeJavaResourcesToRemove = arrayString2
.split(/\s*,\s*/)
.map((item) => item.trim())
.filter((item) => item !== "");
for (const icon in data_icons) {
const i = data_icons[icon];
let type = i.type;
if (!type) type = "light";
const xmlFilePath = drawable_xml_path + icon + ".xml";
let svgPath = fa_path + type + "/" + `${i["icon-name"]}.svg`;
if (!fs.existsSync(svgPath)) {
svgPath = "custom/" + `${i["icon-name"]}.svg`;
if (!fs.existsSync(svgPath)) {
console.log(`Source doesn't exist: ${svgPath}`);
process.exit();
}
}
const svgContent = fs.readFileSync(svgPath, "utf-8");
generateXml(icon, svgContent, xmlFilePath);
}
const time = new Date();
fs.appendFileSync(
build_log_path,
`\n${time}\nCount of spotted files : ${count}\nNumber of xml icons generated : ${icount}\n=====================================\n\n\n`
);
//console.log(kiwiJavaResources);
updateContent();
} catch (error) {
console.log(error);
}