Skip to content

Commit

Permalink
refactor: update
Browse files Browse the repository at this point in the history
  • Loading branch information
ikatyang committed Apr 9, 2019
1 parent 19b858e commit 90641f0
Show file tree
Hide file tree
Showing 19 changed files with 2,486 additions and 2,325 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
coverage/
node_modules/
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
language: node_js

node_js:
- stable
- "8"

script:
- yarn run lint
- yarn run test -- --verbose --coverage
- yarn lint
- yarn test --ci

after_success:
- if [ "$TRAVIS_EVENT_TYPE" = "cron" ]; then bash ./scripts/deploy.sh; fi
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2017 Ika
Copyright (c) Ika <ikatyang@gmail.com> (https://github.com/ikatyang)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
21 changes: 0 additions & 21 deletions jest.json

This file was deleted.

40 changes: 22 additions & 18 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,38 @@
"name": "emoji-cheat-sheet",
"version": "0.0.0-dev",
"private": true,
"author": "ikatyang",
"license": "MIT",
"repository": "https://github.com/ikatyang/emoji-cheat-sheet",
"repository": "ikatyang/emoji-cheat-sheet",
"homepage": "https://github.com/ikatyang/emoji-cheat-sheet#readme",
"author": {
"name": "Ika",
"email": "ikatyang@gmail.com",
"url": "https://github.com/ikatyang"
},
"license": "MIT",
"scripts": {
"lint": "tslint -p ./tsconfig.json",
"test": "jest -c ./jest.json",
"generate": "ts-node ./scripts/generate.ts ./README.md"
"lint": "tslint -p . --type-check",
"test": "jest",
"generate": "node ./scripts/generate.js > ./README.md"
},
"dependencies": {
"cheerio": "^0.22.0",
"request": "^2.82.0"
"dedent": "^0.7.0",
"request": "^2.88.0"
},
"devDependencies": {
"@types/cheerio": "0.22.11",
"@types/jest": "21.1.10",
"@types/dedent": "0.7.0",
"@types/jest": "24.0.11",
"@types/node": "8.10.44",
"@types/request": "2.48.1",
"jest": "21.2.1",
"jest-playback": "1.0.1",
"prettier": "1.14.3",
"prettier-config-ikatyang": "1.1.1",
"ts-jest": "21.2.4",
"ts-node": "4.1.0",
"tslint": "5.14.0",
"tslint-config-ikatyang": "2.5.1",
"tslint-config-prettier": "1.18.0",
"jest": "24.7.1",
"jest-playback": "2.0.2",
"prettier": "1.16.4",
"tslint": "5.15.0",
"tslint-plugin-prettier": "2.0.1",
"typescript": "2.9.2"
"typescript": "3.4.2"
},
"engines": {
"node": ">= 8"
}
}
2 changes: 1 addition & 1 deletion scripts/deploy.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
yarn run generate
yarn generate

git config --global user.name ikatyang-bot
git config --global user.email ikatyang+bot@gmail.com
Expand Down
153 changes: 153 additions & 0 deletions scripts/generate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
const $ = require("cheerio");
const dedent = require("dedent");
const request = require("request");
const packageJson = require("../package.json");

const apiUrl = "https://api.github.com/emojis";
const sheetUrl = "http://www.emoji-cheat-sheet.com";

const columns = 2;

/**
* @typedef {string} EmojiId
* @typedef {{ [category: string]: EmojiId[] }} EmojiData
*/

const tocName = "Table of Contents";
const topName = "top";
const topHref = "#table-of-contents";

async function generateCheatSheet() {
return buildTable(await getData());
}

/**
* @returns {Promise<EmojiData>}
*/
async function getData() {
const apiHtml = await fetchHtml(apiUrl);
const sheetHtml = await fetchHtml(sheetUrl);

const apiJson = /** @type {Record<EmojiId, string>} */ (JSON.parse(apiHtml));

const emojiIds = Object.keys(apiJson);
const emojiData = /** @type {EmojiData} */ ({});

const $html = $.load(sheetHtml).root();
$html.find("h2").each((_, $category) => {
const localEmojiIds = /** @type {string[]} */ ([]);
const category = $($category).text();
$html
.find(`#emoji-${category.toLowerCase()} li .name`)
.each((_, $emoji) => {
const emoji = $($emoji).text();
const index = emojiIds.indexOf(emoji);
if (index !== -1) {
localEmojiIds.push(...emojiIds.splice(index, 1));
}
});
emojiData[category] = localEmojiIds;
});

if (emojiIds.length !== 0) {
emojiData["Uncategorized"] = emojiIds;
}

return emojiData;
}

/**
* @param {EmojiData} emojiData
* @returns {string}
*/
function buildTable(emojiData) {
const travisRepoUrl = `https://travis-ci.org/${packageJson.repository}`;
const travisBadgeUrl = `${travisRepoUrl}.svg?branch=master`;
const categories = Object.keys(emojiData);
return dedent(`
# ${packageJson.name}
[![build](${travisBadgeUrl})](${travisRepoUrl})
This cheat sheet is automatically generated from ${[
["GitHub Emoji API", apiUrl],
["Emoji Cheat Sheet", sheetUrl]
]
.map(([siteName, siteUrl]) => `[${siteName}](${siteUrl})`)
.join(" and ")}.
## ${tocName}
${categories
.map(category => `- [${category}](#${category.toLowerCase()})`)
.join("\n")}
${categories
.map(category => {
const emojis = emojiData[category];
return dedent(`
### ${category}
${buildTableHead()}
${buildTableContent(emojis)}
`);
})
.join("\n".repeat(2))}
`);
}

/**
* @param {string[]} emojis
*/
function buildTableContent(emojis) {
let tableContent = "";
for (let i = 0; i < emojis.length; i += columns) {
const rowEmojis = emojis.slice(i, i + columns);
while (rowEmojis.length < columns) {
rowEmojis.push("");
}
tableContent += `| [${topName}](${topHref}) |${rowEmojis
.map(x => (x.length !== 0 ? ` :${x}: | \`:${x}:\` ` : " | "))
.join("|")}|\n`;
}
return tableContent;
}

function buildTableHead() {
return dedent(`
| |${" ico | emoji |".repeat(columns)}
| - |${" --- | ----- |".repeat(columns)}
`);
}

/**
* @param {string} url
* @returns {Promise<string>}
*/
async function fetchHtml(url) {
return new Promise((resolve, reject) => {
const options = /** @type {request.Options} */ ({ url });
if (url === apiUrl) {
options.headers = {
"User-Agent": "https://github.com/ikatyang/emoji-cheat-sheet"
};
}
request.get(options, (error, response, html) => {
if (!error && response.statusCode === 200) {
resolve(html);
} else {
reject(
error
? error
: `Unexpected response status code: ${response.statusCode}`
);
}
});
});
}

if (require.main === /** @type {unknown} */ (module)) {
generateCheatSheet().then(cheatSheet => console.log(cheatSheet));
} else {
module.exports = generateCheatSheet;
}
Loading

0 comments on commit 90641f0

Please sign in to comment.