@valbuild/cli 0.12.0 → 0.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -2
- package/src/cli.ts +122 -0
- package/src/logger.ts +9 -0
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@valbuild/cli",
|
3
|
-
"version": "0.
|
3
|
+
"version": "0.13.0",
|
4
4
|
"description": "Val CLI tools",
|
5
5
|
"bin": {
|
6
6
|
"val": "./bin.js"
|
@@ -16,7 +16,7 @@
|
|
16
16
|
"typecheck": "tsc --noEmit"
|
17
17
|
},
|
18
18
|
"dependencies": {
|
19
|
-
"@valbuild/server": "~0.
|
19
|
+
"@valbuild/server": "~0.13.0",
|
20
20
|
"chalk": "^4.1.2",
|
21
21
|
"cors": "^2.8.5",
|
22
22
|
"express": "^4.18.2",
|
package/src/cli.ts
ADDED
@@ -0,0 +1,122 @@
|
|
1
|
+
import path from "path";
|
2
|
+
import meow from "meow";
|
3
|
+
import { createRequestHandler, createService } from "@valbuild/server";
|
4
|
+
import { error, info } from "./logger";
|
5
|
+
import express from "express";
|
6
|
+
import cors from "cors";
|
7
|
+
import { createServer, Server } from "node:http";
|
8
|
+
import { LocalValServer } from "@valbuild/server/src/LocalValServer";
|
9
|
+
|
10
|
+
async function serve({
|
11
|
+
root,
|
12
|
+
port,
|
13
|
+
cfg,
|
14
|
+
}: {
|
15
|
+
root?: string;
|
16
|
+
port: number;
|
17
|
+
cfg?: string;
|
18
|
+
}): Promise<void> {
|
19
|
+
const projectRoot = root ? path.resolve(root) : process.cwd();
|
20
|
+
const service = await createService(projectRoot, {
|
21
|
+
valConfigPath: cfg ?? "./val.config",
|
22
|
+
});
|
23
|
+
const valReqHandler = createRequestHandler(
|
24
|
+
new LocalValServer({
|
25
|
+
service,
|
26
|
+
})
|
27
|
+
);
|
28
|
+
const app = express();
|
29
|
+
// TODO: Properly configure CORS
|
30
|
+
app.use(cors(), valReqHandler);
|
31
|
+
const server: Server = createServer(app);
|
32
|
+
await new Promise<void>((resolve) => {
|
33
|
+
server.listen(port, resolve);
|
34
|
+
});
|
35
|
+
|
36
|
+
info(`Root is ${projectRoot}`);
|
37
|
+
info(`Config is ${service.valConfigPath}`);
|
38
|
+
info(`Listening on port ${port}`);
|
39
|
+
|
40
|
+
let handled = false;
|
41
|
+
const handleInterrupt: NodeJS.SignalsListener = async () => {
|
42
|
+
if (handled) return;
|
43
|
+
handled = true;
|
44
|
+
|
45
|
+
info("Shutting down...");
|
46
|
+
|
47
|
+
server.close((err) => {
|
48
|
+
if (!err) return;
|
49
|
+
error(err.toString());
|
50
|
+
});
|
51
|
+
};
|
52
|
+
|
53
|
+
process.on("SIGINT", handleInterrupt);
|
54
|
+
process.on("SIGTERM", handleInterrupt);
|
55
|
+
|
56
|
+
return new Promise((resolve) => {
|
57
|
+
server.on("close", () => {
|
58
|
+
service.dispose();
|
59
|
+
resolve();
|
60
|
+
});
|
61
|
+
});
|
62
|
+
}
|
63
|
+
|
64
|
+
async function main(): Promise<void> {
|
65
|
+
const { input, flags, showHelp } = meow(
|
66
|
+
`
|
67
|
+
Usage
|
68
|
+
$ val [command]
|
69
|
+
Commands
|
70
|
+
serve Run val development server
|
71
|
+
|
72
|
+
Options
|
73
|
+
--help Show this message
|
74
|
+
--port [port], -p [port] Set server port (default 4123)
|
75
|
+
--root [root], -r [root] Set project root directory (default process.cwd())
|
76
|
+
--cfg [cfg], -c [cfg] Set path to config relative to root (default ./val.config)
|
77
|
+
`,
|
78
|
+
{
|
79
|
+
flags: {
|
80
|
+
port: {
|
81
|
+
type: "number",
|
82
|
+
alias: "p",
|
83
|
+
default: 4123,
|
84
|
+
},
|
85
|
+
root: {
|
86
|
+
type: "string",
|
87
|
+
alias: "r",
|
88
|
+
},
|
89
|
+
cfg: {
|
90
|
+
type: "string",
|
91
|
+
alias: "c",
|
92
|
+
},
|
93
|
+
},
|
94
|
+
hardRejection: false,
|
95
|
+
}
|
96
|
+
);
|
97
|
+
|
98
|
+
if (input.length === 0) {
|
99
|
+
return showHelp();
|
100
|
+
}
|
101
|
+
|
102
|
+
if (input.length !== 1) {
|
103
|
+
return error(`Unknown command "${input.join(" ")}"`);
|
104
|
+
}
|
105
|
+
|
106
|
+
const [command] = input;
|
107
|
+
switch (command) {
|
108
|
+
case "serve":
|
109
|
+
return serve({
|
110
|
+
root: flags.root,
|
111
|
+
port: flags.port,
|
112
|
+
cfg: flags.cfg,
|
113
|
+
});
|
114
|
+
default:
|
115
|
+
return error(`Unknown command "${input.join(" ")}"`);
|
116
|
+
}
|
117
|
+
}
|
118
|
+
|
119
|
+
void main().catch((err) => {
|
120
|
+
error(err);
|
121
|
+
process.exitCode = 1;
|
122
|
+
});
|