1 | var Command, Option, Signature, _, parse, settings, state, utils;
|
2 |
|
3 | _ = require('lodash');
|
4 |
|
5 | _.str = require('underscore.string');
|
6 |
|
7 | parse = require('./parse');
|
8 |
|
9 | settings = require('./settings');
|
10 |
|
11 | state = require('./state');
|
12 |
|
13 | Option = require('./option');
|
14 |
|
15 | Signature = require('./signature');
|
16 |
|
17 | utils = require('./utils');
|
18 |
|
19 | module.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 | try {
|
85 | parsedOptions = _this._parseOptions(args.options);
|
86 | } catch (_error) {
|
87 | error = _error;
|
88 | return typeof callback === "function" ? callback(error) : void 0;
|
89 | }
|
90 | return _this.applyPermissions(function(error) {
|
91 | if (error != null) {
|
92 | return typeof callback === "function" ? callback(error) : void 0;
|
93 | }
|
94 | try {
|
95 | _this.action(params, parsedOptions, callback);
|
96 | } catch (_error) {
|
97 | error = _error;
|
98 | return typeof callback === "function" ? callback(error) : void 0;
|
99 | }
|
100 | if (_this.action.length < 3) {
|
101 | return typeof callback === "function" ? callback() : void 0;
|
102 | }
|
103 | });
|
104 | });
|
105 | };
|
106 | })(this));
|
107 | };
|
108 |
|
109 | Command.prototype.option = function(option) {
|
110 | if (!(option instanceof Option)) {
|
111 | throw new Error('Invalid option');
|
112 | }
|
113 | if (_.find(this.options, option) != null) {
|
114 | return;
|
115 | }
|
116 | return this.options.push(option);
|
117 | };
|
118 |
|
119 | Command.prototype.isWildcard = function() {
|
120 | return this.signature.isWildcard();
|
121 | };
|
122 |
|
123 | return Command;
|
124 |
|
125 | })();
|