|
42 | 42 | function Spawnify(command, options) { |
43 | 43 | var self = this; |
44 | 44 |
|
45 | | - this._child = null; |
46 | | - |
47 | 45 | EventEmitter.call(this); |
48 | 46 |
|
49 | 47 | process.nextTick(function() { |
|
103 | 101 | }; |
104 | 102 |
|
105 | 103 | Spawnify.prototype._set = function set(type, command, options) { |
106 | | - var args, error, |
107 | | - self = this; |
| 104 | + var args, error, child; |
108 | 105 |
|
109 | 106 | check(arguments, ['type', 'command', 'options']); |
110 | 107 |
|
|
114 | 111 |
|
115 | 112 | case 'exec': |
116 | 113 | error = tryCatch(function() { |
117 | | - self._child = exec(command, options); |
| 114 | + child = exec(command, options); |
118 | 115 | }); |
119 | 116 | break; |
120 | 117 |
|
|
123 | 120 | command = args.shift(); |
124 | 121 |
|
125 | 122 | error = tryCatch(function() { |
126 | | - self._child = spawn(command, args, options); |
| 123 | + child = spawn(command, args, options); |
127 | 124 | }); |
128 | 125 | break; |
129 | 126 | } |
130 | 127 |
|
| 128 | + child.stderr.setEncoding('utf-8'); |
| 129 | + child.stdout.setEncoding('utf-8'); |
| 130 | + child.stdin.setEncoding('utf-8'); |
| 131 | + |
131 | 132 | if (error) { |
132 | 133 | this._emit('error', newLine(error)); |
133 | 134 | } else { |
| 135 | + this._setListeners(child); |
| 136 | + |
134 | 137 | this.on('kill', function(code) { |
135 | | - self._child.kill(code); |
| 138 | + child.kill(code); |
136 | 139 | }); |
137 | 140 |
|
138 | | - this._emit('start'); |
| 141 | + this.on('write', function(data) { |
| 142 | + child.stdin.write(data); |
| 143 | + }); |
139 | 144 |
|
140 | | - this._setListeners(this._child); |
| 145 | + this._emit('start'); |
141 | 146 | } |
142 | 147 | }; |
143 | 148 |
|
144 | 149 | Spawnify.prototype.kill = function(code) { |
145 | 150 | this._emit('kill', code); |
146 | 151 | }; |
147 | 152 |
|
| 153 | + Spawnify.prototype.write = function(data) { |
| 154 | + this._emit('write', data); |
| 155 | + }; |
| 156 | + |
148 | 157 | Spawnify.prototype._setListeners = function setListeners(child) { |
149 | 158 | var self = this; |
150 | 159 |
|
151 | 160 | check(arguments, ['child']); |
152 | 161 |
|
153 | | - child.stderr.setEncoding('utf8'); |
154 | | - child.stdout.setEncoding('utf8'); |
155 | | - |
156 | 162 | child.stdout.on('data', function(data) { |
157 | 163 | self._emit('data', data); |
158 | 164 | }); |
|
0 commit comments