-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunoconv.js
88 lines (69 loc) · 1.66 KB
/
unoconv.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
// Derived from the MIT-licensed https://github.com/gfloyd/node-unoconv
var _ = require('underscore'),
childProcess = require('child_process'),
unoconv;
exports = unoconv = module.exports = {};
/**
* Convert a document.
*
* @param {String} file
* @param {String} outputFormat
* @param {Object|Function} options
* @param {Function} callback
* @api public
*/
unoconv.convert = function(file, outputFormat, options, callback) {
'use strict';
var self = this,
args,
bin = 'unoconv',
child,
stderr = [],
resultFile = file + '.' + outputFormat;
if (_.isFunction(options)) {
callback = options;
options = null;
}
args = [
'-f' + outputFormat,
'-o' + resultFile
];
if (options && options.port) {
args.push('-p' + options.port);
}
args.push(file);
if (options && options.bin) {
bin = options.bin;
}
child = childProcess.spawn(bin, args);
child.stderr.on('data', function (data) {
stderr.push(data);
});
child.on('exit', function () {
if (stderr.length) {
return callback(new Error(Buffer.concat(stderr).toString()));
}
callback(null, resultFile);
});
};
/**
* Start a listener.
*
* @param {Object} options
* @return {ChildProcess}
* @api public
*/
unoconv.listen = function (options) {
'use strict';
var self = this,
args,
bin = 'unoconv';
args = [ '--listener' ];
if (options && options.port) {
args.push('-p' + options.port);
}
if (options && options.bin) {
bin = options.bin;
}
return childProcess.spawn(bin, args);
};