-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
224 lines (186 loc) · 7.75 KB
/
index.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
"use strict";
const { dfRegExOption, dfRegExAlias, typeOfParam, removeAliasSymbol } = require("./helpers");
// COMMAND constructor
function Command() {
this.options = {}; // Option definitions
this.parsed = {}; // Parsing the user's command by using this.options
this.settings = {
// Custom RegExp
regexOption: false,
regexAlias: false,
regexArgument: false
};
}
/**
* OPTION constructor
* @param {string} option is like -o (even the character '-')
* @param {string} alias is used as a key (without the characters '--')
* @param {string} description
*/
function Option(option, aliasKey, description) {
this[aliasKey] = {
option: option,
description: description,
subOptions: {}
};
}
Command.prototype.setting = function(customSettings = { regexOption: false, regexAlias: false, regexArgument: false }) {
if (customSettings.regexOption === RegExp(customSettings.regexOption)) {
this.settings.regexOption = customSettings.regexOption;
}
if (customSettings.regexAlias === RegExp(customSettings.regexAlias)) {
this.settings.regexAlias = customSettings.regexAlias;
}
if (customSettings.regexArgument === RegExp(customSettings.regexArgument)) {
this.settings.regexArgument = customSettings.regexArgument;
}
return this;
};
Command.prototype.option = function(taskName, option, alias, description = "") {
const regExOption = this.settings.regexOption ? this.settings.regexOption : dfRegExOption,
regExAlias = this.settings.regexAlias ? this.settings.regexAlias : dfRegExAlias;
// Checking input params
if (!taskName || !option || !alias) {
if (!taskName) {
throw new Error("Missing the input parameter -> taskName");
} else if (!option) {
throw new Error("Missing the input parameter -> option");
} else if (!alias) {
throw new Error("Missing the input parameter -> alias");
}
} else if (!regExOption.test(option) || !regExAlias.test(alias)) {
throw new Error("The input params are invalid");
}
const aliasKey = removeAliasSymbol(alias); // Using alias as a key and remove the characters "--"
const optionRef = new Map([[option, alias]]);
const newOption = new Option(option, aliasKey, description);
// Merge with the existence root key
if (this.options[taskName] && !this.options[taskName][aliasKey]) {
// Update reference by using mapping
this.options[taskName].reference = new Map([...this.options[taskName].reference, ...optionRef]);
// Add new option
this.options[taskName] = {
...this.options[taskName],
...newOption
};
} else {
// Create a new root key by using taskName and then create a new option
this.options = {
...this.options,
[taskName]: {
reference: optionRef,
...newOption
}
};
}
return this;
};
Command.prototype.subOption = function(taskName, optionAlias, subOption, subOptionAlias, description = "") {
const regExOption = this.settings.regexOption ? this.settings.regexOption : dfRegExOption,
regExAlias = this.settings.regexAlias ? this.settings.regexAlias : dfRegExAlias;
// Checking input params
if (!taskName || !optionAlias || !subOption || !subOptionAlias) {
if (!taskName) {
throw new Error("Missing the parameter -> taskName");
} else if (!optionAlias) {
throw new Error("Missing the parameter -> optionAlias");
} else if (!subOption) {
throw new Error("Missing the parameter -> subOption");
} else if (!subOptionAlias) {
throw new Error("Missing the parameter -> subOptionAlias");
}
} else if (!regExAlias.test(optionAlias) || !regExOption.test(subOption) || !regExAlias.test(subOptionAlias)) {
throw new Error("The input params are invalid");
}
const aliasKey = removeAliasSymbol(optionAlias);
if (this.options[taskName] && this.options[taskName][aliasKey]) {
const subOptKey = removeAliasSymbol(subOptionAlias);
const newSubOption = {
[subOptKey]: {
subOption: subOption,
description: description
}
};
if (!this.options[taskName][aliasKey].subOptions[subOptKey]) {
this.options[taskName][aliasKey].subOptions = {
...this.options[taskName][aliasKey].subOptions,
...newSubOption
};
// Update reference
const subOptRef = new Map([[subOption, subOptionAlias]]);
this.options[taskName].reference = new Map([...this.options[taskName].reference, ...subOptRef]);
}
}
return this;
};
Command.prototype.getOptions = function() {
return this.options;
};
Command.prototype.parse = function(processArgv = []) {
try {
if (!Array.isArray(processArgv)) {
throw new Error("Missing the parameter processArgv. It must be an array of argument(s)");
}
const customRegEx = {
regexOption: this.settings.regexOption,
regexAlias: this.settings.regexAlias,
regexArgument: this.settings.regexArgument
};
const processArgvLength = processArgv.length,
taskName = processArgv[0];
let parsedCommand = {},
cmdPart,
mainOptKey;
const taskRef = this.options[taskName];
if (taskRef) {
const optionRef = taskRef.reference; // Get reference list (Mapping)
for (let i = 1; i < processArgvLength; ++i) {
cmdPart = typeOfParam(processArgv[i], customRegEx);
switch (cmdPart) {
case "option":
case "alias":
const optAlias = cmdPart === "option" ?
optionRef.get(processArgv[i]) :
cmdPart === "alias" ?
processArgv[i] :
null;
if (optAlias) {
const optKey = removeAliasSymbol(optAlias);
if (!mainOptKey && taskRef[optKey] && !parsedCommand[optKey]) {
// Parsing the main option key
mainOptKey = optKey;
parsedCommand = {
...parsedCommand,
[mainOptKey]: {}
};
} else if (parsedCommand[mainOptKey]) {
// Parsing the subOption key
if (taskRef[mainOptKey].subOptions[optKey]) {
parsedCommand[mainOptKey] = {
...parsedCommand[mainOptKey],
[optKey]: true
};
}
}
}
break;
case "argument":
if (parsedCommand[mainOptKey]) {
parsedCommand[mainOptKey] = {
...parsedCommand[mainOptKey],
argument: processArgv[i]
};
mainOptKey = null; // Clearing when finishing a command part
}
break;
default:
break;
}
}
}
return parsedCommand;
} catch (err) {
return err;
}
};
module.exports = { Command };