This repository has been archived by the owner on Feb 24, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 321
/
arapaho.js
executable file
·130 lines (115 loc) · 3.7 KB
/
arapaho.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
#!/usr/bin/env node
// @ts-check
'use strict';
const fs = require('fs');
const path = require('path');
const express = require('express');
const ejs = require('ejs');
const compression = require('compression');
const args = require('tiny-opts-parser')(process.argv);
const open = require('open');
const chokidar = require('chokidar');
const shins = require('./index.js');
let includesModified = false;
let lastGenTime = {};
if (args.p) args.preserve = args.p;
if (args.l) args.launch = args.l;
if (args.h) args.help = args.h;
if (args.v) args.verbose = args.v;
if (args.help) {
console.log('Usage: node arapaho [port] [-l|--launch] [-p|--preserve] [shins-options]');
process.exit(0);
}
let app = express();
app.use(compression());
app.set('view engine', 'html');
app.engine('html', ejs.renderFile);
if (fs.existsSync('source/includes')) {
chokidar.watch('source/includes', {ignoreInitial:true}).on('all',function(eventType, filename) {
if (args.verbose) console.log(eventType, filename);
includesModified = true;
});
}
function getLastGenTime(fpath) {
if (lastGenTime[fpath]) return lastGenTime[fpath];
return new Date(0);
}
function check(req,res,fpath) {
fpath = fpath.split('/').join('');
var srcStat = {mtime: new Date(0)};
try {
srcStat = fs.statSync(path.join(__dirname,'source',fpath+'.md'));
}
catch (ex) {}
var dstStat = {mtime:getLastGenTime(fpath)};
if (!args.preserve) {
try {
dstStat = fs.statSync(path.join(__dirname,fpath));
}
catch (ex) { }
}
if (includesModified || (srcStat.mtime>dstStat.mtime)) {
includesModified = false;
lastGenTime[fpath] = new Date();
let source = path.join(__dirname,'source',fpath+'.md');
console.log('Rebuilding',fpath,'from',source);
fs.readFile(source,'utf8',function(err,markdown){
if (markdown) {
let options = Object.assign({},args);
if (req.query.customcss) {
options.customCss = true;
}
if (req.query.inline) {
options.inline = true;
}
if (req.query.minify) {
options.minify = true;
}
if (req.query.attr) {
options.attr = true;
}
options.source = source;
shins.render(markdown,options,function(err,html){
if (err) {
console.warn(err);
res.send(err);
}
else {
res.send(html);
if (!args.preserve) {
fs.writeFile(path.join(__dirname,fpath),html,'utf8',function(){});
}
}
});
}
else {
res.send(err);
}
});
}
else {
res.render(path.join(__dirname,fpath));
}
}
app.get('/', function(req,res) {
check(req,res,'index.html');
});
app.get('*.html', function(req,res) {
check(req,res,req.path);
});
app.use("/", express.static(__dirname));
var myport = process.env.PORT || 4567;
if (args._.length>2) myport = args._[2];
var server = app.listen(myport, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Arapaho server listening at http://%s:%s', host, port);
if (args.launch) {
console.log('Launching...');
var url = 'http://'+(host === '::' ? 'localhost' : 'host') + ':' +port+'/'
open(url).catch(function (ex) {
console.error(`Unable to open URL '${url}'`);
console.error(ex);
})
}
});