Skip to content

Instantly share code, notes, and snippets.

@sliptrixx
Created June 13, 2023 07:09
Show Gist options
  • Save sliptrixx/93271e9343f0e5ed468679cadc59178b to your computer and use it in GitHub Desktop.
Save sliptrixx/93271e9343f0e5ed468679cadc59178b to your computer and use it in GitHub Desktop.
This script is used extract fluentui emoji icon svgs from microsoft/fluentui-emoji into a typescript file for use in Astro
// File: extract_fluentui_icon.ts
// Description: This script is used to extract fluentui emoji icons svgs into a typsecript class as a single file so
// that astro components can use it. Place this script in the root folder of the cloned
// microsoft/fluentui-emoji GitHub repository and use tsc to compile + node to execute the created javascript
// Author: sliptrixx (for Hibzz.Games)
// Date: 2023-06-13
// libraries
import * as fs from 'fs';
// Configurations
const OUT_FILENAME : string = "fluentui_icons.ts";
const ASSET_PATH : string = "assets/";
const ICON_TYPE : string = "Color/";
// Make calls to the functions
ExecuteExtraction();
// print with option to replace current line
function Print(text : string, replace : boolean) {
if(replace) {
process.stdout.clearLine(0);
process.stdout.cursorTo(0);
}
process.stdout.write(text);
}
// execute the extraction process
function ExecuteExtraction() {
let current_icon : number = 0; // the index of the current icon being processed
let dictionary : { [Key: string] : string} = {};
// let the user know that we are currently scanning
Print("scanning directories...", false);
// scan the directories
let directories = fs.readdirSync(ASSET_PATH);
let total_count = directories.length;
Print(`Found ${total_count} icons...\n`, true);
Print(`Beginning extraction...\n`, false);
// for each of the directory read the svg file inside the ICON_TYPE folder
directories.forEach(directory => {
// increment the counter
current_icon++;
Print(`${current_icon}/${total_count} done`, true);
// string together the directory name that hosts the svg icon
let icon_dir = `${ASSET_PATH}${directory}/${ICON_TYPE}`;
// by default it should be in the ICON_TYPE folder, but if it's not, the fallback is Default/ICON_TYPE
if(!fs.existsSync(icon_dir)) {
icon_dir = `${ASSET_PATH}${directory}/Default/${ICON_TYPE}`;
}
// the first file in it should be the svg
let svg_file_dir = fs.readdirSync(icon_dir);
let svg_file_name = svg_file_dir[0];
let svg_file_path = `${icon_dir}${svg_file_name}`;
// read the contents of this file
let svg_content = fs.readFileSync(svg_file_path, {encoding: 'utf8', flag: 'r'});
let processed_svg_content = ProcessSVGContent(svg_content);
// the key to access the content is just the directory name (with spaces replaced by dashes)
let key : string = directory.replace(' ', '-');
dictionary[key] = processed_svg_content;
});
// done reading, let the user know that.
Print('\nDone reading all icons. Preparing to write...\n', false);
// loop through all content in the dictionary and prepare to write to the file
let output : string = `export const fluentui_icons: {[key: string] : string} = {\n`;
for(const [key, value] of Object.entries(dictionary)) {
output += `'${key}' : '${value}',\n`;
}
// done filing the content, end it with a closing brace
output += "}";
// write this to the output file
fs.writeFileSync(OUT_FILENAME, output);
// let the user know that the process is done
Print('Done!', false);
}
// minify svg and remove base svg wrapper
function ProcessSVGContent(svg : string) : string {
// remove the base svg wrapper... we need just the path inside the svg
svg = svg.substring(svg.indexOf('>') + 1, svg.lastIndexOf('<'));
// minify it by removing new lines
svg = svg.replace(/(\r\n|\n|\r)/gm, "");
return svg;
}
@sliptrixx
Copy link
Author

MIT License

Copyright (c) 2023 hibzz.games

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment