-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtinypng.js
56 lines (53 loc) · 1.94 KB
/
tinypng.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
const fs = require('fs')
const path = require('path')
const tinify = require('tinify')
tinify.key = "cnTL7cgyyHHyX1Rl4RGHhVlGtx5k1qCQ"
const fileDisplay = function (filePath) {
if (!filePath) return
if (/node_modules/.test(filePath)) return
fs.stat(filePath, (error, stats) => {
if (error) {
console.warn(`解析路径(${filePath})失败`)
return
}
const isFile = stats.isFile()
const isDir = stats.isDirectory()
if (isDir) {
fs.readdir(filePath, (err, files) => {
if (err) {
console.warn(`读取文件夹(${filePath})失败`)
return
}
files.forEach(fileName => {
const fileDir = path.join(filePath, fileName)
fileDisplay(fileDir)
})
})
} else if (isFile) {
if (/[\s\S]*\.(png|jpe?g)$/.test(filePath)) {
console.log(`解析图像:${filePath}`)
const source = tinify.fromFile(filePath);
source.toFile(filePath).then(() => console.log(`转化完成:${filePath}`))
}
}
})
}
const tinypng = function (filePath) {
console.log('======== 密钥验证 ========')
tinify.validate(function(err) {
if (err) throw err;
const compressionsThisMonth = tinify.compressionCount
console.log(`本月剩余转化次数:${500 - compressionsThisMonth}`)
if (compressionsThisMonth >= 500) {
console.log('本月免费次数已用完,请下月再次使用,或联系管理员更换密钥 >_<')
return
}
console.log('======== 解析开始 ========')
if (Array.isArray(filePath)) {
filePath.forEach(path => fileDisplay(path))
} else if (typeof filePath === 'string') {
fileDisplay(filePath)
}
})
}
module.exports = tinypng