-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
57 lines (42 loc) · 1.15 KB
/
index.js
File metadata and controls
57 lines (42 loc) · 1.15 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
var execSync = require('child_process').execSync;
var fs = require('fs');
var temp = require('temp-fs');
var path = require('path');
var JSONB = require('json-buffer');
module.exports = function (filePath, args) {
if (args === undefined) {
args = [];
}
if (typeof args !== 'object' || args.length === undefined) {
throw new Error('Arguments should be an array.');
}
var content = null;
var result = null;
var error = false;
var inputFile = temp.name();
var outputFile = temp.name();
try {
// Prepare
fs.writeFileSync(inputFile, JSONB.stringify(args), {encoding: 'utf8'});
var workerPath = path.join(__dirname, 'lib', 'worker.js');
// Execute synchronously
execSync('node "' + workerPath + '" "' + filePath + '" "' + inputFile + '" "' + outputFile + '"').toString();
} catch (err) {
error = true;
} finally {
if (fs.existsSync(inputFile)) {
content = fs.readFileSync(outputFile, {encoding: 'utf8'});
}
result = JSONB.parse(content);
if (fs.existsSync(inputFile)) {
fs.unlinkSync(inputFile);
}
if (fs.existsSync(outputFile)) {
fs.unlinkSync(outputFile);
}
}
if (error) {
throw result;
}
return result;
};