-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·194 lines (178 loc) · 6.83 KB
/
Copy pathindex.js
File metadata and controls
executable file
·194 lines (178 loc) · 6.83 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*jslint sub:true */
var CMDS = [' info \t show convert status information'],
PYTHON_VERSION = '2.7',
IMAGEMAGICK_VERSION = '6.7.6',
TAG_ERROR = 'error',
path = require('path'),
fs = require('fs'),
Doc = require('./doc'),
exec = require('child_process').exec;
function find_plugin_config(config) {
for (var i = 0; i < config.plugins.length; i++) {
if (config.plugins[i].name === 'doc') {
return config.plugins[i];
}
}
return null;
}
function info (rootdir, pconfig) {
var rootfolder = path.resolve(rootdir, pconfig.docfolder),
errlist = [],
count = 0,
errcount = 0,
j;
function _find (dir) {
var ds = fs.readdirSync(path.join(rootfolder, dir)),
filename, i;
if (ds.length === 0) return;
if (fs.existsSync(path.join(rootfolder, dir, 'origin'))) {
count++;
if (fs.existsSync(path.join(rootfolder, dir, TAG_ERROR))) {
filename = fs.readdirSync(path.join(rootfolder, dir, 'origin'))[0];
errlist.push(path.resolve(rootfolder, dir, 'origin', filename));
errcount++;
}
return;
}
for (i = 0; i < ds.length; i++) {
if (fs.lstatSync(path.join(rootfolder, dir, ds[i])).isDirectory()) {
_find(path.join(dir, ds[i]));
}
}
return;
}
process.stdout.write('\nGathering doc convert status information... ');
_find('.');
process.stdout.write('done\n\n');
if (errcount > 0) {
console.log('Error converting ' + errcount + ':\n');
for (j = 0; j < errlist.length; j++) {
console.log(' ' + errlist[j]);
}
console.log('\n');
}
console.log(' Total docs: ' + count + ', Total error: ' + errcount);
}
function docmd (argv, config, rootdir) {
var pconfig = {};
if (argv.length === 0 || argv[0] === '-h' || argv[0] === '--help') {
return console.log('Available commands: \n' + CMDS[0] + '\n');
}
if (argv[0] === 'info') {
pconfig = find_plugin_config(config);
info(rootdir, pconfig);
}
}
function check_python (next) {
exec('python -V', function(error, stdout, stderr) {
if (error) {
next('Python no found');
} else if (('' + stderr).indexOf(PYTHON_VERSION) === -1){
next('Bad version: ' + stderr + ' But Python ' + PYTHON_VERSION + '.x is needed');
} else {
console.log('Doc converter: Found python ' + stderr.replace(/\s+$/, ''));
next();
}
});
}
function check_imageMagick (next) {
exec('convert --version', function(error, stdout, stderr) {
var m = ('' + stdout).match(/Version:\s+ImageMagick\s+(\S+)\s+/);
if (error) {
next('Command "convert" no found -- perhaps imageMagick and Ghostscript not installed, or not in path environment variables');
} else if (!m){
next('Bad imageMagick version --- ' + IMAGEMAGICK_VERSION + '+ is needed');
} else {
console.log('Doc converter: Found imageMagick ' + m[1]);
next();
}
});
}
function pdfconvert_test (rootdir, pconfig, next) {
var script = path.resolve(__dirname, 'support', 'scripts', pconfig.msoffice2pdf ? 'msoffice2pdf.py' : 'unoconv'),
pathstr = path.resolve(__dirname, 'support', 'convertest', 'test'),
cmdstr = 'python "' + script + '"' + ' -o "' + pathstr + '.pdf" "' + pathstr + '.docx"',
option = {};
option = {
timeout: 60 * 2 * 1000
};
exec(cmdstr, option, function(error, stdout, stderr) {
if (error) {
next('pdf convert fail -- please check office environment');
} else if (!fs.existsSync(path.join(__dirname, 'support', 'convertest', 'test.pdf'))){
next('pdf convert fail -- please check office environment');
} else {
next();
}
});
}
function imageconvert_test (rootdir, pconfig, next) {
var cmdstr = 'convert -density 96 test.pdf test.png',
option = {};
option = {
timeout: 60 * 1 * 1000,
cwd: path.join(__dirname, 'support', 'convertest')
};
exec(cmdstr, option, function(error, stdout, stderr) {
if (error) {
next('image convert fail -- please check imageMagick and Ghostscript environment');
} else if (!fs.existsSync(path.join(__dirname, 'support', 'convertest', 'test-1.png'))){
next('image convert fail -- please check imageMagick and Ghostscript environment');
} else {
next();
}
});
}
function build (config, rootdir, next) {
var pconfig = find_plugin_config(config);
if (fs.existsSync(path.join(__dirname, 'support', 'convertest', 'test-1.png'))) {
console.log('Doc converter: found test result image files, bypass converting test');
} else {
console.log('Doc converter: testing python...');
check_python(function(err) {
if (err) {
console.log('\t' + err);
next('Doc-plugin fail');
} else {
console.log('Doc converter: testing imageMagick( and Ghostscript)...');
check_imageMagick(function(err) {
if (err) {
console.log('\t' + err);
next('Doc-plugin fail');
} else {
console.log('Doc converter: testing pdf convert...');
pdfconvert_test(rootdir, pconfig, function(err) {
if (err) {
console.log('\t' + err);
next('Doc-plugin fail');
} else {
console.log('Doc converter: testing pdf convert...');
imageconvert_test(rootdir, pconfig, function(err) {
if (err) {
console.log('\t' + err);
next('Doc-plugin fail');
} else {
console.log('Convert test success!');
next();
}
});
}
});
}
});
}
});
}
}
function init (mm) {
var pconfig = find_plugin_config(mm.config),
libreofficescript = path.resolve(__dirname, 'scripts', 'unoconv');
//start a permanent listener to use by unoconv clients
exec('python "' + libreofficescript + '" -l');
new Doc(mm, pconfig);
}
exports.docmd = docmd;
exports.build = build;
exports.post_build = null;
exports.init = init;
exports.post_init = null;