From 6ecf908ded7ec776dc7a730e79eb6d88e8d74794 Mon Sep 17 00:00:00 2001 From: Karsa Date: Mon, 30 May 2022 17:52:11 +0200 Subject: [PATCH] Added migration from tags to icons and vice versa --- icons.json | 4 +-- package.json | 2 ++ scripts/helpers.js | 11 +++++++++ scripts/migrateIconsToTags.js | 43 ++++++++++++++++++++++++++++++++ scripts/migrateTagsToIcons.js | 46 +++++++++++++++++++++++++++++++++++ 5 files changed, 103 insertions(+), 3 deletions(-) create mode 100644 scripts/migrateIconsToTags.js create mode 100644 scripts/migrateTagsToIcons.js diff --git a/icons.json b/icons.json index 88c063afd..5ae07710a 100644 --- a/icons.json +++ b/icons.json @@ -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" } } -} +} \ No newline at end of file diff --git a/package.json b/package.json index 7fd1c814a..001a34d85 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/scripts/helpers.js b/scripts/helpers.js index 8514d1636..fd31ae8a9 100644 --- a/scripts/helpers.js +++ b/scripts/helpers.js @@ -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; +}; diff --git a/scripts/migrateIconsToTags.js b/scripts/migrateIconsToTags.js new file mode 100644 index 000000000..000a3612d --- /dev/null +++ b/scripts/migrateIconsToTags.js @@ -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, '..')); diff --git a/scripts/migrateTagsToIcons.js b/scripts/migrateTagsToIcons.js new file mode 100644 index 000000000..2806e49a5 --- /dev/null +++ b/scripts/migrateTagsToIcons.js @@ -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, '..'));