Skip to content

Instantly share code, notes, and snippets.

@nucklehead
Last active May 20, 2024 19:04
Show Gist options
  • Save nucklehead/b568dc13d01b18b902c524754a7c9cd4 to your computer and use it in GitHub Desktop.
Save nucklehead/b568dc13d01b18b902c524754a7c9cd4 to your computer and use it in GitHub Desktop.
Script to clean unused icons in ionic
#!/bin/env node
let path = require('path'), fs=require('fs');
function fromDir(startPath, filter, callback){
if (!fs.existsSync(startPath)){
console.log("no dir ",startPath);
return;
}
let files = fs.readdirSync(startPath);
for (let i = 0; i < files.length; i++) {
let filename = path.join(startPath,files[i]);
let stat = fs.lstatSync(filename);
if (stat.isDirectory()) {
fromDir(filename, filter, callback); //recurse
}
else if (filter.test(filename)) {
callback(filename, files[i])
}
}
}
function justPrint(filePath, fileName){
console.log('-- found: ', fileName.split('.')[0]);
}
let iconNames = []
function addTolist(filePath, fileName) {
iconNames.push({
iconName: fileName.split('.')[0],
filePath,
used: false
})
}
function checkIfUnsused(filePath, fileName) {
let data = fs.readFileSync(filePath)
iconNames.forEach((icon, index, iconList) => {
if (data.includes(`["name","${icon.iconName}"]`) || // for Angular 8 / view engine
data.includes(`"name","${icon.iconName}"`) || // for Angular 9+ / ivy
data.includes(`name:"${icon.iconName}"`) ||
data.includes(`icon:"${icon.iconName}"`) ||
data.includes(`"backButtonIcon","${icon.iconName}"`)) { // for stenciljs
console.log('--- icon used', icon.iconName)
iconList[index].used = true;
}
})
}
fromDir('www/svg',/\.svg$/, justPrint);
fromDir('www/svg',/\.svg$/, addTolist);
fromDir('www',/\.(js|css|html)$/, checkIfUnsused);
iconNames.forEach((icon) => {
if(!icon.used){
console.log('--- will delete unused icon', icon.iconName)
fs.unlink(icon.filePath, (err) => {
if (err) {
console.error(err);
}
})
}
})
@ramikhafagi96
Copy link

I know it's been a long time but thank you @nucklehead and to all contributors to this script, kudos to you all 🙏🏼

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