2024-12-24 01:49:08 +08:00
|
|
|
#!/usr/bin/env node
|
2025-03-04 03:12:53 +01:00
|
|
|
// @ts-check
|
2024-12-24 01:49:08 +08:00
|
|
|
/**
|
|
|
|
* @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';
|
2025-03-07 01:35:59 +08:00
|
|
|
import {search} from '@inquirer/prompts';
|
|
|
|
import {search as fuzzySearch} from 'fast-fuzzy';
|
2025-05-27 19:22:37 +08:00
|
|
|
import {getIconSlug, getIconsData} from '../sdk.mjs';
|
2024-12-24 01:49:08 +08:00
|
|
|
import {writeIconsData} from './utils.js';
|
|
|
|
|
2025-03-17 00:26:10 +08:00
|
|
|
process.exitCode = 1;
|
2025-03-07 01:35:59 +08:00
|
|
|
process.on('uncaughtException', (error) => {
|
|
|
|
if (error instanceof Error && error.name === 'ExitPromptError') {
|
|
|
|
process.stdout.write('\nAborted\n');
|
2025-03-06 04:05:36 +01:00
|
|
|
process.exit(1);
|
2025-03-07 01:35:59 +08:00
|
|
|
} else {
|
|
|
|
throw error;
|
2025-03-06 04:05:36 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2025-05-27 19:22:37 +08:00
|
|
|
const rootDirectory = path.resolve(import.meta.dirname, '..');
|
2025-03-07 01:35:59 +08:00
|
|
|
const svgFilesDirectory = path.resolve(rootDirectory, 'icons');
|
|
|
|
|
2024-12-24 01:49:08 +08:00
|
|
|
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},
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2025-03-07 01:35:59 +08:00
|
|
|
const found = await search({
|
|
|
|
message: 'Search for an icon to remove:',
|
2024-12-24 01:49:08 +08:00
|
|
|
async source(input) {
|
2025-03-07 01:35:59 +08:00
|
|
|
if (!input) return [];
|
|
|
|
return fuzzySearch(input, icons, {
|
2024-12-24 01:49:08 +08:00
|
|
|
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`);
|
2025-03-17 00:26:10 +08:00
|
|
|
process.exit(0);
|