UNPKG

3.54 kBJavaScriptView Raw
1var Command, Option, Signature, _, parse, settings, state, utils;
2
3_ = require('lodash');
4
5_.str = require('underscore.string');
6
7parse = require('./parse');
8
9settings = require('./settings');
10
11state = require('./state');
12
13Option = require('./option');
14
15Signature = require('./signature');
16
17utils = require('./utils');
18
19module.exports = Command = (function() {
20 function Command(options) {
21 if (options == null) {
22 options = {};
23 }
24 if (!(options.signature instanceof Signature)) {
25 throw new Error('Missing or invalid command signature');
26 }
27 if (!_.isFunction(options.action)) {
28 throw new Error('Missing or invalid command action');
29 }
30 if ((options.options != null) && !_.isArray(options.options)) {
31 throw new Error('Invalid options');
32 }
33 this.options = [];
34 _.each(options.options, this.option, this);
35 _.extend(this, _.omit(options, 'options'));
36 }
37
38 Command.prototype.applyPermissions = function(callback) {
39 var error, permissionFunction;
40 if (this.permission == null) {
41 return callback();
42 }
43 permissionFunction = state.permissions[this.permission];
44 if (permissionFunction == null) {
45 error = new Error("Permission not found: " + this.permission);
46 return callback(error);
47 }
48 return permissionFunction.call(this, callback);
49 };
50
51 Command.prototype._parseOptions = function(options) {
52 var allOptions, parsedOptions;
53 allOptions = _.union(state.globalOptions, this.options);
54 return parsedOptions = parse.parseOptions(allOptions, options);
55 };
56
57 Command.prototype._checkElevation = function(callback) {
58 if (this.root != null) {
59 return utils.isElevated(callback);
60 } else {
61 return callback(null, true);
62 }
63 };
64
65 Command.prototype.execute = function(args, callback) {
66 if (args == null) {
67 args = {};
68 }
69 return this.signature.compileParameters(args.command, (function(_this) {
70 return function(error, params) {
71 if (error != null) {
72 return typeof callback === "function" ? callback(error) : void 0;
73 }
74 return _this._checkElevation(function(error, isElevated) {
75 var parsedOptions;
76 if (error != null) {
77 return typeof callback === "function" ? callback(error) : void 0;
78 }
79 if (_this.root && !isElevated) {
80 error = new Error('You need admin privileges to run this command');
81 error.code = 'EACCES';
82 return callback(error);
83 }
84 parsedOptions = _this._parseOptions(args.options);
85 return _this.applyPermissions(function(error) {
86 if (error != null) {
87 return typeof callback === "function" ? callback(error) : void 0;
88 }
89 try {
90 _this.action(params, parsedOptions, callback);
91 } catch (_error) {
92 error = _error;
93 return typeof callback === "function" ? callback(error) : void 0;
94 }
95 if (_this.action.length < 3) {
96 return typeof callback === "function" ? callback() : void 0;
97 }
98 });
99 });
100 };
101 })(this));
102 };
103
104 Command.prototype.option = function(option) {
105 if (!(option instanceof Option)) {
106 throw new Error('Invalid option');
107 }
108 if (_.find(this.options, option) != null) {
109 return;
110 }
111 return this.options.push(option);
112 };
113
114 Command.prototype.isWildcard = function() {
115 return this.signature.isWildcard();
116 };
117
118 return Command;
119
120})();