forked from pegjs/website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pegjs-website
executable file
·95 lines (76 loc) · 1.91 KB
/
pegjs-website
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/env node
var util = require("util");
/* Helpers */
function printHelp() {
util.puts("Usage: pegjs-website [options]");
util.puts("");
util.puts("Runs the PEG.js website.");
util.puts("");
util.puts("Options:");
util.puts(" -e, --environment <environment> environment the website will run in");
util.puts(" (default: development)");
util.puts(" -p, --port <port> port to listen at (default: 3000)");
util.puts(" -h, --help print help and exit");
}
function exitSuccess() {
process.exit(0);
}
function exitFailure() {
process.exit(1);
}
function abort(message) {
sys.error(message);
exitFailure();
}
/* Arguments */
var args = process.argv.slice(2); // Trim "node" and the script path.
function isOption(arg) {
return /^-/.test(arg);
}
function nextArg() {
args.shift();
}
/* Main */
var environment = "development",
port = 3000;
while (args.length > 0 && isOption(args[0])) {
switch (args[0]) {
case "-p":
case "--port":
nextArg();
if (args.length === 0) {
abort("Missing parameter of the -p/--port option.");
}
if (!/[1-9][0-9]*/.test(args[0])) {
abort("Parameter of the -p/--port option must be a positive integer.");
}
port = parseInt(args[0], 10);
break;
case "-e":
case "--environment":
nextArg();
if (args.length === 0) {
abort("Missing parameter of the -e/--environment option.");
}
environment = args[0];
break;
case "-h":
case "--help":
printHelp();
exitSuccess();
break;
default:
abort("Unknown option: " + args[0] + ".");
}
nextArg();
}
if (args.length > 0) {
abort("Too many arguments.");
}
process.env.NODE_ENV = environment;
require("./app").listen(port);
console.log(
"PEG.js website running on port %d in %s mode.",
port,
environment
);