-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
74 lines (58 loc) · 1.71 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
var spawn = require('child_process').spawn;
var through = require('through2');
var fs = require('fs');
var path = require('path');
var haxeRe = /\.hx$/i;
function isHaxe(file) {
return haxeRe.test(file);
}
function haxeToJs(file) {
return file + '.js';
}
function haxeToClass(file) {
return path.basename(file.replace(haxeRe, ''));
}
function haxeify(file, options) {
var cmd, args;
if (!isHaxe(file)) {
return through();
}
options = options || {};
cmd = options.haxe || 'haxe';
args = [haxeToClass(file), '-js', haxeToJs(file)];
if (typeof options.hxml == 'string') {
args.unshift(options.hxml);
}
if (typeof options.lib == 'string') {
args.push('-lib', options.lib);
} else if (Array.isArray(options.lib)) {
args = options.lib.reduce(function(prev, lib) { return prev.concat(['-lib', lib]); }, args);
}
if (typeof options.dce == 'string') {
args.push('-dce', options.dce);
}
if (process.cwd() != path.dirname(file)) {
args.push('-cp', path.resolve(process.cwd(), path.dirname(file)));
}
return through(function(_, __, next) {
var that = this;
var stderr = '';
var haxe = spawn(cmd, args);
haxe.stderr.on('data', function (buf) {
stderr += buf;
});
haxe.on('close', function (code) {
if (code !== 0) {
that.emit('error', stderr);
}
fs.readFile(haxeToJs(file), function(err, data) {
if (err) {
that.emit('error', err);
}
that.push(data);
next();
});
});
});
}
module.exports = haxeify;