-
Notifications
You must be signed in to change notification settings - Fork 15
/
cli.js
executable file
·91 lines (72 loc) · 2.61 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
#!/usr/bin/env node
// Very simple CLI wrapper around canvas-plus
// Beware: This will REPLACE local images with the specified filters applied, unless "--output FILE" is specified.
// Usage: ./cli.js FILE1 [FILE2 ...] --filter "resize/width:400/height:300" --filter "adjust/hue:-60" --filter "write/quality:90"
var fs = require('fs');
var cli = require('pixl-cli');
var CanvasPlus = require('./main.js');
cli.global();
var Tools = cli.Tools;
var async = Tools.async;
var args = cli.args;
var files = args.other;
var usage = `Usage: ./cli.js FILE1 [FILE2 ...] --filter "resize/width:400/height:300" --filter "adjust/hue:-60" --filter "write/quality:90"\n`;
if (!files || !files.length) die(usage);
if (!args.filter) die(usage);
var filters = [];
Tools.alwaysArray(args.filter).forEach( function(raw) {
if (!raw.match(/^(\w+)\//)) die("Malformed filter: " + raw + "\n");
var name = RegExp.$1;
var params = {};
raw = raw.replace(/^(\w+)\//, '');
raw.split(/\//).forEach( function(arg) {
if (!arg.match(/^(.+)\:(.+)$/)) die("Malformed filter: " + raw + "\n");
var key = RegExp.$1;
var value = RegExp.$2;
if (value.match(/^\-?\d+\.\d+$/)) value = parseFloat(value);
else if (value.match(/^\-?\d+$/)) value = parseInt(value);
else if (value.match(/^true$/)) value = true;
else if (value.match(/^false$/)) value = false;
params[ key ] = value;
});
params._name = name;
filters.push( params );
} );
async.eachSeries( files,
function(file, callback) {
var canvas = new CanvasPlus();
if (args.debug || args.verbose) canvas.set('debug', true);
var obj = {};
for (var key in args) {
if (key.match(/^params\.(.+)$/)) Tools.setPath(obj, RegExp.$1, args[key]);
}
canvas.set(obj);
canvas.load( file, function(err) {
if (err) return callback(err);
var write_opts = {};
filters.forEach( function(filter) {
var name = filter._name;
var params = Tools.copyHashRemoveKeys(filter, { _name: 1 });
if (name == 'write') write_opts = params;
else canvas[name](params);
if (canvas.getLastError()) {
die("Failed to process image: " + file + ": " + name + ":" + err + "\n");
}
}); // forEach filter
// write
canvas.write( write_opts, function(err, buf) {
if (err) return callback(err);
fs.writeFile( args.output || file, buf, function(err) {
if (err) return callback(err);
print("Wrote: " + (args.output || file) + "\n");
canvas.logDebug(3, "Performance Metrics: ", canvas.getMetrics() );
callback();
});
} );
}); // load
},
function(err) {
if (err) die("Error: " + err + "\n");
print("Done!\n");
}
); // eachSeries