-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
86 lines (64 loc) · 2.5 KB
/
index.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
const fs = require('fs');
const path = require('path')
const isEqual = require('lodash.isequal');
const emojiList = require('emojis-list');
const SPECIAL_SYMBOLS = [0xFE0F, 0x200D];
const args = process.argv;
const DIR_SRC = path.resolve('./', args[2]);
const DIR_DEST = path.resolve('./', args[3]);
const CUT = args[4] === 'cut';
const emojiUnicodePoints = emojiList.map((code) => [...code].map((point) => point.codePointAt(0)));
const omitSymbols = (code) => !SPECIAL_SYMBOLS.includes(code);
function main({DIR_SRC, DIR_DEST, CUT}) {
function convertFiles() {
return fs.readdir(DIR_SRC, (err, files) => {
console.time('Converted in')
files.forEach(file => {
const [name, ext] = file.split('.'); // '1f3c3-1f3ff-2642.png' -> ['1f3c3-1f3ff-2642', '.png']
if (!name || !ext) return;
const points = name
.split('-') // '1f3c3-1f3ff-2642' -> ['1f3c3', '1f3ff', '2642']
.map(code => parseInt(code, 16)) // ['1f3c3', '1f3ff', '2642'] -> [127939, 127999, 9794]
.filter(omitSymbols); // [127939, 127999, 9794]
const equal = (codes) => isEqual(codes, points);
const filtredEmojis = emojiUnicodePoints // [..., [127939, 127999, 8205, 9794, 65039], ...]
// [..., [127939, 127999, 9794], ...]
.filter((codes) => equal(codes.filter(omitSymbols)))
if (filtredEmojis.length === 1) {
copy({file, pointsEmoji: filtredEmojis[0], DIR_SRC, DIR_DEST, CUT})
} else {
console.log(`Not supported in Emoji 12.0 or something went wrong with: ${file}`)
}
});
emojiUnicodePoints.forEach((emojiCode) => {
const emoji = String.fromCodePoint(...emojiCode)
const fileName = `${DIR_DEST}/${emoji}`;
const exist = fs.existsSync(`${fileName}.png`) || fs.existsSync(`${fileName}.svg`)
if (!exist) {
console.log(`Not found file for emoji: ${emoji}`)
}
})
console.timeEnd('Converted in')
});
};
function copy({file, pointsEmoji}) {
const [name, ext] = file.split('.');
const emoji = String.fromCodePoint(...pointsEmoji);
const filePath = path.resolve(DIR_SRC, file);
const emojiPath = path.resolve(DIR_DEST, `${emoji}.${ext}`)
fs.copyFile(filePath, emojiPath, fs.constants.COPYFILE_EXCL, (err) => {
if (err) {
console.log({emoji, pointsEmoji})
throw err;
}
if (CUT) {
fs.unlinkSync(filePath)
}
});
return;
}
convertFiles()
}
module.exports = {
main
}