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 | const greyX = colorize('x', grey)
|
28 | const greyImage = colorize(image, grey)
|
29 | const size = imageSize(image)
|
30 | const sizes = size.images || [size]
|
31 | sizes.forEach(size => {
|
32 | let greyType = ''
|
33 | if (size.type) {
|
34 | greyType = colorize(' (' + size.type + ')', grey)
|
35 | }
|
36 | console.info(
|
37 | colorize(size.width, green) + greyX + colorize(size.height, green)
|
38 | + ' - ' + greyImage + greyType
|
39 | )
|
40 | })
|
41 | } else {
|
42 | console.error('file doesn\'t exist - ', image)
|
43 | }
|
44 | } catch (e) {
|
45 |
|
46 | console.error(colorize(e.message, red), '-', image)
|
47 | }
|
48 | })
|