UNPKG

2.08 kBJavaScriptView Raw
1#!/usr/bin/env node
2
3const argv = require('yargs').argv
4const colors = require('colors')
5const fs = require('fs')
6const glob = require('glob')
7const jo = require('./main.js')
8const manifest = require('../package.json')
9const promisify = require('util').promisify
10
11if (argv.version) {
12 console.log(manifest.name + ' ' + manifest.version)
13 process.exit(0)
14}
15
16if (argv.help || typeof argv._ === 'undefined' || argv._.length === 0) {
17 const help = [
18 '',
19 'Rotate JPEG images based on EXIF orientation',
20 '',
21 colors.underline('Usage'),
22 'jpeg-autorotate <path>',
23 '',
24 colors.underline('Options'),
25 '--quality=<0-100> JPEG output quality',
26 '--version Output current version',
27 '--help Output help',
28 '',
29 ]
30 console.log(help.join('\n'))
31 process.exit(0)
32}
33
34listFiles()
35 .then(processFiles)
36 .then(() => {
37 process.exit(0)
38 })
39 .catch((error) => {
40 console.log(error.message)
41 process.exit(1)
42 })
43
44function listFiles() {
45 return Promise.all(argv._.map((arg) => promisify(glob)(arg, {}))).then((files) => {
46 return [].concat.apply([], files)
47 })
48}
49
50function processFiles(files, index = 0) {
51 if (index + 1 > files.length) {
52 return Promise.resolve()
53 }
54 const filePath = files[index]
55 return jo
56 .rotate(filePath, {quality: argv.quality})
57 .then(({buffer, orientation, quality, dimensions}) => {
58 return promisify(fs.writeFile)(files[index], buffer).then(() => {
59 return {orientation, quality, dimensions}
60 })
61 })
62 .then(({orientation, quality, dimensions}) => {
63 const message =
64 'Processed (Orientation: ' +
65 orientation +
66 ') (Quality: ' +
67 quality +
68 '%) (Dimensions: ' +
69 dimensions.width +
70 'x' +
71 dimensions.height +
72 ')'
73 console.log(filePath + ': ' + colors.green(message))
74 })
75 .catch((error) => {
76 const isFatal = error.code !== jo.errors.correct_orientation
77 console.log(filePath + ': ' + (isFatal ? colors.red(error.message) : colors.yellow(error.message)))
78 })
79}