@tkeron/commands 0.2.1 → 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,24 @@
|
|
|
1
|
+
name: commands package
|
|
2
|
+
on:
|
|
3
|
+
push:
|
|
4
|
+
branches:
|
|
5
|
+
- main
|
|
6
|
+
|
|
7
|
+
jobs:
|
|
8
|
+
build-test-publish:
|
|
9
|
+
runs-on: ubuntu-latest
|
|
10
|
+
steps:
|
|
11
|
+
- uses: actions/checkout@v4
|
|
12
|
+
- uses: actions/setup-node@v4.0.3
|
|
13
|
+
with:
|
|
14
|
+
node-version: 20.x
|
|
15
|
+
registry-url: "https://registry.npmjs.org/"
|
|
16
|
+
|
|
17
|
+
- uses: oven-sh/setup-bun@v2
|
|
18
|
+
|
|
19
|
+
- run: |
|
|
20
|
+
bun i
|
|
21
|
+
bun test
|
|
22
|
+
npm publish --access public
|
|
23
|
+
env:
|
|
24
|
+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
|
package/LICENSE
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2022 tkeron
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 tkeron
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/bun.lockb
ADDED
|
Binary file
|
package/changelog.md
CHANGED
package/package.json
CHANGED
|
@@ -1,22 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tkeron/commands",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "
|
|
5
|
-
"main": "
|
|
6
|
-
"
|
|
7
|
-
|
|
8
|
-
"test": "jest",
|
|
9
|
-
"lint": "prettier --write ."
|
|
10
|
-
},
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"description": "library for handling command line arguments",
|
|
5
|
+
"main": "src/index.ts",
|
|
6
|
+
"module": "src/index.ts",
|
|
7
|
+
"type": "module",
|
|
11
8
|
"author": "tkeron",
|
|
12
9
|
"license": "MIT",
|
|
13
10
|
"devDependencies": {
|
|
14
|
-
"@types/
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
"
|
|
18
|
-
"ts-jest": "^29.1.1",
|
|
19
|
-
"typescript": "^4.9.4"
|
|
11
|
+
"@types/bun": "latest"
|
|
12
|
+
},
|
|
13
|
+
"peerDependencies": {
|
|
14
|
+
"typescript": "^5.0.0"
|
|
20
15
|
},
|
|
21
16
|
"keywords": [
|
|
22
17
|
"cli",
|
|
@@ -25,6 +20,6 @@
|
|
|
25
20
|
"arguments"
|
|
26
21
|
],
|
|
27
22
|
"repository": {
|
|
28
|
-
"url": "git@github.com:tkeron/
|
|
23
|
+
"url": "git@github.com:tkeron/commands.git"
|
|
29
24
|
}
|
|
30
25
|
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { describe, expect, it, spyOn } from "bun:test";
|
|
2
|
+
|
|
3
|
+
describe("example e2e", () => {
|
|
4
|
+
const { log } = globalThis.console;
|
|
5
|
+
let logs: any[] = [];
|
|
6
|
+
spyOn(globalThis.console, "log").mockImplementation((...args: any) => {
|
|
7
|
+
logs.push(args);
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
it("should runs ok", (done) => {
|
|
11
|
+
expect(logs).toHaveLength(0);
|
|
12
|
+
//@ts-ignore
|
|
13
|
+
import("./example");
|
|
14
|
+
const check = () => {
|
|
15
|
+
// @ts-ignore
|
|
16
|
+
const { commands } = globalThis;
|
|
17
|
+
if (!commands) return;
|
|
18
|
+
expect(logs).toHaveLength(3);
|
|
19
|
+
expect(logs).toStrictEqual([
|
|
20
|
+
["argument 'asdasd' not defined"],
|
|
21
|
+
["arguments 'pos0value, pos1value, asdasd' not defined"],
|
|
22
|
+
[
|
|
23
|
+
{
|
|
24
|
+
opt1: "qw111erty",
|
|
25
|
+
opt2: "as222d",
|
|
26
|
+
pos0: "pos0value",
|
|
27
|
+
pos1: "pos1value",
|
|
28
|
+
pos3: "asdasd",
|
|
29
|
+
},
|
|
30
|
+
],
|
|
31
|
+
]);
|
|
32
|
+
clearInterval(handler);
|
|
33
|
+
done();
|
|
34
|
+
};
|
|
35
|
+
const handler = setInterval(check, 10);
|
|
36
|
+
});
|
|
37
|
+
});
|
package/src/example.ts
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
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;
|
|
@@ -0,0 +1,94 @@
|
|
|
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
|
+
});
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
import { getStart } from "./getStart";
|
|
2
|
+
import { buildHelpText, getCommandText } from "./textFuncs";
|
|
3
|
+
import type {
|
|
4
|
+
CommandFactory,
|
|
5
|
+
Command,
|
|
6
|
+
Commands,
|
|
7
|
+
CommandsCollection,
|
|
8
|
+
Callback,
|
|
9
|
+
} from "./types";
|
|
10
|
+
export * from "./types";
|
|
11
|
+
|
|
12
|
+
export const getCommands = (
|
|
13
|
+
programName: string = "program",
|
|
14
|
+
version: string = "0.0.1"
|
|
15
|
+
): Commands => {
|
|
16
|
+
const commandsCollection: CommandsCollection = {};
|
|
17
|
+
|
|
18
|
+
const commands = initCommands(commandsCollection);
|
|
19
|
+
commands.programName = programName;
|
|
20
|
+
commands.version = version;
|
|
21
|
+
|
|
22
|
+
initHelpAndVersion(commands, commandsCollection);
|
|
23
|
+
|
|
24
|
+
return commands;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export const initCommands = (
|
|
28
|
+
commandsCollection: CommandsCollection,
|
|
29
|
+
commands: Commands = <Commands>(<unknown>{
|
|
30
|
+
programName: "",
|
|
31
|
+
version: "",
|
|
32
|
+
footerText: "",
|
|
33
|
+
headerText: "",
|
|
34
|
+
addCommand: undefined,
|
|
35
|
+
start: undefined,
|
|
36
|
+
addHeaderText: undefined,
|
|
37
|
+
addFooterText: undefined,
|
|
38
|
+
}),
|
|
39
|
+
commandFactory: CommandFactory = <CommandFactory>(<unknown>{
|
|
40
|
+
commands: undefined,
|
|
41
|
+
name: undefined,
|
|
42
|
+
addAlias: undefined,
|
|
43
|
+
addOption: undefined,
|
|
44
|
+
addPositionedArgument: undefined,
|
|
45
|
+
addDescription: undefined,
|
|
46
|
+
setCallback: undefined,
|
|
47
|
+
})
|
|
48
|
+
): Commands => {
|
|
49
|
+
if (!commandFactory.commands) commandFactory.commands = () => commands;
|
|
50
|
+
commands.addCommand = getAddCommand(commandsCollection, commandFactory);
|
|
51
|
+
commands.start = getStart(commandsCollection);
|
|
52
|
+
commands.addHeaderText = getAddHeaderText(commands);
|
|
53
|
+
commands.addFooterText = getAddFooterText(commands);
|
|
54
|
+
|
|
55
|
+
return commands;
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
export const initHelpAndVersion = (
|
|
59
|
+
commands: Commands,
|
|
60
|
+
commandsCollection: CommandsCollection
|
|
61
|
+
) => {
|
|
62
|
+
const helpCallback = () =>
|
|
63
|
+
console["log"](buildHelpText(commands, commandsCollection));
|
|
64
|
+
const versionCallback = () =>
|
|
65
|
+
console["log"](
|
|
66
|
+
`${commands.headerText || ""}\n${commands.version}\n${
|
|
67
|
+
commands.footerText || ""
|
|
68
|
+
}\n`
|
|
69
|
+
);
|
|
70
|
+
|
|
71
|
+
commands
|
|
72
|
+
.addCommand("help")
|
|
73
|
+
.addAlias("-h")
|
|
74
|
+
.addAlias("--help")
|
|
75
|
+
.addDescription("show program help")
|
|
76
|
+
.setCallback(helpCallback);
|
|
77
|
+
commands
|
|
78
|
+
.addCommand("version")
|
|
79
|
+
.addAlias("-v")
|
|
80
|
+
.addAlias("--version")
|
|
81
|
+
.addDescription("show program version")
|
|
82
|
+
.setCallback(versionCallback);
|
|
83
|
+
|
|
84
|
+
return { helpCallback, versionCallback };
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
export const getAddCommand =
|
|
88
|
+
(commandsCollection: CommandsCollection, commandFactory: CommandFactory) =>
|
|
89
|
+
(commandName: string) => {
|
|
90
|
+
commandFactory.name = commandName;
|
|
91
|
+
const command: Command = <Command>(<unknown>{
|
|
92
|
+
aliases: [],
|
|
93
|
+
callback: undefined,
|
|
94
|
+
description: "",
|
|
95
|
+
name: commandName,
|
|
96
|
+
options: [],
|
|
97
|
+
optionsExamples: [],
|
|
98
|
+
positionedArguments: [],
|
|
99
|
+
getHelpLine: undefined,
|
|
100
|
+
});
|
|
101
|
+
commandsCollection[commandName] = command;
|
|
102
|
+
command.getHelpLine = getGetHelpLine(command);
|
|
103
|
+
|
|
104
|
+
commandFactory.addAlias = getAddAlias(commandFactory, commandsCollection);
|
|
105
|
+
commandFactory.addOption = getAddOption(commandFactory, commandsCollection);
|
|
106
|
+
commandFactory.addDescription = getAddDescription(
|
|
107
|
+
commandFactory,
|
|
108
|
+
commandsCollection
|
|
109
|
+
);
|
|
110
|
+
commandFactory.setCallback = getSetCallback(
|
|
111
|
+
commandFactory,
|
|
112
|
+
commandsCollection
|
|
113
|
+
);
|
|
114
|
+
commandFactory.addPositionedArgument = getAddPositionedArgument(
|
|
115
|
+
commandFactory,
|
|
116
|
+
commandsCollection
|
|
117
|
+
);
|
|
118
|
+
|
|
119
|
+
return commandFactory;
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
export const getAddAlias = (
|
|
123
|
+
commandFactory: CommandFactory,
|
|
124
|
+
commandsCollection: CommandsCollection
|
|
125
|
+
) => {
|
|
126
|
+
return (alias: string) => {
|
|
127
|
+
commandsCollection[commandFactory.name].aliases.push(alias);
|
|
128
|
+
commandsCollection[alias] = commandsCollection[commandFactory.name];
|
|
129
|
+
|
|
130
|
+
return commandFactory;
|
|
131
|
+
};
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
export const getAddOption =
|
|
135
|
+
(commandFactory: CommandFactory, commandsCollection: CommandsCollection) =>
|
|
136
|
+
(option: string, example?: string) => {
|
|
137
|
+
commandsCollection[commandFactory.name].options.push(option);
|
|
138
|
+
commandsCollection[commandFactory.name].optionsExamples.push(example || "");
|
|
139
|
+
|
|
140
|
+
return commandFactory;
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
export const getAddPositionedArgument = (
|
|
144
|
+
commandFactory: CommandFactory,
|
|
145
|
+
commandsCollection: CommandsCollection
|
|
146
|
+
) => {
|
|
147
|
+
return (arg: string): CommandFactory => {
|
|
148
|
+
const positionedArguments =
|
|
149
|
+
commandsCollection[commandFactory.name].positionedArguments;
|
|
150
|
+
|
|
151
|
+
if (!positionedArguments.includes(commandFactory.name)) {
|
|
152
|
+
positionedArguments.push(arg);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
return commandFactory;
|
|
156
|
+
};
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
export const getSetCallback = (
|
|
160
|
+
commandFactory: CommandFactory,
|
|
161
|
+
commandsCollection: CommandsCollection
|
|
162
|
+
) => {
|
|
163
|
+
return (fn: Callback): CommandFactory => {
|
|
164
|
+
commandsCollection[commandFactory.name].callback = fn;
|
|
165
|
+
|
|
166
|
+
return commandFactory;
|
|
167
|
+
};
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
export const getAddDescription = (
|
|
171
|
+
commandFactory: CommandFactory,
|
|
172
|
+
commandsCollection: CommandsCollection
|
|
173
|
+
) => {
|
|
174
|
+
return (description: string) => {
|
|
175
|
+
commandsCollection[commandFactory.name].description = description;
|
|
176
|
+
return commandFactory;
|
|
177
|
+
};
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
export const getAddHeaderText = (commands: Commands) => {
|
|
181
|
+
return (text: string) => {
|
|
182
|
+
commands.headerText = text;
|
|
183
|
+
return commands;
|
|
184
|
+
};
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
export const getAddFooterText = (commands: Commands) => {
|
|
188
|
+
return (text: string) => {
|
|
189
|
+
commands.footerText = text;
|
|
190
|
+
return commands;
|
|
191
|
+
};
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
export const getGetHelpLine =
|
|
195
|
+
(command: Command) =>
|
|
196
|
+
(width: number = 50) => {
|
|
197
|
+
let helpLine =
|
|
198
|
+
getCommandText(command).padEnd(width, ".") +
|
|
199
|
+
" " +
|
|
200
|
+
command.description +
|
|
201
|
+
"\n";
|
|
202
|
+
|
|
203
|
+
return helpLine;
|
|
204
|
+
};
|
|
@@ -0,0 +1,79 @@
|
|
|
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/getStart.ts
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import type { CommandsCollection } from "./types";
|
|
2
|
+
|
|
3
|
+
export const getStart =
|
|
4
|
+
(commandsCollection: CommandsCollection) => (argv?: string[]) => {
|
|
5
|
+
if (!argv) argv = process.argv;
|
|
6
|
+
if (!Array.isArray(argv)) throw new Error("no arguments passed");
|
|
7
|
+
if (argv.length < 2) throw Error("arguments out of range");
|
|
8
|
+
argv = argv.slice(2);
|
|
9
|
+
if (argv.length === 0) {
|
|
10
|
+
commandsCollection.help.callback();
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const commandName = argv[0];
|
|
15
|
+
|
|
16
|
+
const command = commandsCollection[commandName];
|
|
17
|
+
if (!command) {
|
|
18
|
+
console["log"](`command '${commandName}' not found`);
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const options = argv
|
|
23
|
+
.slice(1)
|
|
24
|
+
.filter((arg) => /\=/g.test(arg))
|
|
25
|
+
.map((arg) => arg.split("="))
|
|
26
|
+
.reduce((p: any, c) => {
|
|
27
|
+
p[c[0]] = c[1];
|
|
28
|
+
|
|
29
|
+
return p;
|
|
30
|
+
}, {});
|
|
31
|
+
|
|
32
|
+
const positionedArgsValues = argv
|
|
33
|
+
.slice(1)
|
|
34
|
+
.filter((arg) => !/\=/g.test(arg));
|
|
35
|
+
|
|
36
|
+
if (positionedArgsValues.length <= command.positionedArguments.length) {
|
|
37
|
+
positionedArgsValues.forEach(
|
|
38
|
+
(arg, n) => (options[command.positionedArguments[n]] = arg)
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (positionedArgsValues.length > command.positionedArguments.length) {
|
|
43
|
+
console["log"](
|
|
44
|
+
`argument${
|
|
45
|
+
positionedArgsValues.length - command.positionedArguments.length === 1
|
|
46
|
+
? ""
|
|
47
|
+
: "s"
|
|
48
|
+
} '${positionedArgsValues
|
|
49
|
+
.slice(command.positionedArguments.length)
|
|
50
|
+
.join(", ")}' not defined`
|
|
51
|
+
);
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
command.callback(options);
|
|
56
|
+
};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { Command, Commands, CommandsCollection } from "./types";
|
|
2
|
+
|
|
3
|
+
export const command: Command = <Command>(<unknown>{
|
|
4
|
+
name: "command_1",
|
|
5
|
+
aliases: ["al1", "al2"],
|
|
6
|
+
callback: undefined,
|
|
7
|
+
description: "command 1 description",
|
|
8
|
+
getHelpLine: () => "help line...\n",
|
|
9
|
+
options: ["op1", "op2"],
|
|
10
|
+
optionsExamples: ["opEx1"],
|
|
11
|
+
positionedArguments: ["pos1", "pos2"],
|
|
12
|
+
});
|
|
13
|
+
export const commands = <Commands>{
|
|
14
|
+
headerText: "header text...",
|
|
15
|
+
footerText: "footer text...",
|
|
16
|
+
};
|
|
17
|
+
export const commandsCollection: CommandsCollection = {
|
|
18
|
+
command_1: command,
|
|
19
|
+
al1: command,
|
|
20
|
+
al2: command,
|
|
21
|
+
command_2: {
|
|
22
|
+
...command,
|
|
23
|
+
name: "command_2",
|
|
24
|
+
description: "command 2 description",
|
|
25
|
+
aliases: [],
|
|
26
|
+
},
|
|
27
|
+
help: {
|
|
28
|
+
...command,
|
|
29
|
+
name: "help",
|
|
30
|
+
callback: () => console["log"]("help text..."),
|
|
31
|
+
},
|
|
32
|
+
};
|