printable-shell-command 1.1.1 → 1.1.2
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,150 @@
|
|
|
1
|
+
import type { ChildProcess as NodeChildProcess, SpawnOptions as NodeSpawnOptions, SpawnOptionsWithoutStdio as NodeSpawnOptionsWithoutStdio, SpawnOptionsWithStdioTuple as NodeSpawnOptionsWithStdioTuple, StdioNull as NodeStdioNull, StdioPipe as NodeStdioPipe } from "node:child_process";
|
|
2
|
+
import { styleText } from "node:util";
|
|
3
|
+
import type { SpawnOptions as BunSpawnOptions, Subprocess as BunSubprocess } from "bun";
|
|
4
|
+
type SingleArgument = string;
|
|
5
|
+
type FlagArgumentPair = [string, string];
|
|
6
|
+
type ArgsEntry = SingleArgument | FlagArgumentPair;
|
|
7
|
+
type Args = ArgsEntry[];
|
|
8
|
+
export interface PrintOptions {
|
|
9
|
+
/** Defaults to "" */
|
|
10
|
+
mainIndentation?: string;
|
|
11
|
+
/** Defaults to " " */
|
|
12
|
+
argIndentation?: string;
|
|
13
|
+
/**
|
|
14
|
+
* - `"auto"`: Quote only arguments that need it for safety. This tries to be
|
|
15
|
+
* portable and safe across shells, but true safety and portability is hard
|
|
16
|
+
* to guarantee.
|
|
17
|
+
* - `"extra-safe"`: Quote all arguments, even ones that don't need it. This is
|
|
18
|
+
* more likely to be safe under all circumstances.
|
|
19
|
+
*/
|
|
20
|
+
quoting?: "auto" | "extra-safe";
|
|
21
|
+
/** Line wrapping to use between arguments. Defaults to `"by-entry"`. */
|
|
22
|
+
argumentLineWrapping?: "by-entry" | "nested-by-entry" | "by-argument" | "inline";
|
|
23
|
+
/**
|
|
24
|
+
* Style text using `node`'s [`styleText(…)`](https://nodejs.org/api/util.html#utilstyletextformat-text-options)
|
|
25
|
+
*
|
|
26
|
+
* Example usage:
|
|
27
|
+
*
|
|
28
|
+
* ```
|
|
29
|
+
* new PrintableShellCommand("echo", ["hi"]).print({
|
|
30
|
+
* styleTextFormat: ["gray", "bold"],
|
|
31
|
+
* });
|
|
32
|
+
* */
|
|
33
|
+
styleTextFormat?: Parameters<typeof styleText>[0];
|
|
34
|
+
}
|
|
35
|
+
export declare class PrintableShellCommand {
|
|
36
|
+
#private;
|
|
37
|
+
private args;
|
|
38
|
+
constructor(commandName: string, args?: Args);
|
|
39
|
+
get commandName(): string;
|
|
40
|
+
/** For use with `bun`.
|
|
41
|
+
*
|
|
42
|
+
* Usage example:
|
|
43
|
+
*
|
|
44
|
+
* ```
|
|
45
|
+
* import { PrintableShellCommand } from "printable-shell-command";
|
|
46
|
+
* import { spawn } from "bun";
|
|
47
|
+
*
|
|
48
|
+
* const command = new PrintableShellCommand( … );
|
|
49
|
+
* await spawn(command.toFlatCommand()).exited;
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
toFlatCommand(): string[];
|
|
53
|
+
/**
|
|
54
|
+
* Convenient alias for `toFlatCommand()`.
|
|
55
|
+
*
|
|
56
|
+
* Usage example:
|
|
57
|
+
*
|
|
58
|
+
* ```
|
|
59
|
+
* import { PrintableShellCommand } from "printable-shell-command";
|
|
60
|
+
* import { spawn } from "bun";
|
|
61
|
+
*
|
|
62
|
+
* const command = new PrintableShellCommand( … );
|
|
63
|
+
* await spawn(command.forBun()).exited;
|
|
64
|
+
* ```
|
|
65
|
+
*
|
|
66
|
+
* */
|
|
67
|
+
forBun(): string[];
|
|
68
|
+
/**
|
|
69
|
+
* For use with `node:child_process`
|
|
70
|
+
*
|
|
71
|
+
* Usage example:
|
|
72
|
+
*
|
|
73
|
+
* ```
|
|
74
|
+
* import { PrintableShellCommand } from "printable-shell-command";
|
|
75
|
+
* import { spawn } from "node:child_process";
|
|
76
|
+
*
|
|
77
|
+
* const command = new PrintableShellCommand( … );
|
|
78
|
+
* const child_process = spawn(...command.toCommandWithFlatArgs()); // Note the `...`
|
|
79
|
+
* ```
|
|
80
|
+
*
|
|
81
|
+
*/
|
|
82
|
+
toCommandWithFlatArgs(): [string, string[]];
|
|
83
|
+
/**
|
|
84
|
+
* For use with `node:child_process`
|
|
85
|
+
*
|
|
86
|
+
* Usage example:
|
|
87
|
+
*
|
|
88
|
+
* ```
|
|
89
|
+
* import { PrintableShellCommand } from "printable-shell-command";
|
|
90
|
+
* import { spawn } from "node:child_process";
|
|
91
|
+
*
|
|
92
|
+
* const command = new PrintableShellCommand( … );
|
|
93
|
+
* const child_process = spawn(...command.forNode()); // Note the `...`
|
|
94
|
+
* ```
|
|
95
|
+
*
|
|
96
|
+
* Convenient alias for `toCommandWithFlatArgs()`.
|
|
97
|
+
*/
|
|
98
|
+
forNode(): [string, string[]];
|
|
99
|
+
getPrintableCommand(options?: PrintOptions): string;
|
|
100
|
+
print(options?: PrintOptions): PrintableShellCommand;
|
|
101
|
+
/**
|
|
102
|
+
* The returned child process includes a `.success` `Promise` field, per https://github.com/oven-sh/bun/issues/8313
|
|
103
|
+
*/
|
|
104
|
+
spawnNode<Stdin extends NodeStdioNull | NodeStdioPipe, Stdout extends NodeStdioNull | NodeStdioPipe, Stderr extends NodeStdioNull | NodeStdioPipe>(options?: NodeSpawnOptions | NodeSpawnOptionsWithoutStdio | NodeSpawnOptionsWithStdioTuple<Stdin, Stdout, Stderr>): // TODO: figure out how to return `ChildProcessByStdio<…>` without duplicating fragile boilerplate.
|
|
105
|
+
NodeChildProcess & {
|
|
106
|
+
success: Promise<void>;
|
|
107
|
+
};
|
|
108
|
+
/** A wrapper for `.spawnNode(…)` that sets stdio to `"inherit"` (common for
|
|
109
|
+
* invoking commands from scripts whose output and interaction should be
|
|
110
|
+
* surfaced to the user). */
|
|
111
|
+
spawnNodeInherit(options?: Omit<NodeSpawnOptions, "stdio">): NodeChildProcess & {
|
|
112
|
+
success: Promise<void>;
|
|
113
|
+
};
|
|
114
|
+
/** Equivalent to:
|
|
115
|
+
*
|
|
116
|
+
* ```
|
|
117
|
+
* await this.print().spawnNodeInherit().success;
|
|
118
|
+
* ```
|
|
119
|
+
*/
|
|
120
|
+
shellOutNode(options?: Omit<NodeSpawnOptions, "stdio">): Promise<void>;
|
|
121
|
+
/**
|
|
122
|
+
* The returned subprocess includes a `.success` `Promise` field, per https://github.com/oven-sh/bun/issues/8313
|
|
123
|
+
*/
|
|
124
|
+
spawnBun<const In extends BunSpawnOptions.Writable = "ignore", const Out extends BunSpawnOptions.Readable = "pipe", const Err extends BunSpawnOptions.Readable = "inherit">(options?: Omit<BunSpawnOptions.OptionsObject<In, Out, Err>, "cmd">): BunSubprocess<In, Out, Err> & {
|
|
125
|
+
success: Promise<void>;
|
|
126
|
+
};
|
|
127
|
+
/**
|
|
128
|
+
* A wrapper for `.spawnBunInherit(…)` that sets stdio to `"inherit"` (common
|
|
129
|
+
* for invoking commands from scripts whose output and interaction should be
|
|
130
|
+
* surfaced to the user).
|
|
131
|
+
*/
|
|
132
|
+
spawnBunInherit(options?: Omit<Omit<BunSpawnOptions.OptionsObject<"inherit", "inherit", "inherit">, "cmd">, "stdio">): BunSubprocess<"inherit", "inherit", "inherit"> & {
|
|
133
|
+
success: Promise<void>;
|
|
134
|
+
};
|
|
135
|
+
/** Equivalent to:
|
|
136
|
+
*
|
|
137
|
+
* ```
|
|
138
|
+
* new Response(this.spawnBun(options).stdout);
|
|
139
|
+
* ```
|
|
140
|
+
*/
|
|
141
|
+
spawnBunStdout(options?: Omit<Omit<BunSpawnOptions.OptionsObject<"inherit", "inherit", "inherit">, "cmd">, "stdio">): Response;
|
|
142
|
+
/** Equivalent to:
|
|
143
|
+
*
|
|
144
|
+
* ```
|
|
145
|
+
* await this.print().spawnBunInherit().success;
|
|
146
|
+
* ```
|
|
147
|
+
*/
|
|
148
|
+
shellOutBun(options?: Omit<Omit<BunSpawnOptions.OptionsObject<"inherit", "inherit", "inherit">, "cmd">, "stdio">): Promise<void>;
|
|
149
|
+
}
|
|
150
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "printable-shell-command",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.2",
|
|
4
4
|
"main": "./src/index.ts",
|
|
5
5
|
"devDependencies": {
|
|
6
6
|
"@biomejs/biome": "^2.1.4",
|
|
@@ -23,5 +23,8 @@
|
|
|
23
23
|
"./dist/",
|
|
24
24
|
"./src/"
|
|
25
25
|
],
|
|
26
|
-
"type": "module"
|
|
26
|
+
"type": "module",
|
|
27
|
+
"scripts": {
|
|
28
|
+
"prepublishOnly": "make prepublishOnly"
|
|
29
|
+
}
|
|
27
30
|
}
|