-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
30 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,35 @@ | ||
'use strict' | ||
|
||
var fs = require('fs') | ||
var tmp = require('tmp') | ||
var cheerio = require('cheerio') | ||
var Acho = require('acho') | ||
var path = require('path') | ||
var Download = require('download') | ||
var logger = new Acho({ | ||
color: true | ||
}) | ||
var got = require('got') | ||
var fs = require('fs') | ||
var log = Acho() | ||
|
||
var fromCodePoint = function (codepoint) { | ||
var code = typeof codepoint === 'string' ? parseInt(codepoint, 16) : codepoint | ||
if (code < 0x10000) return String.fromCharCode(code) | ||
code -= 0x10000 | ||
return String.fromCharCode(0xD800 + (code >> 10), 0xDC00 + (code & 0x3FF)) | ||
var CONST = { | ||
URL: 'https://twitter.github.io/twemoji/2/test/preview.html' | ||
} | ||
|
||
tmp.dir(function _tempDirCreated (err, tmpFolder, cleanup) { | ||
if (err) return logger.error(err) | ||
|
||
logger.info('Created temporal folder in ' + tmpFolder) | ||
logger.info('Downloading file...') | ||
|
||
new Download({ | ||
mode: '755', | ||
extract: true | ||
}) | ||
.get('https://github.com/twitter/twemoji/archive/gh-pages.zip') | ||
.dest(tmpFolder) | ||
.run(function (err) { | ||
if (err) return logger.error(err) | ||
logger.info('File downloaded and extracted succesful.') | ||
|
||
var folder = fs.readdirSync(tmpFolder)[0] | ||
var emojiDirectory = path.resolve(tmpFolder, folder, 'assets') | ||
function exitOnError (err) { | ||
log.error(err) | ||
process.exit(err.code || 1) | ||
} | ||
|
||
logger.info('Read the directory ' + emojiDirectory) | ||
function stringify (val) { | ||
return JSON.stringify(val, null, 2) | ||
} | ||
|
||
fs.readdir(emojiDirectory, function (err, files) { | ||
logger.info('Generating emoji keywords') | ||
if (err) return logger.error(err) | ||
var result = files.map(function (file) { | ||
file = file.split('.') | ||
return fromCodePoint(file[0]) | ||
}) | ||
got(CONST.URL, function (err, data, res) { | ||
if (err) return exitOnError(err) | ||
var $ = cheerio.load(data) | ||
|
||
var data = JSON.stringify(result, null, 2) | ||
fs.writeFile('emojis.json', data, function (err) { | ||
if (err) throw err | ||
logger.success('File saved!') | ||
}) | ||
var emojis = $('li').map(function (i, el) { | ||
var emoji = $(this).text() | ||
log.debug('detected', emoji) | ||
return emoji | ||
}).get() | ||
|
||
// cleanup(); disable to prevent remove other tmp files of the system. | ||
}) | ||
}) | ||
log.info('total', emojis.length) | ||
fs.writeFileSync('index.js', 'module.exports = ' + stringify(emojis), 'utf8') | ||
log.info('saved at index.js') | ||
}) |