/
index.js
executable file
·192 lines (174 loc) · 4.77 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#!/usr/bin/env node
const fs = require('fs');
const meow = require('meow');
const url = require('url');
const path = require('path');
const async = require('./lib/async');
const printResult = require('./lib/print-result');
const getStatus = require('./lib/get-status');
const request = require('request');
const MAIN_HOST = 'https://imagecompressor.com';
const cli = meow(
`
Usage
$ optimizilla <input>
Options
--output, -o Destination of the optimized file
--replace, -r Replace the original file
--dry, -d Dry run, upload, optimize and print out links
Examples
$ optimizilla xpto.jpg --output ./ --replace
`,
{
alias: {
o: 'output',
r: 'replace',
d: 'dry'
}
}
);
if (!cli.input.length) {
cli.showHelp(-1);
}
/**
* Guid function to generate uid
* @return {Function}
*/
var guid = (function() {
var counter = 0;
return function(prefix) {
var guid = new Date().getTime().toString(32),
i;
for (i = 0; i < 5; i++) {
guid += Math.floor(Math.random() * 65535).toString(32);
}
return (prefix || 'o_') + guid + (counter++).toString(32);
};
})();
/**
* Random string generator
* @return {String}
*/
function randomString() {
for (
var t = '0123456789abcdefghiklmnopqrstuvwxyz', e = 16, i = '', n = 0;
e > n;
n++
) {
var a = Math.floor(Math.random() * t.length);
i += t.substring(a, a + 1);
}
return i;
}
/**
* startProcessingFile
* @param {String}
* @return {Promise}
*/
function startProcessingFile(fileName) {
return new Promise((resolve, reject) => {
let uniqPathId = randomString();
let randomId = guid();
const formData = {
file: fs.createReadStream(
fileName[0] == path.sep
? fileName
: path.resolve(process.cwd() + path.sep + fileName)
),
id: randomId,
name: fileName
};
request.post(
{
url: `${MAIN_HOST}/upload/${uniqPathId}`,
formData
},
error => {
if (error) {
reject({ fileName, uniqPathId, randomId, error });
} else {
resolve({ fileName, uniqPathId, randomId });
}
}
);
});
}
/**
* downloadFinalFile
* @param {Object} body
* @param {Object} options
*/
function downloadFinalFile(body, options, flags) {
let outputFile = flags.output ? flags.output : process.cwd();
if (flags.replace) {
outputFile = options.fileName;
} else {
outputFile = path.resolve(outputFile + path.sep + body.image.result);
}
if (cli.flags.dry) {
printResult(
Object.assign(options, {
status: 'success',
savings: body.image.savings,
compressedUrl: url.resolve(MAIN_HOST, body.image.compressed_url)
})
);
} else {
request
.get(url.resolve(MAIN_HOST, body.image.compressed_url))
.pipe(fs.createWriteStream(outputFile));
printResult(
Object.assign(options, {
status: 'success',
savings: body.image.savings
})
);
}
}
/**
* Main process generator
* @param {Object} options
* @return {Function}
*/
function processGenerator(options, flags) {
return function*() {
let content = {};
content = yield getStatus('auto', options);
while (true) {
content = yield getStatus('status', options);
printResult(
Object.assign(options, {
status: 'processing',
percent: content.body.auto_progress
})
);
if (content.body.auto_progress >= 100) {
break;
}
}
content = yield getStatus('panel', options);
downloadFinalFile(content.body, options, flags);
return content;
};
}
cli.input
.reduce((newArray, singleFileName) => {
if (singleFileName.toLowerCase().match(/png|jpg|jpeg/)) {
return newArray.concat(singleFileName);
}
console.log(
`${singleFileName} format is invalid, only png/jpeg/jpg can be used`
);
return newArray;
}, [])
.forEach(singleFileName => {
startProcessingFile(singleFileName, cli.flags)
.then(options => async(processGenerator(options, cli.flags)))
.catch(options => {
printResult(
Object.assign(options, {
status: 'error'
})
);
});
});