-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
38 lines (32 loc) · 1004 Bytes
/
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
const tinify = require("tinify");
const fs = require("fs");
const path = require("path");
const entry = "./entry";
const outDir = "./output";
const suffixs = ["png", "jpeg", "webp"];
!fs.existsSync(outDir) && fs.mkdirSync(outDir);
function setup(dir) {
const files = fs.readdirSync(dir, { withFileTypes: true });
files.forEach((item) => {
const currPath = `${dir}/${item.name}`;
const outPath = currPath.replace(entry, outDir);
if (item.isDirectory()) {
if (!fs.existsSync(outPath)) {
fs.mkdirSync(outPath);
}
return setup(currPath);
}
const suffix = path.extname(currPath).replace(".", "").toLowerCase();
if (suffixs.includes(suffix)) {
build(currPath);
}
});
}
tinify.key = "Your TinyPNG API-KEY";
function build(dir) {
// https://tinypng.com/developers/reference/nodejs
// tinify.proxy = "http://user:[email protected]:8080";
const source = tinify.fromFile(dir);
source.toFile(dir.replace(entry, outDir));
}
setup(entry);