emoji-picker-element-data/build.js

100 lines
3.8 KiB
JavaScript
Raw Normal View History

2023-06-11 05:16:35 +02:00
import process from 'node:process'
import fs from 'node:fs'
import path from 'node:path'
2020-11-03 22:48:08 +01:00
2021-12-20 22:20:32 +01:00
const IGNORE_FOLDERS = ['meta', 'messages', 'versions']
2020-11-03 22:48:08 +01:00
const emojiKeys = new Set([
2021-12-20 22:20:32 +01:00
'label',
2020-11-03 22:48:08 +01:00
'emoji',
'emoticon',
'group',
'order',
'shortcodes',
'skins',
'tags',
'version'
])
const skinKeys = new Set(['tone', 'emoji', 'version'])
2020-11-03 23:06:31 +01:00
async function main () {
2020-11-03 22:48:08 +01:00
const emojibaseDir = path.resolve('./node_modules', 'emojibase-data')
const langs = fs.readdirSync(emojibaseDir)
.filter(file => !IGNORE_FOLDERS.includes(file))
.filter(file => fs.statSync(path.resolve(emojibaseDir, file)).isDirectory())
for (const lang of langs) {
2023-06-11 05:16:35 +02:00
fs.rmSync(path.resolve('./', lang), { recursive: true, force: true })
2020-11-03 22:48:08 +01:00
const shortcodeFiles = fs.readdirSync(path.resolve(emojibaseDir, lang, 'shortcodes'))
2021-12-20 22:20:32 +01:00
.filter(_ => _.endsWith('.json'))
2020-11-03 22:48:08 +01:00
const baseData = JSON.parse(fs.readFileSync(path.resolve(emojibaseDir, lang, 'data.json'), 'utf8'))
for (const shortcodeFile of shortcodeFiles) {
2021-12-20 22:20:32 +01:00
const fullShortcodeFilename = path.resolve(emojibaseDir, lang, 'shortcodes', shortcodeFile)
const shortcodeData = JSON.parse(fs.readFileSync(fullShortcodeFilename, 'utf8'))
2020-11-03 22:48:08 +01:00
2020-11-03 23:00:54 +01:00
const outData = baseData
.filter(emoji => 'group' in emoji) // skip odd emoji with no group, e.g. regional indicator (1F1E6)
.map(emoji => {
const outEmoji = {}
// normalize shortcodes to an array of strings
// sometimes these don't exist for the given shortcodes file though (e.g. too-new emoji)
let shortcodes = shortcodeData[emoji.hexcode]
if (shortcodes) {
if (!Array.isArray(shortcodes)) {
shortcodes = [shortcodes]
}
outEmoji.shortcodes = shortcodes
2020-11-03 22:48:08 +01:00
}
2020-11-03 23:00:54 +01:00
// trim keys we don't need
for (const key of Object.keys(emoji)) {
if (emojiKeys.has(key)) {
2021-12-20 22:20:32 +01:00
if (key === 'label') {
// Rename to annotation for backwards compat for pre-v7
// https://github.com/milesj/emojibase/blob/master/packages/data/CHANGELOG.md#700---2021-10-15
// TODO: breaking change to rename this
outEmoji.annotation = emoji[key]
} else if (key === 'emoticon') {
// In case of an array, just take one string for backwards compat for pre-v7
// https://github.com/milesj/emojibase/blob/master/packages/data/CHANGELOG.md#700---2021-10-15
// TODO: breaking change to allow arrays as well as strings
if (Array.isArray(emoji[key])) {
// These are usually just variations on the capitalization, with the capitalized version last,
// which in my opinion usually looks best (e.g. "XD" instead of "xD", "XO" instead of "xo")
outEmoji[key] = emoji[key][emoji[key].length - 1]
} else {
outEmoji[key] = emoji[key]
}
} else if (key === 'skins') {
2020-11-06 18:59:35 +01:00
const skins = []
for (const skin of emoji.skins) {
const outSkin = {}
for (const skinKey of Object.keys(skin)) {
if (skinKeys.has(skinKey)) {
outSkin[skinKey] = skin[skinKey]
}
2020-11-03 23:00:54 +01:00
}
2020-11-06 18:59:35 +01:00
skins.push(outSkin)
2020-11-03 23:00:54 +01:00
}
outEmoji.skins = skins
} else {
outEmoji[key] = emoji[key]
}
2020-11-03 22:48:08 +01:00
}
}
2020-11-03 23:06:31 +01:00
return outEmoji
2020-11-03 23:00:54 +01:00
})
2020-11-03 22:48:08 +01:00
const outPath = path.resolve('./', lang, shortcodeFile.replace('.json', ''))
2023-06-11 05:16:35 +02:00
fs.mkdirSync(outPath, { recursive: true })
2020-11-03 23:00:54 +01:00
fs.writeFileSync(path.resolve(outPath, 'data.json'), JSON.stringify(outData), 'utf8')
2020-11-03 22:48:08 +01:00
}
}
}
main().catch(err => {
console.error(err)
process.exit(1)
2020-11-03 23:06:31 +01:00
})