-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathcli.js
executable file
·80 lines (67 loc) · 1.91 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
#!/usr/bin/env node
var meow = require("meow");
var devip = require("dev-ip")();
var logger = require("./lib/logger");
var foxy = require("./");
var http = require("http");
var path = require("path");
var fs = require("fs");
var https = require("https");
/**
* Handle cli input
*/
if (!module.parent) {
handleCli(meow({
help: fs.readFileSync(path.join(__dirname, "/help.txt"), "utf8")
}));
}
module.exports = handleCli;
/**
* @param cli
*/
function handleCli (cli) {
if (!cli.input.length) {
logger.info("At least 1 argument is required {yellow:(url)}");
logger.info("For help, run {cyan:foxy --help");
return false;
}
var url = require("url").parse(cli.input[0]);
var scheme = url.protocol === "https:" ? "https" : "http";
var target = url.protocol + "//" + url.host;
var app = foxy(target, cli.flags);
var server = getServer(scheme, app).listen(cli.flags.port);
var port = server.address().port;
var urls = [scheme + "://localhost:" + port];
if (devip.length) {
urls.push(scheme + "://" + devip[0] + ":" + server.address().port);
}
urls.forEach(function (url) {
logger.info("Server running at: {magenta:%s", url);
});
return {
server: server,
app: app,
urls: urls
};
}
/**
* @param scheme
* @param app
* @returns {{staticServer: http.Server, proxyServer: http.Server}}
*/
function getServer (scheme, app) {
var server;
if (scheme === "https") {
server = https.createServer({
key: fs.readFileSync(path.resolve(__dirname, "test/fixtures/certs/server.key")),
cert: fs.readFileSync(path.resolve(__dirname, "test/fixtures/certs/server.cert"))
}, app);
} else {
server = http.createServer(app);
}
/**
* Proxy web sockets
*/
server.on("upgrade", app.handleUpgrade);
return server;
}