Skip to content
This repository has been archived by the owner on Feb 18, 2021. It is now read-only.

Commit

Permalink
refactor application.js
Browse files Browse the repository at this point in the history
  • Loading branch information
vivaxy committed Nov 21, 2015
1 parent df9f877 commit 075c654
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 54 deletions.
29 changes: 18 additions & 11 deletions lib/application.js
Expand Up @@ -12,6 +12,7 @@ const usageTracker = require('usage-tracker');

const isDebug = require('./is-debug.js');
const startServer = require('./server.js');
const startWatcher = require('./watcher.js');
const packageJson = require('../package.json');

const main = () => {
Expand Down Expand Up @@ -66,28 +67,34 @@ const main = () => {


let middlewareList = [];
let workingDirectory = process.cwd();
let absoluteWorkingDirectory = process.cwd();
let directory = commander.directory;
if (path.isAbsolute(directory)) {
workingDirectory = directory;
absoluteWorkingDirectory = directory;
} else {
workingDirectory = path.join(workingDirectory, directory);
absoluteWorkingDirectory = path.join(absoluteWorkingDirectory, directory);
}
let liveReload = commander.watch;
if (liveReload === undefined) {
liveReload = false;
} else if (isNaN(parseFloat(liveReload))) {
liveReload = 0;
} else if (liveReload < 0) {
liveReload = 0;
}

try {
middlewareList = require(path.join(workingDirectory, 'here.js'));
middlewareList = require(path.join(absoluteWorkingDirectory, 'here.js'));
log.debug('route :', 'custom route found');
} catch (e) {
log.debug('route :', 'custom route not found');
}

startServer({
port: commander.port,
directory: workingDirectory,
middlewareList: middlewareList,
liveReload: commander.watch,
openBrowser: !commander.silent
});
let nativeServer = startServer(commander.port, absoluteWorkingDirectory, middlewareList, liveReload, !commander.silent);

if (liveReload !== false) {
startWatcher(nativeServer, absoluteWorkingDirectory, liveReload);
}

};

Expand Down
27 changes: 3 additions & 24 deletions lib/server.js
Expand Up @@ -4,34 +4,19 @@
*/
'use strict';

const path = require('path');

const ip = require('ip');
const koa = require('koa');
const log = require('log-util');
const usageTracker = require('usage-tracker');

const isDebug = require('./is-debug.js');

module.exports = (config) => {
module.exports = (port, absoluteWorkingDirectory, middlewareList, liveReload, openBrowser) => {

const server = koa();

let port = config.port || 3000;
let directory = config.directory || '.';
let middlewareList = config.middlewareList || [];
let liveReload = config.liveReload;
let openBrowser = config.openBrowser;

let absoluteWorkingDirectory = process.cwd();
if (path.isAbsolute(directory)) {
absoluteWorkingDirectory = directory;
} else {
absoluteWorkingDirectory = path.join(absoluteWorkingDirectory, directory);
}

middlewareList.push(require('../middleware/error.js'));
if (liveReload !== undefined) {
if (liveReload !== false) {
middlewareList.push(require('../middleware/live-reload.js'));
}
middlewareList.push(require('../middleware/file-explorer.js')(absoluteWorkingDirectory));
Expand All @@ -42,7 +27,7 @@ module.exports = (config) => {

const listen = () => {

let nativeServer = server.listen(port, () => {
return server.listen(port, () => {
// success
isDebug || usageTracker.send({
// event
Expand Down Expand Up @@ -70,12 +55,6 @@ module.exports = (config) => {
return listen();
}
});

if (liveReload !== undefined) {
require('./watcher.js')(nativeServer, absoluteWorkingDirectory, liveReload);
}

return server;
};

return listen();
Expand Down
4 changes: 0 additions & 4 deletions lib/watcher.js
Expand Up @@ -13,10 +13,6 @@ const socketServer = require('socket.io');

module.exports = (server, absoluteWorkingDirectory, interval) => {

if (interval === true || interval < 0) {
interval = 0;
}

let socketList = [];

let io = socketServer(server);
Expand Down
2 changes: 1 addition & 1 deletion middleware/error.js
Expand Up @@ -11,7 +11,7 @@ module.exports = function* (next) {
yield next;
} catch (e) {
this.status = 404;
this.body = e.stack.split('\n');
this.body = e.stack;
this.type = FALLBACK_CONTENT_TYPE;
}
};
75 changes: 61 additions & 14 deletions test/mocha.js
Expand Up @@ -3,46 +3,93 @@
* @author vivaxy
*/
'use strict';
var assert = require('assert');
var spawn = require('child_process').spawn;
var packageJson = require('../package.json');
const assert = require('assert');
const childProcess = require('child_process');

describe('test terminal command `here`', function () {
var here;
afterEach(function () {
const packageJson = require('../package.json');

const spawn = childProcess.spawn;

describe('test terminal command `here`', () => {
let here;
afterEach(() => {
here.kill();
});
it('`here` should output `[??:??:??.???] server : listen http://*.*.*.*:*/`', function (done) {
it('`here` should output `[??:??:??.???] server : listen http://*.*.*.*:*/`', done => {
here = spawn('node', ['./index.js']);
here.stdout.on('data', function (data) {
here.stdout.on('data', data => {
data = data.toString();
assert.equal(true, /^\[\d{2}:\d{2}:\d{2}\.\d{3}\] server : listen http:\/\/\d+\.\d+\.\d+\.\d+:\d+\/\n$/.test(data));
here.kill();
done();
});
});
it('`here -v` should output `version`', function (done) {
it('`here -v` should output `version`', done => {
here = spawn('node', ['./index.js', '-v']);
here.stdout.on('data', function (data) {
here.stdout.on('data', data => {
data = data.toString();
assert.equal(packageJson.version + '\n', data);
done();
});
});
it('`here --version` should output `version`', function (done) {
it('`here --version` should output `version`', done => {
here = spawn('node', ['./index.js', '--version']);
here.stdout.on('data', function (data) {
here.stdout.on('data', data => {
data = data.toString();
assert.equal(packageJson.version + '\n', data);
done();
});
});
it('`here -h` should output help', function (done) {
it('`here -h` should output help', done => {
here = spawn('node', ['./index.js', '-h']);
here.stdout.on('data', function (data) {
here.stdout.on('data', data => {
data = data.toString();
assert.equal(true, !!~data.indexOf('Usage: index [options]') && !!~data.indexOf('Options:'));
done();
});
});
it('`here -w` should output `[??:??:??.???] server : listen http://*.*.*.*:*/` and `[??:??:??.???] watcher: pages will be reloaded in 0 seconds after final change was taken`', done => {
here = spawn('node', ['./index.js', '-w']);
let stdoutCount = 0;
here.stdout.on('data', data => {
stdoutCount++;
data = data.toString();
switch (stdoutCount) {
case 1:
assert.equal(true, /^\[\d{2}:\d{2}:\d{2}\.\d{3}\] server : listen http:\/\/\d+\.\d+\.\d+\.\d+:\d+\/\n$/.test(data));
break;
case 2:
assert.equal(true, /^\[\d{2}:\d{2}:\d{2}\.\d{3}\] watcher: pages will be reloaded in 0 seconds after final change was taken\n$/.test(data));
here.kill();
done();
break;
default:
assert.fail(stdoutCount, 2);
done();
break;
}
});
});
it('`here --watch 3` should output `[??:??:??.???] server : listen http://*.*.*.*:*/` and `[??:??:??.???] watcher: pages will be reloaded in 3 seconds after final change was taken`', done => {
here = spawn('node', ['./index.js', '--watch', '3']);
let stdOutCount = 0;
here.stdout.on('data', data => {
stdOutCount++;
data = data.toString();
switch (stdOutCount) {
case 1:
assert.equal(true, /^\[\d{2}:\d{2}:\d{2}\.\d{3}\] server : listen http:\/\/\d+\.\d+\.\d+\.\d+:\d+\/\n$/.test(data));
break;
case 2:
assert.equal(true, /^\[\d{2}:\d{2}:\d{2}\.\d{3}\] watcher: pages will be reloaded in 3 seconds after final change was taken\n$/.test(data));
here.kill();
done();
break;
default:
assert.fail(stdOutCount, 2);
done();
break;
}
});
});
});

0 comments on commit 075c654

Please sign in to comment.