-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.ts
64 lines (57 loc) · 1.42 KB
/
main.ts
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
import { open } from "./src/deps/opn.ts";
import { runServer } from "./src/server.ts";
async function browse(url: string) {
await open(url);
return 0;
}
const commands: Map<string, (opts?: any) => Promise<number>> = new Map([
// TODO: Revise this later
["build", (opts?: any) => {
let cmd = [
Deno.execPath(), "bundle", "--config", "tsconfig.json",
"src/client.tsx", "public/assets/js/client.js",
];
const runOpts = {
cmd,
env: Deno.env.toObject(),
};
console.log("Deno.run():", runOpts);
const process: Deno.Process = Deno.run(runOpts);
return process.status().then(status => status.code)
}],
["serve", (opts?: any) => {
const host = opts?.host || "localhost";
const port = opts?.port || 3000;
return runServer({ host: host, port: port })
.then(() =>
(opts?.browse) ? browse(`http://${host}:${port}/`) : Promise.resolve(0)
)
.then(() => new Promise((resolve, reject) => {}));
}],
]);
let rc;
let command;
let cmdOpts = {
browse: false,
};
for (let arg of Deno.args) {
if (arg === "--browse") {
cmdOpts.browse = true;
} else if (!command) {
command = commands.get(arg);
}
}
if (!command) {
console.error(
"usage: deno --allow-read --allow-net --unstable main.ts -- [--browse] (serve)",
);
rc = -1;
Deno.exit(rc);
}
try {
rc = await command(cmdOpts);
} catch (err) {
console.error(err);
rc = -1;
}
Deno.exit(rc);