You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

91 lines
2.3 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. import * as fs from 'fs';
  2. import * as https from 'https';
  3. import * as path from 'path';
  4. import LZString from 'lz-string';
  5. async function generate() {
  6. /**
  7. * @type {Map<string, string>}
  8. */
  9. const shortcodeMap = new Map();
  10. // Get emoji data from https://github.com/milesj/emojibase
  11. // https://github.com/milesj/emojibase/
  12. const files = ['github.raw.json', 'emojibase.raw.json']; //, 'iamcal.raw.json', 'joypixels.raw.json'];
  13. for (const file of files) {
  14. /**
  15. * @type {Record<string, string | string[]>}}
  16. */
  17. const data = await downloadToJSON(
  18. `https://raw.githubusercontent.com/milesj/emojibase/master/packages/data/en/shortcodes/${file}`,
  19. );
  20. for (const [emojis, codes] of Object.entries(data)) {
  21. const emoji = emojis
  22. .split('-')
  23. .map(c => String.fromCodePoint(parseInt(c, 16)))
  24. .join('');
  25. for (const code of Array.isArray(codes) ? codes : [codes]) {
  26. if (shortcodeMap.has(code)) {
  27. // console.warn(`${file}: ${code}`);
  28. continue;
  29. }
  30. shortcodeMap.set(code, emoji);
  31. }
  32. }
  33. }
  34. // Get gitmoji data from https://github.com/carloscuesta/gitmoji
  35. // https://github.com/carloscuesta/gitmoji/blob/master/src/data/gitmojis.json
  36. /**
  37. * @type {({ code: string; emoji: string })[]}
  38. */
  39. const gitmojis = (
  40. await downloadToJSON(
  41. 'https://raw.githubusercontent.com/carloscuesta/gitmoji/master/packages/gitmojis/src/gitmojis.json',
  42. )
  43. ).gitmojis;
  44. for (const emoji of gitmojis) {
  45. if (emoji.code.startsWith(':') && emoji.code.endsWith(':')) {
  46. emoji.code = emoji.code.substring(1, emoji.code.length - 2);
  47. }
  48. if (shortcodeMap.has(emoji.code)) {
  49. // console.warn(`GitHub: ${emoji.code}`);
  50. continue;
  51. }
  52. shortcodeMap.set(emoji.code, emoji.emoji);
  53. }
  54. // Sort the emojis for easier diff checking
  55. const list = [...shortcodeMap.entries()];
  56. list.sort();
  57. const map = list.reduce((m, [key, value]) => {
  58. m[key] = value;
  59. return m;
  60. }, Object.create(null));
  61. fs.writeFileSync(
  62. path.join(process.cwd(), 'src/emojis.generated.ts'),
  63. `export const emojis = '${LZString.compressToBase64(JSON.stringify(map))}';\n`,
  64. 'utf8',
  65. );
  66. }
  67. function downloadToJSON(url) {
  68. return new Promise(resolve => {
  69. https.get(url, rsp => {
  70. rsp.setEncoding('utf8');
  71. let data = '';
  72. rsp.on('data', chunk => (data += chunk));
  73. rsp.on('end', () => resolve(JSON.parse(data)));
  74. });
  75. });
  76. }
  77. void generate();