-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
95 lines (79 loc) · 2.25 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
87
88
89
90
91
92
93
94
95
const fs = require('fs');
const exec = require('child_process').execSync;
const config = {
lettersPath: './Letters/',
inputPath: './src/',
outputPath: './dist/',
};
async function genTextsFromFile() {
const textDir = await fs.promises
.readdir(config.lettersPath)
.catch(err => console.error(err));
let letters = '';
let num = 0;
for (let i = 0; i < textDir.length; i++) {
const textFile = textDir[i];
let textFilePath = config.lettersPath + textFile;
const letter = await fs.promises
.readFile(textFilePath, 'utf-8')
.catch(err => console.error(err));
letters += letter;
num++;
if (num === textDir.length) {
return letters;
}
}
}
async function getSrcFonts() {
const inputDir = await fs.promises
.readdir(config.inputPath)
.catch(err => console.error(err));
return inputDir.filter(fontFile => {
return (
fs.statSync(config.inputPath + fontFile).isFile() &&
/.*\.otf$/.test(fontFile)
);
});
}
const subset = async () => {
const [text, srcFonts] = await Promise.all([
genTextsFromFile(),
getSrcFonts(),
]);
const tmpTextFile = 'tmpTextFile.txt';
await fs.promises
.writeFile(tmpTextFile, text)
.catch(err => console.error(err));
if (!fs.existsSync(config.outputPath)) {
fs.mkdirSync(config.outputPath);
}
srcFonts.forEach((fontFile, fontIndex) => {
const reg = /(.*)(?:\.([^.]+$))/;
const fontName = fontFile.match(reg)[1];
const extensions = ['ttf', 'woff', 'woff2'];
extensions.forEach((ext, extIndex) => {
const flavorOpt =
ext === 'woff' || ext === 'woff2' ? `--flavor=${ext} --with-zopfli --desubroutinize` : '';
const command = `pyftsubset ./${
config.inputPath
}${fontFile} --text-file=./${tmpTextFile} --layout-features='*' --output-file=./${
config.outputPath
}${fontName}.min.${ext} --no-hinting ${flavorOpt}`;
try {
exec(command);
} catch (e) {
console.error(err);
} finally {
if (
fontIndex + 1 >= srcFonts.length &&
extIndex + 1 >= extensions.length
) {
fs.unlink(tmpTextFile, err => {
if (err) throw err;
});
}
}
});
});
};
module.exports = subset();