From 4c707356d0b2a93e4d5595bf2cf125f0d8682792 Mon Sep 17 00:00:00 2001 From: Maximilian Antoni Date: Tue, 21 Aug 2018 15:38:33 +0200 Subject: [PATCH] Consolidate `connect` logic --- lib/client.js | 19 +------------------ lib/connect.js | 20 ++++++++++++++++++++ lib/launcher.js | 22 +++------------------- 3 files changed, 24 insertions(+), 37 deletions(-) create mode 100644 lib/connect.js diff --git a/lib/client.js b/lib/client.js index 036e1e3..20b6c68 100644 --- a/lib/client.js +++ b/lib/client.js @@ -1,24 +1,7 @@ 'use strict'; -const net = require('net'); const out = require('./out'); -const portfile = require('./portfile'); - -function connect(callback) { - portfile.read((config) => { - if (!config) { - callback('Not running'); - return; - } - const socket = net.connect(config.port, '127.0.0.1', () => { - callback(null, socket, config.token); - }); - socket.once('error', () => { - process.exitCode = 1; - callback('Could not connect'); - }); - }); -} +const connect = require('./connect'); exports.stop = function (callback) { connect((err, socket, token) => { diff --git a/lib/connect.js b/lib/connect.js new file mode 100644 index 0000000..31fb3cf --- /dev/null +++ b/lib/connect.js @@ -0,0 +1,20 @@ +'use strict'; + +const net = require('net'); +const portfile = require('./portfile'); + +module.exports = function (callback) { + portfile.read((config) => { + if (!config) { + callback('Not running'); + return; + } + const socket = net.connect(config.port, '127.0.0.1', () => { + callback(null, socket, config.token); + }); + socket.once('error', () => { + process.exitCode = 1; + callback('Could not connect'); + }); + }); +}; diff --git a/lib/launcher.js b/lib/launcher.js index b2db3d7..3cf0bd1 100644 --- a/lib/launcher.js +++ b/lib/launcher.js @@ -1,26 +1,10 @@ 'use strict'; -const net = require('net'); const out = require('./out'); -const portfile = require('./portfile'); - -function check(callback) { - portfile.read((config) => { - if (!config) { - callback('Not running'); - return; - } - const socket = net.connect(config.port, '127.0.0.1', () => { - callback(null, socket, config.token); - }); - socket.once('error', () => { - callback('Could not connect'); - }); - }); -} +const connect = require('./connect'); function wait(callback) { - check((err, socket, token) => { + connect((err, socket, token) => { if (err) { setTimeout(() => { wait(callback); @@ -54,7 +38,7 @@ function launch(callback) { } exports.launch = function (callback) { - check((err, socket) => { + connect((err, socket) => { if (err) { launch(callback); } else {