-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
86 lines (71 loc) · 2.17 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
var stat = require("fs").stat;
var spawn = require("child_process").spawn;
module.exports = async function() {
// Convert arguments to actual array.
var args= Array.prototype.slice.call(arguments);;
return new Promise((resolve, reject) => {
var src;
var options;
var callback;
var pdSpawn;
var result = "";
var isURL;
// Event Handlers
var onStdOutData;
var onStdOutEnd;
var onStdErrData;
var onStatCheck;
isURL = function(src) {
return /^(https?|ftp):\/\//i.test(src);
};
onStdOutData = function(data) {
result += data;
};
onStdOutEnd = function() {
console.log('Finish')
resolve(result || true);
pdSpawn.kill('SIGINT');
};
onStdErrData = function(err) {
reject(new Error(err));
pdSpawn.kill('SIGINT');
};
onStatCheck = function(err, stats) {
// If src is a file or valid web URL, push the src back into args array
if ((stats && stats.isFile()) || isURL) {
args.unshift(src);
}
// Create child_process.spawn
pdSpawn = spawn("inkscape", args, options);
// If src is not a file, assume a string input.
if (typeof stats === "undefined" && !isURL) {
pdSpawn.stdin.end(src, "utf-8");
}
// Set handlers...
pdSpawn.stdout.on("data", onStdOutData);
pdSpawn.stdout.on("end", onStdOutEnd);
pdSpawn.on("error", onStdErrData);
};
// Save src out of the args array.
src = args.shift();
// Check if src is URL match.
isURL = isURL(src);
// At this point, args array should be atlest .length
// of 1. If .length is 2, we have an Options object.
if (args.length == 2 && args[1].constructor !== Array) {
options = args.pop();
}
// Pull only remaining element from
// the args Array and overwrite itself.
args = args.shift();
// Array of arguments are required for inkscape.
// If arguments are in String format, convert
// them to an array to use
// in the child_process.spawn() call.
if (args.constructor === String) {
args = args.split(" ");
}
// Check file status of src
stat(src, onStatCheck);
});
};