Skip to content

Commit 45c541a

Browse files
committed
feature(spawnify) add write
1 parent 32ba781 commit 45c541a

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

lib/spawnify.js

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@
4242
function Spawnify(command, options) {
4343
var self = this;
4444

45-
this._child = null;
46-
4745
EventEmitter.call(this);
4846

4947
process.nextTick(function() {
@@ -103,8 +101,7 @@
103101
};
104102

105103
Spawnify.prototype._set = function set(type, command, options) {
106-
var args, error,
107-
self = this;
104+
var args, error, child;
108105

109106
check(arguments, ['type', 'command', 'options']);
110107

@@ -114,7 +111,7 @@
114111

115112
case 'exec':
116113
error = tryCatch(function() {
117-
self._child = exec(command, options);
114+
child = exec(command, options);
118115
});
119116
break;
120117

@@ -123,36 +120,45 @@
123120
command = args.shift();
124121

125122
error = tryCatch(function() {
126-
self._child = spawn(command, args, options);
123+
child = spawn(command, args, options);
127124
});
128125
break;
129126
}
130127

128+
child.stderr.setEncoding('utf-8');
129+
child.stdout.setEncoding('utf-8');
130+
child.stdin.setEncoding('utf-8');
131+
131132
if (error) {
132133
this._emit('error', newLine(error));
133134
} else {
135+
this._setListeners(child);
136+
134137
this.on('kill', function(code) {
135-
self._child.kill(code);
138+
child.kill(code);
136139
});
137140

138-
this._emit('start');
141+
this.on('write', function(data) {
142+
child.stdin.write(data);
143+
});
139144

140-
this._setListeners(this._child);
145+
this._emit('start');
141146
}
142147
};
143148

144149
Spawnify.prototype.kill = function(code) {
145150
this._emit('kill', code);
146151
};
147152

153+
Spawnify.prototype.write = function(data) {
154+
this._emit('write', data);
155+
};
156+
148157
Spawnify.prototype._setListeners = function setListeners(child) {
149158
var self = this;
150159

151160
check(arguments, ['child']);
152161

153-
child.stderr.setEncoding('utf8');
154-
child.stdout.setEncoding('utf8');
155-
156162
child.stdout.on('data', function(data) {
157163
self._emit('data', data);
158164
});

0 commit comments

Comments
 (0)