-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
114 lines (105 loc) · 2.78 KB
/
index.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
113
/**
* Needed libaries to successfully access
* @type {"fs"}
* @type {"chokidar"}
* @type {"cmd"}
*/
const fs = require('fs');
const chokidar = require('chokidar');
const cmd = require('node-cmd');
/**
* Variables used within the process;
* Autoprint folder
* Foxit location,
* Printer name
* @type {string}
*/
var autoPrintFolder = '\\\\KENJI-LAPTOP\\autoprint';
var foxit = 'C:\\Program Files (x86)\\Foxit Software\\Foxit Reader\\FoxitReader.exe';
var printer = 'HP Officejet Pro 8620';
/**
* Loop through provided arguments for the file
* If found, replace default values
*/
process.argv.forEach(function (val, index, array) {
if(index == 2) {
autoPrintFolder = val;
}
if(index == 3) {
foxit = val;
}
if(index == 4) {
printer = val;
}
});
/**
* Provides the user with information on the arguments for the process
*/
console.log('Selected folder: ' + autoPrintFolder + '\n' +
'Foxit location: ' + foxit + '\n' +
'Selected Printer: ' + printer);
/**
* File watcher checks provided path foe new files
* Timer that resets on file changes
*/
//var watcher = chokidar.watch(autoPrintFolder, {ignored: /^\./, persistent: true,usePolling: true});
//'\\\\NEO-TH-AD12\\files\\scanned-files\\auto-print'
var watcher = chokidar.watch(autoPrintFolder, {
ignored: /(^|[\/\\])\../,
persistent: true,
usePolling: true
});
var initialTimer;
/**
* Watch for file changes
* @function {attemptPrint} Starts after clearing the timer
* ensuring that the printing doesn't start premature
*/
watcher
.on('add', function(path) {//wait 10 seconds before attempting to print
console.log('File', path, 'has been changed');
clearTimeout(initialTimer);
attemptPrint(path);
});
/**
* When run, the function sets an empty var as a timer
* After the timeout is reached, the file is checked to see its okay to write
* @param path
*/
function attemptPrint(path) {
initialTimer = setTimeout(function() {
canWrite(path, function(err, isWritable) {
if(isWritable) {
pdfPrint(path);
}
});
},15000);
}
/**
* Prints to PDF using the foxit PDF reader location
* provided path and printer name.
* If errors occur they are printed.
* @param path
*/
function pdfPrint(path) {
cmd.get('"' + foxit + '" /t ' + path + ' "' + printer + '"', function(err, data, stderr)
{
if(!err) {
console.log('Printing to',path,'successful.');
}else{
console.log(err);
}
if(data)
console.log(data);
});
}
/**
* Simple check to see if a file can be written to.
* @param path
* @param callback
*/
function canWrite(path, callback) {
fs.access(path, fs.W_OK, function(err) {
callback(null, !err);
});
}