@tkeron/commands 0.0.2 → 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 ADDED
@@ -0,0 +1,8 @@
1
+ # v0.1.1
2
+
3
+ - fixed commands ordered arguments
4
+
5
+ # v0.1.0
6
+
7
+ - passes positioned arguments to the callback in the second parameter.
8
+
package/dist/index.d.ts CHANGED
@@ -2,14 +2,22 @@ export interface CommandHandler {
2
2
  name: string;
3
3
  aliases: string[];
4
4
  options: string[];
5
- callback: CallableFunction;
5
+ callback: Callback;
6
6
  }
7
+ export type Options = {
8
+ [key: string]: CommandHandler;
9
+ };
10
+ export type parsedOptions = {
11
+ [key: string]: string;
12
+ };
13
+ export type Callback = (options: parsedOptions) => void;
7
14
  export interface Command {
8
15
  name: string;
9
16
  next: () => Commands;
10
17
  addAliases: (...alias: string[]) => Command;
11
18
  addOptions: (...options: string[]) => Command;
12
- setCallback: (callback: CallableFunction) => Command;
19
+ addPositionedArgument: (arg: string) => Command;
20
+ setCallback: (callback: Callback) => Command;
13
21
  }
14
22
  export interface Commands {
15
23
  addCommand: (commandName: string) => Command;
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
@@ -12,16 +13,32 @@ const run = () => {
12
13
  p[k] = v;
13
14
  return p;
14
15
  }, {});
16
+ const orderedArguments = currentCommands?.filter((c) => !/[:=-]/g.test(c));
17
+ if (orderedArguments &&
18
+ orderedArguments.length &&
19
+ Object.keys(_commands).includes(orderedArguments[0]))
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
+ }
15
29
  for (const commandIndex of currentCommands) {
16
30
  const command = _commands[commandIndex];
17
31
  if (!command)
18
32
  continue;
19
- const options = parsedOptions && command.options
20
- ? command.options.reduce((p, c) => {
21
- p[c] = parsedOptions[c];
22
- return p;
23
- }, {})
24
- : {};
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
+ };
25
42
  command.callback(options);
26
43
  }
27
44
  };
@@ -35,6 +52,7 @@ const getCommands = () => {
35
52
  name,
36
53
  addAliases: undefined,
37
54
  addOptions: undefined,
55
+ addPositionedArgument: undefined,
38
56
  next: undefined,
39
57
  setCallback: undefined,
40
58
  };
@@ -62,6 +80,10 @@ const getCommands = () => {
62
80
  _commands[name].callback = callback;
63
81
  return command;
64
82
  };
83
+ command.addPositionedArgument = (arg) => {
84
+ orderedArgumentNames.push(arg);
85
+ return command;
86
+ };
65
87
  return command;
66
88
  };
67
89
  return commands;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tkeron/commands",
3
- "version": "0.0.2",
3
+ "version": "0.1.1",
4
4
  "description": "node library for handling command line arguments",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {