@tkeron/commands 0.3.1 → 0.4.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/.github/workflows/npm_deploy.yml +13 -7
- package/bun.lockb +0 -0
- package/changelog.md +21 -0
- package/examples/example.ts +116 -0
- package/package.json +8 -2
- package/readme.md +119 -2
- package/src/applyDefaults.ts +20 -0
- package/src/getCommandsFuncs.ts +92 -21
- package/src/getStart.ts +23 -28
- package/src/index.ts +1 -1
- package/src/parseArgs.ts +233 -0
- package/src/textFuncs.ts +32 -7
- package/src/types.ts +59 -4
- package/src/validateOptions.ts +39 -0
- package/tests/applyDefaults.test.ts +133 -0
- package/tests/backward-compatibility.test.ts +187 -0
- package/tests/example.e2e.test.ts +42 -0
- package/tests/getCommandsFuncs.test.ts +492 -0
- package/tests/getStart.test.ts +265 -0
- package/{src → tests/helpers}/testConstants.ts +1 -1
- package/tests/parseArgs.test.ts +565 -0
- package/tests/textFuncs.test.ts +179 -0
- package/tests/validateOptions.test.ts +157 -0
- package/tests/version-command.test.ts +292 -0
- package/tsconfig.json +2 -3
- package/src/example.e2e.test.ts +0 -37
- package/src/example.ts +0 -69
- package/src/getCommandsFuncs.test.ts +0 -94
- package/src/getStart.test.ts +0 -79
- package/src/textFuncs.test.ts +0 -59
package/src/example.ts
DELETED
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
import { getCommands } from ".";
|
|
2
|
-
import type { Commands } from "./types";
|
|
3
|
-
|
|
4
|
-
const commands = 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
|
-
|
|
14
|
-
.commands()
|
|
15
|
-
|
|
16
|
-
.addCommand("com2")
|
|
17
|
-
.addAlias("c2")
|
|
18
|
-
.addAlias("a2")
|
|
19
|
-
.addOption("opt1")
|
|
20
|
-
.addDescription("command 002 test...")
|
|
21
|
-
.setCallback(console.log)
|
|
22
|
-
|
|
23
|
-
.commands()
|
|
24
|
-
|
|
25
|
-
.addCommand("com3")
|
|
26
|
-
.addAlias("c3")
|
|
27
|
-
.addOption("opt1")
|
|
28
|
-
.addOption("opt2", "exOpt2...")
|
|
29
|
-
.addOption("opt3")
|
|
30
|
-
.addDescription("command 003 test...")
|
|
31
|
-
.addPositionedArgument("pos0")
|
|
32
|
-
.addPositionedArgument("pos1")
|
|
33
|
-
.addPositionedArgument("pos3")
|
|
34
|
-
.setCallback(console.log)
|
|
35
|
-
|
|
36
|
-
.commands()
|
|
37
|
-
|
|
38
|
-
.addHeaderText("header...\n\n")
|
|
39
|
-
.addFooterText("\n\nFooter...");
|
|
40
|
-
|
|
41
|
-
commands.start([
|
|
42
|
-
"",
|
|
43
|
-
"",
|
|
44
|
-
..."com1 pos0value opt1=qw111erty pos1value opt2=as222d asdasd"
|
|
45
|
-
.replace(/\s+/g, " ")
|
|
46
|
-
.split(" "),
|
|
47
|
-
]);
|
|
48
|
-
|
|
49
|
-
commands.start([
|
|
50
|
-
"",
|
|
51
|
-
"",
|
|
52
|
-
..."a2 pos0value opt1=qw111erty pos1value opt2=as222d asdasd"
|
|
53
|
-
.replace(/\s+/g, " ")
|
|
54
|
-
.split(" "),
|
|
55
|
-
]);
|
|
56
|
-
|
|
57
|
-
commands.start([
|
|
58
|
-
"",
|
|
59
|
-
"",
|
|
60
|
-
..."c3 pos0value opt1=qw111erty pos1value opt2=as222d asdasd"
|
|
61
|
-
.replace(/\s+/g, " ")
|
|
62
|
-
.split(" "),
|
|
63
|
-
]);
|
|
64
|
-
|
|
65
|
-
declare global {
|
|
66
|
-
var commands: Commands;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
globalThis.commands = commands;
|
|
@@ -1,94 +0,0 @@
|
|
|
1
|
-
import { beforeEach, describe, expect, it } from "bun:test";
|
|
2
|
-
import type { CommandFactory, Commands, CommandsCollection } from "./types";
|
|
3
|
-
import {
|
|
4
|
-
getAddFooterText,
|
|
5
|
-
getAddHeaderText,
|
|
6
|
-
getAddOption,
|
|
7
|
-
getAddPositionedArgument,
|
|
8
|
-
getCommands,
|
|
9
|
-
getGetHelpLine,
|
|
10
|
-
initCommands,
|
|
11
|
-
initHelpAndVersion,
|
|
12
|
-
} from "./getCommandsFuncs";
|
|
13
|
-
|
|
14
|
-
describe("main", () => {
|
|
15
|
-
let commandsCollection: CommandsCollection;
|
|
16
|
-
let commands: Commands;
|
|
17
|
-
let commandFactory: CommandFactory;
|
|
18
|
-
|
|
19
|
-
beforeEach(() => {
|
|
20
|
-
commandsCollection = {};
|
|
21
|
-
commandFactory = <CommandFactory>(<unknown>{
|
|
22
|
-
commands: undefined,
|
|
23
|
-
name: undefined,
|
|
24
|
-
addAlias: undefined,
|
|
25
|
-
addOption: undefined,
|
|
26
|
-
addPositionedArgument: undefined,
|
|
27
|
-
addDescription: undefined,
|
|
28
|
-
setCallback: undefined,
|
|
29
|
-
});
|
|
30
|
-
commands = initCommands(commandsCollection, undefined, commandFactory);
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
it("getCommands", () => {
|
|
34
|
-
const commands = getCommands();
|
|
35
|
-
expect(commands).toBeTruthy;
|
|
36
|
-
});
|
|
37
|
-
it("initHelpAndVersion", () => {
|
|
38
|
-
expect(commandsCollection).not.toHaveProperty("help");
|
|
39
|
-
expect(commandsCollection).not.toHaveProperty("version");
|
|
40
|
-
const { helpCallback, versionCallback } = initHelpAndVersion(
|
|
41
|
-
commands,
|
|
42
|
-
commandsCollection
|
|
43
|
-
);
|
|
44
|
-
expect(commandsCollection).toHaveProperty("help");
|
|
45
|
-
expect(commandsCollection).toHaveProperty("version");
|
|
46
|
-
|
|
47
|
-
expect(helpCallback).not.toThrow();
|
|
48
|
-
expect(versionCallback).not.toThrow();
|
|
49
|
-
});
|
|
50
|
-
it("getAddOption", () => {
|
|
51
|
-
commands.addCommand("test");
|
|
52
|
-
const addOption = getAddOption(commandFactory, commandsCollection);
|
|
53
|
-
addOption("op001");
|
|
54
|
-
const { test } = commandsCollection;
|
|
55
|
-
expect(test.options.includes("op001"));
|
|
56
|
-
});
|
|
57
|
-
it("getAddPositionedArgument", () => {
|
|
58
|
-
commands.addCommand("test");
|
|
59
|
-
const addPositionedArgument = getAddPositionedArgument(
|
|
60
|
-
commandFactory,
|
|
61
|
-
commandsCollection
|
|
62
|
-
);
|
|
63
|
-
|
|
64
|
-
addPositionedArgument("pos001");
|
|
65
|
-
const { test } = commandsCollection;
|
|
66
|
-
|
|
67
|
-
expect(test.positionedArguments.includes("pos001"));
|
|
68
|
-
});
|
|
69
|
-
it("getAddHeaderText", () => {
|
|
70
|
-
const addHeaderText = getAddHeaderText(commands);
|
|
71
|
-
const txt = "test header text...";
|
|
72
|
-
addHeaderText(txt);
|
|
73
|
-
expect(commands.headerText).toBe(txt);
|
|
74
|
-
});
|
|
75
|
-
it("getAddFooterText", () => {
|
|
76
|
-
const addFooterText = getAddFooterText(commands);
|
|
77
|
-
const txt = "test footer text...";
|
|
78
|
-
addFooterText(txt);
|
|
79
|
-
expect(commands.footerText).toBe(txt);
|
|
80
|
-
});
|
|
81
|
-
it("getGetHelpLine", () => {
|
|
82
|
-
commands.addCommand("test");
|
|
83
|
-
const { test } = commandsCollection;
|
|
84
|
-
const getHelpLine = getGetHelpLine(test);
|
|
85
|
-
const hl50 = getHelpLine();
|
|
86
|
-
expect(hl50).toBe("test ............................................ \n");
|
|
87
|
-
});
|
|
88
|
-
it("", () => {
|
|
89
|
-
commandFactory.commands = <() => Commands>(<unknown>undefined);
|
|
90
|
-
initCommands(commandsCollection, commands, commandFactory);
|
|
91
|
-
expect(commandFactory.commands).toBeTruthy;
|
|
92
|
-
expect(commandFactory.commands).not.toThrow();
|
|
93
|
-
});
|
|
94
|
-
});
|
package/src/getStart.test.ts
DELETED
|
@@ -1,79 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
afterEach,
|
|
3
|
-
beforeEach,
|
|
4
|
-
describe,
|
|
5
|
-
expect,
|
|
6
|
-
it,
|
|
7
|
-
mock,
|
|
8
|
-
spyOn,
|
|
9
|
-
type Mock,
|
|
10
|
-
} from "bun:test";
|
|
11
|
-
|
|
12
|
-
import { getStart } from "./getStart";
|
|
13
|
-
import { commandsCollection } from "./testConstants";
|
|
14
|
-
|
|
15
|
-
describe("getStart", () => {
|
|
16
|
-
let callback: Mock<any>;
|
|
17
|
-
let start: (argv?: string[]) => void;
|
|
18
|
-
let logMock: Mock<any>;
|
|
19
|
-
let logs: any[] = [];
|
|
20
|
-
|
|
21
|
-
beforeEach(() => {
|
|
22
|
-
callback = mock();
|
|
23
|
-
commandsCollection.command_1.callback = callback;
|
|
24
|
-
start = getStart(commandsCollection);
|
|
25
|
-
logMock = spyOn(console, "log").mockImplementation((...args: any) => {
|
|
26
|
-
logs.push(args);
|
|
27
|
-
});
|
|
28
|
-
});
|
|
29
|
-
afterEach(() => {
|
|
30
|
-
callback.mockClear();
|
|
31
|
-
logMock.mockClear();
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
it("happy path, run command", () => {
|
|
35
|
-
start(["", "", "command_1"]);
|
|
36
|
-
expect(callback).toBeCalledTimes(1);
|
|
37
|
-
});
|
|
38
|
-
it("run command with option", () => {
|
|
39
|
-
start(["", "", "command_1", "op1=value1"]);
|
|
40
|
-
expect(callback).toBeCalledTimes(1);
|
|
41
|
-
});
|
|
42
|
-
it("run command with extra positioned arguments", () => {
|
|
43
|
-
logs = [];
|
|
44
|
-
start(["", "", "command_1", "pos1", "pos2", "pos3"]);
|
|
45
|
-
start(["", "", "command_1", "pos1", "pos2", "pos3", "pos4"]);
|
|
46
|
-
expect(logs).toHaveLength(2);
|
|
47
|
-
expect(logs[0]).toHaveLength(1);
|
|
48
|
-
expect(logs[0][0]).toBe("argument 'pos3' not defined");
|
|
49
|
-
expect(logs[1]).toHaveLength(1);
|
|
50
|
-
expect(logs[1][0]).toBe("arguments 'pos3, pos4' not defined");
|
|
51
|
-
});
|
|
52
|
-
it("run with process.argv", () => {
|
|
53
|
-
process.argv = ["", ""];
|
|
54
|
-
start();
|
|
55
|
-
logs = [];
|
|
56
|
-
commandsCollection.help.callback();
|
|
57
|
-
expect(logs).toHaveLength(1);
|
|
58
|
-
});
|
|
59
|
-
it("should throw when no args passed", () => {
|
|
60
|
-
process.argv = <string[]>(<unknown>undefined);
|
|
61
|
-
expect(start).toThrow(new Error("no arguments passed"));
|
|
62
|
-
});
|
|
63
|
-
it("should throw when less than 2 args passed", () => {
|
|
64
|
-
process.argv = [];
|
|
65
|
-
expect(start).toThrow(new Error("arguments out of range"));
|
|
66
|
-
});
|
|
67
|
-
it("should show message when passed an unexistent commnad", () => {
|
|
68
|
-
process.argv = ["", "", "fakeCommand"];
|
|
69
|
-
logs = [];
|
|
70
|
-
start();
|
|
71
|
-
expect(logs).toHaveLength(1);
|
|
72
|
-
expect(logs[0]).toHaveLength(1);
|
|
73
|
-
expect(logs[0][0]).toBe("command 'fakeCommand' not found");
|
|
74
|
-
});
|
|
75
|
-
it("run with less positioned arguments", () => {
|
|
76
|
-
start(["", "", "al1", "arg001"]);
|
|
77
|
-
expect(callback).toBeCalledWith({ pos1: "arg001" });
|
|
78
|
-
});
|
|
79
|
-
});
|
package/src/textFuncs.test.ts
DELETED
|
@@ -1,59 +0,0 @@
|
|
|
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
|
-
});
|