-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
132 lines (106 loc) · 3.36 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
var fs = require("fs");
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;
var DEFAULT_ENCODE = "utf-8";
function build(cwd, options){
var fileName = options.args[0] || "";
var encode = options.encode || DEFAULT_ENCODE;
var template_dir = path.join(__dirname, "template");
var dist_dir = options.dist || path.join(cwd, DEFAULT_DIST);
if (!fileName || !fs.existsSync(fileName)) {
console.error("Not Found:", fileName);
return;
}
del(dist_dir).then(function(){
gulp.src(path.join(template_dir, '**'), { dot: true })
.pipe(replace(/\{FILE_NANE\}/g, path.basename(fileName) ))
.pipe(gulp.dest(dist_dir));
gulp.src(path.join(cwd, fileName))
.pipe(gulp.dest(dist_dir));
});
}
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[0] || "";
if (!fileName || !fs.existsSync(fileName)) {
console.error("Not Found:", fileName);
return;
}
var app = require('http').createServer(function (req, res) {
var url = req.url.split(/[?#]/)[0];
if (url === "/" + fileName) {
fs.readFile(path.join(cwd, fileName), encode, function(err, markdown){
res.writeHead(200, {'Content-Type': 'text/markdown'});
res.end(markdown);
});
} else {
var MIME_TYPE = {
"html": "text/html",
"js": "text/javascript",
"css": "text/css"
};
if (url === "/") {
url = "/index.html";
}
var ext = url.split(".");
ext = ext[ext.length - 1];
var filePath = path.join(__dirname, "template", url);
if (MIME_TYPE.hasOwnProperty(ext) && fs.existsSync(filePath)) {
fs.readFile(filePath, encode, function(err, html){
res.writeHead(200, {'Content-Type': MIME_TYPE[ext]});
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 {
res.writeHead(404, {'Content-Type': 'text/html'});
res.end('404\n');
}
}
}).listen(port).on("error", function(error){
if (error.code === 'EADDRINUSE') {
console.error('Error: This port is in use, change to another one');
} else {
throw error;
}
}).on("listening", function(){
console.log("Server started at 127.0.0.1:" + port);
});
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});
}
});
});
}
}
function watch(){
}
module.exports = {
build: build,
server: server,
watch: watch
}