image-size.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #!/usr/bin/env node
  2. 'use strict';
  3. var fs = require('fs');
  4. var path = require('path');
  5. var imageSize = require('..');
  6. var files = process.argv.slice(2);
  7. if (!files.length) {
  8. console.error('Usage: image-size image1 [image2] [image3] ...');
  9. process.exit(-1);
  10. }
  11. var red = ['\x1B[31m', '\x1B[39m'];
  12. // var bold = ['\x1B[1m', '\x1B[22m'];
  13. var grey = ['\x1B[90m', '\x1B[39m'];
  14. var green = ['\x1B[32m', '\x1B[39m'];
  15. function colorize(text, color) {
  16. return color[0] + text + color[1]
  17. }
  18. files.forEach(function (image) {
  19. try {
  20. if (fs.existsSync(path.resolve(image))) {
  21. var size = imageSize(image);
  22. var greyX = colorize('x', grey);
  23. var greyImage = colorize(image, grey);
  24. (size.images || [size]).forEach(function (size) {
  25. var greyType = '';
  26. if (size.type) {
  27. greyType = colorize(' (' + size.type + ')', grey);
  28. }
  29. console.info(
  30. colorize(size.width, green) + greyX + colorize(size.height, green)
  31. + ' - ' + greyImage + greyType
  32. );
  33. });
  34. } else {
  35. console.error('file doesn\'t exist - ', image);
  36. }
  37. } catch (e) {
  38. // console.error(e.stack);
  39. console.error(colorize(e.message, red), '-', image);
  40. }
  41. });