@tkeron/commands 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/changelog.md +4 -0
- package/dist/index.d.ts +5 -1
- package/dist/index.js +24 -7
- package/package.json +1 -1
package/changelog.md
CHANGED
package/dist/index.d.ts
CHANGED
|
@@ -7,12 +7,16 @@ export interface CommandHandler {
|
|
|
7
7
|
export type Options = {
|
|
8
8
|
[key: string]: CommandHandler;
|
|
9
9
|
};
|
|
10
|
-
export type
|
|
10
|
+
export type parsedOptions = {
|
|
11
|
+
[key: string]: string;
|
|
12
|
+
};
|
|
13
|
+
export type Callback = (options: parsedOptions) => void;
|
|
11
14
|
export interface Command {
|
|
12
15
|
name: string;
|
|
13
16
|
next: () => Commands;
|
|
14
17
|
addAliases: (...alias: string[]) => Command;
|
|
15
18
|
addOptions: (...options: string[]) => Command;
|
|
19
|
+
addPositionedArgument: (arg: string) => Command;
|
|
16
20
|
setCallback: (callback: Callback) => Command;
|
|
17
21
|
}
|
|
18
22
|
export interface Commands {
|
package/dist/index.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.getCommands = void 0;
|
|
4
4
|
const _commands = {};
|
|
5
|
+
const orderedArgumentNames = [];
|
|
5
6
|
const run = () => {
|
|
6
7
|
const currentCommands = process.argv.slice(2);
|
|
7
8
|
const parsedOptions = currentCommands
|
|
@@ -17,17 +18,28 @@ const run = () => {
|
|
|
17
18
|
orderedArguments.length &&
|
|
18
19
|
Object.keys(_commands).includes(orderedArguments[0]))
|
|
19
20
|
orderedArguments.shift();
|
|
21
|
+
const orderedOptions = {};
|
|
22
|
+
if (orderedArguments &&
|
|
23
|
+
orderedArguments.length > 0 &&
|
|
24
|
+
orderedArgumentNames.length === orderedArguments.length) {
|
|
25
|
+
orderedArgumentNames.forEach((key, n) => {
|
|
26
|
+
orderedOptions[key] = orderedArguments[n];
|
|
27
|
+
});
|
|
28
|
+
}
|
|
20
29
|
for (const commandIndex of currentCommands) {
|
|
21
30
|
const command = _commands[commandIndex];
|
|
22
31
|
if (!command)
|
|
23
32
|
continue;
|
|
24
|
-
const options =
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
33
|
+
const options = {
|
|
34
|
+
...orderedOptions,
|
|
35
|
+
...(parsedOptions && command.options
|
|
36
|
+
? command.options.reduce((p, c) => {
|
|
37
|
+
p[c] = parsedOptions[c];
|
|
38
|
+
return p;
|
|
39
|
+
}, {})
|
|
40
|
+
: {}),
|
|
41
|
+
};
|
|
42
|
+
command.callback(options);
|
|
31
43
|
}
|
|
32
44
|
};
|
|
33
45
|
const getCommands = () => {
|
|
@@ -40,6 +52,7 @@ const getCommands = () => {
|
|
|
40
52
|
name,
|
|
41
53
|
addAliases: undefined,
|
|
42
54
|
addOptions: undefined,
|
|
55
|
+
addPositionedArgument: undefined,
|
|
43
56
|
next: undefined,
|
|
44
57
|
setCallback: undefined,
|
|
45
58
|
};
|
|
@@ -67,6 +80,10 @@ const getCommands = () => {
|
|
|
67
80
|
_commands[name].callback = callback;
|
|
68
81
|
return command;
|
|
69
82
|
};
|
|
83
|
+
command.addPositionedArgument = (arg) => {
|
|
84
|
+
orderedArgumentNames.push(arg);
|
|
85
|
+
return command;
|
|
86
|
+
};
|
|
70
87
|
return command;
|
|
71
88
|
};
|
|
72
89
|
return commands;
|