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
+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, '..'));