forked from microsoft/fluentui-emoji
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.cjs
159 lines (140 loc) · 4.2 KB
/
index.cjs
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
const fs = require('fs/promises');
const path = require('path');
const emojiFolder = './assets';
const emojiDataFile = 'emojiData.json';
// Valid emoji skintones
const skintones = [
'Default',
'Light',
'Medium-Light',
'Medium',
'Medium-Dark',
'Dark',
]
// All valid Fluent Emoji styles
const styles = [
'3D',
'Color',
'Flat',
'High Contrast',
]
// Retrieve command-line arguments
const args = process.argv.slice(2);
let emojiData = []
// Get all the folders in a directory
async function getFolders(dir) {
return (await fs.readdir(dir, { withFileTypes: true }))
.filter((dir) => dir.isDirectory())
.map((dir) => dir.name);
}
// Get all the files in a directory
async function getFiles(dir) {
return (await fs.readdir(dir, { withFileTypes: true }))
.map((dir) => dir.name);
}
// Get all the emojis
async function generateEmojiData() {
async function getEmojiData(name) {
const meta = JSON.parse(
(await fs.readFile(path.join(__dirname, emojiFolder, name, 'metadata.json'))).toString()
);
const { unicodeSkintones, unicode, glyph } = meta;
if (unicodeSkintones) {
let hasSkinToneFolder;
try {
await fs.stat(path.join(__dirname, emojiFolder, name, 'Default'));
hasSkinToneFolder = true;
} catch {
hasSkinToneFolder = false;
}
for (let i = 0; i < unicodeSkintones.length; i++) {
const skintoneCode = unicodeSkintones[i];
const skintone = skintones[i];
const glyphMod = String.fromCodePoint(
...skintoneCode.split(' ').map((c) => parseInt(c, 16))
);
const folder = `${emojiFolder}/${name}${hasSkinToneFolder ? '/' + skintone : ''}`;
const emojiStyles = await getFolders(path.join(__dirname, folder));
let images = {}
for (let index = 0; index < emojiStyles.length; index++) {
const style = emojiStyles[index];
const image = await getFiles(path.join(__dirname, folder, style));
images[style] = image
}
emojiData.push({
glyph: glyphMod,
unicode,
folder: folder.replace(emojiFolder, ""),
images,
emojiStyles
})
}
} else {
const folder = `${emojiFolder}/${name}`;
const emojiStyles = await getFolders(path.join(__dirname, folder));
let images = {}
for (let index = 0; index < emojiStyles.length; index++) {
const style = emojiStyles[index];
const image = await getFiles(path.join(__dirname, folder, style));
images[style] = image
}
emojiData.push({
glyph,
unicode,
folder: folder.replace(emojiFolder, ""),
images,
emojiStyles
})
}
}
const emojiFolders = await getFolders(emojiFolder);
for (let i = 0; i < emojiFolders.length; i++) {
await getEmojiData(emojiFolders[i]);
console.log(`Processed ${i + 1} / ${emojiFolders.length}`);
}
// Save emojiData to json file
await fs.writeFile(path.join(__dirname, emojiDataFile), JSON.stringify(emojiData))
console.log(`Generated ${emojiDataFile}`)
}
// Check if "--generate" argument is provided
// if so regenerate emoji data
if (args.includes("--generate")) {
generateEmojiData();
}
// Load emoji data from file
async function loadEmojiData() {
emojiData = JSON.parse(
(await fs.readFile(path.join(__dirname, emojiDataFile))).toString()
)
}
async function getEmoji(key, q, style) {
await loadEmojiData();
// Even if this is valid that does not mean that emoji has that style
if (!styles.includes(style)) {
console.log('Invalad style');
return;
}
const data = emojiData.find(item => item[key] == q);
if (!data) {
console.log('Emoji not found');
return;
}
const folder = `${data.folder}/${style}`;
const image = data.images[style];
if (image.length == 1) {
return `${folder}/${image}`;
} else {
console.log('Unexpected folder contents');
return;
}
}
// Get image by glyph
async function fromGlyph(glyph, style) {
return await getEmoji('glyph', glyph, style);
}
// Get image by unicode
async function fromCode(code, style) {
code = code.toLowerCase();
return await getEmoji('unicode', code, style);
}
module.exports = { fromGlyph, fromCode }