-
Notifications
You must be signed in to change notification settings - Fork 38
/
printer.js
86 lines (63 loc) · 2.09 KB
/
printer.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
var Job = require ("./job");
var spawn = require("child_process").spawn;
var _ = require ("underscore");
var optionsFactory = function (options) {
var defaultOptions = {};
defaultOptions.encryption = false;
defaultOptions.username = null;
defaultOptions.backwardsCompatibility = false;
defaultOptions.destination = null;
defaultOptions.hostname = null;
defaultOptions.numCopies = 1;
defaultOptions.priority = 1;
defaultOptions.media = "a4";
defaultOptions.fitplot = false;
return _.defaults(options, defaultOptions);
};
var argsFactory = function (options) {
var args = [];
if (true === options.encryption) {
args.push ("-E");
}
if (_.isString(options.username)) {
args.push("-U " + options.username);
}
if (true === options.backwardsCompatibility) {
args.push("-c");
}
if (_.isString(options.destination)) {
args.push("-d " + options.destination);
}
if (_.isString(options.hostname)) {
args.push("-h " + options.hostname);
}
if (!_.isEmpty(options.numCopies) && _.isNumber(options.numCopies) && options.numCopies > 1) {
args.push("-n " + options.numCopies);
}
if (!_.isEmpty(options.priority) && _.isNumber(options.priority) && options.priority > 1) {
args.push("-q " + options.priority);
}
if (_.isString(options.media)) {
args.push("-o media=" + options.media);
}
if (true === options.fitplot) {
args.push("-o fitplot");
}
return args;
};
module.exports.printText = function (text, options, identifier) {
options = optionsFactory(options);
var args = argsFactory(options);
var lp = spawn("lp", args);
lp.stdin.write(text);
lp.stdin.end();
return new Job(lp, identifier);
}
module.exports.printFile = function (file, options, identifier) {
options = optionsFactory(options);
var args = argsFactory(options);
args.push ("--");
args.push (file);
var lp = spawn("lp", args);
return new Job(lp, identifier);
}