-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprepareFlags.ts
29 lines (22 loc) · 1011 Bytes
/
prepareFlags.ts
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
import { join } from 'path';
import { fileURLToPath } from 'url';
import { dirname } from 'path';
// import svg2img from 'svg2img';
import sharp from 'sharp';
import { readdirSync, writeFileSync, readFileSync } from 'fs';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const flagsDir = join(__dirname, 'node_modules', 'flag-icons', 'flags', '4x3');
const outputDir = join(__dirname, 'static', 'flags');
const svgFiles = readdirSync(flagsDir).filter((f) => f.endsWith('.svg'));
for (const file of svgFiles) {
const svgContent = readFileSync(join(flagsDir, file), 'utf-8');
const outputPath = join(outputDir, file.replace('.svg', '.png'));
// https://github.com/fuzhenn/node-svg2img/issues/86
// svg2img(svgContent, (error, buffer) => {
// writeFileSync(outputPath, buffer);
// });
const png = await sharp(Buffer.from(svgContent)).resize(1000, 750).png().toBuffer();
writeFileSync(outputPath, png);
console.info(`Converted ${file} to ${outputPath}`);
}