This repository has been archived by the owner on Nov 24, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·82 lines (69 loc) · 2.38 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env node --harmony_default_parameters
'use strict';
const program = require('commander')
const fs = require('fs')
const path = require('path')
const chalk = require('chalk')
const ImageMerge = require('./src/image-merge')
const _ = require('lodash')
let files = []
let errors = []
program
.version('1.0.2')
.arguments('[file...]')
.option('-o, --output <filename>', 'output file path')
.option('-d, --direction <type>', 'indicate merge image direction, option values: [horizontal, vertical], default is [vertical]', /^(horizontal|vertical)$/i, 'vertical')
.option('--dir <dir>', 'input directory')
.option('--background <color>', 'fill background with [#FFFFFF]', '#FFFFFF')
.option('--maxWidth <maxWidth>', 'max width [0]', '0')
.option('--maxHeight <maxHeight>', 'max height [0]', '0')
.action((_files) => {
if (_files.length > 0) {
_files.forEach(file => {
let _path = file
if (!path.isAbsolute(_path)) {
_path = path.normalize(path.join(__dirname, _path))
}
let _exists = fs.existsSync(_path)
if (_exists) {
files.push(_path)
} else {
errors.push(chalk.bold.red('File not exists: ') + _path)
}
})
}
// process.exit(1)
})
.parse(process.argv);
let dir = program.dir
if (dir) {
dir = path.isAbsolute(dir) ? path.normalize(dir) : path.normalize(path.join(__dirname, dir))
if (!fs.existsSync(dir)) {
errors.push(chalk.bold.red('Directory not exists: ') + dir)
} else {
files = files.concat(fs.readdirSync(dir).map(file => path.join(dir, file)))
}
}
files = files.filter(file => {
return ['.BMP', '.JPG', '.JPEG', '.PNG', '.GIF'].indexOf(path.extname(file).toUpperCase()) > -1
})
if (files.length === 0) {
errors.push(chalk.bold.red('No file input!'))
}
let output = program.output
if (_.isEmpty(output)) {
errors.push(chalk.bold.red('Output file path required! --output <path>'))
}
let background = program.background
// background = Number(background)
let direction = program.direction
if (errors.length > 0) {
errors.forEach((error, index) => console.log((index + 1) + '. ' + error))
program.outputHelp()
process.exit(1)
}
let maxWidth = Number(program.maxWidth)
maxWidth = maxWidth > 0 ? maxWidth : 0
let maxHeight = Number(program.maxHeight)
maxHeight = maxHeight > 0 ? maxHeight : 0
ImageMerge(files, output, direction, background, maxWidth, maxHeight)