-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
executable file
·101 lines (92 loc) · 3.47 KB
/
Copy pathapp.js
File metadata and controls
executable file
·101 lines (92 loc) · 3.47 KB
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
#!/usr/bin/env node
var im = require('imagemagick');
var fs = require('fs');
var async = require('async');
var mkpath = require('mkpath');
var program = require('commander');
var package = require('./package.json');
var path = require('path');
var imgResizeTask = [];
// Help Window
program
.version(package.version)
.usage(' -d destinationFolder -s sourceFolder [-w width] [-h height]')
.option('-s, --source <source-directory-path>', 'mention the source directory path.')
.option('-d, --destination <destination-directory-path>', 'mention destination directory path.')
.option('-w, --width <width>', 'mention required width.')
.option('-h, --height <height>', 'mention required height.')
.parse(process.argv);
// Default Options
var option = {
sourceDir: '',
destinationDir: '',
width: 0,
height: 0
};
option.sourceDir = program.source || option.sourceDir;
option.destinationDir = program.destination || option.destinationDir;
option.width = program.width || option.width;
option.height = program.height || option.height;
if(!option.width && !option.height){
console.log('Invalid Command\nPlease pass at least one args out of [-w / -h]');
}else 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, 'resize-image');
}
mkpath.sync(option.destinationDir);
var listOfFiles = readImageFilesFromDirectory(option.sourceDir);
listOfFiles.forEach(function (imgLink) {
var fileName = path.basename(imgLink);
var destinationCompletePath = path.join(option.destinationDir, fileName);
imgResizeTask.push(function(callback){
im.resize({
srcPath: imgLink,
dstPath: destinationCompletePath,
width: option.width,
height: option.height
}, function(err, stdout, stderr){
if(err){
console.log('Error: ' + imgLink);
callback(null, 'Error: ' + imgLink);
}else{
console.log('Success: ' + imgLink);
callback(null, 'Success: ' + imgLink);
}
});
});
});
async.series(imgResizeTask, function (err, data) {
if(err){
console.log('\nError in Resize: ', err);
}else {
console.log('\n-- Image Resize Task Completed --\n');
}
})
}
}else{
console.log('Invalid Command\nRun Command: resize-image --help \nFor More Details');
}
function readImageFilesFromDirectory(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;
}