-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathttf2woff2.js
39 lines (36 loc) · 1 KB
/
ttf2woff2.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
import { fileURLToPath } from "url";
import path from "path";
import { promises as fss } from "fs";
import ttf2woff2 from "ttf2woff2";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
/**
* 获得所有文件
* @param {string[]} dirPath
*/
async function* getAllFiles(...dirPath) {
const dirPaths = [...dirPath];
while (dirPaths.length) {
const dirPath = dirPaths.pop();
for (const file of (await fss.readdir(dirPath)).map((e) =>
path.join(dirPath, e)
))
if ((await fss.stat(file)).isDirectory()) dirPaths.push(file);
else yield file;
}
}
for await (const file of getAllFiles(
path.join(__dirname, "docs/public/fonts")
)) {
if (path.extname(file) === ".ttf") {
console.log("开始处理", file);
await fss.writeFile(
path.join(
path.dirname(file),
path.basename(file, path.extname(file)) + ".woff2"
),
ttf2woff2(await fss.readFile(file))
);
console.log("处理完成", file);
}
}