UNPKG

4.6 kBJavaScriptView Raw
1"use strict";
2
3/**
4 * Function library for Vantage"s out-of-the-box
5 * API commands. Imported into a Vantage server
6 * through vantage.use(module).
7 */
8
9/**
10 * Module dependencies.
11 */
12
13var _ = require("lodash")
14 , util = require("./util")
15 , chalk = require("chalk")
16 ;
17
18module.exports = function(vantage) {
19
20 /**
21 * Help for a particular command.
22 */
23
24 vantage
25 .command("help [command]")
26 .description("Provides help for a given command.")
27 .action(function(args, cb) {
28 if (args.command) {
29 var name = _.findWhere(this.parent.commands, { _name: String(args.command).toLowerCase().trim() });
30 if (name && !name._hidden) {
31 this.log(name.helpInformation());
32 } else {
33 this.log(this.parent._commandHelp(args.command));
34 }
35 } else {
36 this.log(this.parent._commandHelp(args.command));
37 }
38 cb();
39 });
40
41 /**
42 * Exits Vantage.
43 */
44
45 vantage
46 .command("exit")
47 .option("-f, --force", "Forces process kill without confirmation.")
48 .description("Exits instance of Vantage.")
49 .action(function(args) {
50 args.options = args.options || {};
51 args.options.sessionId = this.id;
52 this.parent.exit(args.options);
53 });
54
55 /**
56 * Lists out active sessions.
57 */
58
59 vantage
60 .command("who", "Lists active vantage sessions.")
61 .hidden()
62 .action(function(args, cb) {
63 var ssns = _.clone(this.parent.server.sessions || []);
64 ssns.unshift(this.parent.session);
65 var hdr = " ID User ";
66 this.log("\n" + hdr);
67 for (var i = 0; i < ssns.length; ++i) {
68 var ssn = ssns[i];
69 var id = ssn.id.slice(ssn.id.length - 4, ssn.id.length);
70 var res = ""
71 + " "
72 + id
73 + " "
74 + util.pad(ssn.user + "@" + (ssn.address || "local"), 20)
75 + ""
76 ;
77 res = (ssn.id === this.id) ? chalk.white(res) : res;
78 this.log(res);
79 }
80 this.log(" ");
81 cb();
82 });
83
84 /**
85 * Connects to another instance of Vantage.
86 */
87
88 vantage
89 .command("vantage <server>")
90 .alias("vtg")
91 .alias("nsh")
92 .option("-s, --ssl", "Connect using SSL.")
93 .option("-u, --user [user]", "Connect as a given user.")
94 .option("-p, --pass [user]", "Password for given user.")
95 .description("Connects to another instance of Node running Vantage.")
96 .autocompletion(function(text, iteration, cb){
97 cb(void 0, "vantage 127.0.0.1:80");
98 })
99 .action(function(args, cb) {
100 var self = this;
101 var str = (!args.server) ? "" : args.server;
102 var parts = String(str).split(":");
103 var port = (parts.length === 2) ? parts[1] : void 0;
104 var server = (parts.length === 2) ? parts[0] : void 0;
105 if (parts.length === 1) {
106 server = (String(parts[0]).split(".").length === 4) ? parts[0] : void 0;
107 port = (String(parts[0]).length < 6 && !isNaN(parts[0])) ? parts[0] : void 0;
108 }
109 server = (!server) ? "127.0.0.1" : server;
110 port = (!port) ? "80" : port;
111 var options = {
112 ssl: (args.options.ssl === true) ? true : false,
113 user: (args.options.user) ? args.options.user : void 0,
114 pass: (args.options.pass) ? args.options.pass : void 0,
115 sessionId: self.id
116 };
117 this.parent.client.connect(server, port, options, function(err, data) {
118 cb(err, data);
119 });
120 });
121 //..after;
122
123 /**
124 * Imports node module in realtime.
125 */
126
127 vantage
128 .command("use <module>")
129 .description("Installs a vantage extension in realtime.")
130 .option("-l, --loglevel", "Sets log level of module install")
131 .action(function(args, cb) {
132 var self = this;
133 var options = {
134 loglevel: args.options.loglevel || "error",
135 module: args.module
136 };
137 self.log(chalk.white("Installing " + options.module + " from the NPM registry:"));
138 this.parent._use(options, function(err, data) {
139 if (err) {
140 self.log(data);
141 } else {
142 var commands = (data || {}).registeredCommands;
143 if (commands < 1) {
144 self.log(chalk.yellow("No new commands were registered. Are you sure you " + options.module + " is a vantage extension?"));
145 } else {
146 self.log(chalk.white("Successfully registered " + commands + " new command" + ((commands > 1) ? "s" : "") + "."));
147 }
148 }
149 cb();
150 });
151 });
152
153};
154