From 211a26027cd882c6718e8d048803ac279b104f64 Mon Sep 17 00:00:00 2001 From: Tom MacWright Date: Thu, 14 Jul 2016 13:51:43 -0400 Subject: [PATCH] Allow the user to specify port. (#474) Fixes #464 --- lib/commands/serve.js | 16 ++++++++++------ lib/serve/server.js | 9 +++++++-- test/bin-watch-serve.js | 12 ++++++++++++ test/lib/server.js | 12 +++++++++++- 4 files changed, 40 insertions(+), 9 deletions(-) diff --git a/lib/commands/serve.js b/lib/commands/serve.js index 566d7e6..c1cb0e1 100644 --- a/lib/commands/serve.js +++ b/lib/commands/serve.js @@ -16,14 +16,14 @@ module.exports.description = 'generate, update, and display HTML documentation'; */ module.exports.parseArgs = function (yargs) { return sharedOptions.sharedOutputOptions( - sharedOptions.sharedInputOptions(yargs)); + sharedOptions.sharedInputOptions(yargs)) + .option('port', { + describe: 'port for the local server', + type: 'number', + default: 4001 + }); }; -var server = new Server(); -server.on('listening', function () { - process.stdout.write('documentation.js serving on port 4001\n'); -}); - /** * Wrap the documentation build command along with a server, making it possible * to preview changes live @@ -33,6 +33,10 @@ server.on('listening', function () { * @returns {undefined} has side effects */ function serve(documentation, parsedArgs) { + var server = new Server(parsedArgs.options.port); + server.on('listening', function () { + process.stdout.write('documentation.js serving on port ' + parsedArgs.options.port + '\n'); + }); parsedArgs.commandOptions.format = 'html'; build(documentation, parsedArgs, function (err, output) { if (err) { diff --git a/lib/serve/server.js b/lib/serve/server.js index 15cdb73..edfc059 100644 --- a/lib/serve/server.js +++ b/lib/serve/server.js @@ -11,8 +11,13 @@ var http = require('http'), * of files and notifies any browsers using LiveReload to reload * and display the new content. * @class + * @param {number} port server port to serve on. */ -function Server() { +function Server(port) { + if (typeof port !== 'number') { + throw new Error('port argument required to initialize a server'); + } + this._port = port; this._files = []; } @@ -79,7 +84,7 @@ Server.prototype.start = function (callback) { this._http = http.createServer(this.handler.bind(this)); this._lr.listen(35729, function () { - this._http.listen(4001, function () { + this._http.listen(this._port, function () { this.emit('listening'); callback(); }.bind(this)); diff --git a/test/bin-watch-serve.js b/test/bin-watch-serve.js index 07a2616..59ebcab 100644 --- a/test/bin-watch-serve.js +++ b/test/bin-watch-serve.js @@ -53,6 +53,18 @@ test('provides index.html', function (t) { }); }, options); +test('accepts port argument', function (t) { + var docProcess = documentation(['serve', 'fixture/simple.input.js', '--port=4004']); + docProcess.stdout.on('data', function (data) { + t.equal(data.toString().trim(), 'documentation.js serving on port 4004', 'shows listening message'); + get('http://localhost:4004/', function (text) { + t.ok(text.match(//), 'sends an html index file'); + docProcess.kill(); + t.end(); + }); + }); +}, options); + test('--watch', function (t) { var tmpFile = path.join(os.tmpdir(), '/simple.js'); fs.writeFileSync(tmpFile, '/** a function */function apples() {}'); diff --git a/test/lib/server.js b/test/lib/server.js index 2fca53c..3015108 100644 --- a/test/lib/server.js +++ b/test/lib/server.js @@ -26,8 +26,18 @@ var indexFile = new File({ contents: new Buffer('') }); +test('server - throws on bad port', function (t) { + t.throws(function () { + var server = new Server('4001'); + }, 'port must be a number'); + t.throws(function () { + var server = new Server(); + }, 'port must be provided'); + t.end(); +}); + test('server', function (t) { - var server = new Server(); + var server = new Server(4001); t.ok(server, 'server is initialized'); server.start(function () {