-
Notifications
You must be signed in to change notification settings - Fork 3
/
build.js
184 lines (175 loc) · 5.69 KB
/
build.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// Helpers
const fs = require('fs-extra');
const path = require('path');
const sharp = require('sharp');
const svgtofont = require('svgtofont');
// Constants
const icons = require('./icons.json');
const iconSizes = [32, 64, 128, 256, 512, 1024];
const iconPngPath = path.resolve(__dirname, 'png-16');
const iconSvgPath = path.resolve(__dirname, 'svg');
const fontsPath = path.resolve(__dirname, 'fonts');
const distPath = path.resolve(__dirname, 'dist');
let declaration = `declare interface PixelSvgData {
svg: string;
colors: string[];
}
declare interface PixelIcon {
tags: string[];
data: PixelSvgData
}
declare module '@pixel/icons' {
const value: { [icon in PixelIconList]: PixelIcon; };
export default value;
}`;
function rimraf(folder) {
if (fs.existsSync(folder)) {
fs.readdirSync(folder).forEach((entry) => {
const currentPath = path.join(folder, entry);
if (fs.lstatSync(currentPath).isDirectory()) {
rimraf(currentPath);
} else {
fs.unlinkSync(currentPath);
}
});
fs.rmdirSync(folder);
}
}
module.exports.rimraf = rimraf;
function generateList(list) {
let markdownContent = '# Icon list\n\n';
markdownContent += '<table>\n\t<tbody>\n';
markdownContent += '\t\t<tr>\n';
let count = 0;
for (const icon in list) {
count++;
markdownContent += `\t\t\t<td align="center"><img src="./png-128/${icon}.png" width="100px"/><br/><span>${icon}</span><br/><span>[${icons[icon].tags.join(', ')}]</span></td>\n`;
if (count > 7) {
markdownContent += '\t\t</tr>\n';
markdownContent += '\t\t<tr>\n';
count = 0;
}
}
markdownContent += '\t</tbody>\n</table>\n';
fs.writeFileSync(path.resolve(__dirname, 'ICONS.md'), markdownContent);
}
async function createIcon(icon) {
const src = path.resolve(iconPngPath, icon);
for (const size of iconSizes) {
const destPath = path.resolve(__dirname, `png-${size}/${icon}`);
await sharp(src).resize(size, size, { kernel: 'nearest' }).toFile(destPath);
}
const iSvgFilePath = path.resolve(__dirname, `svg/${icon.replace(/.png/g, '.svg')}`);
return await generateSvg(src, iSvgFilePath);
}
async function generateSvg(src, dest) {
const factor = Math.pow(2, 4);
return new Promise((resolve, reject) => {
sharp(src).raw()
.toBuffer((err, buffer, img) => {
if (err) { reject(err); }
const svgData = {
colors: []
};
let svgContent = `<svg xmlns="http://www.w3.org/2000/svg" shape-rendering="crispEdges" viewBox="0 0 ${img.height * factor} ${img.height * factor}" width="${img.width * factor}" height="${img.height * factor}">\n`;
const colors = [];
for (let i = 0; i < img.height; i++) {
for (let j = 0; j < img.width; j++) {
const idx = (img.width * i + j) << 2;
if (buffer[idx + 3] !== 0) {
const style = `rgba(${buffer[idx]},${buffer[idx + 1]},${buffer[idx + 2]},${buffer[idx + 3]})`;
let index = colors.indexOf(style);
if (index === -1) {
colors.push(style);
index = colors.indexOf(style);
}
svgContent += `\t<rect fill="${style}" x="${j * factor}" y="${i * factor}" width="${factor}" height="${factor}"/>\n`;
}
}
}
for (let k = 0; k < colors.length; k++) {
svgData.colors.push(colors[k]);
}
svgContent += '</svg>';
svgData.svg = svgContent;
fs.writeFileSync(dest, svgContent);
resolve({ svgIcon: dest, svgData: svgData });
});
});
}
async function main() {
try {
for (const size of iconSizes) {
fs.emptyDirSync(path.resolve(__dirname, `png-${size}`));
}
fs.emptyDirSync(iconSvgPath);
// Building icons
const svgFiles = [];
const finalIconList = {};
const iconList = Object.keys(icons);
const iconFiles = fs.readdirSync(iconPngPath);
for (let i = iconFiles.length; i--;) {
const iconName = iconFiles[i].slice(0, -4);
if (iconList.indexOf(iconName) === -1) {
console.warn(`${iconName} seems to not be used.`);
iconFiles.splice(i, 1);
} else {
finalIconList[iconName] = icons[iconName];
}
}
for (let i = iconList.length; i--;) {
const lookout = `${iconList[i]}.png`;
if (iconFiles.indexOf(lookout) === -1) {
console.warn(`${iconList[i]} does not exist.`);
iconList.splice(i, 1);
}
}
declaration += '\n\ndeclare enum PixelIconList {';
for (const icon in finalIconList) {
const { svgIcon, svgData } = await createIcon(`${icon}.png`);
finalIconList[icon].data = svgData;
svgFiles.push(svgIcon);
declaration += `\n\t'${icon}' = '${icon}',`;
}
declaration = declaration.slice(0, -1);
declaration += '\n}';
// Building icon list
if (fs.existsSync(distPath)) {
rimraf(distPath);
}
fs.mkdirSync(distPath);
fs.writeFileSync(path.resolve(distPath, 'icons.d.ts'), declaration);
fs.writeFileSync(path.resolve(distPath, 'icons.json'), JSON.stringify(finalIconList, null, 2));
// Building docs
generateList(finalIconList);
// Building fonts
if (!fs.existsSync(fontsPath)) {
fs.mkdirSync(fontsPath);
} else {
rimraf(fontsPath);
}
svgtofont({
src: iconSvgPath,
dist: fontsPath,
fontName: 'PixelIcons',
classNamePrefix: 'pi',
css: {
fontSize: '16px'
},
svgicons2svgfont: {
fontHeight: 1000,
normalize: true
},
website: {
title: 'Pixel Icons',
logo: path.resolve(__dirname, 'svg/github.svg')
}
});
} catch (error) {
console.error(error);
}
}
module.exports.main = main;
if (require.main === module) {
main();
}