@flowscripter/example-cli 1.0.8 → 1.1.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/release-bun-executable.yml +1 -0
- package/assets/logo.png +0 -0
- package/bun.lock +14 -11
- package/demo.svg +371 -1
- package/functional_tests/README.md +1 -1
- package/functional_tests/features/executable.feature +37 -1
- package/functional_tests/features/steps/executable.py +7 -2
- package/functional_tests/features/support/pexpect_wrapper.py +28 -8
- package/package.json +4 -4
- package/src/cli.ts +18 -14
- package/src/commands/{command2.ts → arg-parsing.ts} +5 -4
- package/src/commands/banners.ts +47 -0
- package/src/commands/completion.ts +39 -0
- package/src/commands/configuration.ts +74 -0
- package/src/commands/demo.ts +30 -0
- package/src/commands/image.ts +45 -0
- package/src/commands/interruption.ts +65 -0
- package/src/commands/printing.ts +213 -0
- package/src/commands/prompting.ts +121 -0
- package/tests/cli_test.ts +0 -1
- package/src/commands/command1.ts +0 -74
|
@@ -1,6 +1,42 @@
|
|
|
1
1
|
Feature: Executable
|
|
2
2
|
|
|
3
|
-
Scenario:
|
|
3
|
+
Scenario: No command specified
|
|
4
4
|
When the executable is launched
|
|
5
5
|
Then the executable should complete with exit code 2
|
|
6
6
|
And the executable should have output "No command specified"
|
|
7
|
+
|
|
8
|
+
Scenario: Printing demo
|
|
9
|
+
When the executable is launched with "demo:printing"
|
|
10
|
+
Then the executable should complete with exit code 0
|
|
11
|
+
And the executable should have output "Hello, World!"
|
|
12
|
+
And the executable should have output "Finished waiting"
|
|
13
|
+
And the executable should have output "Background Red"
|
|
14
|
+
And the executable should have output "Background Blue"
|
|
15
|
+
And the executable should have output "Flowscripter on GitHub"
|
|
16
|
+
And the executable should have output "--- Tree ---"
|
|
17
|
+
And the executable should have output "--- Table ---"
|
|
18
|
+
And the executable should have output "--- Data Dump ---"
|
|
19
|
+
|
|
20
|
+
Scenario: Argument parsing demo
|
|
21
|
+
When the executable is launched with "demo:arg-parsing true 1 hello"
|
|
22
|
+
Then the executable should complete with exit code 0
|
|
23
|
+
And the executable should have output "booleanPositional"
|
|
24
|
+
And the executable should have output "numberPositional"
|
|
25
|
+
And the executable should have output "stringPositional"
|
|
26
|
+
|
|
27
|
+
Scenario: Banners demo
|
|
28
|
+
When the executable is launched with "demo:banners"
|
|
29
|
+
Then the executable should complete with exit code 0
|
|
30
|
+
And the executable should have output "Chisel Font ASCII Banner Demo"
|
|
31
|
+
And the executable should have output "ASCII Banner Subtitle Demo"
|
|
32
|
+
|
|
33
|
+
Scenario: Completion demo
|
|
34
|
+
When the executable is launched with "demo:completion"
|
|
35
|
+
Then the executable should complete with exit code 0
|
|
36
|
+
And the executable should have output "Bash bootstrap script"
|
|
37
|
+
|
|
38
|
+
Scenario: Configuration demo
|
|
39
|
+
When the executable is launched with "demo:configuration"
|
|
40
|
+
Then the executable should complete with exit code 0
|
|
41
|
+
And the executable should have output "Key-Value Storage"
|
|
42
|
+
And the executable should have output "Secret Storage"
|
|
@@ -3,14 +3,19 @@ from behave import when, then
|
|
|
3
3
|
|
|
4
4
|
@when('the executable is launched')
|
|
5
5
|
def step_impl(context):
|
|
6
|
-
context.pexpect_wrapper.start()
|
|
6
|
+
context.pexpect_wrapper.start('--no-prompt')
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
@when('the executable is launched with "{args}"')
|
|
10
|
+
def step_impl(context, args):
|
|
11
|
+
context.pexpect_wrapper.start('--no-prompt ' + args)
|
|
7
12
|
|
|
8
13
|
|
|
9
14
|
@then('the executable should complete with exit code {code:d}')
|
|
10
15
|
def step_impl(context, code):
|
|
11
16
|
context.pexpect_wrapper.expect_eof()
|
|
12
17
|
status = context.pexpect_wrapper.complete()
|
|
13
|
-
assert status == code, 'unexpected exit status: {}'.format(status)
|
|
18
|
+
assert status == code, 'unexpected exit status: {} (expected {})'.format(status, code)
|
|
14
19
|
|
|
15
20
|
|
|
16
21
|
@then('the executable should have output "{message}"')
|
|
@@ -1,8 +1,14 @@
|
|
|
1
|
-
from pexpect.popen_spawn import PopenSpawn
|
|
2
|
-
from pexpect import EOF
|
|
3
1
|
import logging
|
|
2
|
+
import platform
|
|
3
|
+
import pexpect
|
|
4
|
+
|
|
4
5
|
log = logging.getLogger("pexpect_wrapper")
|
|
5
6
|
|
|
7
|
+
IS_WINDOWS = platform.system() == 'Windows'
|
|
8
|
+
|
|
9
|
+
if IS_WINDOWS:
|
|
10
|
+
from pexpect.popen_spawn import PopenSpawn
|
|
11
|
+
|
|
6
12
|
|
|
7
13
|
class PExpectWrapper:
|
|
8
14
|
|
|
@@ -11,10 +17,16 @@ class PExpectWrapper:
|
|
|
11
17
|
self.child = None
|
|
12
18
|
self.output = None
|
|
13
19
|
|
|
14
|
-
def start(self):
|
|
20
|
+
def start(self, args=''):
|
|
15
21
|
assert self.child is None
|
|
16
22
|
|
|
17
|
-
|
|
23
|
+
cmd = self.executable
|
|
24
|
+
if args:
|
|
25
|
+
cmd = cmd + ' ' + args
|
|
26
|
+
if IS_WINDOWS:
|
|
27
|
+
self.child = PopenSpawn(cmd, encoding='utf-8', timeout=30)
|
|
28
|
+
else:
|
|
29
|
+
self.child = pexpect.spawn(cmd, encoding='utf-8', timeout=30)
|
|
18
30
|
|
|
19
31
|
def expect(self, message):
|
|
20
32
|
assert self.child is not None
|
|
@@ -29,17 +41,25 @@ class PExpectWrapper:
|
|
|
29
41
|
|
|
30
42
|
assert found != '', 'expected {} in output'.format(message)
|
|
31
43
|
|
|
32
|
-
|
|
33
44
|
def expect_eof(self):
|
|
34
45
|
assert self.child is not None
|
|
35
46
|
|
|
36
|
-
self.child.expect(EOF)
|
|
47
|
+
self.child.expect(pexpect.EOF)
|
|
37
48
|
|
|
38
49
|
def complete(self):
|
|
39
50
|
assert self.child is not None
|
|
40
51
|
|
|
41
|
-
|
|
52
|
+
if IS_WINDOWS:
|
|
53
|
+
self.child.wait()
|
|
54
|
+
exit_status = self.child.exitstatus
|
|
55
|
+
if exit_status is None:
|
|
56
|
+
exit_status = -1
|
|
57
|
+
else:
|
|
58
|
+
self.child.close()
|
|
59
|
+
exit_status = self.child.exitstatus
|
|
60
|
+
if exit_status is None:
|
|
61
|
+
exit_status = self.child.signalstatus or -1
|
|
42
62
|
|
|
43
|
-
self.output = self.child.before.split('\n')
|
|
63
|
+
self.output = self.child.before.split('\n') if self.child.before else []
|
|
44
64
|
|
|
45
65
|
return exit_status
|
package/package.json
CHANGED
|
@@ -19,17 +19,17 @@
|
|
|
19
19
|
],
|
|
20
20
|
"module": "index.ts",
|
|
21
21
|
"type": "module",
|
|
22
|
-
"version": "1.0
|
|
22
|
+
"version": "1.1.0",
|
|
23
23
|
"publishConfig": {
|
|
24
24
|
"access": "public"
|
|
25
25
|
},
|
|
26
26
|
"devDependencies": {
|
|
27
|
-
"@types/bun": "^1.
|
|
27
|
+
"@types/bun": "^1.3.14"
|
|
28
28
|
},
|
|
29
29
|
"peerDependencies": {
|
|
30
|
-
"typescript": "^
|
|
30
|
+
"typescript": "^6.0.3"
|
|
31
31
|
},
|
|
32
32
|
"dependencies": {
|
|
33
|
-
"@flowscripter/dynamic-cli-framework": "^1.
|
|
33
|
+
"@flowscripter/dynamic-cli-framework": "^1.2.0"
|
|
34
34
|
}
|
|
35
35
|
}
|
package/src/cli.ts
CHANGED
|
@@ -1,30 +1,34 @@
|
|
|
1
1
|
import {
|
|
2
2
|
AsciiBannerGeneratorServiceProvider,
|
|
3
3
|
BannerServiceProvider,
|
|
4
|
+
DataDumpGeneratorServiceProvider,
|
|
4
5
|
launchMultiCommandCLI,
|
|
5
6
|
SyntaxHighlighterServiceProvider,
|
|
7
|
+
TreePrinterServiceProvider,
|
|
6
8
|
} from "@flowscripter/dynamic-cli-framework";
|
|
7
|
-
import
|
|
8
|
-
import command2 from "./commands/command2.ts";
|
|
9
|
+
import demo from "./commands/demo.ts";
|
|
9
10
|
import packageJson from "../package.json";
|
|
10
11
|
|
|
11
|
-
/**
|
|
12
|
-
* Run the CLI.
|
|
13
|
-
*/
|
|
14
12
|
export async function cli(): Promise<void> {
|
|
15
|
-
// use launchMultiCommandCLI to show 2 sample commands
|
|
16
13
|
await launchMultiCommandCLI(
|
|
17
|
-
[
|
|
14
|
+
[demo],
|
|
18
15
|
"Simple example CLI using dynamic-cli-framework.",
|
|
19
|
-
"example-cli",
|
|
16
|
+
"example-cli",
|
|
20
17
|
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
18
|
[
|
|
25
|
-
new BannerServiceProvider(50),
|
|
26
|
-
new AsciiBannerGeneratorServiceProvider(45),
|
|
27
|
-
new SyntaxHighlighterServiceProvider(40),
|
|
19
|
+
new BannerServiceProvider(50),
|
|
20
|
+
new AsciiBannerGeneratorServiceProvider(45),
|
|
21
|
+
new SyntaxHighlighterServiceProvider(40),
|
|
22
|
+
new TreePrinterServiceProvider(35),
|
|
23
|
+
new DataDumpGeneratorServiceProvider(30),
|
|
28
24
|
],
|
|
25
|
+
{
|
|
26
|
+
configFileSupportEnabled: true,
|
|
27
|
+
keyValueServiceEnabled: true,
|
|
28
|
+
secretServiceEnabled: true,
|
|
29
|
+
argumentPrompterServiceEnabled: true,
|
|
30
|
+
completionServiceEnabled: true,
|
|
31
|
+
imagePrinterServiceEnabled: true,
|
|
32
|
+
},
|
|
29
33
|
);
|
|
30
34
|
}
|
|
@@ -10,9 +10,10 @@ import {
|
|
|
10
10
|
type SyntaxHighlighterService,
|
|
11
11
|
} from "@flowscripter/dynamic-cli-framework";
|
|
12
12
|
|
|
13
|
-
const
|
|
14
|
-
name: "
|
|
15
|
-
description:
|
|
13
|
+
const argParsing: SubCommand = {
|
|
14
|
+
name: "arg-parsing",
|
|
15
|
+
description:
|
|
16
|
+
"Demonstrates argument parsing, types, validation, and syntax-highlighted output",
|
|
16
17
|
options: [
|
|
17
18
|
{
|
|
18
19
|
name: "booleanOption",
|
|
@@ -134,4 +135,4 @@ const command2: SubCommand = {
|
|
|
134
135
|
},
|
|
135
136
|
};
|
|
136
137
|
|
|
137
|
-
export default
|
|
138
|
+
export default argParsing;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type ArgumentValues,
|
|
3
|
+
ASCII_BANNER_GENERATOR_SERVICE_ID,
|
|
4
|
+
type AsciiBannerGeneratorService,
|
|
5
|
+
ChiselFontAsciiBannerGeneratorService,
|
|
6
|
+
type Context,
|
|
7
|
+
PRINTER_SERVICE_ID,
|
|
8
|
+
type PrinterService,
|
|
9
|
+
type SubCommand,
|
|
10
|
+
} from "@flowscripter/dynamic-cli-framework";
|
|
11
|
+
|
|
12
|
+
const banners: SubCommand = {
|
|
13
|
+
name: "banners",
|
|
14
|
+
description:
|
|
15
|
+
"Demonstrates chisel font ASCII banner and ASCII banner subtitle",
|
|
16
|
+
options: [],
|
|
17
|
+
positionals: [],
|
|
18
|
+
execute: async (
|
|
19
|
+
context: Context,
|
|
20
|
+
_argumentValues: ArgumentValues,
|
|
21
|
+
): Promise<void> => {
|
|
22
|
+
const printerService = context.getServiceById(
|
|
23
|
+
PRINTER_SERVICE_ID,
|
|
24
|
+
) as PrinterService;
|
|
25
|
+
const asciiBannerService = context.getServiceById(
|
|
26
|
+
ASCII_BANNER_GENERATOR_SERVICE_ID,
|
|
27
|
+
) as AsciiBannerGeneratorService;
|
|
28
|
+
|
|
29
|
+
// --- Chisel Font ASCII Banner Demo ---
|
|
30
|
+
await printerService.print("--- Chisel Font ASCII Banner Demo ---\n");
|
|
31
|
+
|
|
32
|
+
const chiselBanner = new ChiselFontAsciiBannerGeneratorService();
|
|
33
|
+
const chiselResult = await chiselBanner.generate("CHISEL");
|
|
34
|
+
await printerService.print(chiselResult + "\n");
|
|
35
|
+
|
|
36
|
+
// --- ASCII Banner Subtitle Demo ---
|
|
37
|
+
await printerService.print("--- ASCII Banner Subtitle Demo ---\n");
|
|
38
|
+
|
|
39
|
+
const subtitleResult = await asciiBannerService.generate("Hello", {
|
|
40
|
+
fontName: "standard",
|
|
41
|
+
subMessage: "A subtitle message",
|
|
42
|
+
});
|
|
43
|
+
await printerService.print(subtitleResult + "\n");
|
|
44
|
+
},
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
export default banners;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type ArgumentValues,
|
|
3
|
+
COMPLETION_SERVICE_ID,
|
|
4
|
+
type CompletionService,
|
|
5
|
+
type Context,
|
|
6
|
+
PRINTER_SERVICE_ID,
|
|
7
|
+
type PrinterService,
|
|
8
|
+
ShellType,
|
|
9
|
+
type SubCommand,
|
|
10
|
+
} from "@flowscripter/dynamic-cli-framework";
|
|
11
|
+
|
|
12
|
+
const completion: SubCommand = {
|
|
13
|
+
name: "completion",
|
|
14
|
+
description: "Demonstrates shell completion service",
|
|
15
|
+
options: [],
|
|
16
|
+
positionals: [],
|
|
17
|
+
execute: async (
|
|
18
|
+
context: Context,
|
|
19
|
+
_argumentValues: ArgumentValues,
|
|
20
|
+
): Promise<void> => {
|
|
21
|
+
const printerService = context.getServiceById(
|
|
22
|
+
PRINTER_SERVICE_ID,
|
|
23
|
+
) as PrinterService;
|
|
24
|
+
const completionService = context.getServiceById(
|
|
25
|
+
COMPLETION_SERVICE_ID,
|
|
26
|
+
) as CompletionService;
|
|
27
|
+
|
|
28
|
+
const bootstrapScript = completionService.getBootstrapScript(
|
|
29
|
+
ShellType.BASH,
|
|
30
|
+
"example-cli",
|
|
31
|
+
);
|
|
32
|
+
await printerService.print(`Bash bootstrap script:\n${bootstrapScript}\n`);
|
|
33
|
+
|
|
34
|
+
const configPath = completionService.getDefaultConfigPath(ShellType.BASH);
|
|
35
|
+
await printerService.print(`Bash default config path: ${configPath}\n`);
|
|
36
|
+
},
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
export default completion;
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type ArgumentValues,
|
|
3
|
+
type Context,
|
|
4
|
+
KEY_VALUE_SERVICE_ID,
|
|
5
|
+
type KeyValueService,
|
|
6
|
+
PRINTER_SERVICE_ID,
|
|
7
|
+
type PrinterService,
|
|
8
|
+
type SubCommand,
|
|
9
|
+
} from "@flowscripter/dynamic-cli-framework";
|
|
10
|
+
|
|
11
|
+
const configuration: SubCommand = {
|
|
12
|
+
name: "configuration",
|
|
13
|
+
description: "Demonstrates secrets storage and key-value service",
|
|
14
|
+
options: [],
|
|
15
|
+
positionals: [],
|
|
16
|
+
async execute(
|
|
17
|
+
context: Context,
|
|
18
|
+
_argumentValues: ArgumentValues,
|
|
19
|
+
): Promise<void> {
|
|
20
|
+
const printerService = context.getServiceById(
|
|
21
|
+
PRINTER_SERVICE_ID,
|
|
22
|
+
) as PrinterService;
|
|
23
|
+
const keyValueService = context.getServiceById(
|
|
24
|
+
KEY_VALUE_SERVICE_ID,
|
|
25
|
+
) as KeyValueService;
|
|
26
|
+
|
|
27
|
+
// --- Key-Value Storage ---
|
|
28
|
+
await printerService.print("--- Key-Value Storage ---\n");
|
|
29
|
+
|
|
30
|
+
await keyValueService.setKey("demo-string", "hello world");
|
|
31
|
+
await keyValueService.setKey("demo-number", "42");
|
|
32
|
+
await keyValueService.setKey("demo-json", '{"enabled":true,"retries":3}');
|
|
33
|
+
|
|
34
|
+
const keys = ["demo-string", "demo-number", "demo-json"];
|
|
35
|
+
for (const key of keys) {
|
|
36
|
+
const value = await keyValueService.getKey(key);
|
|
37
|
+
await printerService.print(` ${key} = ${value}\n`);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const missing = await keyValueService.hasKey("demo-nonexistent");
|
|
41
|
+
await printerService.print(` demo-nonexistent exists: ${missing}\n`);
|
|
42
|
+
|
|
43
|
+
for (const key of keys) {
|
|
44
|
+
await keyValueService.deleteKey(key);
|
|
45
|
+
}
|
|
46
|
+
await printerService.print("Cleaned up keys\n");
|
|
47
|
+
|
|
48
|
+
// --- Secret Storage ---
|
|
49
|
+
await printerService.print("--- Secret Storage ---\n");
|
|
50
|
+
|
|
51
|
+
try {
|
|
52
|
+
await keyValueService.setKey("demo-api-key", "sk-secret-12345", true);
|
|
53
|
+
await printerService.print("Stored secret: demo-api-key\n");
|
|
54
|
+
|
|
55
|
+
const exists = await keyValueService.hasKey("demo-api-key");
|
|
56
|
+
await printerService.print(`Key exists: ${exists}\n`);
|
|
57
|
+
|
|
58
|
+
const secret = await keyValueService.getKey("demo-api-key");
|
|
59
|
+
const masked = secret.substring(0, 3) + "***";
|
|
60
|
+
await printerService.print(`Retrieved secret (masked): ${masked}\n`);
|
|
61
|
+
|
|
62
|
+
await keyValueService.deleteKey("demo-api-key");
|
|
63
|
+
await printerService.print("Deleted secret: demo-api-key\n");
|
|
64
|
+
} catch (error) {
|
|
65
|
+
await printerService.warn(
|
|
66
|
+
`Secret storage unavailable (OS keyring may be locked): ${
|
|
67
|
+
(error as Error).message
|
|
68
|
+
}\n`,
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
},
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
export default configuration;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type Context,
|
|
3
|
+
type GroupCommand,
|
|
4
|
+
} from "@flowscripter/dynamic-cli-framework";
|
|
5
|
+
import printing from "./printing.ts";
|
|
6
|
+
import prompting from "./prompting.ts";
|
|
7
|
+
import argParsing from "./arg-parsing.ts";
|
|
8
|
+
import configuration from "./configuration.ts";
|
|
9
|
+
import completion from "./completion.ts";
|
|
10
|
+
import banners from "./banners.ts";
|
|
11
|
+
import image from "./image.ts";
|
|
12
|
+
import interruption from "./interruption.ts";
|
|
13
|
+
|
|
14
|
+
const demo: GroupCommand = {
|
|
15
|
+
name: "demo",
|
|
16
|
+
description: "Demonstration commands for dynamic-cli-framework features",
|
|
17
|
+
memberSubCommands: [
|
|
18
|
+
printing,
|
|
19
|
+
prompting,
|
|
20
|
+
argParsing,
|
|
21
|
+
configuration,
|
|
22
|
+
completion,
|
|
23
|
+
banners,
|
|
24
|
+
image,
|
|
25
|
+
interruption,
|
|
26
|
+
],
|
|
27
|
+
execute: async (_context: Context): Promise<void> => {},
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export default demo;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type ArgumentValues,
|
|
3
|
+
type Context,
|
|
4
|
+
IMAGE_PRINTER_SERVICE_ID,
|
|
5
|
+
type ImagePrinterService,
|
|
6
|
+
PRINTER_SERVICE_ID,
|
|
7
|
+
type PrinterService,
|
|
8
|
+
type SubCommand,
|
|
9
|
+
} from "@flowscripter/dynamic-cli-framework";
|
|
10
|
+
import path from "node:path";
|
|
11
|
+
import process from "node:process";
|
|
12
|
+
|
|
13
|
+
const image: SubCommand = {
|
|
14
|
+
name: "image",
|
|
15
|
+
description: "Demonstrates image rendering in the terminal",
|
|
16
|
+
options: [],
|
|
17
|
+
positionals: [],
|
|
18
|
+
execute: async (
|
|
19
|
+
context: Context,
|
|
20
|
+
_argumentValues: ArgumentValues,
|
|
21
|
+
): Promise<void> => {
|
|
22
|
+
const printerService = context.getServiceById(
|
|
23
|
+
PRINTER_SERVICE_ID,
|
|
24
|
+
) as PrinterService;
|
|
25
|
+
const imagePrinterService = context.getServiceById(
|
|
26
|
+
IMAGE_PRINTER_SERVICE_ID,
|
|
27
|
+
) as ImagePrinterService;
|
|
28
|
+
|
|
29
|
+
const baseDir = path.dirname(process.execPath);
|
|
30
|
+
let imagePath = path.join(baseDir, "assets", "logo.png");
|
|
31
|
+
const logoFile = Bun.file(imagePath);
|
|
32
|
+
if (!(await logoFile.exists())) {
|
|
33
|
+
imagePath = path.join(import.meta.dir, "..", "..", "assets", "logo.png");
|
|
34
|
+
}
|
|
35
|
+
const imageBuffer = await Bun.file(imagePath).bytes();
|
|
36
|
+
const imageOutput = await imagePrinterService.image(
|
|
37
|
+
imageBuffer,
|
|
38
|
+
25,
|
|
39
|
+
"#FFFFFF",
|
|
40
|
+
);
|
|
41
|
+
await printerService.print(imageOutput + "\n");
|
|
42
|
+
},
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
export default image;
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type ArgumentValues,
|
|
3
|
+
type Context,
|
|
4
|
+
Icon,
|
|
5
|
+
PRINTER_SERVICE_ID,
|
|
6
|
+
type PrinterService,
|
|
7
|
+
SHUTDOWN_SERVICE_ID,
|
|
8
|
+
type ShutdownService,
|
|
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 interruption: SubCommand = {
|
|
17
|
+
name: "interruption",
|
|
18
|
+
description:
|
|
19
|
+
"Demonstrates graceful shutdown with signal handling (press Ctrl+C to interrupt)",
|
|
20
|
+
options: [],
|
|
21
|
+
positionals: [],
|
|
22
|
+
execute: async (
|
|
23
|
+
context: Context,
|
|
24
|
+
_argumentValues: ArgumentValues,
|
|
25
|
+
): Promise<void> => {
|
|
26
|
+
const printerService = context.getServiceById(
|
|
27
|
+
PRINTER_SERVICE_ID,
|
|
28
|
+
) as PrinterService;
|
|
29
|
+
const shutdownService = context.getServiceById(
|
|
30
|
+
SHUTDOWN_SERVICE_ID,
|
|
31
|
+
) as ShutdownService;
|
|
32
|
+
|
|
33
|
+
await printerService.print(
|
|
34
|
+
"Entering long-running mode. Press Ctrl+C to request graceful shutdown...\n",
|
|
35
|
+
);
|
|
36
|
+
|
|
37
|
+
shutdownService.enterLongRunningMode();
|
|
38
|
+
try {
|
|
39
|
+
for (let i = 1; i <= 30; i++) {
|
|
40
|
+
await printerService.showSpinner(`Working... iteration ${i}/30`);
|
|
41
|
+
await sleep(1);
|
|
42
|
+
|
|
43
|
+
if (shutdownService.isShutdownRequested) {
|
|
44
|
+
await printerService.hideSpinner();
|
|
45
|
+
await printerService.print(
|
|
46
|
+
"Shutdown requested, finishing gracefully...\n",
|
|
47
|
+
Icon.ALERT,
|
|
48
|
+
);
|
|
49
|
+
break;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
await printerService.hideSpinner();
|
|
54
|
+
|
|
55
|
+
if (!shutdownService.isShutdownRequested) {
|
|
56
|
+
await printerService.print("Work completed normally\n", Icon.SUCCESS);
|
|
57
|
+
}
|
|
58
|
+
} finally {
|
|
59
|
+
shutdownService.leaveLongRunningMode();
|
|
60
|
+
await printerService.print("Exited long-running mode\n", Icon.SUCCESS);
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
export default interruption;
|