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