UNPKG

1.24 kBJavaScriptView Raw
1#!/usr/bin/env node
2'use strict'
3
4const fs = require('fs')
5const path = require('path')
6const { imageSize } = require('..')
7
8const files = process.argv.slice(2)
9
10if (!files.length) {
11 console.error('Usage: image-size image1 [image2] [image3] ...')
12 process.exit(-1)
13}
14
15const red = ['\x1B[31m', '\x1B[39m']
16// const bold = ['\x1B[1m', '\x1B[22m']
17const grey = ['\x1B[90m', '\x1B[39m']
18const green = ['\x1B[32m', '\x1B[39m']
19
20function colorize(text, color) {
21 return color[0] + text + color[1]
22}
23
24files.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 // console.error(e.stack)
46 console.error(colorize(e.message, red), '-', image)
47 }
48})