-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter_base.js
94 lines (75 loc) · 2.58 KB
/
filter_base.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
/* jshint: node=true */
(function() {
'use strict';
var fs = require( 'fs' );
var _ = require('lodash');
var Promise = require('bluebird');
var exec = Promise.promisifyAll( require('child_process') );
/**
* Inspect the input data and may return usefull
* interpreation of them
*/
exports.inspect = function( filter_name, callback ) {
return function( data, time ) {
var stats;
if ( fs.existsSync( data.filename ) ) {
stats = fs.statSync( data.filename );
}
data.size = stats.size;
data.filter = filter_name;
data.time = time;
if ( typeof callback === 'function' ) {
data = callback( stats, data );
}
return data;
};
};
exports.execCmd = function( filter_name, bin_path, default_options, callback ) {
return function ( filename, options ) {
options = options || {};
var action, promise;
_.defaults( options, default_options );
if ( typeof callback === 'function' ) {
action = callback( filename, options );
}
if ( _.isArray( action ) || typeof action === 'string' ) {
promise = exec.execFileAsync( bin_path, action );
} else if ( typeof action === 'function' ) {
promise = action();
} else if ( action ) { // not null, undefined
promise = action;
}
promise = promise.then( function( output ) {
return {
filename : options.resultFilename,
output : {
stderr : output[ 0 ],
stdout : output[ 1 ]
},
parameters : options.parameters
};
} );
return promise;
};
};
exports.cleanup = function( filter_name, callback ) {
return function() {
if ( typeof callback === 'function' ) {
callback();
}
};
};
exports.cleanupManualTemps = function( filter_name, temp_files, callback ) {
return function() {
console.log( filter_name + ': cleaning up pngcrush' );
temp_files.forEach( function( path ) {
if ( fs.existsSync( path ) ) {
fs.unlinkSync( path );
}
} );
if ( typeof callback === 'function' ) {
callback();
}
};
};
}());