@tkeron/commands 0.2.0 → 0.3.0
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/.github/workflows/npm_deploy.yml +24 -0
- package/LICENSE +21 -21
- package/bun.lockb +0 -0
- package/changelog.md +4 -0
- package/package.json +10 -15
- package/src/example.e2e.test.ts +37 -0
- package/src/example.ts +69 -0
- package/src/getCommandsFuncs.test.ts +94 -0
- package/src/getCommandsFuncs.ts +204 -0
- package/src/getStart.test.ts +79 -0
- package/src/getStart.ts +56 -0
- package/src/testConstants.ts +32 -0
- package/src/textFuncs.test.ts +59 -0
- package/src/textFuncs.ts +47 -0
- package/src/types.ts +39 -0
- package/tsconfig.json +23 -11
- package/dist/example.d.ts +0 -1
- package/dist/example.e2e.test.d.ts +0 -1
- package/dist/example.js +0 -55
- package/dist/getCommandsFuncs.d.ts +0 -17
- package/dist/getCommandsFuncs.js +0 -156
- package/dist/getCommandsFuncs.test.d.ts +0 -1
- package/dist/getStart.d.ts +0 -2
- package/dist/getStart.js +0 -46
- package/dist/getStart.test.d.ts +0 -1
- package/dist/index.js +0 -5
- package/dist/testConstants.d.ts +0 -4
- package/dist/testConstants.js +0 -33
- package/dist/textFuncs.d.ts +0 -4
- package/dist/textFuncs.js +0 -36
- package/dist/textFuncs.test.d.ts +0 -1
- package/dist/types.d.ts +0 -39
- package/dist/types.js +0 -2
- package/jest.config.js +0 -8
- /package/{dist/index.d.ts → src/index.ts} +0 -0
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { command, commands, commandsCollection } from "./testConstants";
|
|
2
|
+
import {
|
|
3
|
+
buildDescriptionsText,
|
|
4
|
+
buildHelpText,
|
|
5
|
+
getCommandText,
|
|
6
|
+
} from "./textFuncs";
|
|
7
|
+
import { describe, expect, it } from "bun:test";
|
|
8
|
+
|
|
9
|
+
describe("text Functions", () => {
|
|
10
|
+
describe("getCommandText", () => {
|
|
11
|
+
it("should return command text", () => {
|
|
12
|
+
const result = getCommandText(command);
|
|
13
|
+
|
|
14
|
+
expect(result).toBe(
|
|
15
|
+
"command_1|al1|al2 [pos1] [pos2] [op1=opEx1] [op2=VALUE] "
|
|
16
|
+
);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it("should return command text without aliases", () => {
|
|
20
|
+
const result = getCommandText({ ...command, aliases: [] });
|
|
21
|
+
|
|
22
|
+
expect(result).toBe(
|
|
23
|
+
"command_1 [pos1] [pos2] [op1=opEx1] [op2=VALUE] "
|
|
24
|
+
);
|
|
25
|
+
});
|
|
26
|
+
});
|
|
27
|
+
describe("buildHelpText", () => {
|
|
28
|
+
it("should return help text", () => {
|
|
29
|
+
const result = buildHelpText(commands, commandsCollection);
|
|
30
|
+
expect(result).toBe(
|
|
31
|
+
"\nheader text...\nhelp line...\nhelp line...\nhelp line...\nfooter text...\n"
|
|
32
|
+
);
|
|
33
|
+
});
|
|
34
|
+
it("should return help text without headerText", () => {
|
|
35
|
+
const result = buildHelpText(
|
|
36
|
+
{ ...commands, headerText: "" },
|
|
37
|
+
commandsCollection
|
|
38
|
+
);
|
|
39
|
+
expect(result).toBe(
|
|
40
|
+
"\n\nhelp line...\nhelp line...\nhelp line...\nfooter text...\n"
|
|
41
|
+
);
|
|
42
|
+
});
|
|
43
|
+
it("should return help text without footerText", () => {
|
|
44
|
+
const result = buildHelpText(
|
|
45
|
+
{ ...commands, footerText: "" },
|
|
46
|
+
commandsCollection
|
|
47
|
+
);
|
|
48
|
+
expect(result).toBe(
|
|
49
|
+
"\nheader text...\nhelp line...\nhelp line...\nhelp line...\n\n"
|
|
50
|
+
);
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
describe("buildDescriptionsText", () => {
|
|
54
|
+
it("should return description text", () => {
|
|
55
|
+
const result = buildDescriptionsText(commandsCollection);
|
|
56
|
+
expect(result).toBe("help line...\n".repeat(3));
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
});
|
package/src/textFuncs.ts
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { Command, Commands, CommandsCollection } from "./types";
|
|
2
|
+
|
|
3
|
+
export const buildHelpText = (
|
|
4
|
+
commands: Commands,
|
|
5
|
+
commandsCollection: CommandsCollection
|
|
6
|
+
): string => {
|
|
7
|
+
let descriptions = buildDescriptionsText(commandsCollection);
|
|
8
|
+
|
|
9
|
+
const helpText = `\n${
|
|
10
|
+
(commands.headerText.length > 0 && commands.headerText) || ""
|
|
11
|
+
}\n${descriptions}${
|
|
12
|
+
(commands.footerText.length > 0 && commands.footerText) || ""
|
|
13
|
+
}\n`;
|
|
14
|
+
return helpText;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export const buildDescriptionsText = (
|
|
18
|
+
commandsCollection: CommandsCollection
|
|
19
|
+
): string => {
|
|
20
|
+
let descriptions = "";
|
|
21
|
+
let max = 0;
|
|
22
|
+
const ready: string[] = [];
|
|
23
|
+
|
|
24
|
+
for (const com of Object.values(commandsCollection)) {
|
|
25
|
+
const commandText = getCommandText(com);
|
|
26
|
+
if (commandText.length > max) max = commandText.length;
|
|
27
|
+
}
|
|
28
|
+
for (const com of Object.values(commandsCollection)) {
|
|
29
|
+
if (ready.includes(com.name)) continue;
|
|
30
|
+
descriptions += com.getHelpLine(max + 4);
|
|
31
|
+
ready.push(com.name);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return descriptions;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
export const getCommandText = (command: Command): string => {
|
|
38
|
+
const names = [command.name, ...command.aliases].join("|");
|
|
39
|
+
|
|
40
|
+
const args = command.positionedArguments.map((a) => `[${a}]`).join(" ");
|
|
41
|
+
|
|
42
|
+
const options = command.options
|
|
43
|
+
.map((o, n) => `[${o}=${command.optionsExamples[n] || "VALUE"}]`)
|
|
44
|
+
.join(" ");
|
|
45
|
+
|
|
46
|
+
return `${names} ${args} ${options}`.trim() + " ";
|
|
47
|
+
};
|
package/src/types.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
|
+
|
|
12
|
+
export type parsedOptions = { [key: string]: string };
|
|
13
|
+
|
|
14
|
+
export type Callback = (options?: parsedOptions) => void;
|
|
15
|
+
|
|
16
|
+
export type CommandsCollection = { [key: string]: Command };
|
|
17
|
+
|
|
18
|
+
export type OrderedArgumentNames = { [key: string]: string[] };
|
|
19
|
+
|
|
20
|
+
export interface CommandFactory {
|
|
21
|
+
name: string;
|
|
22
|
+
commands: () => Commands;
|
|
23
|
+
addAlias: (alias: string) => CommandFactory;
|
|
24
|
+
addDescription: (description: string) => CommandFactory;
|
|
25
|
+
addOption: (option: string, example?: string) => CommandFactory;
|
|
26
|
+
addPositionedArgument: (arg: string) => CommandFactory;
|
|
27
|
+
setCallback: (callback: Callback) => CommandFactory;
|
|
28
|
+
}
|
|
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/tsconfig.json
CHANGED
|
@@ -1,15 +1,27 @@
|
|
|
1
1
|
{
|
|
2
2
|
"compilerOptions": {
|
|
3
|
-
|
|
4
|
-
"
|
|
3
|
+
// Enable latest features
|
|
4
|
+
"lib": ["ESNext", "DOM"],
|
|
5
5
|
"target": "ESNext",
|
|
6
|
-
"
|
|
7
|
-
"
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
"
|
|
14
|
-
|
|
6
|
+
"module": "ESNext",
|
|
7
|
+
"moduleDetection": "force",
|
|
8
|
+
"jsx": "react-jsx",
|
|
9
|
+
"allowJs": true,
|
|
10
|
+
|
|
11
|
+
// Bundler mode
|
|
12
|
+
"moduleResolution": "bundler",
|
|
13
|
+
"allowImportingTsExtensions": true,
|
|
14
|
+
"verbatimModuleSyntax": true,
|
|
15
|
+
"noEmit": true,
|
|
16
|
+
|
|
17
|
+
// Best practices
|
|
18
|
+
"strict": true,
|
|
19
|
+
"skipLibCheck": true,
|
|
20
|
+
"noFallthroughCasesInSwitch": true,
|
|
21
|
+
|
|
22
|
+
// Some stricter flags (disabled by default)
|
|
23
|
+
"noUnusedLocals": false,
|
|
24
|
+
"noUnusedParameters": false,
|
|
25
|
+
"noPropertyAccessFromIndexSignature": false
|
|
26
|
+
}
|
|
15
27
|
}
|
package/dist/example.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/dist/example.js
DELETED
|
@@ -1,55 +0,0 @@
|
|
|
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;
|
|
@@ -1,17 +0,0 @@
|
|
|
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;
|
package/dist/getCommandsFuncs.js
DELETED
|
@@ -1,156 +0,0 @@
|
|
|
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;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/dist/getStart.d.ts
DELETED
package/dist/getStart.js
DELETED
|
@@ -1,46 +0,0 @@
|
|
|
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;
|
package/dist/getStart.test.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/dist/index.js
DELETED
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getCommands = void 0;
|
|
4
|
-
var getCommandsFuncs_1 = require("./getCommandsFuncs");
|
|
5
|
-
Object.defineProperty(exports, "getCommands", { enumerable: true, get: function () { return getCommandsFuncs_1.getCommands; } });
|
package/dist/testConstants.d.ts
DELETED
package/dist/testConstants.js
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
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
|
-
};
|
package/dist/textFuncs.d.ts
DELETED
|
@@ -1,4 +0,0 @@
|
|
|
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;
|
package/dist/textFuncs.js
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
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;
|
package/dist/textFuncs.test.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/dist/types.d.ts
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
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
DELETED
package/jest.config.js
DELETED
|
File without changes
|