I have the following code that exports SVG icons from Iconify JSON files:
import { promises as fs } from 'fs';
import { IconSet, exportToDirectory } from '@iconify/tools';
import { validateIconSet } from '@iconify/utils';
let iconsetName = "dashicons";
(async () => {
// Read data, parse JSON
const rawData = JSON.parse(
await fs.readFile('JSON/' + iconsetName + '.json', 'utf8')
);
// Validate icon set (optional step)
const validatedData = validateIconSet(rawData);
// Create new IconSet instance
const iconSet = new IconSet(validatedData);
// Done. Do something with icon set...
// For example, export as SVG files
await exportToDirectory(iconSet, {
target: iconsetName
})
})();
I have about 90-100 icon sets to process, so I figured I could store all names in an array and then wrap the async logic at the bottom in a for loop, allowing the script to process every icon set I have.
This is the code I tried:
import { promises as fs } from "fs";
import { IconSet, exportToDirectory } from "@iconify/tools";
import { validateIconSet } from "@iconify/utils";
let iconSets: string[] = [
"devicon-line",
"devicon-original",
"devicon-plain",
"devicon",
"ei",
"el",
"emblemicons",
"emojione-monotone",
"emojione-v1",
"emojione",
"entypo-social",
"entypo",
"eos-icons",
"ep",
"et",
"eva",
"fa-brands",
"fa-regular",
"fa-solid",
"fa",
"fa6-brands",
"fa6-regular",
"fa6-solid",
"fad",
"fe",
"feather",
"file-icons",
"flag",
"flagpack",
"flat-color-icons",
"flat-ui",
"fluent-emoji-flat",
"fluent-emoji-high-contrast",
"fluent-emoji",
"fluent-mdl2",
"fluent",
"fontelico",
"fontisto",
"formkit",
"foundation",
"fxemoji",
"gala",
"game-icons",
"geo",
"gg",
"gis",
"gridicons",
"grommet-icons",
"guidance",
"healthicons",
"heroicons-outline",
"heroicons-solid",
"heroicons",
"humbleicons",
"ic",
"icomoon-free",
"icon-park-outline",
"icon-park-solid",
"icon-park-twotone",
"icon-park",
"iconamoon",
"iconoir",
"icons8",
"il",
"ion",
"iwwa",
"jam",
"la",
"line-md",
"logos",
"ls",
"lucide",
"majesticons",
"maki",
"map",
"material-symbols",
"mdi-light",
"mdi",
"medical-icon",
"memory",
"mi",
"mingcute",
"mono-icons",
"nimbus",
"nonicons",
"noto-v1",
"noto",
"octicon",
"oi",
"ooui",
"openmoji",
"pajamas",
"pepicons-pencil",
"pepicons-pop",
"pepicons-print",
"pepicons",
"ph",
"pixelarticons",
"prime",
"ps",
"quill",
"radix-icons",
"raphael",
"ri",
"si-glyph",
"simple-icons",
"simple-line-icons",
"skill-icons",
"solar",
"streamline-emojis",
"streamline",
"subway",
"svg-spinners",
"system-uicons",
"tabler",
"teenyicons",
"topcoat",
"twemoji",
"typcn",
"uil",
"uim",
"uis",
"uit",
"uiw",
"vaadin",
"vs",
"vscode-icons",
"websymbol",
"whh",
"wi",
"wpf",
"zmdi",
"zondicons"
];
const forLoop = async (_: any) => {
console.log("Start");
for (let index = 0; index < iconSets.length; index++) {
const setName = iconSets[index];
(async () => {
// Read data, parse JSON
const rawData = JSON.parse(
await fs.readFile("JSON/" + setName + ".json", "utf8")
);
// Validate icon set (optional step)
const validatedData = validateIconSet(rawData);
// Create new IconSet instance
const iconSet = new IconSet(validatedData);
// Done. Do something with icon set...
// For example, export as SVG files
await exportToDirectory(iconSet, {
target: setName
});
})();
}
console.log("End");
};
But I’m positive I’m doing something horribly wrong (I’m new to TS and JS). And the script doesn’t work.
How do I get this script to process my JSON files sequentially and save everything out at once?