1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const fs_1 = require("fs");
|
4 | const path_1 = require("path");
|
5 | const fs_extra_1 = require("fs-extra");
|
6 | const lodash_1 = require("lodash");
|
7 | const async = require("async");
|
8 | const cssBuilders = require("./css-builders");
|
9 | const parse_1 = require("./parse");
|
10 | const customizations_1 = require("./customizations");
|
11 | const nconf = require.main.require('nconf');
|
12 | const winston = require.main.require('winston');
|
13 | const db = require.main.require('./src/database');
|
14 | const plugins = require.main.require('./src/plugins');
|
15 | exports.assetsDir = path_1.join(__dirname, `../emoji`);
|
16 | const linkDirs = (sourceDir, destDir, callback) => {
|
17 | const type = (process.platform === 'win32') ? 'junction' : 'dir';
|
18 | fs_1.symlink(sourceDir, destDir, type, callback);
|
19 | };
|
20 | exports.tableFile = path_1.join(exports.assetsDir, 'table.json');
|
21 | exports.aliasesFile = path_1.join(exports.assetsDir, 'aliases.json');
|
22 | exports.asciiFile = path_1.join(exports.assetsDir, 'ascii.json');
|
23 | exports.charactersFile = path_1.join(exports.assetsDir, 'characters.json');
|
24 | exports.categoriesFile = path_1.join(exports.assetsDir, 'categories.json');
|
25 | exports.packsFile = path_1.join(exports.assetsDir, 'packs.json');
|
26 | function build(callback) {
|
27 | winston.verbose('[emoji] Building emoji assets');
|
28 | async.waterfall([
|
29 |
|
30 | (next) => plugins.fireHook('filter:emoji.packs', { packs: [] }, next),
|
31 |
|
32 | ({ packs }, next) => {
|
33 | const filtered = packs.filter((pack) => {
|
34 | if (pack && pack.id && pack.name && pack.mode && pack.dictionary && pack.path) {
|
35 | return true;
|
36 | }
|
37 | winston.warn('[emoji] pack invalid', pack.path || pack.id);
|
38 | return false;
|
39 | });
|
40 | winston.verbose('[emoji] Loaded packs', filtered.map(pack => pack.id).join(', '));
|
41 | async.series([
|
42 | cb => fs_extra_1.remove(exports.assetsDir, cb),
|
43 | cb => fs_extra_1.mkdirp(exports.assetsDir, cb),
|
44 | ], (err) => next(err, filtered));
|
45 | },
|
46 | (packs, next) => {
|
47 | customizations_1.getCustomizations((err, custs) => next(err, packs, custs));
|
48 | },
|
49 | (packs, customizations, next) => {
|
50 | const table = {};
|
51 | const aliases = {};
|
52 | const ascii = {};
|
53 | const characters = {};
|
54 | const categoriesInfo = {};
|
55 | const packsInfo = [];
|
56 | packs.forEach((pack) => {
|
57 | packsInfo.push({
|
58 | name: pack.name,
|
59 | id: pack.id,
|
60 | attribution: pack.attribution,
|
61 | license: pack.license,
|
62 | });
|
63 | Object.keys(pack.dictionary).forEach((key) => {
|
64 | const name = key.toLowerCase();
|
65 | const emoji = pack.dictionary[key];
|
66 | if (!table[name]) {
|
67 | table[name] = {
|
68 | name,
|
69 | character: emoji.character || `:${name}:`,
|
70 | image: emoji.image || '',
|
71 | pack: pack.id,
|
72 | aliases: emoji.aliases || [],
|
73 | keywords: emoji.keywords || [],
|
74 | };
|
75 | }
|
76 | if (!characters[emoji.character]) {
|
77 | characters[emoji.character] = name;
|
78 | }
|
79 | if (emoji.aliases) {
|
80 | emoji.aliases.forEach((alias) => {
|
81 | const a = alias.toLowerCase();
|
82 | if (!aliases[a]) {
|
83 | aliases[a] = name;
|
84 | }
|
85 | });
|
86 | }
|
87 | if (emoji.ascii) {
|
88 | emoji.ascii.forEach((str) => {
|
89 | if (!ascii[str]) {
|
90 | ascii[str] = name;
|
91 | }
|
92 | });
|
93 | }
|
94 | const categories = emoji.categories || ['other'];
|
95 | categories.forEach((category) => {
|
96 | categoriesInfo[category] = categoriesInfo[category] || [];
|
97 | categoriesInfo[category].push(name);
|
98 | });
|
99 | });
|
100 | });
|
101 | Object.keys(categoriesInfo).forEach((category) => {
|
102 | categoriesInfo[category] = lodash_1.uniq(categoriesInfo[category]);
|
103 | });
|
104 | customizations.emojis.forEach((emoji) => {
|
105 | const name = emoji.name.toLowerCase();
|
106 | table[name] = {
|
107 | name,
|
108 | character: `:${name}:`,
|
109 | pack: 'customizations',
|
110 | keywords: [],
|
111 | image: emoji.image,
|
112 | aliases: emoji.aliases,
|
113 | };
|
114 | emoji.aliases.forEach((alias) => {
|
115 | const a = alias.toLowerCase();
|
116 | if (!aliases[a]) {
|
117 | aliases[a] = name;
|
118 | }
|
119 | });
|
120 | emoji.ascii.forEach((str) => {
|
121 | if (!ascii[str]) {
|
122 | ascii[str] = name;
|
123 | }
|
124 | });
|
125 | categoriesInfo.custom = categoriesInfo.custom || [];
|
126 | categoriesInfo.custom.push(name);
|
127 | });
|
128 | customizations.adjuncts.forEach((adjunct) => {
|
129 | const name = adjunct.name;
|
130 | if (!table[name]) {
|
131 | return;
|
132 | }
|
133 | table[name] = Object.assign({}, table[name], { aliases: table[name].aliases.concat(adjunct.aliases) });
|
134 | adjunct.aliases.forEach(alias => aliases[alias] = name);
|
135 | adjunct.ascii.forEach(str => ascii[str] = name);
|
136 | });
|
137 | async.parallel([
|
138 |
|
139 | (cb) => {
|
140 | const css = packs.map(pack => cssBuilders[pack.mode](pack)).join('\n');
|
141 | fs_1.writeFile(path_1.join(exports.assetsDir, 'styles.css'), `${css}\n.emoji-customizations {` +
|
142 | 'display: inline-block;' +
|
143 | 'height: 23px;' +
|
144 | 'margin-top: -1px;' +
|
145 | 'margin-bottom: -1px;' +
|
146 | '}', { encoding: 'utf8' }, cb);
|
147 | },
|
148 |
|
149 | cb => fs_1.writeFile(exports.tableFile, JSON.stringify(table), cb),
|
150 | cb => fs_1.writeFile(exports.aliasesFile, JSON.stringify(aliases), cb),
|
151 | cb => fs_1.writeFile(exports.asciiFile, JSON.stringify(ascii), cb),
|
152 | cb => fs_1.writeFile(exports.charactersFile, JSON.stringify(characters), cb),
|
153 | cb => fs_1.writeFile(exports.categoriesFile, JSON.stringify(categoriesInfo), cb),
|
154 | cb => fs_1.writeFile(exports.packsFile, JSON.stringify(packsInfo), cb),
|
155 |
|
156 | cb => async.each(packs, (pack, next) => {
|
157 | const dir = path_1.join(exports.assetsDir, pack.id);
|
158 | if (pack.mode === 'images') {
|
159 | linkDirs(path_1.resolve(pack.path, pack.images.directory), dir, next);
|
160 | }
|
161 | else if (pack.mode === 'sprite') {
|
162 | const filename = path_1.basename(pack.sprite.file);
|
163 | async.series([
|
164 | cb => fs_extra_1.mkdirp(dir, cb),
|
165 | cb => fs_extra_1.copy(path_1.resolve(pack.path, pack.sprite.file), path_1.join(dir, filename), cb),
|
166 | ], next);
|
167 | }
|
168 | else {
|
169 | const fontFiles = [
|
170 | pack.font.eot,
|
171 | pack.font.svg,
|
172 | pack.font.woff,
|
173 | pack.font.ttf,
|
174 | pack.font.woff2,
|
175 | ].filter(Boolean);
|
176 | async.series([
|
177 | cb => fs_extra_1.mkdirp(dir, cb),
|
178 | cb => async.each(fontFiles, (file, next) => {
|
179 | const filename = path_1.basename(file);
|
180 | fs_extra_1.copy(path_1.resolve(pack.path, file), path_1.join(dir, filename), next);
|
181 | }, cb),
|
182 | ], next);
|
183 | }
|
184 | }, cb),
|
185 |
|
186 | cb => linkDirs(path_1.join(nconf.get('upload_path'), 'emoji'), path_1.join(exports.assetsDir, 'customizations'), cb),
|
187 | ], next);
|
188 | },
|
189 | (results, next) => {
|
190 | parse_1.clearCache();
|
191 | next();
|
192 | },
|
193 | ], callback);
|
194 | }
|
195 | exports.default = build;
|
196 |
|
\ | No newline at end of file |