-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
executable file
·59 lines (49 loc) · 1.62 KB
/
server.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
#!/usr/bin/env node
const path = require("path")
const express = require("express")
const { Disk } = require("jtree/products/Disk.node.js")
const { readFile } = require("fs")
const { TypeScriptRewriter } = require("jtree/products/TypeScriptRewriter.js")
const { getExamples } = require("./examples")
const grammarParser = require("jtree/products/grammar.nodejs.js")
class Server {
start(port = 80) {
const app = express()
app.get("/*.js", (req, res) => {
const filename = req.path.substr(1)
readFile(__dirname + "/" + filename, "utf8", (err, code) => {
if (err) throw err
res.send(
new TypeScriptRewriter(code)
.removeRequires()
.removeNodeJsOnlyLines()
.changeNodeExportsToWindowExports()
.getString()
)
})
})
app.get("/examples", (req, res) => {
res.send(getExamples())
})
app.get("/examples", (req, res) => {
res.send(getExamples())
})
app.get("/dist/simoji.grammar", (req, res) => {
res.send(this.grammar)
})
app.use(express.static(__dirname + "/"))
app.listen(port, () => {
console.log(`Running Simoji Dev Server. cmd+dblclick: http://localhost:${port}/dev.html`)
})
}
get grammar() {
const asOneFile = Disk.getFiles(path.join(__dirname, "grammar"))
.filter(file => file.endsWith(".grammar"))
.map(filePath => Disk.read(filePath))
.join("\n\n")
.trim()
return new grammarParser(asOneFile)._sortNodesByInScopeOrder()._sortWithParentParsersUpTop().asString
}
}
if (!module.parent) new Server().start()
module.exports = { Server }