@tkeron/commands 0.1.1 → 0.2.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 +0 -1
- package/dist/example.e2e.test.d.ts +1 -0
- package/dist/example.js +55 -0
- package/dist/getCommandsFuncs.d.ts +17 -0
- package/dist/getCommandsFuncs.js +156 -0
- package/dist/getCommandsFuncs.test.d.ts +1 -0
- package/dist/getStart.d.ts +2 -0
- package/dist/getStart.js +46 -0
- package/dist/getStart.test.d.ts +1 -0
- package/dist/index.d.ts +1 -26
- package/dist/index.js +2 -88
- package/dist/testConstants.d.ts +4 -0
- package/dist/testConstants.js +33 -0
- package/dist/textFuncs.d.ts +4 -0
- package/dist/textFuncs.js +36 -0
- package/dist/textFuncs.test.d.ts +1 -0
- package/dist/types.d.ts +39 -0
- package/dist/types.js +2 -0
- package/jest.config.js +8 -0
- package/package.json +30 -27
- package/readme.md +1 -1
- package/tsconfig.json +15 -15
- /package/dist/{index.test.d.ts → example.d.ts} +0 -0
package/changelog.md
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/example.js
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const _1 = require(".");
|
|
4
|
+
const commands = (0, _1.getCommands)("test program", "0.0.14")
|
|
5
|
+
.addCommand("com1")
|
|
6
|
+
.addAlias("c1")
|
|
7
|
+
.addOption("opt1")
|
|
8
|
+
.addOption("opt2")
|
|
9
|
+
.addDescription("command 001 test...")
|
|
10
|
+
.addPositionedArgument("pos0")
|
|
11
|
+
.addPositionedArgument("pos1")
|
|
12
|
+
.setCallback(console.log)
|
|
13
|
+
.commands()
|
|
14
|
+
.addCommand("com2")
|
|
15
|
+
.addAlias("c2")
|
|
16
|
+
.addAlias("a2")
|
|
17
|
+
.addOption("opt1")
|
|
18
|
+
.addDescription("command 002 test...")
|
|
19
|
+
.setCallback(console.log)
|
|
20
|
+
.commands()
|
|
21
|
+
.addCommand("com3")
|
|
22
|
+
.addAlias("c3")
|
|
23
|
+
.addOption("opt1")
|
|
24
|
+
.addOption("opt2", "exOpt2...")
|
|
25
|
+
.addOption("opt3")
|
|
26
|
+
.addDescription("command 003 test...")
|
|
27
|
+
.addPositionedArgument("pos0")
|
|
28
|
+
.addPositionedArgument("pos1")
|
|
29
|
+
.addPositionedArgument("pos3")
|
|
30
|
+
.setCallback(console.log)
|
|
31
|
+
.commands()
|
|
32
|
+
.addHeaderText("header...\n\n")
|
|
33
|
+
.addFooterText("\n\nFooter...");
|
|
34
|
+
commands.start([
|
|
35
|
+
"",
|
|
36
|
+
"",
|
|
37
|
+
..."com1 pos0value opt1=qw111erty pos1value opt2=as222d asdasd"
|
|
38
|
+
.replace(/\s+/g, " ")
|
|
39
|
+
.split(" "),
|
|
40
|
+
]);
|
|
41
|
+
commands.start([
|
|
42
|
+
"",
|
|
43
|
+
"",
|
|
44
|
+
..."a2 pos0value opt1=qw111erty pos1value opt2=as222d asdasd"
|
|
45
|
+
.replace(/\s+/g, " ")
|
|
46
|
+
.split(" "),
|
|
47
|
+
]);
|
|
48
|
+
commands.start([
|
|
49
|
+
"",
|
|
50
|
+
"",
|
|
51
|
+
..."c3 pos0value opt1=qw111erty pos1value opt2=as222d asdasd"
|
|
52
|
+
.replace(/\s+/g, " ")
|
|
53
|
+
.split(" "),
|
|
54
|
+
]);
|
|
55
|
+
globalThis.commands = commands;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { CommandFactory, Command, Commands, CommandsCollection, Callback } from "./types";
|
|
2
|
+
export * from "./types";
|
|
3
|
+
export declare const getCommands: (programName?: string, version?: string) => Commands;
|
|
4
|
+
export declare const initCommands: (commandsCollection: CommandsCollection, commands?: Commands, commandFactory?: CommandFactory) => Commands;
|
|
5
|
+
export declare const initHelpAndVersion: (commands: Commands, commandsCollection: CommandsCollection) => {
|
|
6
|
+
helpCallback: () => void;
|
|
7
|
+
versionCallback: () => void;
|
|
8
|
+
};
|
|
9
|
+
export declare const getAddCommand: (commandsCollection: CommandsCollection, commandFactory: CommandFactory) => (commandName: string) => CommandFactory;
|
|
10
|
+
export declare const getAddAlias: (commandFactory: CommandFactory, commandsCollection: CommandsCollection) => (alias: string) => CommandFactory;
|
|
11
|
+
export declare const getAddOption: (commandFactory: CommandFactory, commandsCollection: CommandsCollection) => (option: string, example?: string) => CommandFactory;
|
|
12
|
+
export declare const getAddPositionedArgument: (commandFactory: CommandFactory, commandsCollection: CommandsCollection) => (arg: string) => CommandFactory;
|
|
13
|
+
export declare const getSetCallback: (commandFactory: CommandFactory, commandsCollection: CommandsCollection) => (fn: Callback) => CommandFactory;
|
|
14
|
+
export declare const getAddDescription: (commandFactory: CommandFactory, commandsCollection: CommandsCollection) => (description: string) => CommandFactory;
|
|
15
|
+
export declare const getAddHeaderText: (commands: Commands) => (text: string) => Commands;
|
|
16
|
+
export declare const getAddFooterText: (commands: Commands) => (text: string) => Commands;
|
|
17
|
+
export declare const getGetHelpLine: (command: Command) => (width?: number) => string;
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.getGetHelpLine = exports.getAddFooterText = exports.getAddHeaderText = exports.getAddDescription = exports.getSetCallback = exports.getAddPositionedArgument = exports.getAddOption = exports.getAddAlias = exports.getAddCommand = exports.initHelpAndVersion = exports.initCommands = exports.getCommands = void 0;
|
|
18
|
+
const getStart_1 = require("./getStart");
|
|
19
|
+
const textFuncs_1 = require("./textFuncs");
|
|
20
|
+
__exportStar(require("./types"), exports);
|
|
21
|
+
const getCommands = (programName = "program", version = "0.0.1") => {
|
|
22
|
+
const commandsCollection = {};
|
|
23
|
+
const commands = (0, exports.initCommands)(commandsCollection);
|
|
24
|
+
commands.programName = programName;
|
|
25
|
+
commands.version = version;
|
|
26
|
+
(0, exports.initHelpAndVersion)(commands, commandsCollection);
|
|
27
|
+
return commands;
|
|
28
|
+
};
|
|
29
|
+
exports.getCommands = getCommands;
|
|
30
|
+
const initCommands = (commandsCollection, commands = {
|
|
31
|
+
programName: "",
|
|
32
|
+
version: "",
|
|
33
|
+
footerText: "",
|
|
34
|
+
headerText: "",
|
|
35
|
+
addCommand: undefined,
|
|
36
|
+
start: undefined,
|
|
37
|
+
addHeaderText: undefined,
|
|
38
|
+
addFooterText: undefined,
|
|
39
|
+
}, commandFactory = {
|
|
40
|
+
commands: undefined,
|
|
41
|
+
name: undefined,
|
|
42
|
+
addAlias: undefined,
|
|
43
|
+
addOption: undefined,
|
|
44
|
+
addPositionedArgument: undefined,
|
|
45
|
+
addDescription: undefined,
|
|
46
|
+
setCallback: undefined,
|
|
47
|
+
}) => {
|
|
48
|
+
if (!commandFactory.commands)
|
|
49
|
+
commandFactory.commands = () => commands;
|
|
50
|
+
commands.addCommand = (0, exports.getAddCommand)(commandsCollection, commandFactory);
|
|
51
|
+
commands.start = (0, getStart_1.getStart)(commandsCollection);
|
|
52
|
+
commands.addHeaderText = (0, exports.getAddHeaderText)(commands);
|
|
53
|
+
commands.addFooterText = (0, exports.getAddFooterText)(commands);
|
|
54
|
+
return commands;
|
|
55
|
+
};
|
|
56
|
+
exports.initCommands = initCommands;
|
|
57
|
+
const initHelpAndVersion = (commands, commandsCollection) => {
|
|
58
|
+
const helpCallback = () => console["log"]((0, textFuncs_1.buildHelpText)(commands, commandsCollection));
|
|
59
|
+
const versionCallback = () => console["log"](`${commands.headerText || ""}\n${commands.version}\n${commands.footerText || ""}\n`);
|
|
60
|
+
commands
|
|
61
|
+
.addCommand("help")
|
|
62
|
+
.addAlias("-h")
|
|
63
|
+
.addAlias("--help")
|
|
64
|
+
.addDescription("show program help")
|
|
65
|
+
.setCallback(helpCallback);
|
|
66
|
+
commands
|
|
67
|
+
.addCommand("version")
|
|
68
|
+
.addAlias("-v")
|
|
69
|
+
.addAlias("--version")
|
|
70
|
+
.addDescription("show program version")
|
|
71
|
+
.setCallback(versionCallback);
|
|
72
|
+
return { helpCallback, versionCallback };
|
|
73
|
+
};
|
|
74
|
+
exports.initHelpAndVersion = initHelpAndVersion;
|
|
75
|
+
const getAddCommand = (commandsCollection, commandFactory) => (commandName) => {
|
|
76
|
+
commandFactory.name = commandName;
|
|
77
|
+
const command = {
|
|
78
|
+
aliases: [],
|
|
79
|
+
callback: undefined,
|
|
80
|
+
description: "",
|
|
81
|
+
name: commandName,
|
|
82
|
+
options: [],
|
|
83
|
+
optionsExamples: [],
|
|
84
|
+
positionedArguments: [],
|
|
85
|
+
getHelpLine: undefined,
|
|
86
|
+
};
|
|
87
|
+
commandsCollection[commandName] = command;
|
|
88
|
+
command.getHelpLine = (0, exports.getGetHelpLine)(command);
|
|
89
|
+
commandFactory.addAlias = (0, exports.getAddAlias)(commandFactory, commandsCollection);
|
|
90
|
+
commandFactory.addOption = (0, exports.getAddOption)(commandFactory, commandsCollection);
|
|
91
|
+
commandFactory.addDescription = (0, exports.getAddDescription)(commandFactory, commandsCollection);
|
|
92
|
+
commandFactory.setCallback = (0, exports.getSetCallback)(commandFactory, commandsCollection);
|
|
93
|
+
commandFactory.addPositionedArgument = (0, exports.getAddPositionedArgument)(commandFactory, commandsCollection);
|
|
94
|
+
return commandFactory;
|
|
95
|
+
};
|
|
96
|
+
exports.getAddCommand = getAddCommand;
|
|
97
|
+
const getAddAlias = (commandFactory, commandsCollection) => {
|
|
98
|
+
return (alias) => {
|
|
99
|
+
commandsCollection[commandFactory.name].aliases.push(alias);
|
|
100
|
+
commandsCollection[alias] = commandsCollection[commandFactory.name];
|
|
101
|
+
return commandFactory;
|
|
102
|
+
};
|
|
103
|
+
};
|
|
104
|
+
exports.getAddAlias = getAddAlias;
|
|
105
|
+
const getAddOption = (commandFactory, commandsCollection) => (option, example) => {
|
|
106
|
+
commandsCollection[commandFactory.name].options.push(option);
|
|
107
|
+
commandsCollection[commandFactory.name].optionsExamples.push(example || "");
|
|
108
|
+
return commandFactory;
|
|
109
|
+
};
|
|
110
|
+
exports.getAddOption = getAddOption;
|
|
111
|
+
const getAddPositionedArgument = (commandFactory, commandsCollection) => {
|
|
112
|
+
return (arg) => {
|
|
113
|
+
const positionedArguments = commandsCollection[commandFactory.name].positionedArguments;
|
|
114
|
+
if (!positionedArguments.includes(commandFactory.name)) {
|
|
115
|
+
positionedArguments.push(arg);
|
|
116
|
+
}
|
|
117
|
+
return commandFactory;
|
|
118
|
+
};
|
|
119
|
+
};
|
|
120
|
+
exports.getAddPositionedArgument = getAddPositionedArgument;
|
|
121
|
+
const getSetCallback = (commandFactory, commandsCollection) => {
|
|
122
|
+
return (fn) => {
|
|
123
|
+
commandsCollection[commandFactory.name].callback = fn;
|
|
124
|
+
return commandFactory;
|
|
125
|
+
};
|
|
126
|
+
};
|
|
127
|
+
exports.getSetCallback = getSetCallback;
|
|
128
|
+
const getAddDescription = (commandFactory, commandsCollection) => {
|
|
129
|
+
return (description) => {
|
|
130
|
+
commandsCollection[commandFactory.name].description = description;
|
|
131
|
+
return commandFactory;
|
|
132
|
+
};
|
|
133
|
+
};
|
|
134
|
+
exports.getAddDescription = getAddDescription;
|
|
135
|
+
const getAddHeaderText = (commands) => {
|
|
136
|
+
return (text) => {
|
|
137
|
+
commands.headerText = text;
|
|
138
|
+
return commands;
|
|
139
|
+
};
|
|
140
|
+
};
|
|
141
|
+
exports.getAddHeaderText = getAddHeaderText;
|
|
142
|
+
const getAddFooterText = (commands) => {
|
|
143
|
+
return (text) => {
|
|
144
|
+
commands.footerText = text;
|
|
145
|
+
return commands;
|
|
146
|
+
};
|
|
147
|
+
};
|
|
148
|
+
exports.getAddFooterText = getAddFooterText;
|
|
149
|
+
const getGetHelpLine = (command) => (width = 50) => {
|
|
150
|
+
let helpLine = (0, textFuncs_1.getCommandText)(command).padEnd(width, ".") +
|
|
151
|
+
" " +
|
|
152
|
+
command.description +
|
|
153
|
+
"\n";
|
|
154
|
+
return helpLine;
|
|
155
|
+
};
|
|
156
|
+
exports.getGetHelpLine = getGetHelpLine;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/getStart.js
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getStart = void 0;
|
|
4
|
+
const getStart = (commandsCollection) => (argv) => {
|
|
5
|
+
if (!argv)
|
|
6
|
+
argv = process.argv;
|
|
7
|
+
if (!Array.isArray(argv))
|
|
8
|
+
throw new Error("no arguments passed");
|
|
9
|
+
if (argv.length < 2)
|
|
10
|
+
throw Error("arguments out of range");
|
|
11
|
+
argv = argv.slice(2);
|
|
12
|
+
if (argv.length === 0) {
|
|
13
|
+
commandsCollection.help.callback();
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
const commandName = argv[0];
|
|
17
|
+
const command = commandsCollection[commandName];
|
|
18
|
+
if (!command) {
|
|
19
|
+
console["log"](`command '${commandName}' not found`);
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
const options = argv
|
|
23
|
+
.slice(1)
|
|
24
|
+
.filter((arg) => /\=/g.test(arg))
|
|
25
|
+
.map((arg) => arg.split("="))
|
|
26
|
+
.reduce((p, c) => {
|
|
27
|
+
p[c[0]] = c[1];
|
|
28
|
+
return p;
|
|
29
|
+
}, {});
|
|
30
|
+
const positionedArgsValues = argv
|
|
31
|
+
.slice(1)
|
|
32
|
+
.filter((arg) => !/\=/g.test(arg));
|
|
33
|
+
if (positionedArgsValues.length <= command.positionedArguments.length) {
|
|
34
|
+
positionedArgsValues.forEach((arg, n) => (options[command.positionedArguments[n]] = arg));
|
|
35
|
+
}
|
|
36
|
+
if (positionedArgsValues.length > command.positionedArguments.length) {
|
|
37
|
+
console["log"](`argument${positionedArgsValues.length - command.positionedArguments.length === 1
|
|
38
|
+
? ""
|
|
39
|
+
: "s"} '${positionedArgsValues
|
|
40
|
+
.slice(command.positionedArguments.length)
|
|
41
|
+
.join(", ")}' not defined`);
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
command.callback(options);
|
|
45
|
+
};
|
|
46
|
+
exports.getStart = getStart;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/index.d.ts
CHANGED
|
@@ -1,26 +1 @@
|
|
|
1
|
-
export
|
|
2
|
-
name: string;
|
|
3
|
-
aliases: string[];
|
|
4
|
-
options: string[];
|
|
5
|
-
callback: Callback;
|
|
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;
|
|
14
|
-
export interface Command {
|
|
15
|
-
name: string;
|
|
16
|
-
next: () => Commands;
|
|
17
|
-
addAliases: (...alias: string[]) => Command;
|
|
18
|
-
addOptions: (...options: string[]) => Command;
|
|
19
|
-
addPositionedArgument: (arg: string) => Command;
|
|
20
|
-
setCallback: (callback: Callback) => Command;
|
|
21
|
-
}
|
|
22
|
-
export interface Commands {
|
|
23
|
-
addCommand: (commandName: string) => Command;
|
|
24
|
-
run: () => void;
|
|
25
|
-
}
|
|
26
|
-
export declare const getCommands: () => Commands;
|
|
1
|
+
export { getCommands } from "./getCommandsFuncs";
|
package/dist/index.js
CHANGED
|
@@ -1,91 +1,5 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.getCommands = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
const run = () => {
|
|
7
|
-
const currentCommands = process.argv.slice(2);
|
|
8
|
-
const parsedOptions = currentCommands
|
|
9
|
-
.join(" ")
|
|
10
|
-
.match(/(\w+[:=]\w+)/g)
|
|
11
|
-
?.reduce((p, c) => {
|
|
12
|
-
const [k, v] = c.split(/[:=]/);
|
|
13
|
-
p[k] = v;
|
|
14
|
-
return p;
|
|
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
|
-
}
|
|
29
|
-
for (const commandIndex of currentCommands) {
|
|
30
|
-
const command = _commands[commandIndex];
|
|
31
|
-
if (!command)
|
|
32
|
-
continue;
|
|
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);
|
|
43
|
-
}
|
|
44
|
-
};
|
|
45
|
-
const getCommands = () => {
|
|
46
|
-
const commands = {
|
|
47
|
-
run,
|
|
48
|
-
addCommand: undefined,
|
|
49
|
-
};
|
|
50
|
-
commands.addCommand = (name) => {
|
|
51
|
-
const command = {
|
|
52
|
-
name,
|
|
53
|
-
addAliases: undefined,
|
|
54
|
-
addOptions: undefined,
|
|
55
|
-
addPositionedArgument: undefined,
|
|
56
|
-
next: undefined,
|
|
57
|
-
setCallback: undefined,
|
|
58
|
-
};
|
|
59
|
-
_commands[name] = {
|
|
60
|
-
name,
|
|
61
|
-
callback: undefined,
|
|
62
|
-
aliases: [],
|
|
63
|
-
options: [],
|
|
64
|
-
};
|
|
65
|
-
command.next = () => commands;
|
|
66
|
-
command.addAliases = (...alias) => {
|
|
67
|
-
_commands[name].aliases = [..._commands[name].aliases, ...alias];
|
|
68
|
-
alias.forEach((alias) => {
|
|
69
|
-
_commands[alias] = _commands[name];
|
|
70
|
-
});
|
|
71
|
-
return command;
|
|
72
|
-
};
|
|
73
|
-
command.addOptions = (...options) => {
|
|
74
|
-
options.forEach((option) => {
|
|
75
|
-
_commands[name].options.push(option);
|
|
76
|
-
});
|
|
77
|
-
return command;
|
|
78
|
-
};
|
|
79
|
-
command.setCallback = (callback) => {
|
|
80
|
-
_commands[name].callback = callback;
|
|
81
|
-
return command;
|
|
82
|
-
};
|
|
83
|
-
command.addPositionedArgument = (arg) => {
|
|
84
|
-
orderedArgumentNames.push(arg);
|
|
85
|
-
return command;
|
|
86
|
-
};
|
|
87
|
-
return command;
|
|
88
|
-
};
|
|
89
|
-
return commands;
|
|
90
|
-
};
|
|
91
|
-
exports.getCommands = getCommands;
|
|
4
|
+
var getCommandsFuncs_1 = require("./getCommandsFuncs");
|
|
5
|
+
Object.defineProperty(exports, "getCommands", { enumerable: true, get: function () { return getCommandsFuncs_1.getCommands; } });
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.commandsCollection = exports.commands = exports.command = void 0;
|
|
4
|
+
exports.command = {
|
|
5
|
+
name: "command_1",
|
|
6
|
+
aliases: ["al1", "al2"],
|
|
7
|
+
callback: undefined,
|
|
8
|
+
description: "command 1 description",
|
|
9
|
+
getHelpLine: () => "help line...\n",
|
|
10
|
+
options: ["op1", "op2"],
|
|
11
|
+
optionsExamples: ["opEx1"],
|
|
12
|
+
positionedArguments: ["pos1", "pos2"],
|
|
13
|
+
};
|
|
14
|
+
exports.commands = {
|
|
15
|
+
headerText: "header text...",
|
|
16
|
+
footerText: "footer text...",
|
|
17
|
+
};
|
|
18
|
+
exports.commandsCollection = {
|
|
19
|
+
command_1: exports.command,
|
|
20
|
+
al1: exports.command,
|
|
21
|
+
al2: exports.command,
|
|
22
|
+
command_2: {
|
|
23
|
+
...exports.command,
|
|
24
|
+
name: "command_2",
|
|
25
|
+
description: "command 2 description",
|
|
26
|
+
aliases: [],
|
|
27
|
+
},
|
|
28
|
+
help: {
|
|
29
|
+
...exports.command,
|
|
30
|
+
name: "help",
|
|
31
|
+
callback: () => console["log"]("help text..."),
|
|
32
|
+
},
|
|
33
|
+
};
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { Command, Commands, CommandsCollection } from "./types";
|
|
2
|
+
export declare const buildHelpText: (commands: Commands, commandsCollection: CommandsCollection) => string;
|
|
3
|
+
export declare const buildDescriptionsText: (commandsCollection: CommandsCollection) => string;
|
|
4
|
+
export declare const getCommandText: (command: Command) => string;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getCommandText = exports.buildDescriptionsText = exports.buildHelpText = void 0;
|
|
4
|
+
const buildHelpText = (commands, commandsCollection) => {
|
|
5
|
+
let descriptions = (0, exports.buildDescriptionsText)(commandsCollection);
|
|
6
|
+
const helpText = `\n${(commands.headerText.length > 0 && commands.headerText) || ""}\n${descriptions}${(commands.footerText.length > 0 && commands.footerText) || ""}\n`;
|
|
7
|
+
return helpText;
|
|
8
|
+
};
|
|
9
|
+
exports.buildHelpText = buildHelpText;
|
|
10
|
+
const buildDescriptionsText = (commandsCollection) => {
|
|
11
|
+
let descriptions = "";
|
|
12
|
+
let max = 0;
|
|
13
|
+
const ready = [];
|
|
14
|
+
for (const com of Object.values(commandsCollection)) {
|
|
15
|
+
const commandText = (0, exports.getCommandText)(com);
|
|
16
|
+
if (commandText.length > max)
|
|
17
|
+
max = commandText.length;
|
|
18
|
+
}
|
|
19
|
+
for (const com of Object.values(commandsCollection)) {
|
|
20
|
+
if (ready.includes(com.name))
|
|
21
|
+
continue;
|
|
22
|
+
descriptions += com.getHelpLine(max + 4);
|
|
23
|
+
ready.push(com.name);
|
|
24
|
+
}
|
|
25
|
+
return descriptions;
|
|
26
|
+
};
|
|
27
|
+
exports.buildDescriptionsText = buildDescriptionsText;
|
|
28
|
+
const getCommandText = (command) => {
|
|
29
|
+
const names = [command.name, ...command.aliases].join("|");
|
|
30
|
+
const args = command.positionedArguments.map((a) => `[${a}]`).join(" ");
|
|
31
|
+
const options = command.options
|
|
32
|
+
.map((o, n) => `[${o}=${command.optionsExamples[n] || "VALUE"}]`)
|
|
33
|
+
.join(" ");
|
|
34
|
+
return `${names} ${args} ${options}`.trim() + " ";
|
|
35
|
+
};
|
|
36
|
+
exports.getCommandText = getCommandText;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
export interface Command {
|
|
2
|
+
name: string;
|
|
3
|
+
description: string;
|
|
4
|
+
aliases: string[];
|
|
5
|
+
options: string[];
|
|
6
|
+
optionsExamples: string[];
|
|
7
|
+
positionedArguments: string[];
|
|
8
|
+
getHelpLine: (width?: number) => string;
|
|
9
|
+
callback: Callback;
|
|
10
|
+
}
|
|
11
|
+
export type parsedOptions = {
|
|
12
|
+
[key: string]: string;
|
|
13
|
+
};
|
|
14
|
+
export type Callback = (options?: parsedOptions) => void;
|
|
15
|
+
export type CommandsCollection = {
|
|
16
|
+
[key: string]: Command;
|
|
17
|
+
};
|
|
18
|
+
export type OrderedArgumentNames = {
|
|
19
|
+
[key: string]: string[];
|
|
20
|
+
};
|
|
21
|
+
export interface CommandFactory {
|
|
22
|
+
name: string;
|
|
23
|
+
commands: () => Commands;
|
|
24
|
+
addAlias: (alias: string) => CommandFactory;
|
|
25
|
+
addDescription: (description: string) => CommandFactory;
|
|
26
|
+
addOption: (option: string, example?: string) => CommandFactory;
|
|
27
|
+
addPositionedArgument: (arg: string) => CommandFactory;
|
|
28
|
+
setCallback: (callback: Callback) => CommandFactory;
|
|
29
|
+
}
|
|
30
|
+
export interface Commands {
|
|
31
|
+
addCommand: (commandName: string) => CommandFactory;
|
|
32
|
+
programName: string;
|
|
33
|
+
headerText: string;
|
|
34
|
+
footerText: string;
|
|
35
|
+
version: string;
|
|
36
|
+
addHeaderText: (text: string) => Commands;
|
|
37
|
+
addFooterText: (text: string) => Commands;
|
|
38
|
+
start: (argv?: string[]) => void;
|
|
39
|
+
}
|
package/dist/types.js
ADDED
package/jest.config.js
ADDED
package/package.json
CHANGED
|
@@ -1,27 +1,30 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@tkeron/commands",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "node library for handling command line arguments",
|
|
5
|
-
"main": "dist/index.js",
|
|
6
|
-
"scripts": {
|
|
7
|
-
"build": "tsc",
|
|
8
|
-
"test": "jest
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
"
|
|
12
|
-
"
|
|
13
|
-
|
|
14
|
-
"@types/
|
|
15
|
-
"
|
|
16
|
-
"
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
"
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
"
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
"
|
|
26
|
-
|
|
27
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "@tkeron/commands",
|
|
3
|
+
"version": "0.2.1",
|
|
4
|
+
"description": "node library for handling command line arguments",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"build": "tsc",
|
|
8
|
+
"test": "jest",
|
|
9
|
+
"lint": "prettier --write ."
|
|
10
|
+
},
|
|
11
|
+
"author": "tkeron",
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"devDependencies": {
|
|
14
|
+
"@types/jest": "^29.5.6",
|
|
15
|
+
"@types/node": "^18.11.12",
|
|
16
|
+
"jest": "^29.3.1",
|
|
17
|
+
"prettier": "^3.0.3",
|
|
18
|
+
"ts-jest": "^29.1.1",
|
|
19
|
+
"typescript": "^4.9.4"
|
|
20
|
+
},
|
|
21
|
+
"keywords": [
|
|
22
|
+
"cli",
|
|
23
|
+
"commands",
|
|
24
|
+
"command-line",
|
|
25
|
+
"arguments"
|
|
26
|
+
],
|
|
27
|
+
"repository": {
|
|
28
|
+
"url": "git@github.com:tkeron/tkeron.git"
|
|
29
|
+
}
|
|
30
|
+
}
|
package/readme.md
CHANGED
package/tsconfig.json
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"module": "NodeNext",
|
|
4
|
-
"outDir": "dist",
|
|
5
|
-
"target": "ESNext",
|
|
6
|
-
"esModuleInterop": true,
|
|
7
|
-
"declaration": true
|
|
8
|
-
},
|
|
9
|
-
"exclude": [
|
|
10
|
-
"node_modules"
|
|
11
|
-
],
|
|
12
|
-
"include": [
|
|
13
|
-
"src"
|
|
14
|
-
]
|
|
15
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"module": "NodeNext",
|
|
4
|
+
"outDir": "dist",
|
|
5
|
+
"target": "ESNext",
|
|
6
|
+
"esModuleInterop": true,
|
|
7
|
+
"declaration": true
|
|
8
|
+
},
|
|
9
|
+
"exclude": [
|
|
10
|
+
"node_modules"
|
|
11
|
+
],
|
|
12
|
+
"include": [
|
|
13
|
+
"src"
|
|
14
|
+
]
|
|
15
|
+
}
|
|
File without changes
|