-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcli.js
executable file
·79 lines (65 loc) · 2.16 KB
/
cli.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
#!/usr/bin/env node
const fs = require('fs')
const path = require('path')
const emojilib = require('emojilib')
const neodoc = require('neodoc')
const toIco = require('to-ico')
const open = require('open')
const render = require('./lib/render')
function findEmoji (input) {
if (/^[a-z0-9:_-]+$/.test(input)) {
const id = (input.startsWith(':') && input.endsWith(':') ? input.slice(1, -1) : input)
if (!emojilib.lib[id]) {
throw new Error(`Unknown emoji "${id}"`)
}
return emojilib.lib[id].char
}
return input
}
const usage = `
🌴 favicon-emoji
Usage:
favicon-emoji [options]
Options:
-d, --destination <value> favicon destination [default: "./favicon.ico"]
-e, --emoji <value> choose emoji [default: "✨"]
-m, --minimum create favicon with selected sizes (16x16, 32x32, 48x48)
-h, --help Output usage information
-l, --list show list of available emojis
-p, --png <value> png output path [default: "./favicon.png"]
-v, --version Output the version number
`
const args = neodoc.run(usage)
if (args['--list']) {
console.log('🕸 Opening emoji cheat sheet in browser')
open('https://www.webpagefx.com/tools/emoji-cheat-sheet/').then(() => {
process.exit(0)
})
}
if (args['--emoji'] && args['--destination']) {
const dest = path.resolve(args['--destination'])
const pngDest = path.resolve(args['--png'])
const emoji = findEmoji(args['--emoji'])
const sizes = args['--minimum'] ? [16, 32, 48] : [16, 32, 48, 64, 128, 256]
const start = Date.now()
const hrstart = process.hrtime()
Promise.resolve(emoji)
.then(char => render(char, sizes))
.then(images => {
if (pngDest) fs.writeFileSync(pngDest, images[images.length - 1])
return images
})
.then(images => toIco(images, {sizes}))
.then(buf => fs.writeFileSync(dest, buf))
.then(() => {
const end = Date.now() - start
const hrend = process.hrtime(hrstart)
console.log(`
Saved favicon.ico and favicon.png
${dest}
${pngDest}
Execution time: ${end / 1000}s
Execution time (hr): ${hrend[0]}s ${hrend[1] / 1000000}ms
`)
})
}