-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathpack-icons.js
105 lines (89 loc) · 2.47 KB
/
pack-icons.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import { mkdirSync, readFileSync, writeFileSync } from 'fs';
import { createRequire } from 'module';
import { dirname } from 'path';
// Installation: npm install --save-dev @iconify/utils @iconify/json
import { getIcons, minifyIconSet, stringToIcon } from '@iconify/utils';
const require = createRequire(import.meta.url);
// File to save bundle to
const target = 'src/lib/icons.ts';
// Icons to bundle, array
const iconsToBundle = [
'mdi:account',
'mdi:account-group',
'mdi:analytics',
'mdi:add',
'mdi:beta',
'mdi:briefcase-outline',
'mdi:check-circle-outline',
'mdi:check',
'mdi:chevron-left',
'mdi:chevron-right',
'mdi:close',
'mdi:close-circle-outline',
'mdi:credit-card-plus-outline',
'mdi:cog-outline',
'mdi:filter',
'mdi:folder-outline',
'mdi:format-list-bulleted',
'mdi:information-outline',
'mdi:help',
'mdi:login',
'mdi:logout',
'mdi:open-in-new',
'mdi:pencil',
'mdi:plus',
'mdi:shape-outline',
'mdi:swap-horizontal',
'mdi:swap-vertical',
'mdi:translate',
];
// Organize icons by prefix
const sortedIcons = organizeIconsList(iconsToBundle);
// Load icons data
let output = "import { addCollection } from '@iconify/svelte';\n\n";
Object.keys(sortedIcons).forEach((prefix) => {
const iconsList = sortedIcons[prefix];
// Load icon set
const filename = require.resolve(`@iconify/json/json/${prefix}.json`);
const iconSet = JSON.parse(readFileSync(filename, 'utf8'));
// Get data for all icons as string
const data = getIcons(iconSet, iconsList, true);
if (!data) {
throw new Error(`Could not get icons for "${prefix}" icon set.`);
}
if (data.not_found?.length) {
throw new Error(`Could not find icons in "${prefix}" icon set: ${data.not_found.join(', ')}`);
}
minifyIconSet(data);
output += 'addCollection(' + JSON.stringify(data) + ');\n';
});
// Save to file
const dir = dirname(target);
try {
mkdirSync(dir, {
recursive: true,
});
} catch (err) {
//
}
writeFileSync(target, output, 'utf8');
console.log(`Saved ${target} (${output.length} bytes)`);
/**
* Sort icon names by prefix
*/
function organizeIconsList(icons) {
const sorted = Object.create(null);
icons.forEach((icon) => {
const item = stringToIcon(icon);
if (!item) {
return;
}
const prefix = item.prefix;
const prefixList = sorted[prefix] ? sorted[prefix] : (sorted[prefix] = []);
const name = item.name;
if (prefixList.indexOf(name) === -1) {
prefixList.push(name);
}
});
return sorted;
}