-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert.js
132 lines (109 loc) · 3.32 KB
/
convert.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
const glob = require('glob-promise')
const Q = require('q')
const fs = require('fs-extra')
const path = require('path')
// const webp = require('webp-converter')
const imagemin = require('imagemin')
const imageminGuetzli = require('imagemin-guetzli')
const imageminWebp = require('imagemin-webp')
const dirOutput = './output'
let filesFailed = []
const getOptions = (settings = {}) => Object.assign({
output: dirOutput,
webpQuality: 80,
GuetzliQuality: 84,
logType: 'Original'
}, settings)
const convert = (files, settings) => new Promise((resolve, reject) => {
let options = getOptions(settings)
let chain = Q.fcall(() => { })
files.forEach(file => {
chain = chain.then(() => convertFile(file, settings))
})
chain = chain.then(() => {
console.log(options.logType + ' success')
resolve()
}).catch(err => {
console.log(err)
reject(err)
})
})
const convertFile = (file, settings) => {
let options = getOptions(settings)
const { output, webpQuality, GuetzliQuality, logType } = options
console.log(path.basename(file) + ' - ' + logType)
return imagemin([file], output, {
plugins: [
imageminWebp({ quality: webpQuality })
]
}).then(files => {
console.log(path.basename(file) + ' - ' + logType + ' - webp - Converted Successfully')
}).then(() => imagemin([file], output, {
plugins: [
imageminGuetzli({ quality: GuetzliQuality })
]
}).then(files => {
console.log(path.basename(file) + ' - ' + logType + ' - jpeg - Converted Successfully')
}))
.catch(err => {
filesFailed.push({
file,
options
})
console.log(file + ' - FAILED')
// console.log(123123)
// console.log(err)
})
}
const convertFailed = () => new Promise((resolve, reject) => {
let chain = Q.fcall(() => { })
const list = [...filesFailed]
filesFailed = []
list.forEach(file => {
chain = chain.then(() => convertFile(file.file, file.options))
})
chain = chain.then(() => {
if (filesFailed.length) {
convertFailed()
} else {
console.log('failed files success')
resolve()
}
}).catch(err => {
console.log(err)
reject(err)
})
})
// ensure dir: /output
fs.ensureDir(dirOutput)
.then(() => fs.emptyDir(dirOutput))
// convert original
.then(() => glob('./src/*.png'))
.then(files => convert(files))
// convert blured
.then(() => glob('./src/blured/*.png'))
.then(files => convert(files, {
output: path.resolve(dirOutput, 'blured'),
webpQuality: 90,
GuetzliQuality: 84,
logType: 'Blured'
}))
// retry failed files
.then(() => convertFailed())
// rename all .png to .jpg
.then(() => glob(dirOutput + '/**/*.png'))
.then(files => new Promise((resolve, rejact) => {
files.forEach(file => {
fs.moveSync(file, file.replace(/\.png$/, '.jpg'))
})
resolve()
}))
// copy /src/thumbnail
.then(() => fs.copy('./src/thumbnail', path.resolve(dirOutput, 'thumbnail')))
.then(() => {
console.log('')
console.log('done')
})
.catch(err => {
console.log(err)
})