1 | #!/usr/bin/env node
|
2 |
|
3 | "use strict";
|
4 |
|
5 | var commander = require("commander")
|
6 | , Vantage = require("../lib/vantage")
|
7 | , chalk = require("chalk")
|
8 | ;
|
9 |
|
10 | var command
|
11 | , options = {}
|
12 | ;
|
13 |
|
14 | commander
|
15 | .arguments("[server]")
|
16 | .option("-s, --ssl", "Connect using SSL.")
|
17 | .option("-u, --user [user]", "Connect as a given user.")
|
18 | .option("-p, --pass [user]", "Password for given user.")
|
19 | .action(function(cmd, opts) {
|
20 | command = cmd || "";
|
21 | opts = opts || {};
|
22 | options.ssl = (opts.ssl) ? true : void 0;
|
23 | options.user = (opts.user) ? opts.user : void 0;
|
24 | options.pass = (opts.pass) ? opts.pass : void 0;
|
25 | });
|
26 |
|
27 | commander.parse(process.argv);
|
28 |
|
29 | function parseConnection(str) {
|
30 | var parts = String(str).split(":");
|
31 | var port = (parts.length === 2) ? parts[1] : void 0;
|
32 | var server = (parts.length === 2) ? parts[0] : void 0;
|
33 | if (parts.length === 1) {
|
34 | server = (String(parts[0]).split(".").length === 4) ? parts[0] : void 0;
|
35 | port = (String(parts[0]).length < 6 && !isNaN(parts[0])) ? parts[0] : void 0;
|
36 | }
|
37 | server = (!server) ? "127.0.0.1" : server;
|
38 | port = (!port) ? "80" : port;
|
39 | return ({
|
40 | server: server,
|
41 | port: port
|
42 | });
|
43 | }
|
44 |
|
45 | function validateConnection(connection) {
|
46 | var valid = (String(connection.server).split(".").length !== 4 || isNaN(connection.port))
|
47 | ? ("\n Invalid server/port passed: " + connection.server + ":" + connection.port + "\n")
|
48 | : true;
|
49 | return valid;
|
50 | }
|
51 |
|
52 | function connect(vantage, server, port, opt) {
|
53 | return vantage.connect(server, port, opt).then(function(err) {
|
54 | if (err) {
|
55 | vantage._pause();
|
56 | process.exit(1);
|
57 | } else {
|
58 | if (!vantage.ui.midPrompt()) {
|
59 | vantage._prompt();
|
60 | }
|
61 | }
|
62 | }).catch(function(err){
|
63 | if (err.stack) {
|
64 | vantage.log(err.stack);
|
65 | }
|
66 | vantage._pause();
|
67 | process.exit(1);
|
68 | });
|
69 | }
|
70 |
|
71 | function showTour() {
|
72 | var fs = require("fs");
|
73 | var path = require("path");
|
74 | var file = "/../examples/tour/tour.js";
|
75 | if (fs.existsSync(path.join(__dirname, file))) {
|
76 | require(path.join(__dirname, file)); return;
|
77 | } else {
|
78 | console.log(chalk.yellow("\n Looks like the tour isn't included in your Vantage instance.\n Ensure ./examples/ is in your Vantage directory.\n"));
|
79 | process.exit(1);
|
80 | }
|
81 | }
|
82 |
|
83 | function execute(cmd, opt) {
|
84 | if (cmd === "tour") {
|
85 | return showTour();
|
86 | }
|
87 |
|
88 | var vantage = new Vantage().show();
|
89 | var connection = parseConnection(cmd);
|
90 | var valid = validateConnection(connection);
|
91 | if (valid !== true) {
|
92 | vantage.log(valid);
|
93 | process.exit(1);
|
94 | }
|
95 |
|
96 |
|
97 | if (cmd !== undefined) {
|
98 | connect(vantage, connection.server, connection.port, opt);
|
99 | }
|
100 | }
|
101 |
|
102 | execute(command, options);
|
103 |
|