-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
115 lines (98 loc) · 3.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/env node
const path = require('path');
const fs = require('fs');
const argv = require('minimist')(process.argv.slice(2));
const request = require('request');
const glob = require('glob');
const { version } = require('./package.json');
const { getReadableSize, isImageExt } = require('./utils');
const CONFIG_PATH = path.resolve(process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE, '.tinypng');
function tips() {
console.log('请前往 https://tinypng.com/developers 获取api秘钥后,运行 tinpng --key=你的秘钥 设置');
};
function help() {
console.log(`
Usage
tinypng <path>
Example
tinypng resource/images
tinypng resource/images/logo.png
Options
-k --key Set API key
-v --version Get tinypng tool version
-r --recursive Operate directory recursively
-d --dist Download tinyify images to specific directory
`);
};
function main() {
if (argv.v || argv.version) {
console.log(version);
return;
}
if (argv.h || argv.help) {
help();
return;
}
if (argv.key && argv.key.length) {
fs.writeFileSync(CONFIG_PATH, argv.key, 'utf-8');
return;
}
if (!fs.existsSync(CONFIG_PATH)) {
return tips();
}
const config = {};
config.key = fs.readFileSync(CONFIG_PATH, 'utf-8');
if (!config.key) {
return tips();
}
const targetDir = argv._.length ? argv._ : ['.'];
let images = [];
targetDir.forEach(dir => {
if (fs.existsSync(dir)) {
if (fs.lstatSync(dir).isDirectory()) {
const currentDirImages = glob.sync(dir + (argv.r || argv.recursive ? '/**' : '') + '/*.+(png|jpg|jpeg|PNG|JPG|JPEG)');
images = images.concat(currentDirImages);
} else if (isImageExt(dir.trim())) {
images.push(dir);
}
}
});
if (!images.length) {
console.error('没有图片可以上传');
return;
}
images.forEach(image => {
console.log(`上传 ${image} 中...`)
fs.createReadStream(image).pipe(request.post('https://api.tinify.com/shrink', {
auth: {
'user': 'api',
'pass': config.key
}
}, (error, response, body) => {
if (error) {
console.error(error);
return;
}
try {
body = JSON.parse(body);
} catch (e) {
console.log(e);
return;
}
if (response.statusCode === 201) {
const { input: { size: inputSize } = {}, output: { url, size: outputSize } = {} } = body;
console.log(`${image} 压缩前:${getReadableSize(inputSize)}`);
console.log(`${image} 压缩后:${getReadableSize(outputSize)}`);
console.log(`${image} 减少了约${Math.round((inputSize - outputSize) / inputSize * 100) }% 的体积`);
const distPath = argv.d || argv.dist;
if (distPath && !fs.existsSync(path.isAbsolute(distPath) ? distPath : path.resolve(process.cwd(), distPath))) {
fs.mkdirSync(path.isAbsolute(distPath) ? distPath : path.resolve(process.cwd(), distPath), { recursive: true });
}
request.get(url).pipe(fs.createWriteStream(distPath ? path.isAbsolute(distPath) ? `${distPath}/${image}` : path.resolve(process.cwd(), distPath, image.split('/').pop()) : image));
} else {
console.error(body.error);
}
}))
})
}
main();