CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/project/init-program.ts
Views: 687
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
import { blobstore } from "@cocalc/backend/data";
10
11
interface Options {
12
hubPort: number;
13
browserPort: number;
14
hostname: string;
15
kucalc: boolean;
16
daemon: boolean;
17
sshd: boolean;
18
init: string;
19
blobstore: typeof blobstore;
20
}
21
22
const DEFAULTS: Options = {
23
hubPort: 0,
24
browserPort: 0,
25
// It's important to make the hostname '127.0.0.1' instead of 'localhost',
26
// and also be consistent with packages/server/projects/control/util.ts
27
// The distinction can of course matter, e.g,. using '127.0.0.1' causes
28
// our server to ONLY listen on ipv4, but the client will try 'localhost'
29
// which on some hosts will resolve to an ipv6 address ::1 first and that
30
// fails. There's no way to just easily listen on both ipv4 and ipv6 interfaces.
31
// I noticed that with express if you use localhost you get ipv6 only, and
32
// with node-http-proxy if you use localhost you get ipv4 only, so things are
33
// just totally broken. So we explicitly use 127.0.0.1 to force things to
34
// be consistent.
35
hostname: "127.0.0.1",
36
kucalc: false,
37
daemon: false,
38
sshd: false,
39
init: "",
40
blobstore,
41
};
42
43
program
44
.name("cocalc-project")
45
.usage("[?] [options]")
46
.option(
47
"--hub-port <n>",
48
"TCP server port to listen on (default: 0 = random OS assigned); hub connects to this",
49
(n) => parseInt(n),
50
DEFAULTS.hubPort,
51
)
52
.option(
53
"--browser-port <n>",
54
"HTTP server port to listen on (default: 0 = random OS assigned); browser clients connect to this",
55
(n) => parseInt(n),
56
DEFAULTS.browserPort,
57
)
58
.option(
59
"--hostname [string]",
60
'hostname of interface to bind to (default: "127.0.0.1")',
61
DEFAULTS.hostname,
62
)
63
.option("--kucalc", "Running in the kucalc environment")
64
.option(
65
"--sshd",
66
"Start the SSH daemon (setup script and configuration must be present)",
67
)
68
.option(
69
"--init [string]",
70
"Runs the given script via bash and redirects output to .log and .err files.",
71
)
72
.option("--blobstore [string]", "Blobstore type (sqlite or disk)")
73
.option("--daemon", "Run as a daemon")
74
.parse(process.argv);
75
76
function init(): Options {
77
const opts = program.opts();
78
for (const key in opts) {
79
DEFAULTS[key] = opts[key];
80
}
81
return DEFAULTS;
82
}
83
84
const OPTIONS = init();
85
86
export function getOptions() {
87
return OPTIONS;
88
}
89
90