2017-03-17 18:10:09 +01:00
|
|
|
#!/bin/env node
|
|
|
|
|
|
|
|
const fs = require('fs');
|
|
|
|
const path = require('path');
|
|
|
|
const $ = require('cheerio');
|
|
|
|
const request = require('request');
|
|
|
|
const markdown = require('./markdown');
|
|
|
|
|
|
|
|
const title = 'emoji-cheat-sheet';
|
2017-03-18 08:27:33 +01:00
|
|
|
const apiUrl = 'https://api.github.com/emojis';
|
|
|
|
const sheetUrl = 'http://www.emoji-cheat-sheet.com';
|
2017-03-17 18:10:09 +01:00
|
|
|
|
|
|
|
const outDir = path.resolve(process.cwd(), './generated');
|
|
|
|
const outFile = path.join(outDir, 'README.md');
|
|
|
|
|
|
|
|
const columnDivisions = 2;
|
|
|
|
|
2017-03-18 08:27:33 +01:00
|
|
|
const getHTML = (url) => new Promise((resolve, reject) => {
|
|
|
|
const options = { url };
|
|
|
|
if (url === apiUrl) {
|
|
|
|
Object.assign(options, {
|
|
|
|
headers: { 'User-Agent': 'https://github.com/ikatyang/emoji-cheat-sheet' },
|
2017-03-17 18:10:09 +01:00
|
|
|
});
|
2017-03-18 08:27:33 +01:00
|
|
|
}
|
|
|
|
request.get(options, (error, response, html) => {
|
|
|
|
if (error || response.statusCode !== 200) {
|
|
|
|
reject(error || `Unexpected response status code: ${response.statusCode}`);
|
2017-03-17 18:10:09 +01:00
|
|
|
} else {
|
2017-03-18 08:27:33 +01:00
|
|
|
resolve(html);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
Promise.all([getHTML(apiUrl), getHTML(sheetUrl)]).then(([apiHTML, sheetHTML]) => {
|
|
|
|
const apiEmojis = Object.keys(JSON.parse(apiHTML));
|
|
|
|
const emojiTable = {};
|
|
|
|
const $html = $.load(sheetHTML).root();
|
|
|
|
$html.find('h2').each((_, categoryElement) => {
|
|
|
|
const emojis = [];
|
|
|
|
const category = $(categoryElement).text();
|
|
|
|
$html.find(`#emoji-${category.toLowerCase()} li .name`).each((_, emojiElement) => {
|
|
|
|
const emoji = $(emojiElement).text();
|
|
|
|
const index = apiEmojis.indexOf(emoji);
|
|
|
|
if (index !== -1) {
|
|
|
|
apiEmojis.splice(index, 1);
|
|
|
|
emojis.push(emoji);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
emojiTable[category] = emojis;
|
|
|
|
});
|
|
|
|
if (apiEmojis.length > 0) {
|
|
|
|
emojiTable['Uncategorized'] = apiEmojis;
|
|
|
|
}
|
|
|
|
if (fs.existsSync(outDir)) {
|
|
|
|
if (!fs.statSync(outDir).isDirectory()) {
|
|
|
|
throw `OutDir '${outDir}' should be a directory.`;
|
2017-03-17 18:10:09 +01:00
|
|
|
}
|
2017-03-18 08:27:33 +01:00
|
|
|
} else {
|
|
|
|
fs.mkdirSync(outDir);
|
2017-03-17 18:10:09 +01:00
|
|
|
}
|
2017-03-18 08:27:33 +01:00
|
|
|
fs.writeFileSync(outFile, markdown.create({
|
|
|
|
'GitHub Emoji API': apiUrl,
|
|
|
|
'Emoji Cheat Sheet': sheetUrl,
|
|
|
|
}, title, emojiTable, columnDivisions));
|
2017-03-17 18:10:09 +01:00
|
|
|
});
|