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.

92 lines
2.4 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
5 years ago
5 years ago
5 years ago
  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 {{ [code: string]: string }}
  8. */
  9. let map = Object.create(null);
  10. // Get emoji data from https://github.com/milesj/emojibase
  11. // https://github.com/milesj/emojibase/blob/master/packages/data/en/raw.json
  12. await download('https://raw.githubusercontent.com/milesj/emojibase/master/packages/data/en/raw.json', 'raw.json');
  13. /**
  14. * @type {({ emoji: string; shortcodes: string[] })[]}
  15. */
  16. // eslint-disable-next-line import/no-dynamic-require
  17. const emojis = require(path.join(process.cwd(), 'raw.json'));
  18. for (const emoji of emojis) {
  19. if (emoji.shortcodes == null || emoji.shortcodes.length === 0) continue;
  20. for (let code of emoji.shortcodes) {
  21. if (code.startsWith(':') && code.endsWith(':')) {
  22. code = code.substring(1, code.length - 2);
  23. }
  24. if (map[code] !== undefined) {
  25. console.warn(code);
  26. }
  27. map[code] = emoji.emoji;
  28. }
  29. }
  30. fs.unlink('raw.json', () => {});
  31. // Get gitmoji data from https://github.com/carloscuesta/gitmoji
  32. // https://github.com/carloscuesta/gitmoji/blob/master/src/data/gitmojis.json
  33. await download(
  34. 'https://raw.githubusercontent.com/carloscuesta/gitmoji/master/src/data/gitmojis.json',
  35. 'gitmojis.json',
  36. );
  37. /**
  38. * @type {({ code: string; emoji: string })[]}
  39. */
  40. // eslint-disable-next-line import/no-dynamic-require
  41. const gitmojis = require(path.join(process.cwd(), 'gitmojis.json')).gitmojis;
  42. for (const emoji of gitmojis) {
  43. if (emoji.code.startsWith(':') && emoji.code.endsWith(':')) {
  44. emoji.code = emoji.code.substring(1, emoji.code.length - 2);
  45. }
  46. if (map[emoji.code] !== undefined) {
  47. console.warn(emoji.code);
  48. continue;
  49. }
  50. map[emoji.code] = emoji.emoji;
  51. }
  52. fs.unlink('gitmojis.json', () => {});
  53. // Sort the emojis for easier diff checking
  54. /**
  55. * @type { [string, string][] }}
  56. */
  57. const list = Object.entries(map);
  58. list.sort();
  59. map = list.reduce((m, [key, value]) => {
  60. m[key] = value;
  61. return m;
  62. }, Object.create(null));
  63. fs.writeFileSync(path.join(process.cwd(), 'src/emojis.json'), JSON.stringify(map), 'utf8');
  64. }
  65. function download(url, destination) {
  66. return new Promise((resolve, reject) => {
  67. const stream = fs.createWriteStream(destination);
  68. https.get(url, rsp => {
  69. rsp.pipe(stream);
  70. stream.on('finish', () => {
  71. stream.close();
  72. resolve();
  73. });
  74. });
  75. });
  76. }
  77. generate();