forked from lge88/node-gmsh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
127 lines (106 loc) · 2.77 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
var fs = require('fs');
var path = require('path');
var mkdirp = require('mkdirp');
var shortId = require('shortid');
var fifojs = require('fifojs');
var exec = require('child_process').exec;
var spawn = require('child_process').spawn;
var _ = require('underscore');
mkdirp(__dirname + '/tmp');
var inFifo = __dirname + '/tmp/in.' + shortId.generate() + '.fifo';
var outFifo = __dirname + '/tmp/out.' + shortId.generate() + '.fifo';
fifojs.mkfifo(inFifo, 0777);
fifojs.mkfifo(outFifo, 0777);
process.on('exit', function() {
fs.unlink(inFifo);
fs.unlink(outFifo);
});
process.stdout.on('data', function() {
console.log('stdout:', data);
});
process.stderr.on('data', function() {
console.log('stderr:', data);
});
var gmsh = function(source, format) {
if (!(this instanceof gmsh)) {
return new gmsh(source, format);
}
var _this = this;
this._in = inFifo;
this._in = fs.createWriteStream(inFifo);
this._in.on('open', function() {
if (_this._pipe) {
_this._source.pipe(_this._in);
} else {
// FIXME: Without this line it won't work. Don't know why
_this._in.write('\n');
_this._in.write(_this._source);
_this._in.end();
}
});
this._in.on('error', function(err) {
console.log(err);
});
if (typeof source === 'string') {
if (!format) {
this._pipe = true;
this._source =fs.createReadStream(source);
} else {
this._pipe = false;
if (format === 'geo') {
this._source = source;
}
}
} else if (source instanceof fs.ReadStream){
this._pipe = true;
this._source = source;
}
this._out = fs.createReadStream(outFifo);
this._options = {};
this._cmdOptions = '';
this.geo = {};
this._mshCache = '';
return this;
};
gmsh.prototype.options = function(opt) {
if (typeof opt === 'string') {
this._cmdOptions += opt;
} else if (Array.isArray(opt)){
this._cmdOptions += opt.join(' ');
} else if (typeof opt === 'object') {
_.merge(this._options, opt);
}
return this;
};
gmsh.prototype.onOutStream = function(name, cb) {
this._out.on(name, cb);
return this;
};
gmsh.prototype.onData = function(cb) {
this._out.on('data', cb);
return this;
};
gmsh.prototype.writeOptionsToString = function() {
};
gmsh.prototype.mesh = function(cb1, cb2) {
var cmd = ['gmsh']
.concat(this._cmdOptions)
.concat([
path.resolve(this._in.path),
'-o',
path.resolve(this._out.path)
]).join(' ');
var _this = this;
this._out.on('data', function(data) {
_this._mshCache += data.toString();
});
if (arguments.length >= 2 && _.isFunction(cb2)) {
this._out.on('data', cb1);
exec(cmd, cb2);
} else {
exec(cmd, cb1);
}
return this;
};
gmsh.prototype.exec = gmsh.prototype.run = gmsh.prototype.mesh;
module.exports = gmsh;