-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
83 lines (71 loc) · 2.24 KB
/
app.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
var fs = require('fs');
var path = require('path');
var tinify = require("tinify");
var Spinner = require('cli-spinner').Spinner;
var _setApiKey = function (key) {
tinify.key = key;
};
var _compressImage = function (imageLink, destinationLink, callback) {
var completeStr = imageLink + ' -> ' + destinationLink;
var spinner = new Spinner('Compressing: ' + completeStr);
spinner.start();
spinner.setSpinnerDelay(500);
tinify.fromFile(imageLink).toFile(destinationLink, function (err) {
var displayStr = 'Completed: ' + completeStr.trim() + '\t [Free Account Limit Used: ' + tinify.compressionCount + '/500]';
if(err){
callback(err);
}else{
callback(null, displayStr);
}
spinner.stop(true);
console.log(displayStr);
return;
});
};
var _readImageFilesFromDirectory = function (dirPath) {
var allFileList = fs.readdirSync(dirPath);
var imgFileList = allFileList.filter(function (filePath) {
var completePath = path.join(dirPath, filePath);
var stat = fs.lstatSync(completePath);
if (stat.isFile()) {
var ext = path.extname(completePath).toLowerCase().trim();
if (ext.toLowerCase().trim() === '.jpg' || ext === '.jpeg' || ext === '.png') {
return true;
} else {
return false;
}
} else {
return false;
}
});
imgFileList = imgFileList.map(function (imgName) {
return path.join(dirPath, imgName);
});
return imgFileList;
};
var _exit = function () {
process.exit();
};
var _createApiKey = function (apiKeyPath, key) {
var code = fs.writeFileSync(apiKeyPath, key);
return code ? false : true;
};
var _log = function (msg) {
console.log(msg);
};
var _checkApiKeyExist = function (apiKeyPath) {
return fs.existsSync(apiKeyPath)
};
var _readApiKey = function (apiKeyPath) {
return fs.readFileSync(apiKeyPath);
};
module.exports = {
exit: _exit,
log: _log,
setApiKey: _setApiKey,
createApiKey: _createApiKey,
checkApiKeyExist: _checkApiKeyExist,
readApiKey: _readApiKey,
readImageFilesFromDirectory: _readImageFilesFromDirectory,
compressImage: _compressImage
};