-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
135 lines (116 loc) · 3.02 KB
/
app.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
133
134
135
/**
* app.js
*/
const express = require('express');
const app = express();
const http = require('http');
const https = require('https');
const server = http.Server(app);
const io = require('socket.io')(server);
const path = require('path');
const fs = require('fs');
const opn = require('opn');
const docConfig = require(path.resolve(process.argv[2]));
const args = (function () {
const restArgv = process.argv.splice(3);
return restArgv.reduce((prev, item) => {
const pairs = item.split('=');
prev[pairs[0].replace(/-+/g, '')] = pairs[1] || 1;
return prev;
}, {});
})();
const routeMap = (function () {
const map = new Map();
function find(children) {
if (!children) return;
children.forEach((item) => {
if (item.route !== undefined) {
map.set(item.route || '__empty', item);
}
if (item.children) {
find(item.children);
}
});
}
find(docConfig.nav);
return map;
})();
function covertMDFile(filePath, fileData) {
return fileData.replace(/[^!]?\[(.(?!\\\]))*]\(([^)]*)\)/, function () {
const str = arguments[0];
const match = arguments[2];
if (/http(s)?/.test(match)) {
return str;
}
const linkPath = path.join(filePath, '..', match);
let route = '';
routeMap.forEach((item) => {
if (item.file === linkPath) {
route = item.route;
}
});
// use hash
return route ? str.replace(match, '#' + route) : str;
});
}
app.locals = {
version: require('./package.json').version,
}
app.use(express.static(path.join(__dirname, './public')));
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, './views'));
app.get('/', (req, res) => {
res.render('index', { config: docConfig, port: PORT });
});
app.get('/doc', (req, res, next) => {
const config = routeMap.get(req.query.route || '__empty');
if (!config) {
return res.sendStatus(404);
}
// 来自远程
if (/http(s)?:/gi.test(config.file)) {
const { URL } = require('url');
let client = http;
if (config.file.startsWith('https')) {
client = https;
}
client.get(new URL(config.file), (_req) => {
_req.pipe(res)
}).on('error', (e) => {
next(e);
});
return;
}
if (!fs.existsSync(config.file)) {
return res.send(config.file);
}
fs.readFile(config.file, (err, data) => {
if (err) {
return next(err);
}
res.send(covertMDFile(config.file, data.toString()));
});
});
app.get('/imgproxy', (req, res, next) => {
const { src, route } = req.query;
const config = routeMap.get(req.query.route);
const imgPath = path.join(config.file, '..', src);
res.sendFile(imgPath);
});
app.use(function errorHandler(err, req, res, next) {
console.log(err);
res.sendStatus(500);
});
const PORT = args.port || 3000;
server.listen(PORT, () => {
console.log(`Server run port ${PORT}`);
setTimeout(() => {
io.clients((error, clients) => {
if (clients.length === 0) {
opn(`http://localhost:${PORT}`);
}
});
}, 2000);
});
io.on('connection', socket => {
});