@f5-sales-demo/pi-utils 22.4.2 → 22.4.5
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/package.json +2 -2
- package/src/cli-output.ts +19 -0
- package/src/cli.ts +7 -6
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"type": "module",
|
|
3
3
|
"name": "@f5-sales-demo/pi-utils",
|
|
4
|
-
"version": "22.4.
|
|
4
|
+
"version": "22.4.5",
|
|
5
5
|
"description": "Shared utilities for pi packages",
|
|
6
6
|
"homepage": "https://github.com/f5-sales-demo/xcsh",
|
|
7
7
|
"author": "Can Boluk",
|
|
@@ -39,7 +39,7 @@
|
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
41
41
|
"@types/bun": "1.4.2",
|
|
42
|
-
"@f5-sales-demo/pi-natives": "22.4.
|
|
42
|
+
"@f5-sales-demo/pi-natives": "22.4.5"
|
|
43
43
|
},
|
|
44
44
|
"engines": {
|
|
45
45
|
"bun": ">=1.4.2"
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/** A closed downstream pipe is normal CLI termination; other output errors remain fatal. */
|
|
2
|
+
function handleOutputError(error: unknown): never {
|
|
3
|
+
if (error && typeof error === "object" && "code" in error && error.code === "EPIPE") process.exit(0);
|
|
4
|
+
throw error;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export function installCliOutputHandlers(): void {
|
|
8
|
+
process.stdout.on("error", handleOutputError);
|
|
9
|
+
process.stderr.on("error", handleOutputError);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** Bun may throw synchronously from a fast file-stream write. */
|
|
13
|
+
export function writeCliOutput(stream: NodeJS.WriteStream, text: string): void {
|
|
14
|
+
try {
|
|
15
|
+
stream.write(text);
|
|
16
|
+
} catch (error) {
|
|
17
|
+
handleOutputError(error);
|
|
18
|
+
}
|
|
19
|
+
}
|
package/src/cli.ts
CHANGED
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
* - Typed `this.parse()` output matching oclif's API shape
|
|
11
11
|
*/
|
|
12
12
|
import { parseArgs as nodeParseArgs } from "node:util";
|
|
13
|
+
import { writeCliOutput } from "./cli-output";
|
|
13
14
|
|
|
14
15
|
// ---------------------------------------------------------------------------
|
|
15
16
|
// Flag & Arg descriptors
|
|
@@ -305,7 +306,7 @@ export function renderRootHelp(config: CliConfig): void {
|
|
|
305
306
|
lines.push("");
|
|
306
307
|
}
|
|
307
308
|
|
|
308
|
-
process.stdout
|
|
309
|
+
writeCliOutput(process.stdout, lines.join("\n"));
|
|
309
310
|
}
|
|
310
311
|
|
|
311
312
|
/** Render help for a single command. */
|
|
@@ -318,7 +319,7 @@ export function renderCommandHelp(bin: string, id: string, Cmd: CommandCtor): vo
|
|
|
318
319
|
const hasFlags = Object.keys(Cmd.flags ?? {}).length > 0;
|
|
319
320
|
lines.push(` $ ${bin} ${id}${argStr}${hasFlags ? " [FLAGS]" : ""}\n`);
|
|
320
321
|
renderCommandBody(lines, Cmd);
|
|
321
|
-
process.stdout
|
|
322
|
+
writeCliOutput(process.stdout, lines.join("\n"));
|
|
322
323
|
}
|
|
323
324
|
|
|
324
325
|
function renderCommandBody(lines: string[], Cmd: CommandCtor): void {
|
|
@@ -423,7 +424,7 @@ export async function run(opts: RunOptions): Promise<void> {
|
|
|
423
424
|
|
|
424
425
|
// Version
|
|
425
426
|
if (commandId === "--version" || commandId === "-v") {
|
|
426
|
-
process.stdout
|
|
427
|
+
writeCliOutput(process.stdout, `${bin}/${version}\n`);
|
|
427
428
|
return;
|
|
428
429
|
}
|
|
429
430
|
|
|
@@ -434,7 +435,7 @@ export async function run(opts: RunOptions): Promise<void> {
|
|
|
434
435
|
if (entry) {
|
|
435
436
|
renderCommandHelp(bin, entry.name, await entry.load());
|
|
436
437
|
} else {
|
|
437
|
-
process.stderr
|
|
438
|
+
writeCliOutput(process.stderr, `Unknown command: ${commandId}\n`);
|
|
438
439
|
}
|
|
439
440
|
return;
|
|
440
441
|
}
|
|
@@ -443,7 +444,7 @@ export async function run(opts: RunOptions): Promise<void> {
|
|
|
443
444
|
const entry = findEntry(opts.commands, commandId);
|
|
444
445
|
|
|
445
446
|
if (!entry) {
|
|
446
|
-
process.stderr
|
|
447
|
+
writeCliOutput(process.stderr, `Error: command ${commandId} not found\n`);
|
|
447
448
|
process.exitCode = 1;
|
|
448
449
|
return;
|
|
449
450
|
}
|
|
@@ -457,7 +458,7 @@ export async function run(opts: RunOptions): Promise<void> {
|
|
|
457
458
|
await instance.run();
|
|
458
459
|
} catch (error) {
|
|
459
460
|
if (!(error instanceof CliUsageError)) throw error;
|
|
460
|
-
process.stderr
|
|
461
|
+
writeCliOutput(process.stderr, `Error: ${error.message}\n`);
|
|
461
462
|
process.exitCode = 2;
|
|
462
463
|
}
|
|
463
464
|
}
|