@tririga/tri-bundler
Version:
A tool for bundling Polymer 3 TRIRIGA UX views.
93 lines (85 loc) • 4.53 kB
JavaScript
const fs = require('fs');
const ironIconsIbmIconsDupes = [
'bookmark', 'attachment', 'cloud', 'code',
'dashboard', 'folder', 'forward', 'help',
'home', 'print', 'report', 'restore', 'save',
'search', 'settings', 'sort', 'spellcheck', 'warning',
'buildings', 'camera', 'edit', 'details', 'forum',
'email', 'filter', 'forward', 'rss-feed', 'pause',
'stop', 'traffic'
];
const ibmIconsIbmGlyphsDupes = [
'add-new', 'assets', 'backward', 'calendar', 'accessibility',
'close-cancel-error', 'details', 'edit', 'equipment',
'filter', 'floorplan', 'forward', 'home', 'location', 'maximize',
'menu', 'minimize', 'pan-zoom', 'phone-call', 'room-function',
'search', 'select-area', 'sign-out', 'tile-view', 'user', 'video',
'warning', 'watson-globe'
];
const ironIconsIbmGlyphsDupes = ['accessibility', 'forward', 'home', 'menu', 'search', 'remove', 'mail'];
const ironIconsCarbonDupes = ['devices'];
const ibmIconsCarbonDupes = ['user-profile'];
const dupImplIBMIcons = [...(new Set([...ironIconsIbmIconsDupes, ...ibmIconsIbmGlyphsDupes]))].map(icon => (`ibm:${icon}"`));
const dupImplIBMGlyphsIcons = [...ironIconsIbmGlyphsDupes, ...ibmIconsIbmGlyphsDupes].map(icon => (`ibm-glyphs:${icon}"`));
const dupImplCarbonIcons = [...ironIconsCarbonDupes, ...ibmIconsCarbonDupes].map(icon => (`carbon:${icon}"`));
const dupIronIconIbmIconsSvgTags = ironIconsIbmIconsDupes.map(icon => (`<svg version="1.1" id="${icon}"`));
const dupIronIconIbmGlyphsSvgTags = ironIconsIbmGlyphsDupes.map(icon => {
if (icon === 'mail') return `<svg id="${icon}"`;
return `<svg version="1.1" id="${icon}"`;
});
const dupIbmIconsIbmGlyphsSvgTags = ibmIconsIbmGlyphsDupes.map(icon => (`<svg version="1.1" id="${icon}"`));
const dupCarbonSvgTags = [...ironIconsCarbonDupes, ...ibmIconsCarbonDupes].map(icon => (`<svg id="${icon}"`));
const dupSvgTags = new Set([...dupIronIconIbmIconsSvgTags, ...dupIronIconIbmGlyphsSvgTags, ...dupIbmIconsIbmGlyphsSvgTags, ...dupCarbonSvgTags]);
function uniquifySvgIds(filePath) {
return new Promise((resolve) => {
fs.readFile(filePath, 'utf8', (err, data) => {
if (err) {
console.error("Failed to read the file:", err);
return;
}
let updatedData = data;
// Replace duplicated SVG IDs in the header
for (const icon of dupSvgTags) {
const secondReplacement = dupCarbonSvgTags.includes(icon) ? `${icon.slice(0, -1)}--carbon-icons"` : `${icon.slice(0, -1)}--ibm-icons-glyphs"`;
let count = 0;
updatedData = updatedData.replace(new RegExp(icon, 'g'), (match) => {
count++;
return count === 2 ? secondReplacement : match;
});
let firstReplacement = null;
if (dupIronIconIbmIconsSvgTags.includes(icon) || dupIbmIconsIbmGlyphsSvgTags.includes(icon)) {
firstReplacement = `${icon.slice(0, -1)}--ibm-icons"`;
}
else if (dupIronIconIbmGlyphsSvgTags.includes(icon)) {
firstReplacement = `${icon.slice(0, -1)}--ibm-icons-glyphs"`;
}
else if (dupCarbonSvgTags.includes(icon)) {
firstReplacement = `${icon.slice(0, -1)}--carbon-icons"`;
}
updatedData = updatedData.replace(icon, firstReplacement);
}
// Replace duplicated implementing icons
for (const icon of dupImplIBMIcons) {
const replacement = `${icon.slice(0, -1)}--ibm-icons"`;
updatedData = updatedData.replace(new RegExp(icon, 'g'), replacement);
}
for (const icon of dupImplIBMGlyphsIcons) {
const replacement = `${icon.slice(0, -1)}--ibm-icons-glyphs"`;
updatedData = updatedData.replace(new RegExp(icon, 'g'), replacement);
}
for (const icon of dupImplCarbonIcons) {
const replacement = `${icon.slice(0, -1)}--carbon-icons"`;
updatedData = updatedData.replace(new RegExp(icon, 'g'), replacement);
}
fs.writeFile(filePath, updatedData, 'utf8', (err) => {
if (err) {
console.error("Failed to write file:", err);
return;
}
console.log("Assign unique SVG IDs have been successfully updated.");
resolve();
});
});
});
}
module.exports = uniquifySvgIds;