Skip to content

Commit

Permalink
fix: write errors to stderr (#26)
Browse files Browse the repository at this point in the history
fix: write errors to stderr

Co-authored-by: n3oney <neo@neoney.dev>
  • Loading branch information
fsouza and n3oney committed Sep 22, 2023
1 parent b012b6b commit d76e925
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 10 deletions.
2 changes: 1 addition & 1 deletion lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function write(chunk) {
}

function fail(err) {
out.write(`${err}\n`);
out.writeError(`${err}\n`);
}

function sendCommand(command, callback) {
Expand Down
4 changes: 2 additions & 2 deletions lib/launcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ exports.launch = function (callback) {
connect((err, socket) => {
if (err) {
if (process.exitCode) {
out.write(`${err}\n`);
out.writeError(`${err}\n`);
return;
}
launch(callback);
} else {
socket.end();
out.write('Already running\n');
out.writeError('Already running\n');
}
});
};
4 changes: 4 additions & 0 deletions lib/out.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@
exports.write = function (message) {
process.stdout.write(message);
};

exports.writeError = function (message) {
process.stderr.write(message);
};
11 changes: 6 additions & 5 deletions test/client-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ describe('client', () => {
socket = new EventEmitter();
socket.end = sinon.fake();
sinon.replace(out, 'write', sinon.fake());
sinon.replace(out, 'writeError', sinon.fake());
});

afterEach(() => {
Expand All @@ -41,7 +42,7 @@ describe('client', () => {

client[method](...args);

assert.calledOnceWith(out.write, 'Not running\n');
assert.calledOnceWith(out.writeError, 'Not running\n');
assert.isUndefined(process.exitCode);
}

Expand All @@ -55,7 +56,7 @@ describe('client', () => {

socket.emit('error', new Error());

assert.calledOnceWith(out.write, 'Could not connect\n');
assert.calledOnceWith(out.writeError, 'Could not connect\n');
assert.equals(process.exitCode, 1);
}

Expand All @@ -81,7 +82,7 @@ describe('client', () => {
client.start();
net.connect.firstCall.callback();

assert.calledOnceWith(out.write, 'Already running\n');
assert.calledOnceWith(out.writeError, 'Already running\n');
});

});
Expand Down Expand Up @@ -317,7 +318,7 @@ describe('client', () => {

launcher.launch.firstCall.callback('Could not connect');

assert.calledOnceWith(out.write, 'Could not connect\n');
assert.calledOnceWith(out.writeError, 'Could not connect\n');
assert.equals(process.exitCode, 1);
});

Expand All @@ -330,7 +331,7 @@ describe('client', () => {
socket.emit('error', new Error());

refute.called(launcher.launch);
assert.calledOnceWith(out.write, 'Could not connect\n');
assert.calledOnceWith(out.writeError, 'Could not connect\n');
assert.equals(process.exitCode, 1);
});

Expand Down
4 changes: 2 additions & 2 deletions test/launcher-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,13 +184,13 @@ describe('launcher', () => {

it('prints message and does not invoke callback if already running', () => {
sinon.replace(portfile, 'read', sinon.fake.yields({ port: 7654, token }));
sinon.replace(out, 'write', sinon.fake());
sinon.replace(out, 'writeError', sinon.fake());
const connect = sinon.replace(net, 'connect', sinon.fake.returns(socket));

launcher.launch(callback);
connect.firstCall.callback();

assert.calledOnceWith(out.write, 'Already running\n');
assert.calledOnceWith(out.writeError, 'Already running\n');
refute.called(callback);
});

Expand Down

0 comments on commit d76e925

Please sign in to comment.