-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.mjs
More file actions
51 lines (43 loc) · 1.64 KB
/
main.mjs
File metadata and controls
51 lines (43 loc) · 1.64 KB
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
import crypto from 'node:crypto';
import fs from 'node:fs';
import path from 'node:path';
import { program } from 'commander';
import Identicon from 'identicon.js';
import hexRgb from 'hex-rgb';
program.argument('<identifer>');
program.option('-s --size <number>', 'icon size', parseNumber, 64);
program.option('-o --output <path>', 'output path');
program.option('--foreground <hex>', 'foreground color', parseColor);
program.option('--background <hex>', 'background color', parseColor);
program.option('--svg', 'use SVG instead of PNG');
program.version('0.2.1', '-v --version');
program.showHelpAfterError();
program.parse();
const [identifer] = program.args;
const { size, output, foreground, background, svg } = program.opts();
const format = svg ? 'svg' : 'png';
const hash = crypto.createHash('sha256').update(identifer).digest('hex');
const identicon = new Identicon(hash, { size, format, foreground, background });
const mimeType = svg ? 'image/svg+xml' : 'image/png';
const dataUri = `data:${mimeType};base64,${identicon.toString()}`;
if (output) {
const resp = await fetch(dataUri);
fs.mkdirSync(path.dirname(output), { recursive: true });
fs.writeFileSync(output, Buffer.from(await resp.arrayBuffer()));
} else {
console.log(dataUri);
}
function parseNumber(str) {
const num = Number(str);
if (Number.isNaN(num)) {
program.error(`error: option '-s --size <number>' argument must be a number`);
}
return num;
}
function parseColor(str) {
try {
return str && ((rgba) => [rgba.red, rgba.green, rgba.blue, rgba.alpha * 0xff])(hexRgb(str));
} catch (error) {
program.error(`error: option '${this.flags}' ${error.message}`);
}
}