Allow the user to specify port. (#474)

Fixes #464
This commit is contained in:
Tom MacWright 2016-07-14 13:51:43 -04:00 committed by GitHub
parent 2f59200378
commit 211a26027c
4 changed files with 40 additions and 9 deletions

View File

@ -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) {

View File

@ -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));

View File

@ -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(/<html>/), '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() {}');

View File

@ -26,8 +26,18 @@ var indexFile = new File({
contents: new Buffer('<html>')
});
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 () {