2018-12-18 20:45:49 +00:00
|
|
|
import path from 'path'
|
|
|
|
import fs from 'fs'
|
2019-01-19 23:06:25 +00:00
|
|
|
import { promisify } from 'util'
|
2021-07-05 03:19:04 +00:00
|
|
|
import { LOCALE } from '../src/routes/_static/intl.js'
|
|
|
|
import { getIntl, trimWhitespace } from './getIntl.js'
|
2018-12-18 20:45:49 +00:00
|
|
|
|
2021-07-05 03:19:04 +00:00
|
|
|
const __dirname = path.dirname(new URL(import.meta.url).pathname)
|
2019-01-19 23:06:25 +00:00
|
|
|
const readFile = promisify(fs.readFile)
|
2020-06-29 06:12:14 +00:00
|
|
|
const writeFile = promisify(fs.writeFile)
|
2018-12-18 20:45:49 +00:00
|
|
|
|
2020-12-19 04:02:36 +00:00
|
|
|
// Try 'en-US' first, then 'en' if that doesn't exist
|
|
|
|
const PREFERRED_LOCALES = [LOCALE, LOCALE.split('-')[0]]
|
|
|
|
|
|
|
|
// emojibase seems like the most "neutral" shortcodes, but cldr is available in every language
|
|
|
|
const PREFERRED_SHORTCODES = ['emojibase', 'cldr']
|
|
|
|
|
|
|
|
async function getEmojiI18nFile (locale, shortcode) {
|
|
|
|
const filename = path.resolve(__dirname,
|
|
|
|
'../node_modules/emoji-picker-element-data',
|
|
|
|
locale,
|
|
|
|
shortcode,
|
|
|
|
'data.json')
|
|
|
|
try {
|
|
|
|
return JSON.parse(await readFile(filename, 'utf8'))
|
|
|
|
} catch (err) { /* ignore */ }
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getFirstExistingEmojiI18nFile () {
|
|
|
|
for (const locale of PREFERRED_LOCALES) {
|
|
|
|
for (const shortcode of PREFERRED_SHORTCODES) {
|
|
|
|
const json = await getEmojiI18nFile(locale, shortcode)
|
|
|
|
if (json) {
|
|
|
|
return json
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-12 02:40:24 +00:00
|
|
|
async function buildEmojiI18nFile () {
|
2020-12-19 04:02:36 +00:00
|
|
|
const json = await getFirstExistingEmojiI18nFile()
|
|
|
|
|
|
|
|
if (!json) {
|
|
|
|
throw new Error(`Couldn't find i18n data for locale ${LOCALE}. Is it supported in emoji-picker-element-data?`)
|
|
|
|
}
|
|
|
|
|
2020-06-29 06:12:14 +00:00
|
|
|
await writeFile(
|
2020-12-19 04:02:36 +00:00
|
|
|
path.resolve(__dirname, `../static/emoji-${LOCALE}.json`),
|
2020-06-29 06:12:14 +00:00
|
|
|
JSON.stringify(json),
|
|
|
|
'utf8'
|
|
|
|
)
|
2018-12-18 20:45:49 +00:00
|
|
|
}
|
|
|
|
|
2021-04-12 02:40:24 +00:00
|
|
|
async function buildManifestJson () {
|
|
|
|
const template = await readFile(path.resolve(__dirname, '../src/build/manifest.json'), 'utf8')
|
|
|
|
// replace {@intl.foo}
|
|
|
|
const output = template
|
|
|
|
.replace(/{intl\.([^}]+)}/g, (match, p1) => trimWhitespace(getIntl(p1)))
|
|
|
|
|
|
|
|
await writeFile(
|
|
|
|
path.resolve(__dirname, '../static/manifest.json'),
|
|
|
|
JSON.stringify(JSON.parse(output)), // minify json
|
|
|
|
'utf8'
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
async function main () {
|
|
|
|
await Promise.all([
|
|
|
|
buildEmojiI18nFile(),
|
|
|
|
buildManifestJson()
|
|
|
|
])
|
|
|
|
}
|
|
|
|
|
2018-12-18 20:45:49 +00:00
|
|
|
main().catch(err => {
|
|
|
|
console.error(err)
|
|
|
|
process.exit(1)
|
|
|
|
})
|