Skip to content

Commit

Permalink
Add markline server --watch support. fixed #4
Browse files Browse the repository at this point in the history
  • Loading branch information
hotoo committed Oct 6, 2014
1 parent f01f0d5 commit 4d3c18a
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 12 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ Server Started 127.0.0.1:8000
$ markline server -p 80 data.md
Server Started 127.0.0.1:80

$ markline server -w data.md
Server Started 127.0.0.1:8000

$ markline build data.md

$ markline build data.md --dist _site
Expand Down
3 changes: 1 addition & 2 deletions bin/markline
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ commander
.option('build', 'build markdown to markline page.')
.option('server', 'preview markdown to markline page.')
.option('-p, --port [port]', 'server port for preview markline.')
// TODO: watch markdown file, auto reload preview page.
//.option('-w, --watch', 'watch markdown file change, and auto reload for preview markline.')
.option('-w, --watch', 'watch markdown file change, and auto reload for preview markline.')
.option('--dist [dist]', 'build markdown file to markline static site. default is `dist` in current working directory.')
.parse(process.argv);

Expand Down
45 changes: 36 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ var path = require("path");
var gulp = require("gulp");
var replace = require("gulp-replace");
var del = require("del");
var Event = require("events").EventEmitter;

var evt = new Event();

var DEFAULT_DIST = "dist";
var DEFAULT_PORT = 8000;
Expand Down Expand Up @@ -35,22 +38,18 @@ function build(cwd, options){
function server(cwd, options){
var port = options.port || DEFAULT_PORT;
var encode = options.encode || DEFAULT_ENCODE;
var watch = options.watch || false;
var fileName = options.args.join("");

if (!fs.existsSync(fileName)) {
console.error("Not Found:", fileName);
return;
}

var http = require('http').createServer(function (req, res) {
var app = require('http').createServer(function (req, res) {
var url = req.url;

if (url === "/") {
fs.readFile(path.join(__dirname, "template", "index.html"), encode, function(err, html){
res.writeHead(200, {'Content-Type': 'text/html'});
res.end(html.replace("{FILE_NANE}", fileName));
});
} else if (url === "/" + fileName) {
if (url === "/" + fileName) {
fs.readFile(path.join(cwd, fileName), encode, function(err, markdown){
res.writeHead(200, {'Content-Type': 'text/markdown'});
res.end(markdown);
Expand All @@ -64,7 +63,7 @@ function server(cwd, options){
};

if (url === "/") {
url = "index.html";
url = "/index.html";
}

var ext = url.split(".");
Expand All @@ -75,7 +74,21 @@ function server(cwd, options){

fs.readFile(filePath, encode, function(err, html){
res.writeHead(200, {'Content-Type': MIME_TYPE[ext]});
res.end(html.replace("{FILE_NANE}", fileName));
if (url === "/index.html") {
html = html.replace('{FILE_NANE}', fileName);

if (watch) {
html = html.replace('</body>', [
'<script src="/socket.io/socket.io.js"></script><script>',
'var socket = io.connect("/");',
'socket.on("reload", function() { location.reload(); });',
'</script>',
'</body>'
].join('\n')
);
}
}
res.end(html);
});

} else {
Expand All @@ -87,6 +100,20 @@ function server(cwd, options){
}).listen(port).on("error", function(error){
console.error(error);
});

if (watch){
var io = require('socket.io')(app);
io.sockets.on('connection', function(socket) {
socket.emit('hello', {message: 'markline'});

fs.watch(fileName, function(event, fileName) {
if (!socket.disconnected) {
socket.emit('reload', {message: fileName});
}
});
});
}

console.log("Server Started 127.0.0.1:" + port);
}

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"commander": "~1.1.1",
"del": "^0.1.3",
"gulp": "^3.8.8",
"gulp-replace": "^0.4.0"
"gulp-replace": "^0.4.0",
"socket.io": "^1.1.0"
},
"devDependencies": {
"mocha": "1.17.1",
Expand Down

0 comments on commit 4d3c18a

Please sign in to comment.