Export all Iconify icons as SVG
This example shows how to generate SVG files for all icons from @iconify/json package.
First, create a blank Node project, add @iconify/tools as a dependency.
Then create file export-files.mjs and put this content:
export-svg.mjs
mjs
import { readFile } from 'node:fs/promises';
import { downloadNPMPackage, IconSet, exportToDirectory } from '@iconify/tools';
// Directories
const cacheDir = 'cache';
const outDir = 'svg';
// Download all icon sets
console.log('Downloading latest package');
const downloaded = await downloadNPMPackage({
package: '@iconify/json',
target: cacheDir,
});
console.log('Downloaded version', downloaded.version);
// Get a list of icon sets
const list = JSON.parse(
await readFile(downloaded.contentsDir + '/collections.json', 'utf8')
);
const prefixes = Object.keys(list);
console.log('Got', prefixes.length, 'icon sets');
// Export each icon set
for (let i = 0; i < prefixes.length; i++) {
const prefix = prefixes[i];
// Read file
const data = JSON.parse(
await readFile(
downloaded.contentsDir + '/json/' + prefix + '.json',
'utf8'
)
);
// Create IconSet
const iconSet = new IconSet(data);
// Export it
console.log('Exporting', iconSet.info.name);
await exportToDirectory(iconSet, {
target: outDir + '/' + prefix,
});
}
console.log('Done');
Then run that file:
node export-svg.mjs