-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupdate.js
58 lines (52 loc) · 1.31 KB
/
update.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
const _ = require('lodash');
const path = require('path');
const fs = require('fs');
const https = require('https');
const randomUa = require('random-ua');
// get the latest github version
https.get('https://api.github.com/emojis', {
headers: {
'User-Agent': randomUa.generate()
}
}, res => {
let data = '';
res.on('data', chunk => {
data += chunk;
});
res.on('end', () => {
if (res.statusCode === 200) {
parseData(data);
}
});
}).on('error', err => {
console.error('Failded to download Github emojis.');
console.log(err);
});
function parseData(data) {
var json = JSON.parse(data);
if (!_.isObject(json)) {
console.log('Error parsing JSON!');
}
const latestEmojis = Object.keys(json).reduce((emojis, name) => {
emojis[name] = { src: json[name] }
const match = /\/unicode\/(\S+)\./.exec(json[name]);
if (match) {
emojis[name].codepoints = match[1].split('-');
}
return emojis;
}, {});
const emojis = _.assign({}, require('./emojis.json'), latestEmojis);
// update local backup
fs.writeFile(
path.join(__dirname, 'emojis.json'),
JSON.stringify(emojis),
err => {
if (err) {
console.warn(err);
process.exit(1);
} else {
console.log(`Update ${Object.keys(emojis).length} emojis`);
}
}
);
}