-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathcli.js
executable file
·89 lines (79 loc) · 2.1 KB
/
cli.js
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
#!/usr/bin/env node
import chalk from "chalk";
import { Command } from "commander";
import latestVersion from "latest-version";
import DocServer from "./lib/elm-doc-server.js";
import { version } from "./lib/version.js";
/*
* Program options and usage
*/
function init() {
let pkgPath = ".";
const program = new Command();
program
.version(version)
.arguments("[path_to_package_or_application]")
.action((dir) => {
if (dir !== undefined) {
pkgPath = dir;
}
})
.option("-a, --address <address>", "the server listen address", "127.0.0.1")
.option("-b, --no-browser", "do not open in browser when server starts")
.option(
"-d, --debug",
"enable debug (display watched files and keep temporary files)"
)
.option(
"-o, --output <docs.json>",
"generate docs and exit with status code (/dev/null supported)"
)
.option("-p, --port <port>", "the server listen port", Math.floor, 8000)
.option("-r, --no-reload", "disable hot reloading");
program.on("--help", () => {
console.log("");
console.log("Environment variables:");
console.log(" ELM_HOME Elm home directory (cache)");
});
program.parse(process.argv);
program.dir = pkgPath;
return program;
}
/*
* Check if a newer version is available
*/
function checkUpdate(currentVersion) {
latestVersion("elm-doc-preview")
.then((lastVersion) => {
if (lastVersion !== currentVersion) {
console.log(
chalk.yellow(`elm-doc-preview ${lastVersion} is available`)
);
}
})
.catch(() => {});
}
/*
* Run program
*/
const program = init();
checkUpdate(version);
const options = program.opts();
process
.on("SIGINT", () => process.exit(0))
.on("uncaughtException", (e) => {
if (e.errno === "EADDRINUSE") {
console.log(
chalk.red(`port ${program.port} already used, use --port option`)
);
} else {
console.log(chalk.red(e));
}
process.exit(1);
});
const docServer = new DocServer(options);
if (options.output) {
docServer.make(options.output);
} else {
docServer.listen();
}