Guest User

Untitled

a guest
Jul 19th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.18 KB | None | 0 0
  1. #!/usr/bin/env node
  2.  
  3. const sharp = require('sharp');
  4. const fs = require('fs');
  5. const program = require('commander');
  6. const chalk = require('chalk');
  7.  
  8. function increaseVerbosity(v, total) {
  9. return total + 1;
  10. }
  11.  
  12. program
  13. .version('0.1.0', '-V, --version')
  14. .option('-w, --width <width>', 'The maximum width of each image. Aspect ' +
  15. 'ratio will remain. Images smaller than max will not be scaled.',
  16. parseInt)
  17. .option('-v, --verbose', 'Output additional stats, three levels',
  18. increaseVerbosity, 0)
  19. .option('-q, --quality <quality>', 'The quality factor for jpg images (no ' +
  20. 'effect for png). Values: 0-100', parseInt)
  21. .option('-c, --copy_others', 'Copy non jpg or png files to destination ' +
  22. 'directory. Good if you are also includeing PDF or other filetypes in ' +
  23. 'the directory.')
  24. .arguments('<source> <destination>')
  25. .action(function(source, destination) {
  26. var width, quality, copy_others;
  27.  
  28. if (!fs.existsSync(source)) {
  29. console.error(chalk.bold.red('Source directory does not exist, ' +
  30. 'please verify.'));
  31. process.exit(1);
  32. }
  33. if (!fs.existsSync(destination)) {
  34. console.log('Destination directory does not exist, creating it.')
  35. fs.mkdirSync(destination);
  36. }
  37. program.width ? width = program.width : width = 2560;
  38. program.quality ? quality = program.quality : quality = 80;
  39. program.copy_others ? copy_others = program.copy_others : copy_others = false;
  40.  
  41. if (quality > 100 || quality < 0) {
  42. console.error(chalk.bold.red('quality must be greater than 0 and ' +
  43. 'less than 100'));
  44. process.exit(1);
  45. }
  46.  
  47. fs.readdir(source, function(err, files) {
  48. var jpg = 0;
  49. var png = 0;
  50. var other = 0;
  51. for (var i in files) {
  52. file = source + files[i];
  53. if (file.endsWith('png')) {
  54. png++;
  55. if (program.verbose > 2)
  56. console.log('resize ' + file);
  57. sharp(file)
  58. .resize(width)
  59. .max()
  60. .withoutEnlargement(true)
  61. .png()
  62. .toFile(destination + files[i]);
  63. } else if (file.endsWith('jpg')) {
  64. jpg++;
  65. if (program.verbose > 2)
  66. console.log('resize ' + file);
  67. sharp(file)
  68. .resize(width)
  69. .max()
  70. .withoutEnlargement(true)
  71. .jpeg({
  72. quality: quality,
  73. optimizeScans: true
  74. })
  75. .toFile(destination + files[i]);
  76. } else if (copy_others) {
  77. other++;
  78. if (program.verbose > 2)
  79. console.log('move ' + file);
  80. fs.createReadStream(file).pipe(fs.createWriteStream(
  81. destination + files[i]));
  82. }
  83. }
  84. if (err) {
  85. console.error(chalk.bold.red(err));
  86. }
  87. if (program.verbose > 0) {
  88. total = png + jpg + other;
  89. console.log(chalk.bold('================================='));
  90. console.log(chalk.bold('Processed ' + total + ' files.'));
  91. if (program.verbose > 1) {
  92. console.log(' ' + png + ' png files.');
  93. console.log(' ' + jpg + ' jpg files.');
  94. console.log(' ' + other + ' other files.');
  95. }
  96. if (program.width)
  97. console.log('scaled images to ' + width + ' pixels wide');
  98. if (program.quality)
  99. console.log('set quality for jpg to ' + quality + '%');
  100. console.log('Source: ' + source);
  101. console.log('Destination: ' + destination);
  102. console.log(chalk.bold('================================='));
  103. }
  104. });
  105. }).parse(process.argv)
Add Comment
Please, Sign In to add comment