-
Notifications
You must be signed in to change notification settings - Fork 1
/
shrink-pic.js
executable file
·120 lines (113 loc) · 4.71 KB
/
shrink-pic.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
#!/usr/bin/env node
var program = require('commander');
var package = require('./package.json');
var fs = require('fs');
var path = require('path');
var async = require('async');
var mkpath = require('mkpath');
var API_KEY_FILE_NAME = 'key.hash';
var APP_DIR = __dirname;
var API_KEY_FILE_PATH = path.join(APP_DIR, API_KEY_FILE_NAME);
var DEFAULT_DESTINATION_FOLDER_NAME = 'shrink-pic';
var SINGLE_COMPRESSED_FILE_PREFIX = 'shrink_';
var imgCompressTask = [];
var app = require('./app');
// Default Options
var option = {
sourceDir: '',
destinationDir: '',
levelOfCompression: '1',
apiKey: ''
};
// Help Window
program
.version(package.version)
.usage('[ -d destinationFolder ] [ -k SedasdeEEW1231asd213 ] -s sourceFolder ')
.option('-s, --source <source-directory-path>', 'mention the source directory path.')
.option('-d, --destination <destination-directory-path>', 'mention destination directory path.')
// .option('-l, --level <level-of-compresssion>', 'mention level of compression.')
.option('-k, --apikey <api-key-save>', 'mention api key to save or renew.')
.parse(process.argv);
option.sourceDir = program.source || option.sourceDir;
option.destinationDir = program.destination || option.destinationDir;
option.levelOfCompression = program.level || option.levelOfCompression;
option.apiKey = program.apikey;
// If API key option filled.
if (option.apiKey) {
var KEY = option.apiKey && option.apiKey.trim();
var status = app.createApiKey(API_KEY_FILE_PATH, KEY);
if(status){
app.log('\n - Key successfully saved. -');
}else{
app.log('\n ---- ERROR INFO ----\n - API key not able to save. \n ' + status);
app.exit();
}
}
// Check API key exist.
if (app.checkApiKeyExist(API_KEY_FILE_PATH)) {
option.apiKey = app.readApiKey(API_KEY_FILE_PATH);
option.apiKey = option.apiKey.toString();
app.setApiKey(option.apiKey);
if (option.sourceDir) {
var sourceStat = fs.lstatSync(option.sourceDir);
if(sourceStat.isDirectory()){
//If destination not mentioned
if(!option.destinationDir){
option.destinationDir = path.join(option.sourceDir, DEFAULT_DESTINATION_FOLDER_NAME);
}
mkpath.sync(option.destinationDir);
var listOfFiles = app.readImageFilesFromDirectory(option.sourceDir);
listOfFiles.forEach(function (imgLink) {
var fileName = path.basename(imgLink);
var destinationCompletePath = path.join(option.destinationDir, fileName);
imgCompressTask.push(function(callback){
app.compressImage(imgLink, destinationCompletePath, callback);
});
});
async.series(imgCompressTask, function (err, data) {
if(err){
console.log('\nError in compressions: ', err);
}
console.log('\n-- Image Compression completed --\n', data.join('\n'));
app.exit();
})
}else if(sourceStat.isFile()){
var fileName='';
var restOfPath = '';
if(option.destinationDir){
var statExistFile = fs.existsSync(option.destinationDir);
if(!statExistFile){
// do nothing
}else{
var destStat = fs.lstatSync(option.destinationDir);
fileName = path.basename(option.sourceDir);
if(destStat.isDirectory()){
option.destinationDir = path.join(option.destinationDir, SINGLE_COMPRESSED_FILE_PREFIX + fileName);
}else if(destStat.isFile()){
// do nothing
}
}
}else{
fileName = path.basename(option.sourceDir);
restOfPath = path.dirname(option.sourceDir);
option.destinationDir = restOfPath;
option.destinationDir = path.join(option.destinationDir, SINGLE_COMPRESSED_FILE_PREFIX + fileName);
}
app.compressImage(option.sourceDir, option.destinationDir, function (err, data) {
if(err){
console.log('\n\nError in compressions: ', err);
}
console.log('\n-- Compression completed --\n', data);
app.exit();
});
}else{
app.log('Dont understand source content.');
app.exit();
}
} else {
app.log('\nArgument missing, Try command: `shrink-pic -h` to get more help\n');
app.exit();
}
} else {
app.log('\n ---- ERROR INFO ----\n - API key not found.\n - To generate api key.\n - Visit: https://tinypng.com/developers\n');
}