@flowscripter/example-cli 1.0.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,23 @@
1
+ ## Executable Functional Tests
2
+
3
+ #### Setup
4
+
5
+ Ensure the executable is built:
6
+
7
+ bun build ../index.ts --compile --outfile /tmp/example-cli
8
+
9
+ Install requirements:
10
+
11
+ pip3 install -r pip-requirements.txt
12
+
13
+ #### Testing
14
+
15
+ Run the functional tests:
16
+
17
+ export EXECUTABLE=/tmp/example-cli
18
+ behave
19
+
20
+ To run with logging output from the test steps (this is the best set of
21
+ arguments I can find):
22
+
23
+ behave --no-logcapture --no-color --logging-level=DEBUG
@@ -0,0 +1,9 @@
1
+ import os
2
+
3
+ from support.pexpect_wrapper import PExpectWrapper
4
+
5
+
6
+ def before_all(context):
7
+
8
+ context.config.setup_logging()
9
+ context.pexpect_wrapper = PExpectWrapper(os.environ.get('EXECUTABLE'))
@@ -0,0 +1,6 @@
1
+ Feature: Executable
2
+
3
+ Scenario: Executable success
4
+ When the executable is launched
5
+ Then the executable should complete with exit code 2
6
+ And the executable should have output "No command specified"
@@ -0,0 +1,18 @@
1
+ from behave import when, then
2
+
3
+
4
+ @when('the executable is launched')
5
+ def step_impl(context):
6
+ context.pexpect_wrapper.start()
7
+
8
+
9
+ @then('the executable should complete with exit code {code:d}')
10
+ def step_impl(context, code):
11
+ context.pexpect_wrapper.expect_eof()
12
+ status = context.pexpect_wrapper.complete()
13
+ assert status == code, 'unexpected exit status: {}'.format(status)
14
+
15
+
16
+ @then('the executable should have output "{message}"')
17
+ def step_impl(context, message):
18
+ context.pexpect_wrapper.expect(message)
@@ -0,0 +1,45 @@
1
+ from pexpect.popen_spawn import PopenSpawn
2
+ from pexpect import EOF
3
+ import logging
4
+ log = logging.getLogger("pexpect_wrapper")
5
+
6
+
7
+ class PExpectWrapper:
8
+
9
+ def __init__(self, executable):
10
+ self.executable = executable
11
+ self.child = None
12
+ self.output = None
13
+
14
+ def start(self):
15
+ assert self.child is None
16
+
17
+ self.child = PopenSpawn(self.executable, encoding='utf-8')
18
+
19
+ def expect(self, message):
20
+ assert self.child is not None
21
+
22
+ found = ''
23
+ while len(self.output) > 0:
24
+ next_line = self.output.pop(0)
25
+ log.debug('looking for "{}" in "{}"'.format(message, next_line))
26
+ if message in next_line:
27
+ found = next_line
28
+ break
29
+
30
+ assert found != '', 'expected {} in output'.format(message)
31
+
32
+
33
+ def expect_eof(self):
34
+ assert self.child is not None
35
+
36
+ self.child.expect(EOF)
37
+
38
+ def complete(self):
39
+ assert self.child is not None
40
+
41
+ exit_status = self.child.wait()
42
+
43
+ self.output = self.child.before.split('\n')
44
+
45
+ return exit_status
@@ -0,0 +1,2 @@
1
+ pexpect
2
+ behave
package/index.ts ADDED
@@ -0,0 +1,3 @@
1
+ import { cli } from "./src/cli.ts";
2
+
3
+ await cli(import.meta.url);
package/package.json ADDED
@@ -0,0 +1,18 @@
1
+ {
2
+ "name": "@flowscripter/example-cli",
3
+ "module": "index.ts",
4
+ "type": "module",
5
+ "version": "1.0.0",
6
+ "publishConfig": {
7
+ "access": "public"
8
+ },
9
+ "devDependencies": {
10
+ "@types/bun": "^1.2.4"
11
+ },
12
+ "peerDependencies": {
13
+ "typescript": "^5.8.2"
14
+ },
15
+ "dependencies": {
16
+ "@flowscripter/dynamic-cli-framework": "^1.0.3"
17
+ }
18
+ }
@@ -0,0 +1,31 @@
1
+ # Set exit on error
2
+ $ErrorActionPreference = "Stop"
3
+
4
+ # URL of the ZIP file containing the executable
5
+ $zipUrl = "https://github.com/flowscripter/example-cli/releases/latest/download/example-cli_Windows_x86_64.zip"
6
+ $installDir = "$env:ProgramFiles\example-cli"
7
+
8
+ # Create a temporary directory to download the ZIP
9
+ $tempDir = [System.IO.Path]::Combine($env:TEMP, "flowscripter_install")
10
+ New-Item -ItemType Directory -Force -Path $tempDir
11
+
12
+ # Download the ZIP file
13
+ Write-Host "Downloading example-cli..."
14
+ Invoke-WebRequest -Uri $zipUrl -OutFile "$tempDir\executable.zip"
15
+
16
+ # Extract the ZIP file
17
+ Write-Host "Extracting the ZIP file..."
18
+ Expand-Archive -Path "$tempDir\executable.zip" -DestinationPath $tempDir -Force
19
+
20
+ # Install the executable
21
+ Write-Host "Installing the executable..."
22
+ Move-Item -Path "$tempDir\example-cli.exe" -Destination $installDir -Force
23
+
24
+ # Add the executable to the system PATH (for all users)
25
+ $env:Path += ";$installDir"
26
+ [Environment]::SetEnvironmentVariable("Path", $env:Path, [System.EnvironmentVariableTarget]::Machine)
27
+
28
+ # Clean up temporary files
29
+ Remove-Item -Recurse -Force $tempDir
30
+
31
+ Write-Host "✅ Installation complete! You can now run 'example-cli' from any command prompt."
@@ -0,0 +1,25 @@
1
+ #!/bin/sh
2
+
3
+ set -e # Exit on error
4
+
5
+ # Define the download URL
6
+ URL="https://github.com/flowscripter/example-cli/releases/latest/download/example-cli_Linux_x86_64.zip"
7
+
8
+ # Create a temporary directory
9
+ TMP_DIR=$(mktemp -d)
10
+ cd "$TMP_DIR"
11
+
12
+ # Download and extract
13
+ echo "Downloading example-cli..."
14
+ curl -fsSL "$URL" -o executable.zip
15
+ unzip executable.zip
16
+
17
+ # Install
18
+ chmod +x example-cli
19
+ sudo mv example-cli /usr/local/bin/
20
+
21
+ # Clean up
22
+ cd -
23
+ rm -rf "$TMP_DIR"
24
+
25
+ echo "✅ Installation complete! Run 'example-cli' to get started."
package/src/cli.ts ADDED
@@ -0,0 +1,30 @@
1
+ import {
2
+ AsciiBannerGeneratorServiceProvider,
3
+ BannerServiceProvider,
4
+ launchMultiCommandCLI,
5
+ SyntaxHighlighterServiceProvider,
6
+ } from "@flowscripter/dynamic-cli-framework";
7
+ import command1 from "./commands/command1.ts";
8
+ import command2 from "./commands/command2.ts";
9
+ import packageJson from "../package.json";
10
+
11
+ /**
12
+ * Run the CLI.
13
+ */
14
+ export async function cli(): Promise<void> {
15
+ // use launchMultiCommandCLI to show 2 sample commands
16
+ await launchMultiCommandCLI(
17
+ [command1, command2],
18
+ "Simple example CLI using dynamic-cli-framework.",
19
+ `example-cli`, // alternatively, name can be derived from executable name if this is not defined
20
+ packageJson.version,
21
+ false, // simple example, so disable env vars support for configuration defaults
22
+ false, // simple example, so disable configuration support
23
+ false, // simple example, so disable key-value service
24
+ [
25
+ new BannerServiceProvider(50), // renders an ascii banner on CLI launch
26
+ new AsciiBannerGeneratorServiceProvider(45), // exposes ascii banner generation service
27
+ new SyntaxHighlighterServiceProvider(40), // exposes syntax highlighting service
28
+ ],
29
+ );
30
+ }
@@ -0,0 +1,74 @@
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;
@@ -0,0 +1,137 @@
1
+ import {
2
+ type ArgumentValues,
3
+ ArgumentValueTypeName,
4
+ ComplexValueTypeName,
5
+ type Context,
6
+ PRINTER_SERVICE_ID,
7
+ type PrinterService,
8
+ type SubCommand,
9
+ SYNTAX_HIGHLIGHTER_SERVICE_ID,
10
+ type SyntaxHighlighterService,
11
+ } from "@flowscripter/dynamic-cli-framework";
12
+
13
+ const command2: SubCommand = {
14
+ name: "command2",
15
+ description: "Demonstrates argument features and syntax highlighter service",
16
+ options: [
17
+ {
18
+ name: "booleanOption",
19
+ description: "A boolean option",
20
+ type: ArgumentValueTypeName.BOOLEAN,
21
+ shortAlias: "b",
22
+ isOptional: true,
23
+ },
24
+ {
25
+ name: "numberOption",
26
+ description: "A number option",
27
+ type: ArgumentValueTypeName.NUMBER,
28
+ shortAlias: "n",
29
+ isOptional: true,
30
+ },
31
+ {
32
+ name: "stringOption",
33
+ description: "A string option",
34
+ type: ArgumentValueTypeName.STRING,
35
+ shortAlias: "s",
36
+ isOptional: true,
37
+ },
38
+ {
39
+ name: "complexOption",
40
+ description: "An optional complex option",
41
+ type: ComplexValueTypeName.COMPLEX,
42
+ shortAlias: "c",
43
+ isOptional: true,
44
+ properties: [
45
+ {
46
+ name: "numberSubOption",
47
+ description: "A number sub-option",
48
+ type: ArgumentValueTypeName.NUMBER,
49
+ shortAlias: "n",
50
+ isArray: true,
51
+ },
52
+ ],
53
+ },
54
+ ],
55
+ positionals: [
56
+ {
57
+ name: "booleanPositional",
58
+ description: "A boolean positional",
59
+ type: ArgumentValueTypeName.BOOLEAN,
60
+ },
61
+ {
62
+ name: "numberPositional",
63
+ description: "A number positional",
64
+ type: ArgumentValueTypeName.NUMBER,
65
+ },
66
+ {
67
+ name: "stringPositional",
68
+ description: "A string positional",
69
+ type: ArgumentValueTypeName.STRING,
70
+ },
71
+ ],
72
+ usageExamples: [
73
+ {
74
+ exampleArguments: "true 1 hello",
75
+ description: "Required positional arguments",
76
+ output: [
77
+ "{",
78
+ ' "booleanPositional": true,',
79
+ ' "numberPositional": 1,',
80
+ ' "stringPositional": "hello"',
81
+ "}",
82
+ ],
83
+ },
84
+ {
85
+ exampleArguments: "true 1 --booleanOption hello",
86
+ description: "Required positional arguments and optional boolean option",
87
+ output: [
88
+ "{",
89
+ ' "booleanPositional": true,',
90
+ ' "numberPositional": 1,',
91
+ ' "booleanOption": true,',
92
+ ' "stringPositional": "hello"',
93
+ "}",
94
+ ],
95
+ },
96
+ {
97
+ exampleArguments: 'true 1 hello -b -n 2 -s "hello world" -c.n=1 -c.n=2',
98
+ description: "Complex arguments with short aliases",
99
+ output: [
100
+ "{",
101
+ ' "booleanPositional": true,',
102
+ ' "numberPositional": 1,',
103
+ ' "stringPositional": "hello"',
104
+ ' "booleanOption": true,',
105
+ ' "numberOption": 2,',
106
+ ' "stringOption": "hello world",',
107
+ ' "complexOption": {',
108
+ ' "numberSubOption": [',
109
+ " 1,",
110
+ " 2",
111
+ " ]",
112
+ " }",
113
+ "}",
114
+ ],
115
+ },
116
+ ],
117
+ async execute(
118
+ context: Context,
119
+ argumentValues: ArgumentValues,
120
+ ): Promise<void> {
121
+ const printerService = context.getServiceById(
122
+ PRINTER_SERVICE_ID,
123
+ ) as PrinterService;
124
+ const highlighterService = context.getServiceById(
125
+ SYNTAX_HIGHLIGHTER_SERVICE_ID,
126
+ ) as SyntaxHighlighterService;
127
+
128
+ await printerService.print(
129
+ highlighterService.highlight(
130
+ JSON.stringify(argumentValues, null, 2),
131
+ "json",
132
+ ) + "\n",
133
+ );
134
+ },
135
+ };
136
+
137
+ export default command2;
@@ -0,0 +1,16 @@
1
+ import process from "node:process";
2
+ import { describe, expect, spyOn, test } from "bun:test";
3
+ import { cli } from "../src/cli.ts";
4
+
5
+ describe("ExampleCli Tests", () => {
6
+ test("CLI Test", async () => {
7
+ const mockExit = spyOn(process, "exit").mockImplementation(() => {
8
+ throw new Error("Mock exit");
9
+ });
10
+
11
+ await expect(cli()).rejects.toThrow("Mock exit");
12
+
13
+ expect(mockExit).toHaveBeenCalledWith(2);
14
+ mockExit.mockRestore();
15
+ });
16
+ });
package/tsconfig.json ADDED
@@ -0,0 +1,27 @@
1
+ {
2
+ "compilerOptions": {
3
+ // Enable latest features
4
+ "lib": ["ESNext", "DOM"],
5
+ "target": "ESNext",
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
+ }
27
+ }