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.

98 regels
2.5 KiB

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