-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
165 lines (142 loc) · 4.07 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
'use strict'
const { existsSync, readdir, mkdir } = require('fs')
const { promisify } = require('util')
const { remove } = require('fs-extra')
const sharp = require('sharp')
const inquirer = require('inquirer')
const readDirAsync = promisify(readdir)
inquirer.registerPrompt('directory', require('inquirer-directory'))
const outpuDir = (output, keepDir) => {
if (!keepDir) {
remove('./output')
.then(() => {
mkdir(output, { recursive: true }, err => {
if (err) throw console.log(err)
})
})
} else {
mkdir(output, { recursive: true }, err => {
if (err) throw console.log(err)
})
}
}
const setImageType = (imageSharp, quality, outputFormat) => {
switch (outputFormat) {
case 'jpg': return imageSharp.jpeg({
quality,
progressive: true
})
case 'webp': return imageSharp.webp({
quality
})
case 'png': return imageSharp.png({
compressionLevel: 6,
quality,
progressive: true
})
default:
break
}
}
// [576,768,992,1200]
const converter = async (args) => {
try {
const { from, points, format, quality, height, keepDir } = args
// console.log(args)
const outputFormat = format
const outputHeight = height
const qualityImage = +quality
const breakpointsArgs = Number.isInteger(points) === true ? Array.of(points) : points.split(',')
const outputDir = `./output/${from}`
const sourceDir = `./sources/${from}`
// checking if the folder is in sources
if (!existsSync(sourceDir)) {
console.log(`The specified folder does not exist - ${sourceDir}`)
return false
}
// console.log(breakpointsArgs)
// remove output folder and create new one
breakpointsArgs.forEach(breakpointsFolder => {
outpuDir(`${outputDir}/${breakpointsFolder}`, keepDir)
})
await new Promise((resolve, reject) => {
setTimeout(() => resolve('done !'), 5000)
})
// get async files
const res = await readDirAsync(sourceDir)
breakpointsArgs.forEach(breakpointsFolder => {
// remove output folder and create new one
// outpuDir(`${outputDir}/${breakpointsFolder}`, keepDir)
res.forEach(async (image) => {
// file extension replace
const newImage = image.replace(/\.[^\\.]+$/, `.${outputFormat}`)
const imageSharp = await sharp(`${sourceDir}/${image}`)
imageSharp.resize({
// set width from command line or get default with 1200px
width: parseInt(breakpointsFolder),
// default max height is 800px
// if you set height then width i change also
height: +outputHeight,
// maintain aspect ratio
fit: sharp.fit.inside,
// do not enlarge if the width or height are already less than the specified dimensions
withoutEnlargement: true
})
// set type image [jpg, png, webp]
setImageType(imageSharp, qualityImage, outputFormat)
// save to specific folder - breakpoints
imageSharp.toFile(`${outputDir}/${breakpointsFolder}/${newImage}`)
// console.clear()
// show in console
console.log(`saving file [ ${breakpointsFolder}/${newImage} ]`)
})
})
} catch (err) {
console.log(err.message)
}
}
inquirer.prompt([
{
type: 'directory',
name: 'from',
message: 'Folder to convert',
basePath: './sources'
},
{
type: 'input',
name: 'points',
message: 'Select breakpoints e.g. 576,768,992,1200,..., default:',
default: '576,768,992,1200'
},
{
type: 'list',
name: 'format',
choices: [
{ value: 'jpg' },
{ value: 'png' },
{ value: 'webp' }
],
message: 'Select a format, default:',
default: ['jpg']
},
{
type: 'input',
name: 'quality',
message: 'Set photo quality [1 to 100], default:',
default: 60
},
{
type: 'input',
name: 'height',
message: 'Maximum photo height, default:',
default: 800
},
{
type: 'confirm',
name: 'keepDir',
message: 'Keep folders',
default: true
}
]).then(answers => {
converter(answers)
})