Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/project/init-program.ts
1447 views
1
/*
2
* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
// parses command line arguments -- https://github.com/visionmedia/commander.js/
7
import { program } from "commander";
8
9
interface Options {
10
hubPort: number;
11
browserPort: number;
12
hostname: string;
13
kucalc: boolean;
14
daemon: boolean;
15
sshd: boolean;
16
init: string;
17
}
18
19
const DEFAULTS: Options = {
20
hubPort: 0,
21
browserPort: 0,
22
// It's important to make the hostname '127.0.0.1' instead of 'localhost',
23
// and also be consistent with packages/server/projects/control/util.ts
24
// The distinction can of course matter, e.g,. using '127.0.0.1' causes
25
// our server to ONLY listen on ipv4, but the client will try 'localhost'
26
// which on some hosts will resolve to an ipv6 address ::1 first and that
27
// fails. There's no way to just easily listen on both ipv4 and ipv6 interfaces.
28
// I noticed that with express if you use localhost you get ipv6 only, and
29
// with node-http-proxy if you use localhost you get ipv4 only, so things are
30
// just totally broken. So we explicitly use 127.0.0.1 to force things to
31
// be consistent.
32
hostname: "127.0.0.1",
33
kucalc: false,
34
daemon: false,
35
sshd: false,
36
init: "",
37
};
38
39
program
40
.name("cocalc-project")
41
.usage("[?] [options]")
42
.option(
43
"--hub-port <n>",
44
"TCP server port to listen on (default: 0 = random OS assigned); hub connects to this",
45
(n) => parseInt(n),
46
DEFAULTS.hubPort,
47
)
48
.option(
49
"--browser-port <n>",
50
"HTTP server port to listen on (default: 0 = random OS assigned); browser clients connect to this",
51
(n) => parseInt(n),
52
DEFAULTS.browserPort,
53
)
54
.option(
55
"--hostname [string]",
56
'hostname of interface to bind to (default: "127.0.0.1")',
57
DEFAULTS.hostname,
58
)
59
.option("--kucalc", "Running in the kucalc environment")
60
.option(
61
"--sshd",
62
"Start the SSH daemon (setup script and configuration must be present)",
63
)
64
.option(
65
"--init [string]",
66
"Runs the given script via bash and redirects output to .log and .err files.",
67
)
68
.option("--daemon", "Run as a daemon")
69
.parse(process.argv);
70
71
function init(): Options {
72
const opts = program.opts();
73
for (const key in opts) {
74
DEFAULTS[key] = opts[key];
75
}
76
return DEFAULTS;
77
}
78
79
const OPTIONS = init();
80
81
export function getOptions() {
82
return OPTIONS;
83
}
84
85