-
Notifications
You must be signed in to change notification settings - Fork 5
/
tinypng.js
executable file
·303 lines (252 loc) · 5.81 KB
/
tinypng.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#!/usr/bin/env node
require('colors');
const fs = require('fs');
const https = require('https');
const pjson = require('./package.json');
const options_path = `${__dirname}/settings.json`;
const files_io = [];
let options;
let files;
let file_count = 0;
/**
* Get default options from settings file
*
* @returns {*}
*/
function getOptions() {
const buffer = fs.readFileSync(options_path);
options = JSON.parse(buffer.toString());
return options;
}
/**
* Set option whithout saving to settings file
*
* @param {string} param
* @param {(string|boolean)} value
*/
function setLocalOption(param, value) {
if (typeof param !== 'undefined' && typeof value !== 'undefined') {
options[param] = value;
}
}
/**
* Set option with saving to settings file
*
* @param param
* @param value
*/
function setOption(param, value) {
if (typeof param !== 'undefined' && typeof value !== 'undefined') {
setLocalOption(param, value);
const write_data = JSON.stringify(options, null, 2);
fs.writeFileSync(options_path, write_data);
}
}
/**
* Parse command-line arguments to options and files
*
* @returns {boolean}
*/
function parseArgvs() {
let argvs = process.argv.slice(2);
const checkArg = function() {
if (argvs[0] && argvs[0][0] === '-') {
switch (argvs[0]) {
case '-k':
case '--api-key':
setOption('api_key', argvs[1]);
argvs = argvs.slice(2);
break;
case '-r':
case '--allow-rewrite':
setLocalOption('allow_rewrite', true);
argvs = argvs.slice(1);
break;
case '-n':
case '--allow-nonpng':
setLocalOption('allow_nonpng', true);
argvs = argvs.slice(1);
break;
case '-h':
case '--help':
logHelp();
exit();
break;
case '-v':
case '--version':
logVersion();
exit();
break;
case '-p':
case '--postfix':
setLocalOption('postfix', argvs[1]);
argvs = argvs.slice(2);
break;
default:
files = argvs;
break;
}
if (!files) {
checkArg();
}
} else {
files = argvs;
}
};
if (argvs.length === 0) {
argvs[0] = '--help';
}
checkArg();
filterFiles();
return true;
}
/**
* Filter selected files from directories and wrong file extentions
*
* @returns {object}
*/
function filterFiles() {
for (let i = 0, l = files.length; i < l; i++) {
const file = files[i];
if (fs.existsSync(file) && fs.statSync(file)
.isFile()) {
if ((!options.allow_nonpng && file.slice(-4) === '.png') || options.allow_nonpng) {
const pair = [file, file];
if (!options.allow_rewrite) {
pair[1] = postfixedName(file);
}
files_io.push(pair);
}
}
}
return files_io;
}
/**
* Adds postfix to filename
*
* @param {string} filename
* @returns {string} postfixed filename
*/
function postfixedName(filename) {
const arr = filename.split('.');
if (arr.length === 1) {
return `${filename}${options.postfix}`;
}
arr[arr.length - 2] = `${arr[arr.length - 2]}${options.postfix}`;
return arr.join('.');
}
/**
* Check API key and show warning
*
* @returns {boolean}
*/
function checkApiKey() {
if (options.api_key === '') {
logError('TinyPNG API key is empty. Get one from https://tinypng.com/developers, and set default key with --api-key.');
exit(1);
} else {
return true;
}
}
/**
* Shows help message
*/
function logHelp() {
const message = `
Usage: tinypng [options] [image.png|*.png]
${options.api_key === ''
? 'Warning! API key is not defined.'.yellow
: `Current API key: ${options.api_key}`
}
Options:
-k, --api-key \tSet default TinyPNG API key.
-r, --allow-rewrite\tRewrite the original files with compressed data.
-n, --allow-nonpng \tAllow you to compress files without .png extention.
-p, --postfix \tPostfix for compressed files when rewriting disabled.
-h, --help \tThis message.
-v, --version \tShow version.
`;
console.log(message);
}
/**
* Shows version
*/
function logVersion() {
console.log(pjson.version);
}
/**
* Shows error message
*
* @param {*} message
*/
function logError(message) {
console.error('>_<'.red, message);
}
/**
* Shows log message
*
* @param {*} message
*/
function logMessage(message) {
console.log('*Ü*'.green, message);
}
/**
* Exit from application with code
*
* @param {number} [code=0]
*/
function exit(code) {
if (!code) {
code = 0;
}
process.exit(code);
}
/**
* Compress and save image
*/
function makeTiny() {
let pair = files_io[file_count];
if (!pair) {
return logMessage('Compression complete!');
}
const input = pair[0];
const output = pair[1];
process.stdout.write(`${input}${' → '.grey}`);
const req_options = require('url').parse('https://api.tinypng.com/shrink');
req_options.auth = `api:${options.api_key}`;
req_options.method = 'POST';
const request = https.request(req_options, function(res) {
res.on('data', function(d) {
d = JSON.parse(d);
if (d.error) {
process.stdout.write(`${'error'.red}\n`);
logError(`${d.error}: ${d.message}`);
exit(1);
} else {
process.stdout.write(`-${((1 - d.output.ratio) * 100).toFixed(1)}%${' → '.grey}`);
}
});
if (res.statusCode === 201) {
https.get(res.headers.location, function(res) {
res.pipe(fs.createWriteStream(output));
process.stdout.write(`${output.yellow}\n`);
file_count++;
makeTiny();
});
}
});
fs.createReadStream(input)
.pipe(request);
}
/**
* Initialize function
*/
function run() {
getOptions();
parseArgvs();
checkApiKey();
if (files_io.length > 0) {
makeTiny();
}
}
run();