-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
executable file
·40 lines (32 loc) · 1.14 KB
/
cli.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
#!/usr/bin/env node
const path = require("path")
const express = require("express")
const chokidar = require("chokidar")
const Builder = require("./src/builder")
const ReactRenderer = require("./src/react-renderer")
const Project = require("./src/project")
const builder = new Builder()
const renderer = new ReactRenderer()
const project = new Project()
const app = express()
const distDir = path.join(".", "dist")
const buildDir = path.join(".", ".build")
const command = process.argv[2]
if (command === "build") {
builder.build(project, distDir, { sourcemap: true, minify: true })
renderer.render(project, distDir).then(() => {
console.log("Site built")
})
}
if (command === "serve") {
const port = 1234
const watcher = chokidar.watch(".", { ignored: new RegExp(`${buildDir}|${distDir}|node_modules`) })
watcher.on("ready", () => {
watcher.on("all", (event, path) => {
builder.build(project, buildDir, { sourcemap: true })
})
})
builder.build(project, buildDir, { sourcemap: true })
app.use(express.static(buildDir, { extensions: [ "html" ] }))
app.listen(port, () => console.log(`Listening on http://localhost:${port}`))
}