-
Notifications
You must be signed in to change notification settings - Fork 8
/
cli.js
45 lines (40 loc) · 1.46 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
41
42
43
44
45
const express = require("express");
const {routerFromFolder} = require("./index");
const app = express();
const path = require("path");
const commandLineArgs = require('command-line-args');
const optionDefinition = [
{name: 'port', alias: 'p', type: Number },
{name: 'dir', alias: 'd', multiple: true, defaultOption: true},
{name: 'ssr', alias: 's', type: Boolean},
{name: 'stream', type: Boolean},
{name: 'public', alias: 'u', multiple: true},
{name: 'watch', alias: 'w', type: Boolean},
{name: 'minify', alias: 'm', type: Boolean}
];
const runOptions = commandLineArgs(optionDefinition, process.argv);
const port = runOptions.port || process.env.PORT || 3000;
if (!runOptions.dir?.length) {
console.log("Required: dir");
process.exit(0);
}
for (const dir of runOptions.dir) {
const options = {watch: runOptions.watch, esbuild: {}};
if (runOptions.ssr)
options.serverSideRendering = true;
if (runOptions.stream)
options.stream = true;
if (runOptions.minify)
options.esbuild.minify = true;
app.use(routerFromFolder(dir, options));
const {scope} = require(`${path.resolve(dir)}/index.h0.ts`);
console.log(`H0: serving ${dir} at http://localhost:${port}${scope}`);
}
for (const p of runOptions.public || []) {
console.log(`H0: serving ${p} as public folder`);
app.use(express.static(p));
}
app.listen(port, () => {
console.log(`Listening on port ${port}`);
})
globalThis.RUNTIME = "node";