96 行
2.5 KiB

  1. /* eslint-disable @typescript-eslint/no-var-requires */
  2. const fs = require('fs');
  3. const https = require('https');
  4. const path = require('path');
  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. await download(
  15. `https://raw.githubusercontent.com/milesj/emojibase/master/packages/data/en/shortcodes/${file}`,
  16. file,
  17. );
  18. /**
  19. * @type {Record<string, string | string[]>}}
  20. */
  21. // eslint-disable-next-line import/no-dynamic-require
  22. const data = require(path.join(process.cwd(), file));
  23. for (const [emojis, codes] of Object.entries(data)) {
  24. const emoji = emojis
  25. .split('-')
  26. .map(c => String.fromCodePoint(parseInt(c, 16)))
  27. .join('');
  28. for (const code of Array.isArray(codes) ? codes : [codes]) {
  29. if (shortcodeMap.has(code)) {
  30. // console.warn(`${file}: ${code}`);
  31. continue;
  32. }
  33. shortcodeMap.set(code, emoji);
  34. }
  35. }
  36. fs.unlink(file, () => {});
  37. }
  38. // Get gitmoji data from https://github.com/carloscuesta/gitmoji
  39. // https://github.com/carloscuesta/gitmoji/blob/master/src/data/gitmojis.json
  40. await download(
  41. 'https://raw.githubusercontent.com/carloscuesta/gitmoji/master/src/data/gitmojis.json',
  42. 'gitmojis.json',
  43. );
  44. /**
  45. * @type {({ code: string; emoji: string })[]}
  46. */
  47. // eslint-disable-next-line import/no-dynamic-require
  48. const gitmojis = require(path.join(process.cwd(), 'gitmojis.json')).gitmojis;
  49. for (const emoji of gitmojis) {
  50. if (emoji.code.startsWith(':') && emoji.code.endsWith(':')) {
  51. emoji.code = emoji.code.substring(1, emoji.code.length - 2);
  52. }
  53. if (shortcodeMap.has(emoji.code)) {
  54. // console.warn(`GitHub: ${emoji.code}`);
  55. continue;
  56. }
  57. shortcodeMap.set(emoji.code, emoji.emoji);
  58. }
  59. fs.unlink('gitmojis.json', () => {});
  60. // Sort the emojis for easier diff checking
  61. const list = [...shortcodeMap.entries()];
  62. list.sort();
  63. const map = list.reduce((m, [key, value]) => {
  64. m[key] = value;
  65. return m;
  66. }, Object.create(null));
  67. fs.writeFileSync(path.join(process.cwd(), 'src/emojis.json'), JSON.stringify(map), 'utf8');
  68. }
  69. function download(url, destination) {
  70. return new Promise(resolve => {
  71. const stream = fs.createWriteStream(destination);
  72. https.get(url, rsp => {
  73. rsp.pipe(stream);
  74. stream.on('finish', () => {
  75. stream.close();
  76. resolve();
  77. });
  78. });
  79. });
  80. }
  81. void generate();