@flowscripter/example-cli 1.0.9 → 1.2.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.
@@ -0,0 +1,189 @@
1
+ import {
2
+ Align,
3
+ type ArgumentValues,
4
+ ASCII_BANNER_GENERATOR_SERVICE_ID,
5
+ type AsciiBannerGeneratorService,
6
+ type Context,
7
+ DATA_DUMP_GENERATOR_SERVICE_ID,
8
+ type DataDumpGeneratorService,
9
+ DumpFormat,
10
+ Icon,
11
+ PRINTER_SERVICE_ID,
12
+ type PrinterService,
13
+ ProgressStyle,
14
+ SpinnerStyle,
15
+ type SubCommand,
16
+ Table,
17
+ TABLE_GENERATOR_SERVICE_ID,
18
+ type TableGeneratorService,
19
+ TREE_PRINTER_SERVICE_ID,
20
+ type TreeNode,
21
+ type TreePrinterService,
22
+ } from "@flowscripter/dynamic-cli-framework";
23
+ import { Buffer } from "node:buffer";
24
+
25
+ async function sleep(seconds: number): Promise<void> {
26
+ await new Promise((resolve) => setTimeout(resolve, seconds * 1000));
27
+ }
28
+
29
+ const printing: SubCommand = {
30
+ name: "printing",
31
+ description: "Demonstrates printer, tree, table, data dump, and banner output features",
32
+ options: [],
33
+ positionals: [],
34
+ execute: async (context: Context, _argumentValues: ArgumentValues): Promise<void> => {
35
+ const printerService = context.getServiceById(PRINTER_SERVICE_ID) as PrinterService;
36
+ const asciiBannerGenerator = context.getServiceById(
37
+ ASCII_BANNER_GENERATOR_SERVICE_ID,
38
+ ) as AsciiBannerGeneratorService;
39
+ const treePrinterService = context.getServiceById(
40
+ TREE_PRINTER_SERVICE_ID,
41
+ ) as TreePrinterService;
42
+ const tableGeneratorService = context.getServiceById(
43
+ TABLE_GENERATOR_SERVICE_ID,
44
+ ) as TableGeneratorService;
45
+ const dataDumpGeneratorService = context.getServiceById(
46
+ DATA_DUMP_GENERATOR_SERVICE_ID,
47
+ ) as DataDumpGeneratorService;
48
+
49
+ // --- Basic Printer ---
50
+ await printerService.print("--- Basic Printer ---\n");
51
+ await printerService.print("Hello, World!\n");
52
+ await printerService.info("This is an info level message\n");
53
+ await printerService.debug("This is a debug level message\n");
54
+ await printerService.warn("This is a warn level message\n");
55
+ await printerService.error("This is an error level message\n");
56
+
57
+ // --- Default Spinner + Progress Bar ---
58
+ await printerService.print("--- Default Spinner + Progress Bar ---\n");
59
+
60
+ await printerService.showSpinner("Waiting 3 seconds...");
61
+ await sleep(1);
62
+ await printerService.showSpinner("Waiting 2 seconds...");
63
+ await sleep(1);
64
+ await printerService.showSpinner("Waiting 1 seconds...");
65
+ await sleep(1);
66
+ await printerService.hideSpinner();
67
+ await printerService.print("Finished waiting\n", Icon.SUCCESS);
68
+
69
+ await sleep(0.5);
70
+
71
+ const handle1 = await printerService.showProgressBar("sec", "Waiting 3 seconds", 3, 0);
72
+ await sleep(1);
73
+ printerService.updateProgressBar(handle1, 1);
74
+ await sleep(1);
75
+ printerService.updateProgressBar(handle1, 2);
76
+ await sleep(1);
77
+ printerService.updateProgressBar(handle1, 3);
78
+ await printerService.print("Finished waiting\n", Icon.SUCCESS);
79
+
80
+ // --- Star Spinner + Fill Progress Bar ---
81
+ await printerService.print("--- Star Spinner + Fill Progress Bar ---\n");
82
+
83
+ await printerService.showSpinner("Waiting 3 seconds...", SpinnerStyle.STAR);
84
+ await sleep(1);
85
+ await printerService.showSpinner("Waiting 2 seconds...", SpinnerStyle.STAR);
86
+ await sleep(1);
87
+ await printerService.showSpinner("Waiting 1 seconds...", SpinnerStyle.STAR);
88
+ await sleep(1);
89
+ await printerService.hideSpinner();
90
+ await printerService.print("Finished waiting\n", Icon.SUCCESS);
91
+
92
+ await sleep(0.5);
93
+
94
+ const handle2 = await printerService.showProgressBar(
95
+ "sec",
96
+ "Waiting 3 seconds",
97
+ 3,
98
+ 0,
99
+ ProgressStyle.FILL,
100
+ );
101
+ await sleep(1);
102
+ printerService.updateProgressBar(handle2, 1);
103
+ await sleep(1);
104
+ printerService.updateProgressBar(handle2, 2);
105
+ await sleep(1);
106
+ printerService.updateProgressBar(handle2, 3);
107
+ await printerService.print("Finished waiting\n", Icon.SUCCESS);
108
+
109
+ // --- Colors ---
110
+ await printerService.print("--- Colors ---\n");
111
+ await printerService.print(printerService.green("Green text") + "\n");
112
+ await printerService.print(printerService.backgroundRed("Background Red") + "\n");
113
+ await printerService.print(printerService.backgroundBlue("Background Blue") + "\n");
114
+ await printerService.print(printerService.backgroundGreen("Background Green") + "\n");
115
+ await printerService.print(printerService.backgroundCyan("Background Cyan") + "\n");
116
+ await printerService.print(
117
+ printerService.backgroundColor("Custom Hex Background", "#FF6600") + "\n",
118
+ );
119
+
120
+ // --- Hyperlink ---
121
+ await printerService.print("--- Hyperlink ---\n");
122
+ await printerService.print(
123
+ printerService.hyperlink("Flowscripter on GitHub", "https://github.com/flowscripter") + "\n",
124
+ );
125
+
126
+ // --- ASCII Banner ---
127
+ await printerService.print("--- ASCII Banner ---\n");
128
+ await printerService.print(
129
+ printerService.green(
130
+ await asciiBannerGenerator.generate("Goodbye!", {
131
+ fontName: "standard",
132
+ }),
133
+ ) + "\n",
134
+ );
135
+
136
+ // --- Tree ---
137
+ await printerService.print("--- Tree ---\n");
138
+ const tree: TreeNode = {
139
+ label: "Project",
140
+ children: [
141
+ {
142
+ label: "src/",
143
+ children: [
144
+ "cli.ts",
145
+ {
146
+ label: "commands/",
147
+ children: ["printing.ts", "prompting.ts"],
148
+ },
149
+ ],
150
+ },
151
+ {
152
+ label: "tests/",
153
+ children: ["cli_test.ts"],
154
+ },
155
+ "package.json",
156
+ ],
157
+ };
158
+ await printerService.print(treePrinterService.print(tree) + "\n");
159
+
160
+ // --- Table ---
161
+ await printerService.print("--- Table ---\n");
162
+ const table = new Table(4, 3, { border: true })
163
+ .column(0, { align: Align.LEFT })
164
+ .column(2, { align: Align.LEFT })
165
+ .cell(0, 0, "Service")
166
+ .cell(0, 1, "ID")
167
+ .cell(0, 2, "Description")
168
+ .cell(1, 0, "Printer")
169
+ .cell(1, 1, "printer-service")
170
+ .cell(1, 2, "Output formatting")
171
+ .cell(2, 0, "Tree")
172
+ .cell(2, 1, "tree-printer-service")
173
+ .cell(2, 2, "Tree rendering")
174
+ .cell(3, 0, "Table")
175
+ .cell(3, 1, "table-generator-service")
176
+ .cell(3, 2, "Table rendering");
177
+ await printerService.print(tableGeneratorService.render(table) + "\n");
178
+
179
+ // --- Data Dump ---
180
+ await printerService.print("--- Data Dump ---\n");
181
+ const data = Buffer.from("Hello, dynamic-cli-framework!");
182
+ const hexDump = dataDumpGeneratorService.generate(data, {
183
+ format: DumpFormat.HEX,
184
+ });
185
+ await printerService.print(hexDump + "\n");
186
+ },
187
+ };
188
+
189
+ export default printing;
@@ -0,0 +1,112 @@
1
+ import {
2
+ type ArgumentValues,
3
+ ArgumentValueTypeName,
4
+ type Context,
5
+ PRINTER_SERVICE_ID,
6
+ type PrinterService,
7
+ type Prompt,
8
+ PROMPTER_SERVICE_ID,
9
+ type PrompterService,
10
+ PromptType,
11
+ type SubCommand,
12
+ } from "@flowscripter/dynamic-cli-framework";
13
+
14
+ const prompting: SubCommand = {
15
+ name: "prompting",
16
+ description:
17
+ "Demonstrates prompts, argument validation, argument value prompting, and URL opening",
18
+ options: [
19
+ {
20
+ name: "age",
21
+ description: "Your age",
22
+ type: ArgumentValueTypeName.NUMBER,
23
+ shortAlias: "a",
24
+ isOptional: true,
25
+ validate: (value): string | undefined => {
26
+ const num = value as number;
27
+ if (num < 0 || num > 150) {
28
+ return "Age must be between 0 and 150";
29
+ }
30
+ return undefined;
31
+ },
32
+ },
33
+ ],
34
+ positionals: [
35
+ {
36
+ name: "name",
37
+ description: "Your name (must be at least 2 characters)",
38
+ type: ArgumentValueTypeName.STRING,
39
+ validate: (value): string | undefined => {
40
+ const str = value as string;
41
+ if (str.length < 2) {
42
+ return "Name must be at least 2 characters";
43
+ }
44
+ return undefined;
45
+ },
46
+ },
47
+ ],
48
+ async execute(context: Context, argumentValues: ArgumentValues): Promise<void> {
49
+ const printerService = context.getServiceById(PRINTER_SERVICE_ID) as PrinterService;
50
+ const prompterService = context.getServiceById(PROMPTER_SERVICE_ID) as PrompterService;
51
+
52
+ await printerService.print(
53
+ "Argument values: " + JSON.stringify(argumentValues, null, 2) + "\n",
54
+ );
55
+
56
+ const colorPrompt: Prompt = {
57
+ name: "favoriteColor",
58
+ promptText: "What is your favorite color?",
59
+ type: PromptType.TEXT,
60
+ defaultOption: {
61
+ displayValue: "blue",
62
+ returnedValue: "blue",
63
+ },
64
+ options: [],
65
+ };
66
+
67
+ const languagePrompt: Prompt = {
68
+ name: "programmingLanguage",
69
+ promptText: "Choose a programming language:",
70
+ type: PromptType.SINGLE_SELECT,
71
+ options: [
72
+ { displayValue: "TypeScript", returnedValue: "TypeScript" },
73
+ { displayValue: "Python", returnedValue: "Python" },
74
+ { displayValue: "Rust", returnedValue: "Rust" },
75
+ { displayValue: "Go", returnedValue: "Go" },
76
+ ],
77
+ };
78
+
79
+ const notificationsPrompt: Prompt = {
80
+ name: "enableNotifications",
81
+ promptText: "Enable notifications?",
82
+ type: PromptType.TOGGLE,
83
+ options: [],
84
+ };
85
+
86
+ const openUrlPrompt: Prompt = {
87
+ name: "openProject",
88
+ promptText: "Open the project page?",
89
+ type: PromptType.OPEN_URL,
90
+ options: [
91
+ {
92
+ displayValue: "Flowscripter GitHub",
93
+ returnedValue: "https://github.com/flowscripter",
94
+ },
95
+ ],
96
+ };
97
+
98
+ const results = await prompterService.promptAll([
99
+ colorPrompt,
100
+ languagePrompt,
101
+ notificationsPrompt,
102
+ openUrlPrompt,
103
+ ]);
104
+
105
+ await printerService.print("Prompt results:\n");
106
+ for (const result of results) {
107
+ await printerService.print(` ${result.name}: ${JSON.stringify(result.value)}\n`);
108
+ }
109
+ },
110
+ };
111
+
112
+ export default prompting;
package/tests/cli_test.ts CHANGED
@@ -10,7 +10,6 @@ describe("ExampleCli Tests", () => {
10
10
 
11
11
  expect(cli()).rejects.toThrow("Mock exit");
12
12
 
13
- expect(mockExit).toHaveBeenCalledWith(2);
14
13
  mockExit.mockRestore();
15
14
  });
16
15
  });
@@ -1,74 +0,0 @@
1
- import {
2
- type ArgumentValues,
3
- ASCII_BANNER_GENERATOR_SERVICE_ID,
4
- type AsciiBannerGeneratorService,
5
- type Context,
6
- Icon,
7
- PRINTER_SERVICE_ID,
8
- type PrinterService,
9
- type SubCommand,
10
- } from "@flowscripter/dynamic-cli-framework";
11
-
12
- async function sleep(seconds: number): Promise<void> {
13
- await new Promise((resolve) => setTimeout(resolve, seconds * 1000));
14
- }
15
-
16
- const command1: SubCommand = {
17
- name: "command1",
18
- description: "Demonstrates printer service features",
19
- options: [],
20
- positionals: [],
21
- execute: async (
22
- context: Context,
23
- _argumentValues: ArgumentValues,
24
- ): Promise<void> => {
25
- const printerService = context.getServiceById(
26
- PRINTER_SERVICE_ID,
27
- ) as PrinterService;
28
- const asciiBannerGenerator = context.getServiceById(
29
- ASCII_BANNER_GENERATOR_SERVICE_ID,
30
- ) as AsciiBannerGeneratorService;
31
-
32
- await printerService.print("Hello, World!\n");
33
-
34
- await printerService.info("This is an info level message\n");
35
- await printerService.debug("This is a debug level message\n");
36
- await printerService.warn("This is a warn level message\n");
37
- await printerService.error("This is an error level message\n");
38
-
39
- await printerService.showSpinner("Waiting 3 seconds...");
40
- await sleep(1);
41
- await printerService.showSpinner("Waiting 2 seconds...");
42
- await sleep(1);
43
- await printerService.showSpinner("Waiting 1 seconds...");
44
- await sleep(1);
45
- await printerService.hideSpinner();
46
-
47
- await printerService.print("Finished waiting\n", Icon.SUCCESS);
48
-
49
- await sleep(0.5);
50
-
51
- const handle1 = await printerService.showProgressBar(
52
- "sec",
53
- "Waiting 3 seconds",
54
- 3,
55
- 0,
56
- );
57
- await sleep(1);
58
- printerService.updateProgressBar(handle1, 1);
59
- await sleep(1);
60
- printerService.updateProgressBar(handle1, 2);
61
- await sleep(1);
62
- printerService.updateProgressBar(handle1, 3);
63
-
64
- await printerService.print("Finished waiting\n", Icon.SUCCESS);
65
-
66
- await printerService.print(
67
- printerService.green(
68
- await asciiBannerGenerator.generate("Goodbye!", "standard"),
69
- ) + "\n",
70
- );
71
- },
72
- };
73
-
74
- export default command1;