Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/git/build/update-emoji.js
3316 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
/* eslint-disable @typescript-eslint/no-var-requires */
7
const fs = require('fs');
8
const https = require('https');
9
const path = require('path');
10
11
async function generate() {
12
/**
13
* @type {Map<string, string>}
14
*/
15
const shortcodeMap = new Map();
16
17
// Get emoji data from https://github.com/milesj/emojibase
18
// https://github.com/milesj/emojibase/
19
20
const files = ['github.raw.json'] //, 'emojibase.raw.json']; //, 'iamcal.raw.json', 'joypixels.raw.json'];
21
22
for (const file of files) {
23
await download(
24
`https://raw.githubusercontent.com/milesj/emojibase/master/packages/data/en/shortcodes/${file}`,
25
file,
26
);
27
28
/**
29
* @type {Record<string, string | string[]>}}
30
*/
31
// eslint-disable-next-line import/no-dynamic-require
32
const data = require(path.join(process.cwd(), file));
33
for (const [emojis, codes] of Object.entries(data)) {
34
const emoji = emojis
35
.split('-')
36
.map(c => String.fromCodePoint(parseInt(c, 16)))
37
.join('');
38
for (const code of Array.isArray(codes) ? codes : [codes]) {
39
if (shortcodeMap.has(code)) {
40
// console.warn(`${file}: ${code}`);
41
continue;
42
}
43
shortcodeMap.set(code, emoji);
44
}
45
}
46
47
fs.unlink(file, () => { });
48
}
49
50
// Get gitmoji data from https://github.com/carloscuesta/gitmoji
51
// https://github.com/carloscuesta/gitmoji/blob/master/src/data/gitmojis.json
52
await download(
53
'https://raw.githubusercontent.com/carloscuesta/gitmoji/master/src/data/gitmojis.json',
54
'gitmojis.json',
55
);
56
57
/**
58
* @type {({ code: string; emoji: string })[]}
59
*/
60
// eslint-disable-next-line import/no-dynamic-require
61
const gitmojis = require(path.join(process.cwd(), 'gitmojis.json')).gitmojis;
62
for (const emoji of gitmojis) {
63
if (emoji.code.startsWith(':') && emoji.code.endsWith(':')) {
64
emoji.code = emoji.code.substring(1, emoji.code.length - 2);
65
}
66
67
if (shortcodeMap.has(emoji.code)) {
68
// console.warn(`GitHub: ${emoji.code}`);
69
continue;
70
}
71
shortcodeMap.set(emoji.code, emoji.emoji);
72
}
73
74
fs.unlink('gitmojis.json', () => { });
75
76
// Sort the emojis for easier diff checking
77
const list = [...shortcodeMap.entries()];
78
list.sort();
79
80
const map = list.reduce((m, [key, value]) => {
81
m[key] = value;
82
return m;
83
}, Object.create(null));
84
85
fs.writeFileSync(path.join(process.cwd(), 'resources/emojis.json'), JSON.stringify(map), 'utf8');
86
}
87
88
function download(url, destination) {
89
return new Promise(resolve => {
90
const stream = fs.createWriteStream(destination);
91
https.get(url, rsp => {
92
rsp.pipe(stream);
93
stream.on('finish', () => {
94
stream.close();
95
resolve();
96
});
97
});
98
});
99
}
100
101
void generate();
102
103