-
Notifications
You must be signed in to change notification settings - Fork 12
/
cli.js
112 lines (98 loc) · 2.49 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/env node
var smushit = require('./smushit')
, util = require('util')
, Persist = require('./lib/persist').Persist
, persist = new Persist(__dirname + '/persist.json');
var responses = {
error: function (message) {
util.puts('Error occurred: ' + message);
},
help: function () {
util.puts([
'Usage: smushit file1 file2 file3',
'Smash your images down to size. smushit uses !Yahoo online Smush.it web service',
'to reduce your image size.',
'',
'(Note: due to the way the args are parsed, two hyphens -- are required after',
' binary flags if they appear before file paths)',
'',
'Options:',
'',
' General:',
' -v, --verbose verbose mode',
'',
' Traversing:',
' -R, --recursive scan directories recursively',
'',
' Output:',
' -o, --output the path to save',
'',
' Other:',
' -h, --help print this help page',
' --version print program version',
'',
' Examples:',
' Single File',
' smushit image.png',
'',
' Single Directory',
' smushit /var/www/mysite.com/images/products/backgrounds',
'',
' Multiple Files',
' smushit foo.png bar.png baz.png qux.png',
'',
' Recursive Directory',
' smushit -R -- /var/www/mysite.com/images',
''
].join('\n'));
},
report: function () {
},
version: function () {
util.puts('smushit v0.3.0');
}
};
function respond (type) {
responses[type].call();
}
var argv = require('optimist').argv;
if (argv.help || argv.h) {
respond('help');
} else if (argv.version) {
respond('version');
} else if (argv.c || argv.config){
var s = argv.c || argv.config;
if(s === true){
persist.each(function(key, value){
console.log(' smushit config: %s = %s ', key, value);
});
return;
}
s = s.split('=');
var key = s[0],
value = s[1];
if(value == undefined){
console.log(' smushit config: %s = %s ', key, persist.getItem(key));
}else if(value === ''){
persist.removeItem(key);
console.log(' smushit delete config key: %s', key);
}else{
persist.setItem(key, value);
console.log(' smushit config: %s = %s ', key, persist.getItem(key));
}
}else if(argv._.length) {
var settings = {};
if (argv.R || argv.recursive) {
settings.recursive = true;
}
if (argv.v || argv.verbose) {
settings.verbose = true;
}
if(argv.o || argv.output){
settings.output = argv.o || argv.output;
}
settings.service = persist.getItem('service');
smushit.smushit(argv._, settings);
} else {
respond('help');
}