-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·75 lines (68 loc) · 3.05 KB
/
index.js
File metadata and controls
executable file
·75 lines (68 loc) · 3.05 KB
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
#! /usr/bin/env node
const request = require('request')
const fs = require('fs')
const path = require('path')
const [, ,source, output = source] = process.argv
showBanner()
const total = {
input: 0,
output: 0
}
if (fs.statSync(source).isDirectory()) {
if (!fs.statSync(output).isDirectory()) {
console.error('output must be a directory')
return
}
compressDir(source, output)
} else if (source.match(/(\.png|\.jpg|\.jpeg)$/)) {
compress(source, output)
}
function compressDir(sourceDir, outputDir) {
const files = fs.readdirSync(sourceDir).filter(fileName => fileName.match(/(\.png|\.jpg|\.jpeg)$/)).map(fileName => {
return compress(`${sourceDir}${path.sep}${fileName}`, `${outputDir}${path.sep}${fileName}`)
})
Promise.all(files).then(() => {
console.log('——————————————————————————————————————')
console.log(`Total Saved: ${toKb(total.input-total.output)} ${((total.input-total.output) / total.input).toFixed()}%`)
})
}
function compress(sourcePath, outputPath) {
return new Promise((resolve, reject) => {
request({
method: 'POST',
url: 'https://tinypng.com/web/shrink',
headers: {
'cache-control': 'no-cache',
'Content-Type': 'application/octet-stream',
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36',
},
body: fs.createReadStream(sourcePath),
encoding: 'utf8'
}, function (error, response, body) {
if (error) throw new Error(error)
body = JSON.parse(body)
request(body.output.url)
.pipe(fs.createWriteStream(outputPath))
.on('close', () => {
if (error) throw new Error(error)
total.input += body.input.size
total.output += body.output.size
console.log(`${sourcePath}\tsource:${toKb(body.input.size)}\tcompressed:${toKb(body.output.size)}\tsaved: ${toKb(body.input.size-body.output.size)}\t${((1 - body.output.ratio)* 100).toFixed()}%`)
resolve()
})
})
})
}
function showBanner() {
console.log(`
████████╗██╗███╗ ██╗██╗ ██╗██████╗ ███╗ ██╗ ██████╗
╚══██╔══╝██║████╗ ██║╚██╗ ██╔╝██╔══██╗████╗ ██║██╔════╝
██║ ██║██╔██╗ ██║ ╚████╔╝ ██████╔╝██╔██╗ ██║██║ ███╗
██║ ██║██║╚██╗██║ ╚██╔╝ ██╔═══╝ ██║╚██╗██║██║ ██║
██║ ██║██║ ╚████║ ██║ ██║ ██║ ╚████║╚██████╔╝
╚═╝ ╚═╝╚═╝ ╚═══╝ ╚═╝ ╚═╝ ╚═╝ ╚═══╝ ╚═════╝
`)
}
function toKb(byte) {
return (byte / 1024).toFixed(1) + 'k'
}