Added migration from tags to icons and vice versa

This commit is contained in:
Karsa
2022-05-30 17:52:11 +02:00
parent f623ac6362
commit 6ecf908ded
5 changed files with 103 additions and 3 deletions
+1 -3
View File
@@ -2651,7 +2651,6 @@
"remove",
"song",
"subtract",
"remove",
"delete"
],
"categories": []
@@ -3742,7 +3741,6 @@
"tags": [
"security",
"secure",
"security",
"insecure"
],
"categories": []
@@ -4884,4 +4882,4 @@
"icon": "cloud-sun"
}
}
}
}
+2
View File
@@ -37,6 +37,8 @@
"generate:supersprite": "babel-node ./scripts/generateSuperSVG.js",
"optimize": "esbuild ./scripts/optimizeSvgs.js --bundle --platform=node | node",
"addtags": "babel-node ./scripts/addMissingKeysToTags.js --presets @babel/env",
"tags2icons": "babel-node ./scripts/migrateTagsToIcons.js --presets @babel/env",
"icons2tags": "babel-node ./scripts/migrateIconsToTags.js --presets @babel/env",
"generate:changelog": "babel-node ./scripts/generateChangelog.js --presets @babel/env"
},
"devDependencies": {
+11
View File
@@ -139,3 +139,14 @@ export const hasDuplicatedChildren = children => {
(key, index) => index === hashedKeys.findIndex(childKey => childKey === key),
);
};
/**
* @param {array} a
* @param {array} b
* @returns {array}
*/
export const mergeArrays = (a, b) => {
a = a.concat(b);
a = a.filter((i, p) => a.indexOf(i) === p);
return a;
};
+43
View File
@@ -0,0 +1,43 @@
import path from 'path';
import tags from '../tags.json';
import icons from '../icons.json';
import { readSvgDirectory, writeFile, mergeArrays } from './helpers';
const ICONS_DIR = path.resolve(__dirname, '../icons');
console.log(`Read all icons`);
const svgFiles = readSvgDirectory(ICONS_DIR);
const iconNames = svgFiles.map(icon => icon.split('.')[0]);
const iconList = iconNames
.map(iconName => ({
name: iconName,
icon: icons.icons[iconName] || { tags: [] },
tags: tags[iconName] || [],
}))
.sort((a, b) => {
const nameA = a.name;
const nameB = b.name;
if (nameA < nameB) {
return -1;
}
if (nameA > nameB) {
return 1;
}
// names must be equal
return 0;
});
const newTags = iconList.reduce((acc, { name, icon, tags }) => {
acc[name] = mergeArrays(icon.tags, tags);
return acc;
}, {});
const tagsContent = JSON.stringify(newTags, null, 2);
writeFile(tagsContent, 'tags.json', path.resolve(__dirname, '..'));
+46
View File
@@ -0,0 +1,46 @@
import path from 'path';
import tags from '../tags.json';
import icons from '../icons.json';
import { readSvgDirectory, writeFile, mergeArrays } from './helpers';
const ICONS_DIR = path.resolve(__dirname, '../icons');
console.log(`Read all icons`);
const svgFiles = readSvgDirectory(ICONS_DIR);
const iconNames = svgFiles.map(icon => icon.split('.')[0]);
const iconList = iconNames
.map(iconName => ({
name: iconName,
icon: icons.icons[iconName] || { tags: [], categories: [] },
tags: tags[iconName] || [],
}))
.sort((a, b) => {
const nameA = a.name;
const nameB = b.name;
if (nameA < nameB) {
return -1;
}
if (nameA > nameB) {
return 1;
}
// names must be equal
return 0;
});
const newIcons = icons;
newIcons.icons = iconList.reduce((acc, { name, icon, tags }) => {
acc[name] = icon;
acc[name].tags = mergeArrays(icon.tags, tags);
return acc;
}, {});
const iconsContent = JSON.stringify(newIcons, null, 2);
writeFile(iconsContent, 'icons.json', path.resolve(__dirname, '..'));