|
| 1 | +const axios = require('axios'); |
| 2 | +const fs = require('fs'); |
| 3 | +const path = require('path'); |
| 4 | + |
| 5 | +const filePaths = { |
| 6 | + emojiMarkdown: path.resolve(process.cwd(), 'docs', 'emoji.md'), |
| 7 | + emojiJS: path.resolve( |
| 8 | + process.cwd(), |
| 9 | + 'src', |
| 10 | + 'core', |
| 11 | + 'render', |
| 12 | + 'emojify-data.js' |
| 13 | + ), |
| 14 | +}; |
| 15 | + |
| 16 | +async function getEmojiData() { |
| 17 | + const emojiDataURL = 'https://api.github.com/emojis'; |
| 18 | + const response = await axios.get(emojiDataURL); |
| 19 | + const baseURL = Object.values(response.data) |
| 20 | + .find(url => /unicode\//) |
| 21 | + .split('unicode/')[0]; |
| 22 | + const data = { ...response.data }; |
| 23 | + |
| 24 | + // Remove base URL from emoji URLs |
| 25 | + Object.entries(data).forEach( |
| 26 | + ([key, value]) => (data[key] = value.replace(baseURL, '')) |
| 27 | + ); |
| 28 | + |
| 29 | + return { |
| 30 | + baseURL, |
| 31 | + data, |
| 32 | + }; |
| 33 | +} |
| 34 | + |
| 35 | +function writeEmojiPage(emojiData) { |
| 36 | + const emojiPage = |
| 37 | + (fs.existsSync(filePaths.emojiMarkdown) && |
| 38 | + fs.readFileSync(filePaths.emojiMarkdown, 'utf8')) || |
| 39 | + `<!-- START -->\n\n<!-- END -->`; |
| 40 | + const emojiRegEx = /(<!--\s*START.*-->\n)([\s\S]*)(\n<!--\s*END.*-->)/; |
| 41 | + const emojiMatch = emojiPage.match(emojiRegEx); |
| 42 | + const emojiMarkdownStart = emojiMatch[1].trim(); |
| 43 | + const emojiMarkdown = emojiMatch[2].trim(); |
| 44 | + const emojiMarkdownEnd = emojiMatch[3].trim(); |
| 45 | + const newEmojiMarkdown = Object.keys(emojiData.data) |
| 46 | + .reduce( |
| 47 | + (preVal, curVal) => |
| 48 | + (preVal += `:${curVal}: ` + '`' + `:${curVal}:` + '`' + '\n\n'), |
| 49 | + '' |
| 50 | + ) |
| 51 | + .trim(); |
| 52 | + |
| 53 | + if (emojiMarkdown !== newEmojiMarkdown) { |
| 54 | + const newEmojiPage = emojiPage.replace( |
| 55 | + emojiMatch[0], |
| 56 | + `${emojiMarkdownStart}\n${newEmojiMarkdown}\n${emojiMarkdownEnd}` |
| 57 | + ); |
| 58 | + |
| 59 | + fs.writeFileSync(filePaths.emojiMarkdown, newEmojiPage); |
| 60 | + console.info(`- Created new file: ${filePaths.emojiMarkdown}`); |
| 61 | + } else { |
| 62 | + console.info(`- No changes to file: ${filePaths.emojiMarkdown}`); |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +function writeEmojiJS(emojiData) { |
| 67 | + const emojiJS = |
| 68 | + fs.existsSync(filePaths.emojiJS) && |
| 69 | + fs.readFileSync(filePaths.emojiJS, 'utf8'); |
| 70 | + const newEmojiJS = `export default ${JSON.stringify(emojiData, {}, 2)}`; |
| 71 | + |
| 72 | + if (!emojiJS || emojiJS !== newEmojiJS) { |
| 73 | + fs.writeFileSync(filePaths.emojiJS, newEmojiJS); |
| 74 | + console.info(`- Created new file: ${filePaths.emojiJS}`); |
| 75 | + } else { |
| 76 | + console.info(`- No changes to file: ${filePaths.emojiJS}`); |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +(async () => { |
| 81 | + console.log('Build emoji'); |
| 82 | + |
| 83 | + try { |
| 84 | + const emojiData = await getEmojiData(); |
| 85 | + |
| 86 | + writeEmojiPage(emojiData); |
| 87 | + writeEmojiJS(emojiData); |
| 88 | + } catch (e) { |
| 89 | + console.error(e); |
| 90 | + } |
| 91 | +})(); |
0 commit comments