image-size.js 938 B

123456789101112131415161718192021222324252627282930313233343536
  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. files.forEach(function (image) {
  16. try {
  17. if (fs.existsSync(path.resolve(image))) {
  18. var size = imageSize(image);
  19. var label = green[0] + size.width + green[1] +
  20. grey[0] + 'x' + grey[1] +
  21. green[0] + size.height + green[1];
  22. console.info(label, '-', grey[0] + image + grey[1]);
  23. } else {
  24. console.error('file doesn\'t exist - ', image);
  25. }
  26. } catch (e) {
  27. // console.error(e.stack);
  28. console.error(red[0] + e.message + red[1], '-', image);
  29. }
  30. });