-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
78 lines (59 loc) · 1.96 KB
/
cli.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
#!/usr/bin/env node
const csshue = require('./index.js');
const chalk = require('chalk');
const getit = require('getit');
const path = require('path');
const fs = require('fs');
const program = require('commander');
// Avoid require 'magic' and use FS
const packageJson = JSON.parse( fs.readFileSync(__dirname + path.sep + 'package.json').toString() );
program
.version(packageJson.version)
// REQUIRED
.option('-i, --input [url]', 'Url or path of file to use as input.')
//OPTIONAL
.option('-o, --output [file]', 'path to output file, if not specified STDOUT is used')
.option('-r, --rotate [degree]', 'Number of degrees to rotate hue')
.option('-t, --text [value]', '0 - 1 value of text where 0 is black and 1 white, set to 0.1 by default.')
.option('-d, --desaturate [value]', '0 - 1 color desaturation upon rotation, set to 0.5 by default.')
.option('-f, --format [type]', 'HTML color format hex, rgb, hsl, hsv, hwb, cmyk')
.parse(process.argv);
if(!program.input) console.error(chalk.red('Error: input file is required for normal operation.'))
const bean = {
input: program.input,
output: program.output||'STDOUT',
rotate: parseInt(program.rotate)||0,
text: undefined,
desaturate: undefined,
format: program.format||'hex',
}
if(bean.input === bean.output){
console.error(chalk.red('Error: ', 'Input and Output cannot be the same.'))
process.exit(1);
}
if(program.text === '0'){
bean.text = 0;
} else {
bean.text = parseFloat(program.text)||.1;
}
if(program.desaturate === '0'){
bean.desaturate = 0;
} else {
bean.desaturate = parseFloat(program.desaturate)||.5;
}
getit(bean.input, function(err, css) {
if(err){
console.error(chalk.red('Error: ', err))
return;
}
bean.css = css;
csshue(bean).then(function(result){
if(bean.output === 'STDOUT'){
console.log(result)
}else if(bean.output === 'shh'){
// do nothing
}else{
fs.writeFileSync( path.normalize(bean.output), result);
}
});
});