Skip to content

Commit

Permalink
Add remove-icon script (#12425)
Browse files Browse the repository at this point in the history
  • Loading branch information
LitoMore authored Dec 23, 2024
1 parent 03a4a38 commit 18c8d21
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@
"pretest": "npm run prepublishOnly",
"posttest": "npm run postpublish",
"get-filename": "node scripts/get-filename.js",
"add-icon-data": "node scripts/add-icon-data.js"
"add-icon-data": "node scripts/add-icon-data.js",
"remove-icon": "node scripts/remove-icon.js"
},
"engines": {
"node": ">=0.12.18"
Expand Down
47 changes: 47 additions & 0 deletions scripts/remove-icon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/usr/bin/env node
/**
* @file
* Script to remove an icon and its data.
*/

import fs from 'node:fs/promises';
import path from 'node:path';
import process from 'node:process';
import {search} from 'fast-fuzzy';
import autocomplete from 'inquirer-autocomplete-standalone';
import {getDirnameFromImportMeta, getIconSlug, getIconsData} from '../sdk.mjs';
import {writeIconsData} from './utils.js';

const __dirname = getDirnameFromImportMeta(import.meta.url);
const rootDirectory = path.resolve(__dirname, '..');
const svgFilesDirectory = path.resolve(rootDirectory, 'icons');

const iconsData = await getIconsData();
const icons = iconsData.map((icon, index) => {
const slug = getIconSlug(icon);
return {
name: `${icon.title} (${slug})`,
value: {title: icon.title, slug, index},
};
});

const found = await autocomplete({
message: 'Select an icon to remove:',
name: 'icon',
async source(input) {
if (!input) return icons;
return search(input, icons, {
keySelector: (icon) => [icon.value.title, icon.value.slug],
});
},
});

if (!found) {
console.error('No icon selected.');
process.exit(1);
}

iconsData.splice(found.index, 1);
await writeIconsData(iconsData);
await fs.unlink(path.resolve(svgFilesDirectory, `${found.slug}.svg`));
process.stdout.write(`Icon "${found.title} (${found.slug}.svg)" removed.\n`);

0 comments on commit 18c8d21

Please sign in to comment.