forked from gitkraken/vscode-gitlens
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generateEmojiShortcodeMap.js
92 lines (77 loc) · 2.45 KB
/
generateEmojiShortcodeMap.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
/* eslint-disable @typescript-eslint/no-var-requires */
const fs = require('fs');
const https = require('https');
const path = require('path');
async function generate() {
/**
* @type {{ [code: string]: string }}
*/
let map = Object.create(null);
// Get emoji data from https://github.com/milesj/emojibase
// https://github.com/milesj/emojibase/blob/master/packages/data/en/raw.json
await download('https://raw.githubusercontent.com/milesj/emojibase/master/packages/data/en/raw.json', 'raw.json');
/**
* @type {({ emoji: string; shortcodes: string[] })[]}
*/
// eslint-disable-next-line import/no-dynamic-require
const emojis = require(path.join(process.cwd(), 'raw.json'));
for (const emoji of emojis) {
if (emoji.shortcodes == null || emoji.shortcodes.length === 0) continue;
for (let code of emoji.shortcodes) {
if (code[0] === ':' && code[code.length - 1] === ':') {
code = code.substring(1, code.length - 2);
}
if (map[code] !== undefined) {
console.warn(code);
}
map[code] = emoji.emoji;
}
}
fs.unlink('raw.json', () => {});
// Get gitmoji data from https://github.com/carloscuesta/gitmoji
// https://github.com/carloscuesta/gitmoji/blob/master/src/data/gitmojis.json
await download(
'https://raw.githubusercontent.com/carloscuesta/gitmoji/master/src/data/gitmojis.json',
'gitmojis.json'
);
/**
* @type {({ code: string; emoji: string })[]}
*/
// eslint-disable-next-line import/no-dynamic-require
const gitmojis = require(path.join(process.cwd(), 'gitmojis.json')).gitmojis;
for (const emoji of gitmojis) {
if (emoji.code[0] === ':' && emoji.code[emoji.code.length - 1] === ':') {
emoji.code = emoji.code.substring(1, emoji.code.length - 2);
}
if (map[emoji.code] !== undefined) {
console.warn(emoji.code);
continue;
}
map[emoji.code] = emoji.emoji;
}
fs.unlink('gitmojis.json', () => {});
// Sort the emojis for easier diff checking
/**
* @type { [string, string][] }}
*/
const list = Object.entries(map);
list.sort();
map = list.reduce((m, [key, value]) => {
m[key] = value;
return m;
}, Object.create(null));
fs.writeFileSync(path.join(process.cwd(), 'src/emojis.json'), JSON.stringify(map), 'utf8');
}
function download(url, destination) {
return new Promise((resolve, reject) => {
const stream = fs.createWriteStream(destination);
https.get(url, rsp => {
rsp.pipe(stream);
stream.on('finish', () => {
stream.close();
resolve();
});
});
});
}
generate();