printable-shell-command 4.0.4 → 5.0.1
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/README.md +15 -20
- package/dist/lib/printable-shell-command/PrintableShellCommand.d.ts +168 -0
- package/dist/lib/printable-shell-command/index.d.ts +1 -164
- package/dist/lib/printable-shell-command/index.js +24 -8
- package/dist/lib/printable-shell-command/index.js.map +3 -3
- package/package.json +7 -6
- package/src/PrintableShellCommand.test.ts +508 -0
- package/src/PrintableShellCommand.ts +640 -0
- package/src/index.ts +7 -624
package/README.md
CHANGED
|
@@ -42,32 +42,27 @@ ffmpeg \
|
|
|
42
42
|
|
|
43
43
|
### Spawn a process in `node`
|
|
44
44
|
|
|
45
|
-
|
|
46
|
-
import { PrintableShellCommand } from "printable-shell-command";
|
|
45
|
+
````ts example
|
|
47
46
|
import { spawn } from "node:child_process";
|
|
47
|
+
import { PrintableShellCommand } from "printable-shell-command";
|
|
48
|
+
|
|
49
|
+
const command = new PrintableShellCommand("ffmpeg", [
|
|
50
|
+
["-i", "./test/My video.mp4"],
|
|
51
|
+
["-filter:v", "setpts=2.0*PTS"],
|
|
52
|
+
["-filter:a", "atempo=0.5"],
|
|
53
|
+
"./test/My video (slow-mo).mov",
|
|
54
|
+
]);
|
|
48
55
|
|
|
49
|
-
|
|
50
|
-
const child_process = spawn(...command.toCommandWithFlatArgs()); // Note the `...`
|
|
56
|
+
command.shellOut();
|
|
51
57
|
|
|
52
|
-
//
|
|
58
|
+
// Spawn directly
|
|
53
59
|
await command.spawn().success;
|
|
54
60
|
await command.spawnTransparently().success;
|
|
55
|
-
|
|
56
|
-
```
|
|
57
|
-
|
|
58
|
-
### Spawn a process in `bun`
|
|
59
|
-
|
|
60
|
-
```typescript
|
|
61
|
-
import { PrintableShellCommand } from "printable-shell-command";
|
|
62
|
-
import { spawn } from "bun";
|
|
61
|
+
command.spawnDetached();
|
|
63
62
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
// or directly
|
|
68
|
-
await command.bun.spawnBun().success;
|
|
69
|
-
await command.bun.spawnBunInherit().success;
|
|
70
|
-
```
|
|
63
|
+
// Or use `node` spawn (note the `...`).
|
|
64
|
+
spawn(...command.toCommandWithFlatArgs(), {});
|
|
65
|
+
````
|
|
71
66
|
|
|
72
67
|
## Protections
|
|
73
68
|
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import type { ChildProcessByStdio, SpawnOptions as NodeSpawnOptions } from "node:child_process";
|
|
2
|
+
import { Readable, Writable } from "node:stream";
|
|
3
|
+
import type { WriteStream } from "node:tty";
|
|
4
|
+
import { styleText } from "node:util";
|
|
5
|
+
import { Path } from "path-class";
|
|
6
|
+
import type { NodeWithCwd, spawnType, WithSuccess } from "./spawn";
|
|
7
|
+
type StyleTextFormat = Parameters<typeof styleText>[0];
|
|
8
|
+
type SingleArgument = string | Path;
|
|
9
|
+
type ArgsEntry = SingleArgument | SingleArgument[];
|
|
10
|
+
type Args = ArgsEntry[];
|
|
11
|
+
declare const ARGUMENT_LINE_WRAPPING_VALUES: readonly ["by-entry", "nested-by-entry", "by-argument", "inline"];
|
|
12
|
+
type ArgumentLineWrapping = (typeof ARGUMENT_LINE_WRAPPING_VALUES)[number];
|
|
13
|
+
export interface PrintOptions {
|
|
14
|
+
/** Defaults to "" */
|
|
15
|
+
mainIndentation?: string;
|
|
16
|
+
/** Defaults to " " */
|
|
17
|
+
argIndentation?: string;
|
|
18
|
+
/**
|
|
19
|
+
* - `"auto"`: Quote only arguments that need it for safety. This tries to be
|
|
20
|
+
* portable and safe across shells, but true safety and portability is hard
|
|
21
|
+
* to guarantee.
|
|
22
|
+
* - `"extra-safe"`: Quote all arguments, even ones that don't need it. This is
|
|
23
|
+
* more likely to be safe under all circumstances.
|
|
24
|
+
*/
|
|
25
|
+
quoting?: "auto" | "extra-safe";
|
|
26
|
+
/** Line wrapping to use between arguments. Defaults to `"by-entry"`. */
|
|
27
|
+
argumentLineWrapping?: ArgumentLineWrapping;
|
|
28
|
+
/** Include the first arg (or first arg group) on the same line as the command, regardless of the `argumentLineWrapping` setting. */
|
|
29
|
+
skipLineWrapBeforeFirstArg?: true | false;
|
|
30
|
+
/**
|
|
31
|
+
* Style text using `node`'s {@link styleText | `styleText(…)`}.
|
|
32
|
+
*
|
|
33
|
+
* Example usage:
|
|
34
|
+
*
|
|
35
|
+
* ```
|
|
36
|
+
* new PrintableShellCommand("echo", ["hi"]).print({
|
|
37
|
+
* style: ["green", "underline"],
|
|
38
|
+
* });
|
|
39
|
+
* */
|
|
40
|
+
style?: StyleTextFormat;
|
|
41
|
+
}
|
|
42
|
+
export interface StreamPrintOptions extends PrintOptions {
|
|
43
|
+
/**
|
|
44
|
+
* Auto-style the text when:
|
|
45
|
+
*
|
|
46
|
+
* - the output stream is detected to be a TTY
|
|
47
|
+
* - `styleTextFormat` is not specified.
|
|
48
|
+
*
|
|
49
|
+
* The current auto style is: `["gray", "bold"]`
|
|
50
|
+
*/
|
|
51
|
+
autoStyle?: "tty" | "never";
|
|
52
|
+
stream?: WriteStream | Writable;
|
|
53
|
+
}
|
|
54
|
+
export type StdinSource = {
|
|
55
|
+
text: string;
|
|
56
|
+
} | {
|
|
57
|
+
json: any;
|
|
58
|
+
} | {
|
|
59
|
+
path: string | Path;
|
|
60
|
+
} | {
|
|
61
|
+
stream: Readable | ReadableStream;
|
|
62
|
+
};
|
|
63
|
+
export declare class PrintableShellCommand {
|
|
64
|
+
#private;
|
|
65
|
+
private args;
|
|
66
|
+
constructor(commandName: string | Path, args?: Args);
|
|
67
|
+
get commandName(): 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
|
+
getPrintableCommand(options?: PrintOptions): string;
|
|
84
|
+
/**
|
|
85
|
+
* Print the shell command to {@link stderr} (default) or a specified stream.
|
|
86
|
+
*
|
|
87
|
+
* By default, this will be auto-styled (as bold gray) when `.isTTY` is true
|
|
88
|
+
* for the stream. `.isTTY` is populated for the {@link stderr} and
|
|
89
|
+
* {@link stdout} objects. Pass `"autoStyle": "never"` or an explicit
|
|
90
|
+
* `style` to disable this.
|
|
91
|
+
*
|
|
92
|
+
*/
|
|
93
|
+
print(options?: StreamPrintOptions): PrintableShellCommand;
|
|
94
|
+
/**
|
|
95
|
+
* Send data to `stdin` of the subprocess.
|
|
96
|
+
*
|
|
97
|
+
* Note that this will overwrite:
|
|
98
|
+
*
|
|
99
|
+
* - Any previous value set using {@link PrintableShellCommand.stdin | `.stdin(…)`}.
|
|
100
|
+
* - Any value set for `stdin` using the `"stdio"` field of {@link PrintableShellCommand.spawn | `.spawn(…)`}.
|
|
101
|
+
*/
|
|
102
|
+
stdin(source: StdinSource): PrintableShellCommand;
|
|
103
|
+
/**
|
|
104
|
+
* The returned child process includes a `.success` `Promise` field, per https://github.com/oven-sh/bun/issues/8313
|
|
105
|
+
*/
|
|
106
|
+
spawn: typeof spawnType;
|
|
107
|
+
/** A wrapper for `.spawn(…)` that sets stdio to `"inherit"` (common for
|
|
108
|
+
* invoking commands from scripts whose output and interaction should be
|
|
109
|
+
* surfaced to the user).
|
|
110
|
+
*
|
|
111
|
+
* If there is no other interaction with the shell from the calling process,
|
|
112
|
+
* then it acts "transparent" and allows user to interact with the subprocess
|
|
113
|
+
* in its stead.
|
|
114
|
+
*/
|
|
115
|
+
spawnTransparently(options?: NodeWithCwd<Omit<NodeSpawnOptions, "stdio">>): ChildProcessByStdio<null, null, null> & WithSuccess;
|
|
116
|
+
/**
|
|
117
|
+
* A wrapper for {@link PrintableShellCommand.spawn | `.spawn(…)`} that:
|
|
118
|
+
*
|
|
119
|
+
* - sets `detached` to `true`,
|
|
120
|
+
* - sets stdio to `"inherit"`,
|
|
121
|
+
* - calls `.unref()`, and
|
|
122
|
+
* - does not wait for the process to exit.
|
|
123
|
+
*
|
|
124
|
+
* This is similar to starting a command in the background and disowning it (in a shell).
|
|
125
|
+
*
|
|
126
|
+
*/
|
|
127
|
+
spawnDetached(options?: NodeWithCwd<Omit<Omit<NodeSpawnOptions, "stdio">, "detached">>): void;
|
|
128
|
+
stdout(options?: NodeWithCwd<Omit<NodeSpawnOptions, "stdio">>): Response;
|
|
129
|
+
/**
|
|
130
|
+
* Convenience function for:
|
|
131
|
+
*
|
|
132
|
+
* .stdout(options).text()
|
|
133
|
+
*
|
|
134
|
+
* This can make some simple invocations easier to read and/or fit on a single line.
|
|
135
|
+
*/
|
|
136
|
+
text(options?: NodeWithCwd<Omit<NodeSpawnOptions, "stdio">>): Promise<string>;
|
|
137
|
+
/**
|
|
138
|
+
* Convenience function for:
|
|
139
|
+
*
|
|
140
|
+
* .stdout(options).json()
|
|
141
|
+
*
|
|
142
|
+
* This can make some simple invocations easier to read and/or fit on a single line.
|
|
143
|
+
*/
|
|
144
|
+
json<T>(options?: NodeWithCwd<Omit<NodeSpawnOptions, "stdio">>): Promise<T>;
|
|
145
|
+
/**
|
|
146
|
+
* Parse `stdout` into a generator of string values using a NULL delimiter.
|
|
147
|
+
*
|
|
148
|
+
* A trailing NULL delimiter from `stdout` is required and removed.
|
|
149
|
+
*/
|
|
150
|
+
text0(options?: NodeWithCwd<Omit<NodeSpawnOptions, "stdio">>): AsyncGenerator<string>;
|
|
151
|
+
/**
|
|
152
|
+
* Parse `stdout` into a generator of JSON values using a NULL delimiter.
|
|
153
|
+
*
|
|
154
|
+
* A trailing NULL delimiter from `stdout` is required and removed.
|
|
155
|
+
*/
|
|
156
|
+
json0(options?: NodeWithCwd<Omit<NodeSpawnOptions, "stdio">>): AsyncGenerator<any>;
|
|
157
|
+
/** Equivalent to:
|
|
158
|
+
*
|
|
159
|
+
* ```
|
|
160
|
+
* await this.print(…).spawnTransparently(…).success;
|
|
161
|
+
* ```
|
|
162
|
+
*/
|
|
163
|
+
shellOut(options?: NodeWithCwd<Omit<NodeSpawnOptions, "stdio">> & {
|
|
164
|
+
print: StreamPrintOptions | ArgumentLineWrapping | false;
|
|
165
|
+
}): Promise<void>;
|
|
166
|
+
}
|
|
167
|
+
export declare function escapeArg(arg: string, isMainCommand: boolean, options: PrintOptions): string;
|
|
168
|
+
export {};
|
|
@@ -1,164 +1 @@
|
|
|
1
|
-
|
|
2
|
-
import { Readable, Writable } from "node:stream";
|
|
3
|
-
import type { WriteStream } from "node:tty";
|
|
4
|
-
import { styleText } from "node:util";
|
|
5
|
-
import { Path } from "path-class";
|
|
6
|
-
import type { NodeWithCwd, spawnType, WithSuccess } from "./spawn";
|
|
7
|
-
type StyleTextFormat = Parameters<typeof styleText>[0];
|
|
8
|
-
type SingleArgument = string | Path;
|
|
9
|
-
type ArgsEntry = SingleArgument | SingleArgument[];
|
|
10
|
-
type Args = ArgsEntry[];
|
|
11
|
-
export interface PrintOptions {
|
|
12
|
-
/** Defaults to "" */
|
|
13
|
-
mainIndentation?: string;
|
|
14
|
-
/** Defaults to " " */
|
|
15
|
-
argIndentation?: string;
|
|
16
|
-
/**
|
|
17
|
-
* - `"auto"`: Quote only arguments that need it for safety. This tries to be
|
|
18
|
-
* portable and safe across shells, but true safety and portability is hard
|
|
19
|
-
* to guarantee.
|
|
20
|
-
* - `"extra-safe"`: Quote all arguments, even ones that don't need it. This is
|
|
21
|
-
* more likely to be safe under all circumstances.
|
|
22
|
-
*/
|
|
23
|
-
quoting?: "auto" | "extra-safe";
|
|
24
|
-
/** Line wrapping to use between arguments. Defaults to `"by-entry"`. */
|
|
25
|
-
argumentLineWrapping?: "by-entry" | "nested-by-entry" | "by-argument" | "inline";
|
|
26
|
-
/** Include the first arg (or first arg group) on the same line as the command, regardless of the `argumentLineWrapping` setting. */
|
|
27
|
-
skipLineWrapBeforeFirstArg?: true | false;
|
|
28
|
-
/**
|
|
29
|
-
* Style text using `node`'s {@link styleText | `styleText(…)`}.
|
|
30
|
-
*
|
|
31
|
-
* Example usage:
|
|
32
|
-
*
|
|
33
|
-
* ```
|
|
34
|
-
* new PrintableShellCommand("echo", ["hi"]).print({
|
|
35
|
-
* styleTextFormat: ["green", "underline"],
|
|
36
|
-
* });
|
|
37
|
-
* */
|
|
38
|
-
styleTextFormat?: StyleTextFormat;
|
|
39
|
-
}
|
|
40
|
-
export interface StreamPrintOptions extends PrintOptions {
|
|
41
|
-
/**
|
|
42
|
-
* Auto-style the text when:
|
|
43
|
-
*
|
|
44
|
-
* - the output stream is detected to be a TTY
|
|
45
|
-
* - `styleTextFormat` is not specified.
|
|
46
|
-
*
|
|
47
|
-
* The current auto style is: `["gray", "bold"]`
|
|
48
|
-
*/
|
|
49
|
-
autoStyle?: "tty" | "never";
|
|
50
|
-
stream?: WriteStream | Writable;
|
|
51
|
-
}
|
|
52
|
-
export type StdinSource = {
|
|
53
|
-
text: string;
|
|
54
|
-
} | {
|
|
55
|
-
json: any;
|
|
56
|
-
} | {
|
|
57
|
-
path: string | Path;
|
|
58
|
-
} | {
|
|
59
|
-
stream: Readable | ReadableStream;
|
|
60
|
-
};
|
|
61
|
-
export declare class PrintableShellCommand {
|
|
62
|
-
#private;
|
|
63
|
-
private args;
|
|
64
|
-
constructor(commandName: string | Path, args?: Args);
|
|
65
|
-
get commandName(): string;
|
|
66
|
-
/**
|
|
67
|
-
* For use with `node:child_process`
|
|
68
|
-
*
|
|
69
|
-
* Usage example:
|
|
70
|
-
*
|
|
71
|
-
* ```
|
|
72
|
-
* import { PrintableShellCommand } from "printable-shell-command";
|
|
73
|
-
* import { spawn } from "node:child_process";
|
|
74
|
-
*
|
|
75
|
-
* const command = new PrintableShellCommand( … );
|
|
76
|
-
* const child_process = spawn(...command.toCommandWithFlatArgs()); // Note the `...`
|
|
77
|
-
* ```
|
|
78
|
-
*
|
|
79
|
-
*/
|
|
80
|
-
toCommandWithFlatArgs(): [string, string[]];
|
|
81
|
-
getPrintableCommand(options?: PrintOptions): string;
|
|
82
|
-
/**
|
|
83
|
-
* Print the shell command to {@link stderr} (default) or a specified stream.
|
|
84
|
-
*
|
|
85
|
-
* By default, this will be auto-styled (as bold gray) when `.isTTY` is true
|
|
86
|
-
* for the stream. `.isTTY` is populated for the {@link stderr} and
|
|
87
|
-
* {@link stdout} objects. Pass `"autoStyle": "never"` or an explicit
|
|
88
|
-
* `styleTextFormat` to disable this.
|
|
89
|
-
*
|
|
90
|
-
*/
|
|
91
|
-
print(options?: StreamPrintOptions): PrintableShellCommand;
|
|
92
|
-
/**
|
|
93
|
-
* Send data to `stdin` of the subprocess.
|
|
94
|
-
*
|
|
95
|
-
* Note that this will overwrite:
|
|
96
|
-
*
|
|
97
|
-
* - Any previous value set using {@link PrintableShellCommand.stdin | `.stdin(…)`}.
|
|
98
|
-
* - Any value set for `stdin` using the `"stdio"` field of {@link PrintableShellCommand.spawn | `.spawn(…)`}.
|
|
99
|
-
*/
|
|
100
|
-
stdin(source: StdinSource): PrintableShellCommand;
|
|
101
|
-
/**
|
|
102
|
-
* The returned child process includes a `.success` `Promise` field, per https://github.com/oven-sh/bun/issues/8313
|
|
103
|
-
*/
|
|
104
|
-
spawn: typeof spawnType;
|
|
105
|
-
/** A wrapper for `.spawn(…)` that sets stdio to `"inherit"` (common for
|
|
106
|
-
* invoking commands from scripts whose output and interaction should be
|
|
107
|
-
* surfaced to the user).
|
|
108
|
-
*
|
|
109
|
-
* If there is no other interaction with the shell from the calling process,
|
|
110
|
-
* then it acts "transparent" and allows user to interact with the subprocess
|
|
111
|
-
* in its stead.
|
|
112
|
-
*/
|
|
113
|
-
spawnTransparently(options?: NodeWithCwd<Omit<NodeSpawnOptions, "stdio">>): ChildProcessByStdio<null, null, null> & WithSuccess;
|
|
114
|
-
/**
|
|
115
|
-
* A wrapper for {@link PrintableShellCommand.spawn | `.spawn(…)`} that:
|
|
116
|
-
*
|
|
117
|
-
* - sets `detached` to `true`,
|
|
118
|
-
* - sets stdio to `"inherit"`,
|
|
119
|
-
* - calls `.unref()`, and
|
|
120
|
-
* - does not wait for the process to exit.
|
|
121
|
-
*
|
|
122
|
-
* This is similar to starting a command in the background and disowning it (in a shell).
|
|
123
|
-
*
|
|
124
|
-
*/
|
|
125
|
-
spawnDetached(options?: NodeWithCwd<Omit<Omit<NodeSpawnOptions, "stdio">, "detached">>): void;
|
|
126
|
-
stdout(options?: NodeWithCwd<Omit<NodeSpawnOptions, "stdio">>): Response;
|
|
127
|
-
/**
|
|
128
|
-
* Convenience function for:
|
|
129
|
-
*
|
|
130
|
-
* .stdout(options).text()
|
|
131
|
-
*
|
|
132
|
-
* This can make some simple invocations easier to read and/or fit on a single line.
|
|
133
|
-
*/
|
|
134
|
-
text(options?: NodeWithCwd<Omit<NodeSpawnOptions, "stdio">>): Promise<string>;
|
|
135
|
-
/**
|
|
136
|
-
* Convenience function for:
|
|
137
|
-
*
|
|
138
|
-
* .stdout(options).json()
|
|
139
|
-
*
|
|
140
|
-
* This can make some simple invocations easier to read and/or fit on a single line.
|
|
141
|
-
*/
|
|
142
|
-
json<T>(options?: NodeWithCwd<Omit<NodeSpawnOptions, "stdio">>): Promise<T>;
|
|
143
|
-
/**
|
|
144
|
-
* Parse `stdout` into a generator of string values using a NULL delimiter.
|
|
145
|
-
*
|
|
146
|
-
* A trailing NULL delimiter from `stdout` is required and removed.
|
|
147
|
-
*/
|
|
148
|
-
text0(options?: NodeWithCwd<Omit<NodeSpawnOptions, "stdio">>): AsyncGenerator<string>;
|
|
149
|
-
/**
|
|
150
|
-
* Parse `stdout` into a generator of JSON values using a NULL delimiter.
|
|
151
|
-
*
|
|
152
|
-
* A trailing NULL delimiter from `stdout` is required and removed.
|
|
153
|
-
*/
|
|
154
|
-
json0(options?: NodeWithCwd<Omit<NodeSpawnOptions, "stdio">>): AsyncGenerator<any>;
|
|
155
|
-
/** Equivalent to:
|
|
156
|
-
*
|
|
157
|
-
* ```
|
|
158
|
-
* await this.print().spawnTransparently(…).success;
|
|
159
|
-
* ```
|
|
160
|
-
*/
|
|
161
|
-
shellOut(options?: NodeWithCwd<Omit<NodeSpawnOptions, "stdio">>): Promise<void>;
|
|
162
|
-
}
|
|
163
|
-
export declare function escapeArg(arg: string, isMainCommand: boolean, options: PrintOptions): string;
|
|
164
|
-
export {};
|
|
1
|
+
export { escapeArg, PrintableShellCommand, type PrintOptions, type StdinSource, type StreamPrintOptions, } from "./PrintableShellCommand";
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// src/
|
|
1
|
+
// src/PrintableShellCommand.ts
|
|
2
2
|
import assert from "node:assert";
|
|
3
3
|
import { createReadStream } from "node:fs";
|
|
4
4
|
import { stderr } from "node:process";
|
|
@@ -26,6 +26,12 @@ function isValidArgsEntryArray(entries) {
|
|
|
26
26
|
}
|
|
27
27
|
return true;
|
|
28
28
|
}
|
|
29
|
+
var ARGUMENT_LINE_WRAPPING_VALUES = [
|
|
30
|
+
"by-entry",
|
|
31
|
+
"nested-by-entry",
|
|
32
|
+
"by-argument",
|
|
33
|
+
"inline"
|
|
34
|
+
];
|
|
29
35
|
var SPECIAL_SHELL_CHARACTERS = /* @__PURE__ */ new Set([
|
|
30
36
|
" ",
|
|
31
37
|
'"',
|
|
@@ -165,8 +171,8 @@ var PrintableShellCommand = class {
|
|
|
165
171
|
}
|
|
166
172
|
}
|
|
167
173
|
let text = this.#mainIndentation(options) + escapeArg(this.commandName, true, options) + this.#separatorAfterCommand(options, serializedEntries.length) + serializedEntries.join(this.#intraEntrySeparator(options));
|
|
168
|
-
if (options?.
|
|
169
|
-
text = styleText(options.
|
|
174
|
+
if (options?.style) {
|
|
175
|
+
text = styleText(options.style, text);
|
|
170
176
|
}
|
|
171
177
|
return text;
|
|
172
178
|
}
|
|
@@ -176,13 +182,13 @@ var PrintableShellCommand = class {
|
|
|
176
182
|
* By default, this will be auto-styled (as bold gray) when `.isTTY` is true
|
|
177
183
|
* for the stream. `.isTTY` is populated for the {@link stderr} and
|
|
178
184
|
* {@link stdout} objects. Pass `"autoStyle": "never"` or an explicit
|
|
179
|
-
* `
|
|
185
|
+
* `style` to disable this.
|
|
180
186
|
*
|
|
181
187
|
*/
|
|
182
188
|
print(options) {
|
|
183
189
|
const stream = options?.stream ?? stderr;
|
|
184
190
|
const optionsCopy = { ...options };
|
|
185
|
-
optionsCopy.
|
|
191
|
+
optionsCopy.style ??= options?.autoStyle !== "never" && stream.isTTY === true ? TTY_AUTO_STYLE : void 0;
|
|
186
192
|
const writable = stream instanceof Writable ? stream : Writable.fromWeb(stream);
|
|
187
193
|
writable.write(this.getPrintableCommand(optionsCopy));
|
|
188
194
|
writable.write("\n");
|
|
@@ -210,6 +216,7 @@ var PrintableShellCommand = class {
|
|
|
210
216
|
spawn = ((options) => {
|
|
211
217
|
const { spawn } = process.getBuiltinModule("node:child_process");
|
|
212
218
|
const cwd = stringifyIfPath(options?.cwd);
|
|
219
|
+
options = { ...options };
|
|
213
220
|
if (this.#stdinSource) {
|
|
214
221
|
options ??= {};
|
|
215
222
|
if (typeof options.stdio === "undefined") {
|
|
@@ -218,7 +225,7 @@ var PrintableShellCommand = class {
|
|
|
218
225
|
if (typeof options.stdio === "string") {
|
|
219
226
|
options.stdio = new Array(3).fill(options.stdio);
|
|
220
227
|
}
|
|
221
|
-
options.stdio
|
|
228
|
+
options.stdio = ["pipe", ...options.stdio.slice(1)];
|
|
222
229
|
}
|
|
223
230
|
const subprocess = spawn(...this.toCommandWithFlatArgs(), {
|
|
224
231
|
...options,
|
|
@@ -405,11 +412,20 @@ var PrintableShellCommand = class {
|
|
|
405
412
|
/** Equivalent to:
|
|
406
413
|
*
|
|
407
414
|
* ```
|
|
408
|
-
* await this.print().spawnTransparently(…).success;
|
|
415
|
+
* await this.print(…).spawnTransparently(…).success;
|
|
409
416
|
* ```
|
|
410
417
|
*/
|
|
411
418
|
async shellOut(options) {
|
|
412
|
-
|
|
419
|
+
const { print: printOptions, ...spawnOptions } = options ?? {};
|
|
420
|
+
if (printOptions) {
|
|
421
|
+
if (typeof printOptions === "string") {
|
|
422
|
+
assert(ARGUMENT_LINE_WRAPPING_VALUES.includes(printOptions));
|
|
423
|
+
this.print({ argumentLineWrapping: printOptions });
|
|
424
|
+
} else {
|
|
425
|
+
this.print(printOptions);
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
await this.spawnTransparently(spawnOptions).success;
|
|
413
429
|
}
|
|
414
430
|
};
|
|
415
431
|
function escapeArg(arg, isMainCommand, options) {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
|
-
"sources": ["../../../src/
|
|
4
|
-
"sourcesContent": ["import assert from \"node:assert\";\nimport type {\n ChildProcessByStdio,\n ChildProcess as NodeChildProcess,\n SpawnOptions as NodeSpawnOptions,\n} from \"node:child_process\";\nimport { createReadStream } from \"node:fs\";\nimport { stderr } from \"node:process\";\nimport { Readable, Writable } from \"node:stream\";\nimport type { WriteStream } from \"node:tty\";\nimport { styleText } from \"node:util\";\nimport { Path, stringifyIfPath } from \"path-class\";\nimport type {\n NodeWithCwd,\n spawnType,\n WithStderrResponse,\n WithStdoutResponse,\n WithSuccess,\n} from \"./spawn\";\n\n// TODO: does this import work?\n/**\n * @import { stdout } from \"node:process\"\n */\n\nconst DEFAULT_MAIN_INDENTATION = \"\";\nconst DEFAULT_ARG_INDENTATION = \" \";\nconst DEFAULT_ARGUMENT_LINE_WRAPPING = \"by-entry\";\n\nconst INLINE_SEPARATOR = \" \";\nconst LINE_WRAP_LINE_END = \" \\\\\\n\";\n\ntype StyleTextFormat = Parameters<typeof styleText>[0];\n\nconst TTY_AUTO_STYLE: StyleTextFormat = [\"gray\", \"bold\"];\n\n// biome-ignore lint/suspicious/noExplicitAny: This is the correct type nere.\nfunction isString(s: any): s is string {\n return typeof s === \"string\";\n}\n\n// biome-ignore lint/suspicious/noExplicitAny: This is the correct type here.\nfunction isValidArgsEntryArray(entries: any[]): entries is SingleArgument[] {\n for (const entry of entries) {\n if (isString(entry)) {\n continue;\n }\n if (entry instanceof Path) {\n continue;\n }\n return false;\n }\n return true;\n}\n\n// TODO: allow `.toString()`ables?\ntype SingleArgument = string | Path;\ntype ArgsEntry = SingleArgument | SingleArgument[];\ntype Args = ArgsEntry[];\n\nexport interface PrintOptions {\n /** Defaults to \"\" */\n mainIndentation?: string;\n /** Defaults to \" \" */\n argIndentation?: string;\n /**\n * - `\"auto\"`: Quote only arguments that need it for safety. This tries to be\n * portable and safe across shells, but true safety and portability is hard\n * to guarantee.\n * - `\"extra-safe\"`: Quote all arguments, even ones that don't need it. This is\n * more likely to be safe under all circumstances.\n */\n quoting?: \"auto\" | \"extra-safe\";\n /** Line wrapping to use between arguments. Defaults to `\"by-entry\"`. */\n argumentLineWrapping?:\n | \"by-entry\"\n | \"nested-by-entry\"\n | \"by-argument\"\n | \"inline\";\n /** Include the first arg (or first arg group) on the same line as the command, regardless of the `argumentLineWrapping` setting. */\n skipLineWrapBeforeFirstArg?: true | false;\n /**\n * Style text using `node`'s {@link styleText | `styleText(\u2026)`}.\n *\n * Example usage:\n *\n * ```\n * new PrintableShellCommand(\"echo\", [\"hi\"]).print({\n * styleTextFormat: [\"green\", \"underline\"],\n * });\n * */\n styleTextFormat?: StyleTextFormat;\n}\n\nexport interface StreamPrintOptions extends PrintOptions {\n /**\n * Auto-style the text when:\n *\n * - the output stream is detected to be a TTY\n * - `styleTextFormat` is not specified.\n *\n * The current auto style is: `[\"gray\", \"bold\"]`\n */\n autoStyle?: \"tty\" | \"never\";\n // This would be a `WritableStream` (open web standard), but `WriteStream` allows us to query `.isTTY`.\n stream?: WriteStream | Writable;\n}\n\n// https://mywiki.wooledge.org/BashGuide/SpecialCharacters\nconst SPECIAL_SHELL_CHARACTERS = new Set([\n \" \",\n '\"',\n \"'\",\n \"`\",\n \"|\",\n \"$\",\n \"*\",\n \"?\",\n \">\",\n \"<\",\n \"(\",\n \")\",\n \"[\",\n \"]\",\n \"{\",\n \"}\",\n \"&\",\n \"\\\\\",\n \";\",\n \"#\",\n]);\n\n// https://mywiki.wooledge.org/BashGuide/SpecialCharacters\nconst SPECIAL_SHELL_CHARACTERS_FOR_MAIN_COMMAND =\n // biome-ignore lint/suspicious/noExplicitAny: Workaround to make this package easier to use in a project that otherwise only uses ES2022.)\n (SPECIAL_SHELL_CHARACTERS as unknown as any).union(new Set([\"=\"]));\n\n// TODO: Is there an idiomatic ways to check that all potential fields of\n// `StdinSource` satisfy `(typeof STDIN_SOURCE_KEYS)[number]`, without adding\n// extra indirection for type wrangling?\nconst STDIN_SOURCE_KEYS = [\"text\", \"json\", \"path\", \"stream\"] as const;\nexport type StdinSource =\n | { text: string }\n // biome-ignore lint/suspicious/noExplicitAny: `any` is the correct type for JSON data.\n | { json: any }\n | { path: string | Path }\n | { stream: Readable | ReadableStream };\n\nexport class PrintableShellCommand {\n #commandName: string | Path;\n constructor(\n commandName: string | Path,\n private args: Args = [],\n ) {\n if (!isString(commandName) && !(commandName instanceof Path)) {\n // biome-ignore lint/suspicious/noExplicitAny: We want to print this, no matter what it is.\n throw new Error(\"Command name is not a string:\", commandName as any);\n }\n this.#commandName = commandName;\n if (typeof args === \"undefined\") {\n return;\n }\n if (!Array.isArray(args)) {\n throw new Error(\"Command arguments are not an array\");\n }\n for (let i = 0; i < args.length; i++) {\n const argEntry = args[i];\n if (typeof argEntry === \"string\") {\n continue;\n }\n if (argEntry instanceof Path) {\n continue;\n }\n if (Array.isArray(argEntry) && isValidArgsEntryArray(argEntry)) {\n continue;\n }\n throw new Error(`Invalid arg entry at index: ${i}`);\n }\n }\n\n get commandName(): string {\n return stringifyIfPath(this.#commandName);\n }\n\n /**\n * For use with `node:child_process`\n *\n * Usage example:\n *\n * ```\n * import { PrintableShellCommand } from \"printable-shell-command\";\n * import { spawn } from \"node:child_process\";\n *\n * const command = new PrintableShellCommand( \u2026 );\n * const child_process = spawn(...command.toCommandWithFlatArgs()); // Note the `...`\n * ```\n *\n */\n public toCommandWithFlatArgs(): [string, string[]] {\n return [this.commandName, this.args.flat().map(stringifyIfPath)];\n }\n\n #mainIndentation(options: PrintOptions): string {\n return options?.mainIndentation ?? DEFAULT_MAIN_INDENTATION;\n }\n\n #argIndentation(options: PrintOptions): string {\n return (\n this.#mainIndentation(options) +\n (options?.argIndentation ?? DEFAULT_ARG_INDENTATION)\n );\n }\n\n #lineWrapSeparator(options: PrintOptions): string {\n return LINE_WRAP_LINE_END + this.#argIndentation(options);\n }\n\n #argPairSeparator(options: PrintOptions): string {\n switch (options?.argumentLineWrapping ?? DEFAULT_ARGUMENT_LINE_WRAPPING) {\n case \"by-entry\": {\n return INLINE_SEPARATOR;\n }\n case \"nested-by-entry\": {\n return this.#lineWrapSeparator(options) + this.#argIndentation(options);\n }\n case \"by-argument\": {\n return this.#lineWrapSeparator(options);\n }\n case \"inline\": {\n return INLINE_SEPARATOR;\n }\n default:\n throw new Error(\"Invalid argument line wrapping argument.\");\n }\n }\n\n #intraEntrySeparator(options: PrintOptions): string {\n switch (options?.argumentLineWrapping ?? DEFAULT_ARGUMENT_LINE_WRAPPING) {\n case \"by-entry\":\n case \"nested-by-entry\":\n case \"by-argument\": {\n return LINE_WRAP_LINE_END + this.#argIndentation(options);\n }\n case \"inline\": {\n return INLINE_SEPARATOR;\n }\n default:\n throw new Error(\"Invalid argument line wrapping argument.\");\n }\n }\n\n #separatorAfterCommand(\n options: PrintOptions,\n numFollowingEntries: number,\n ): string {\n if (numFollowingEntries === 0) {\n return \"\";\n }\n if (options.skipLineWrapBeforeFirstArg ?? false) {\n return INLINE_SEPARATOR;\n }\n return this.#intraEntrySeparator(options);\n }\n\n public getPrintableCommand(options?: PrintOptions): string {\n // TODO: Why in the world does TypeScript not give the `options` arg the type of `PrintOptions | undefined`???\n options ??= {};\n const serializedEntries: string[] = [];\n\n for (let i = 0; i < this.args.length; i++) {\n const argsEntry = stringifyIfPath(this.args[i]);\n\n if (isString(argsEntry)) {\n serializedEntries.push(escapeArg(argsEntry, false, options));\n } else {\n serializedEntries.push(\n argsEntry\n .map((part) => escapeArg(stringifyIfPath(part), false, options))\n .join(this.#argPairSeparator(options)),\n );\n }\n }\n\n let text =\n this.#mainIndentation(options) +\n escapeArg(this.commandName, true, options) +\n this.#separatorAfterCommand(options, serializedEntries.length) +\n serializedEntries.join(this.#intraEntrySeparator(options));\n if (options?.styleTextFormat) {\n text = styleText(options.styleTextFormat, text);\n }\n return text;\n }\n\n /**\n * Print the shell command to {@link stderr} (default) or a specified stream.\n *\n * By default, this will be auto-styled (as bold gray) when `.isTTY` is true\n * for the stream. `.isTTY` is populated for the {@link stderr} and\n * {@link stdout} objects. Pass `\"autoStyle\": \"never\"` or an explicit\n * `styleTextFormat` to disable this.\n *\n */\n public print(options?: StreamPrintOptions): PrintableShellCommand {\n const stream = options?.stream ?? stderr;\n // Note: we only need to modify top-level fields, so `structuredClone(\u2026)`\n // would be overkill and can only cause performance issues.\n const optionsCopy = { ...options };\n optionsCopy.styleTextFormat ??=\n options?.autoStyle !== \"never\" &&\n (stream as { isTTY?: boolean }).isTTY === true\n ? TTY_AUTO_STYLE\n : undefined;\n const writable =\n stream instanceof Writable ? stream : Writable.fromWeb(stream);\n writable.write(this.getPrintableCommand(optionsCopy));\n writable.write(\"\\n\");\n return this;\n }\n\n #stdinSource: StdinSource | undefined;\n /**\n * Send data to `stdin` of the subprocess.\n *\n * Note that this will overwrite:\n *\n * - Any previous value set using {@link PrintableShellCommand.stdin | `.stdin(\u2026)`}.\n * - Any value set for `stdin` using the `\"stdio\"` field of {@link PrintableShellCommand.spawn | `.spawn(\u2026)`}.\n */\n stdin(source: StdinSource): PrintableShellCommand {\n const [key, ...moreKeys] = Object.keys(source);\n assert.equal(moreKeys.length, 0);\n // TODO: validate values?\n assert((STDIN_SOURCE_KEYS as unknown as string[]).includes(key));\n\n this.#stdinSource = source;\n return this;\n }\n\n /**\n * The returned child process includes a `.success` `Promise` field, per https://github.com/oven-sh/bun/issues/8313\n */\n public spawn: typeof spawnType = ((\n options?: Parameters<typeof spawnType>[0],\n ) => {\n const { spawn } = process.getBuiltinModule(\"node:child_process\");\n const cwd = stringifyIfPath(options?.cwd);\n if (this.#stdinSource) {\n options ??= {};\n if (typeof options.stdio === \"undefined\") {\n options.stdio = \"pipe\";\n }\n if (typeof options.stdio === \"string\") {\n options.stdio = new Array(3).fill(options.stdio);\n }\n options.stdio[0] = \"pipe\";\n }\n // biome-ignore lint/suspicious/noTsIgnore: We don't want linting to depend on *broken* type checking.\n // @ts-ignore: The TypeScript checker has trouble reconciling the optional (i.e. potentially `undefined`) `options` with the third argument.\n const subprocess = spawn(...this.toCommandWithFlatArgs(), {\n ...(options as object),\n cwd,\n }) as NodeChildProcess & {\n success: Promise<void>;\n };\n // TODO: define properties on prototypes instead.\n Object.defineProperty(subprocess, \"success\", {\n get() {\n return new Promise<void>((resolve, reject) => {\n this.addListener(\n \"exit\",\n (exitCode: number /* we only use the first arg */) => {\n if (exitCode === 0) {\n resolve();\n } else {\n reject(`Command failed with non-zero exit code: ${exitCode}`);\n }\n },\n );\n // biome-ignore lint/suspicious/noExplicitAny: We don't have the type available.\n this.addListener(\"error\", (err: any) => {\n reject(err);\n });\n });\n },\n enumerable: false,\n });\n if (subprocess.stdout) {\n // TODO: dedupe\n const s = subprocess as unknown as Readable &\n WithStdoutResponse &\n WithSuccess;\n s.stdout.response = () =>\n new Response(Readable.from(this.#generator(s.stdout, s.success)));\n s.stdout.text = () => s.stdout.response().text();\n const thisCached = this; // TODO: make this type-check using `.bind(\u2026)`\n s.stdout.text0 = async function* () {\n yield* thisCached.#split0(thisCached.#generator(s.stdout, s.success));\n };\n s.stdout.json = <T>() => s.stdout.response().json() as Promise<T>;\n }\n if (subprocess.stderr) {\n // TODO: dedupe\n const s = subprocess as unknown as Readable &\n WithStderrResponse &\n WithSuccess;\n s.stderr.response = () =>\n new Response(Readable.from(this.#generator(s.stderr, s.success)));\n s.stderr.text = () => s.stderr.response().text();\n const thisCached = this; // TODO: make this type-check using `.bind(\u2026)`\n s.stderr.text0 = async function* () {\n yield* thisCached.#split0(thisCached.#generator(s.stderr, s.success));\n };\n s.stderr.json = <T>() => s.stderr.response().json() as Promise<T>;\n }\n if (this.#stdinSource) {\n const { stdin } = subprocess;\n assert(stdin);\n if (\"text\" in this.#stdinSource) {\n stdin.write(this.#stdinSource.text);\n stdin.end();\n } else if (\"json\" in this.#stdinSource) {\n stdin.write(JSON.stringify(this.#stdinSource.json));\n stdin.end();\n } else if (\"path\" in this.#stdinSource) {\n createReadStream(stringifyIfPath(this.#stdinSource.path)).pipe(stdin);\n } else if (\"stream\" in this.#stdinSource) {\n const stream = (() => {\n const { stream } = this.#stdinSource;\n return stream instanceof Readable ? stream : Readable.fromWeb(stream);\n })();\n stream.pipe(stdin);\n } else {\n throw new Error(\"Invalid `.stdin(\u2026)` source?\");\n }\n }\n return subprocess;\n // biome-ignore lint/suspicious/noExplicitAny: Type wrangling\n }) as any;\n\n /** A wrapper for `.spawn(\u2026)` that sets stdio to `\"inherit\"` (common for\n * invoking commands from scripts whose output and interaction should be\n * surfaced to the user).\n *\n * If there is no other interaction with the shell from the calling process,\n * then it acts \"transparent\" and allows user to interact with the subprocess\n * in its stead.\n */\n public spawnTransparently(\n options?: NodeWithCwd<Omit<NodeSpawnOptions, \"stdio\">>,\n ): ChildProcessByStdio<null, null, null> & WithSuccess {\n if (options && \"stdio\" in options) {\n throw new Error(\"Unexpected `stdio` field.\");\n }\n\n // biome-ignore lint/suspicious/noExplicitAny: Type wrangling.\n return this.spawn({ ...options, stdio: \"inherit\" }) as any;\n }\n\n /**\n * A wrapper for {@link PrintableShellCommand.spawn | `.spawn(\u2026)`} that:\n *\n * - sets `detached` to `true`,\n * - sets stdio to `\"inherit\"`,\n * - calls `.unref()`, and\n * - does not wait for the process to exit.\n *\n * This is similar to starting a command in the background and disowning it (in a shell).\n *\n */\n public spawnDetached(\n options?: NodeWithCwd<Omit<Omit<NodeSpawnOptions, \"stdio\">, \"detached\">>,\n ): void {\n if (options) {\n for (const field of [\"stdio\", \"detached\"]) {\n if (field in options) {\n throw new Error(`Unexpected \\`${field}\\` field.`);\n }\n }\n }\n const childProcess = this.spawn({\n stdio: \"ignore\",\n ...options,\n detached: true,\n });\n childProcess.unref();\n }\n\n #generator(\n readable: Readable,\n successPromise: Promise<void>,\n ): AsyncGenerator<string> {\n // TODO: we'd make this a `ReadableStream`, but `ReadableStream.from(\u2026)` is\n // not implemented in `bun`: https://github.com/oven-sh/bun/issues/3700\n return (async function* () {\n for await (const chunk of readable) {\n yield chunk;\n }\n await successPromise;\n })();\n }\n\n #stdoutSpawnGenerator(\n options?: NodeWithCwd<Omit<NodeSpawnOptions, \"stdio\">>,\n ): AsyncGenerator<string> {\n if (options && \"stdio\" in options) {\n throw new Error(\"Unexpected `stdio` field.\");\n }\n const subprocess = this.spawn({\n ...options,\n stdio: [\"ignore\", \"pipe\", \"inherit\"],\n });\n return this.#generator(subprocess.stdout, subprocess.success);\n }\n\n public stdout(\n options?: NodeWithCwd<Omit<NodeSpawnOptions, \"stdio\">>,\n ): Response {\n // TODO: Use `ReadableStream.from(\u2026)` once `bun` implements it: https://github.com/oven-sh/bun/pull/21269\n return new Response(Readable.from(this.#stdoutSpawnGenerator(options)));\n }\n\n async *#split0(generator: AsyncGenerator<string>): AsyncGenerator<string> {\n let pending = \"\";\n for await (const chunk of generator) {\n pending += chunk;\n const newChunks = pending.split(\"\\x00\");\n pending = newChunks.splice(-1)[0];\n yield* newChunks;\n }\n if (pending !== \"\") {\n throw new Error(\n \"Missing a trailing NUL character at the end of a NUL-delimited stream.\",\n );\n }\n }\n\n /**\n * Convenience function for:\n *\n * .stdout(options).text()\n *\n * This can make some simple invocations easier to read and/or fit on a single line.\n */\n public text(\n options?: NodeWithCwd<Omit<NodeSpawnOptions, \"stdio\">>,\n ): Promise<string> {\n return this.stdout(options).text();\n }\n\n /**\n * Convenience function for:\n *\n * .stdout(options).json()\n *\n * This can make some simple invocations easier to read and/or fit on a single line.\n */\n public json<T>(\n options?: NodeWithCwd<Omit<NodeSpawnOptions, \"stdio\">>,\n ): Promise<T> {\n return this.stdout(options).json() as Promise<T>;\n }\n\n /**\n * Parse `stdout` into a generator of string values using a NULL delimiter.\n *\n * A trailing NULL delimiter from `stdout` is required and removed.\n */\n public async *text0(\n options?: NodeWithCwd<Omit<NodeSpawnOptions, \"stdio\">>,\n ): AsyncGenerator<string> {\n yield* this.#split0(this.#stdoutSpawnGenerator(options));\n }\n\n /**\n * Parse `stdout` into a generator of JSON values using a NULL delimiter.\n *\n * A trailing NULL delimiter from `stdout` is required and removed.\n */\n public async *json0(\n options?: NodeWithCwd<Omit<NodeSpawnOptions, \"stdio\">>,\n ): // biome-ignore lint/suspicious/noExplicitAny: `any` is the correct type for JSON\n AsyncGenerator<any> {\n for await (const part of this.#split0(\n this.#stdoutSpawnGenerator(options),\n )) {\n yield JSON.parse(part);\n }\n }\n\n /** Equivalent to:\n *\n * ```\n * await this.print().spawnTransparently(\u2026).success;\n * ```\n */\n public async shellOut(\n options?: NodeWithCwd<Omit<NodeSpawnOptions, \"stdio\">>,\n ): Promise<void> {\n await this.print().spawnTransparently(options).success;\n }\n}\n\nexport function escapeArg(\n arg: string,\n isMainCommand: boolean,\n options: PrintOptions,\n): string {\n const argCharacters = new Set(arg);\n const specialShellCharacters = isMainCommand\n ? SPECIAL_SHELL_CHARACTERS_FOR_MAIN_COMMAND\n : SPECIAL_SHELL_CHARACTERS;\n if (\n options?.quoting === \"extra-safe\" ||\n // biome-ignore lint/suspicious/noExplicitAny: Workaround to make this package easier to use in a project that otherwise only uses ES2022.)\n (argCharacters as unknown as any).intersection(specialShellCharacters)\n .size > 0\n ) {\n // Use single quote to reduce the need to escape (and therefore reduce the chance for bugs/security issues).\n const escaped = arg.replaceAll(\"\\\\\", \"\\\\\\\\\").replaceAll(\"'\", \"\\\\'\");\n return `'${escaped}'`;\n }\n return arg;\n}\n"],
|
|
5
|
-
"mappings": ";AAAA,OAAO,YAAY;AAMnB,SAAS,wBAAwB;AACjC,SAAS,cAAc;AACvB,SAAS,UAAU,gBAAgB;AAEnC,SAAS,iBAAiB;AAC1B,SAAS,MAAM,uBAAuB;AActC,IAAM,2BAA2B;AACjC,IAAM,0BAA0B;AAChC,IAAM,iCAAiC;AAEvC,IAAM,mBAAmB;AACzB,IAAM,qBAAqB;AAI3B,IAAM,iBAAkC,CAAC,QAAQ,MAAM;AAGvD,SAAS,SAAS,GAAqB;AACrC,SAAO,OAAO,MAAM;AACtB;AAGA,SAAS,sBAAsB,SAA6C;AAC1E,aAAW,SAAS,SAAS;AAC3B,QAAI,SAAS,KAAK,GAAG;AACnB;AAAA,IACF;AACA,QAAI,iBAAiB,MAAM;AACzB;AAAA,IACF;AACA,WAAO;AAAA,EACT;AACA,SAAO;AACT;
|
|
3
|
+
"sources": ["../../../src/PrintableShellCommand.ts"],
|
|
4
|
+
"sourcesContent": ["import assert from \"node:assert\";\nimport type {\n ChildProcessByStdio,\n ChildProcess as NodeChildProcess,\n SpawnOptions as NodeSpawnOptions,\n} from \"node:child_process\";\nimport { createReadStream } from \"node:fs\";\nimport { stderr } from \"node:process\";\nimport { Readable, Writable } from \"node:stream\";\nimport type { WriteStream } from \"node:tty\";\nimport { styleText } from \"node:util\";\nimport { Path, stringifyIfPath } from \"path-class\";\nimport type {\n NodeWithCwd,\n spawnType,\n WithStderrResponse,\n WithStdoutResponse,\n WithSuccess,\n} from \"./spawn\";\n\n// TODO: does this import work?\n/**\n * @import { stdout } from \"node:process\"\n */\n\nconst DEFAULT_MAIN_INDENTATION = \"\";\nconst DEFAULT_ARG_INDENTATION = \" \";\nconst DEFAULT_ARGUMENT_LINE_WRAPPING = \"by-entry\";\n\nconst INLINE_SEPARATOR = \" \";\nconst LINE_WRAP_LINE_END = \" \\\\\\n\";\n\ntype StyleTextFormat = Parameters<typeof styleText>[0];\n\nconst TTY_AUTO_STYLE: StyleTextFormat = [\"gray\", \"bold\"];\n\n// biome-ignore lint/suspicious/noExplicitAny: This is the correct type nere.\nfunction isString(s: any): s is string {\n return typeof s === \"string\";\n}\n\n// biome-ignore lint/suspicious/noExplicitAny: This is the correct type here.\nfunction isValidArgsEntryArray(entries: any[]): entries is SingleArgument[] {\n for (const entry of entries) {\n if (isString(entry)) {\n continue;\n }\n if (entry instanceof Path) {\n continue;\n }\n return false;\n }\n return true;\n}\n\n// TODO: allow `.toString()`ables?\ntype SingleArgument = string | Path;\ntype ArgsEntry = SingleArgument | SingleArgument[];\ntype Args = ArgsEntry[];\n\nconst ARGUMENT_LINE_WRAPPING_VALUES = [\n \"by-entry\",\n \"nested-by-entry\",\n \"by-argument\",\n \"inline\",\n] as const;\ntype ArgumentLineWrapping = (typeof ARGUMENT_LINE_WRAPPING_VALUES)[number];\n\nexport interface PrintOptions {\n /** Defaults to \"\" */\n mainIndentation?: string;\n /** Defaults to \" \" */\n argIndentation?: string;\n /**\n * - `\"auto\"`: Quote only arguments that need it for safety. This tries to be\n * portable and safe across shells, but true safety and portability is hard\n * to guarantee.\n * - `\"extra-safe\"`: Quote all arguments, even ones that don't need it. This is\n * more likely to be safe under all circumstances.\n */\n quoting?: \"auto\" | \"extra-safe\";\n /** Line wrapping to use between arguments. Defaults to `\"by-entry\"`. */\n argumentLineWrapping?: ArgumentLineWrapping;\n /** Include the first arg (or first arg group) on the same line as the command, regardless of the `argumentLineWrapping` setting. */\n skipLineWrapBeforeFirstArg?: true | false;\n /**\n * Style text using `node`'s {@link styleText | `styleText(\u2026)`}.\n *\n * Example usage:\n *\n * ```\n * new PrintableShellCommand(\"echo\", [\"hi\"]).print({\n * style: [\"green\", \"underline\"],\n * });\n * */\n style?: StyleTextFormat;\n}\n\nexport interface StreamPrintOptions extends PrintOptions {\n /**\n * Auto-style the text when:\n *\n * - the output stream is detected to be a TTY\n * - `styleTextFormat` is not specified.\n *\n * The current auto style is: `[\"gray\", \"bold\"]`\n */\n autoStyle?: \"tty\" | \"never\";\n // This would be a `WritableStream` (open web standard), but `WriteStream` allows us to query `.isTTY`.\n stream?: WriteStream | Writable;\n}\n\n// https://mywiki.wooledge.org/BashGuide/SpecialCharacters\nconst SPECIAL_SHELL_CHARACTERS = new Set([\n \" \",\n '\"',\n \"'\",\n \"`\",\n \"|\",\n \"$\",\n \"*\",\n \"?\",\n \">\",\n \"<\",\n \"(\",\n \")\",\n \"[\",\n \"]\",\n \"{\",\n \"}\",\n \"&\",\n \"\\\\\",\n \";\",\n \"#\",\n]);\n\n// https://mywiki.wooledge.org/BashGuide/SpecialCharacters\nconst SPECIAL_SHELL_CHARACTERS_FOR_MAIN_COMMAND =\n // biome-ignore lint/suspicious/noExplicitAny: Workaround to make this package easier to use in a project that otherwise only uses ES2022.)\n (SPECIAL_SHELL_CHARACTERS as unknown as any).union(new Set([\"=\"]));\n\n// TODO: Is there an idiomatic ways to check that all potential fields of\n// `StdinSource` satisfy `(typeof STDIN_SOURCE_KEYS)[number]`, without adding\n// extra indirection for type wrangling?\nconst STDIN_SOURCE_KEYS = [\"text\", \"json\", \"path\", \"stream\"] as const;\nexport type StdinSource =\n | { text: string }\n // biome-ignore lint/suspicious/noExplicitAny: `any` is the correct type for JSON data.\n | { json: any }\n | { path: string | Path }\n | { stream: Readable | ReadableStream };\n\nexport class PrintableShellCommand {\n #commandName: string | Path;\n constructor(\n commandName: string | Path,\n private args: Args = [],\n ) {\n if (!isString(commandName) && !(commandName instanceof Path)) {\n // biome-ignore lint/suspicious/noExplicitAny: We want to print this, no matter what it is.\n throw new Error(\"Command name is not a string:\", commandName as any);\n }\n this.#commandName = commandName;\n if (typeof args === \"undefined\") {\n return;\n }\n if (!Array.isArray(args)) {\n throw new Error(\"Command arguments are not an array\");\n }\n for (let i = 0; i < args.length; i++) {\n const argEntry = args[i];\n if (typeof argEntry === \"string\") {\n continue;\n }\n if (argEntry instanceof Path) {\n continue;\n }\n if (Array.isArray(argEntry) && isValidArgsEntryArray(argEntry)) {\n continue;\n }\n throw new Error(`Invalid arg entry at index: ${i}`);\n }\n }\n\n get commandName(): string {\n return stringifyIfPath(this.#commandName);\n }\n\n /**\n * For use with `node:child_process`\n *\n * Usage example:\n *\n * ```\n * import { PrintableShellCommand } from \"printable-shell-command\";\n * import { spawn } from \"node:child_process\";\n *\n * const command = new PrintableShellCommand( \u2026 );\n * const child_process = spawn(...command.toCommandWithFlatArgs()); // Note the `...`\n * ```\n *\n */\n public toCommandWithFlatArgs(): [string, string[]] {\n return [this.commandName, this.args.flat().map(stringifyIfPath)];\n }\n\n #mainIndentation(options: PrintOptions): string {\n return options?.mainIndentation ?? DEFAULT_MAIN_INDENTATION;\n }\n\n #argIndentation(options: PrintOptions): string {\n return (\n this.#mainIndentation(options) +\n (options?.argIndentation ?? DEFAULT_ARG_INDENTATION)\n );\n }\n\n #lineWrapSeparator(options: PrintOptions): string {\n return LINE_WRAP_LINE_END + this.#argIndentation(options);\n }\n\n #argPairSeparator(options: PrintOptions): string {\n switch (options?.argumentLineWrapping ?? DEFAULT_ARGUMENT_LINE_WRAPPING) {\n case \"by-entry\": {\n return INLINE_SEPARATOR;\n }\n case \"nested-by-entry\": {\n return this.#lineWrapSeparator(options) + this.#argIndentation(options);\n }\n case \"by-argument\": {\n return this.#lineWrapSeparator(options);\n }\n case \"inline\": {\n return INLINE_SEPARATOR;\n }\n default:\n throw new Error(\"Invalid argument line wrapping argument.\");\n }\n }\n\n #intraEntrySeparator(options: PrintOptions): string {\n switch (options?.argumentLineWrapping ?? DEFAULT_ARGUMENT_LINE_WRAPPING) {\n case \"by-entry\":\n case \"nested-by-entry\":\n case \"by-argument\": {\n return LINE_WRAP_LINE_END + this.#argIndentation(options);\n }\n case \"inline\": {\n return INLINE_SEPARATOR;\n }\n default:\n throw new Error(\"Invalid argument line wrapping argument.\");\n }\n }\n\n #separatorAfterCommand(\n options: PrintOptions,\n numFollowingEntries: number,\n ): string {\n if (numFollowingEntries === 0) {\n return \"\";\n }\n if (options.skipLineWrapBeforeFirstArg ?? false) {\n return INLINE_SEPARATOR;\n }\n return this.#intraEntrySeparator(options);\n }\n\n public getPrintableCommand(options?: PrintOptions): string {\n // TODO: Why in the world does TypeScript not give the `options` arg the type of `PrintOptions | undefined`???\n options ??= {};\n const serializedEntries: string[] = [];\n\n for (let i = 0; i < this.args.length; i++) {\n const argsEntry = stringifyIfPath(this.args[i]);\n\n if (isString(argsEntry)) {\n serializedEntries.push(escapeArg(argsEntry, false, options));\n } else {\n serializedEntries.push(\n argsEntry\n .map((part) => escapeArg(stringifyIfPath(part), false, options))\n .join(this.#argPairSeparator(options)),\n );\n }\n }\n\n let text =\n this.#mainIndentation(options) +\n escapeArg(this.commandName, true, options) +\n this.#separatorAfterCommand(options, serializedEntries.length) +\n serializedEntries.join(this.#intraEntrySeparator(options));\n if (options?.style) {\n text = styleText(options.style, text);\n }\n return text;\n }\n\n /**\n * Print the shell command to {@link stderr} (default) or a specified stream.\n *\n * By default, this will be auto-styled (as bold gray) when `.isTTY` is true\n * for the stream. `.isTTY` is populated for the {@link stderr} and\n * {@link stdout} objects. Pass `\"autoStyle\": \"never\"` or an explicit\n * `style` to disable this.\n *\n */\n public print(options?: StreamPrintOptions): PrintableShellCommand {\n const stream = options?.stream ?? stderr;\n // Note: we only need to modify top-level fields, so `structuredClone(\u2026)`\n // would be overkill and can only cause performance issues.\n const optionsCopy = { ...options };\n optionsCopy.style ??=\n options?.autoStyle !== \"never\" &&\n (stream as { isTTY?: boolean }).isTTY === true\n ? TTY_AUTO_STYLE\n : undefined;\n const writable =\n stream instanceof Writable ? stream : Writable.fromWeb(stream);\n writable.write(this.getPrintableCommand(optionsCopy));\n writable.write(\"\\n\");\n return this;\n }\n\n #stdinSource: StdinSource | undefined;\n /**\n * Send data to `stdin` of the subprocess.\n *\n * Note that this will overwrite:\n *\n * - Any previous value set using {@link PrintableShellCommand.stdin | `.stdin(\u2026)`}.\n * - Any value set for `stdin` using the `\"stdio\"` field of {@link PrintableShellCommand.spawn | `.spawn(\u2026)`}.\n */\n stdin(source: StdinSource): PrintableShellCommand {\n const [key, ...moreKeys] = Object.keys(source);\n assert.equal(moreKeys.length, 0);\n // TODO: validate values?\n assert((STDIN_SOURCE_KEYS as unknown as string[]).includes(key));\n\n this.#stdinSource = source;\n return this;\n }\n\n /**\n * The returned child process includes a `.success` `Promise` field, per https://github.com/oven-sh/bun/issues/8313\n */\n public spawn: typeof spawnType = ((\n options?: Parameters<typeof spawnType>[0],\n ) => {\n const { spawn } = process.getBuiltinModule(\"node:child_process\");\n const cwd = stringifyIfPath(options?.cwd);\n options = { ...options };\n if (this.#stdinSource) {\n options ??= {};\n if (typeof options.stdio === \"undefined\") {\n options.stdio = \"pipe\";\n }\n if (typeof options.stdio === \"string\") {\n options.stdio = new Array(3).fill(options.stdio);\n }\n options.stdio = [\"pipe\", ...options.stdio.slice(1)];\n }\n // biome-ignore lint/suspicious/noTsIgnore: We don't want linting to depend on *broken* type checking.\n // @ts-ignore: The TypeScript checker has trouble reconciling the optional (i.e. potentially `undefined`) `options` with the third argument.\n const subprocess = spawn(...this.toCommandWithFlatArgs(), {\n ...(options as object),\n cwd,\n }) as NodeChildProcess & {\n success: Promise<void>;\n };\n // TODO: define properties on prototypes instead.\n Object.defineProperty(subprocess, \"success\", {\n get() {\n return new Promise<void>((resolve, reject) => {\n this.addListener(\n \"exit\",\n (exitCode: number /* we only use the first arg */) => {\n if (exitCode === 0) {\n resolve();\n } else {\n reject(`Command failed with non-zero exit code: ${exitCode}`);\n }\n },\n );\n // biome-ignore lint/suspicious/noExplicitAny: We don't have the type available.\n this.addListener(\"error\", (err: any) => {\n reject(err);\n });\n });\n },\n enumerable: false,\n });\n if (subprocess.stdout) {\n // TODO: dedupe\n const s = subprocess as unknown as Readable &\n WithStdoutResponse &\n WithSuccess;\n s.stdout.response = () =>\n new Response(Readable.from(this.#generator(s.stdout, s.success)));\n s.stdout.text = () => s.stdout.response().text();\n const thisCached = this; // TODO: make this type-check using `.bind(\u2026)`\n s.stdout.text0 = async function* () {\n yield* thisCached.#split0(thisCached.#generator(s.stdout, s.success));\n };\n s.stdout.json = <T>() => s.stdout.response().json() as Promise<T>;\n }\n if (subprocess.stderr) {\n // TODO: dedupe\n const s = subprocess as unknown as Readable &\n WithStderrResponse &\n WithSuccess;\n s.stderr.response = () =>\n new Response(Readable.from(this.#generator(s.stderr, s.success)));\n s.stderr.text = () => s.stderr.response().text();\n const thisCached = this; // TODO: make this type-check using `.bind(\u2026)`\n s.stderr.text0 = async function* () {\n yield* thisCached.#split0(thisCached.#generator(s.stderr, s.success));\n };\n s.stderr.json = <T>() => s.stderr.response().json() as Promise<T>;\n }\n if (this.#stdinSource) {\n const { stdin } = subprocess;\n assert(stdin);\n if (\"text\" in this.#stdinSource) {\n stdin.write(this.#stdinSource.text);\n stdin.end();\n } else if (\"json\" in this.#stdinSource) {\n stdin.write(JSON.stringify(this.#stdinSource.json));\n stdin.end();\n } else if (\"path\" in this.#stdinSource) {\n createReadStream(stringifyIfPath(this.#stdinSource.path)).pipe(stdin);\n } else if (\"stream\" in this.#stdinSource) {\n const stream = (() => {\n const { stream } = this.#stdinSource;\n return stream instanceof Readable ? stream : Readable.fromWeb(stream);\n })();\n stream.pipe(stdin);\n } else {\n throw new Error(\"Invalid `.stdin(\u2026)` source?\");\n }\n }\n return subprocess;\n // biome-ignore lint/suspicious/noExplicitAny: Type wrangling\n }) as any;\n\n /** A wrapper for `.spawn(\u2026)` that sets stdio to `\"inherit\"` (common for\n * invoking commands from scripts whose output and interaction should be\n * surfaced to the user).\n *\n * If there is no other interaction with the shell from the calling process,\n * then it acts \"transparent\" and allows user to interact with the subprocess\n * in its stead.\n */\n public spawnTransparently(\n options?: NodeWithCwd<Omit<NodeSpawnOptions, \"stdio\">>,\n ): ChildProcessByStdio<null, null, null> & WithSuccess {\n if (options && \"stdio\" in options) {\n throw new Error(\"Unexpected `stdio` field.\");\n }\n\n // biome-ignore lint/suspicious/noExplicitAny: Type wrangling.\n return this.spawn({ ...options, stdio: \"inherit\" }) as any;\n }\n\n /**\n * A wrapper for {@link PrintableShellCommand.spawn | `.spawn(\u2026)`} that:\n *\n * - sets `detached` to `true`,\n * - sets stdio to `\"inherit\"`,\n * - calls `.unref()`, and\n * - does not wait for the process to exit.\n *\n * This is similar to starting a command in the background and disowning it (in a shell).\n *\n */\n public spawnDetached(\n options?: NodeWithCwd<Omit<Omit<NodeSpawnOptions, \"stdio\">, \"detached\">>,\n ): void {\n if (options) {\n for (const field of [\"stdio\", \"detached\"]) {\n if (field in options) {\n throw new Error(`Unexpected \\`${field}\\` field.`);\n }\n }\n }\n const childProcess = this.spawn({\n stdio: \"ignore\",\n ...options,\n detached: true,\n });\n childProcess.unref();\n }\n\n #generator(\n readable: Readable,\n successPromise: Promise<void>,\n ): AsyncGenerator<string> {\n // TODO: we'd make this a `ReadableStream`, but `ReadableStream.from(\u2026)` is\n // not implemented in `bun`: https://github.com/oven-sh/bun/issues/3700\n return (async function* () {\n for await (const chunk of readable) {\n yield chunk;\n }\n await successPromise;\n })();\n }\n\n #stdoutSpawnGenerator(\n options?: NodeWithCwd<Omit<NodeSpawnOptions, \"stdio\">>,\n ): AsyncGenerator<string> {\n if (options && \"stdio\" in options) {\n throw new Error(\"Unexpected `stdio` field.\");\n }\n const subprocess = this.spawn({\n ...options,\n stdio: [\"ignore\", \"pipe\", \"inherit\"],\n });\n return this.#generator(subprocess.stdout, subprocess.success);\n }\n\n public stdout(\n options?: NodeWithCwd<Omit<NodeSpawnOptions, \"stdio\">>,\n ): Response {\n // TODO: Use `ReadableStream.from(\u2026)` once `bun` implements it: https://github.com/oven-sh/bun/pull/21269\n return new Response(Readable.from(this.#stdoutSpawnGenerator(options)));\n }\n\n async *#split0(generator: AsyncGenerator<string>): AsyncGenerator<string> {\n let pending = \"\";\n for await (const chunk of generator) {\n pending += chunk;\n const newChunks = pending.split(\"\\x00\");\n pending = newChunks.splice(-1)[0];\n yield* newChunks;\n }\n if (pending !== \"\") {\n throw new Error(\n \"Missing a trailing NUL character at the end of a NUL-delimited stream.\",\n );\n }\n }\n\n /**\n * Convenience function for:\n *\n * .stdout(options).text()\n *\n * This can make some simple invocations easier to read and/or fit on a single line.\n */\n public text(\n options?: NodeWithCwd<Omit<NodeSpawnOptions, \"stdio\">>,\n ): Promise<string> {\n return this.stdout(options).text();\n }\n\n /**\n * Convenience function for:\n *\n * .stdout(options).json()\n *\n * This can make some simple invocations easier to read and/or fit on a single line.\n */\n public json<T>(\n options?: NodeWithCwd<Omit<NodeSpawnOptions, \"stdio\">>,\n ): Promise<T> {\n return this.stdout(options).json() as Promise<T>;\n }\n\n /**\n * Parse `stdout` into a generator of string values using a NULL delimiter.\n *\n * A trailing NULL delimiter from `stdout` is required and removed.\n */\n public async *text0(\n options?: NodeWithCwd<Omit<NodeSpawnOptions, \"stdio\">>,\n ): AsyncGenerator<string> {\n yield* this.#split0(this.#stdoutSpawnGenerator(options));\n }\n\n /**\n * Parse `stdout` into a generator of JSON values using a NULL delimiter.\n *\n * A trailing NULL delimiter from `stdout` is required and removed.\n */\n public async *json0(\n options?: NodeWithCwd<Omit<NodeSpawnOptions, \"stdio\">>,\n ): // biome-ignore lint/suspicious/noExplicitAny: `any` is the correct type for JSON\n AsyncGenerator<any> {\n for await (const part of this.#split0(\n this.#stdoutSpawnGenerator(options),\n )) {\n yield JSON.parse(part);\n }\n }\n\n /** Equivalent to:\n *\n * ```\n * await this.print(\u2026).spawnTransparently(\u2026).success;\n * ```\n */\n public async shellOut(\n options?: NodeWithCwd<Omit<NodeSpawnOptions, \"stdio\">> & {\n print: StreamPrintOptions | ArgumentLineWrapping | false;\n },\n ): Promise<void> {\n const { print: printOptions, ...spawnOptions } = options ?? {};\n if (printOptions) {\n if (typeof printOptions === \"string\") {\n assert(ARGUMENT_LINE_WRAPPING_VALUES.includes(printOptions));\n this.print({ argumentLineWrapping: printOptions });\n } else {\n this.print(printOptions);\n }\n }\n await this.spawnTransparently(spawnOptions).success;\n }\n}\n\nexport function escapeArg(\n arg: string,\n isMainCommand: boolean,\n options: PrintOptions,\n): string {\n const argCharacters = new Set(arg);\n const specialShellCharacters = isMainCommand\n ? SPECIAL_SHELL_CHARACTERS_FOR_MAIN_COMMAND\n : SPECIAL_SHELL_CHARACTERS;\n if (\n options?.quoting === \"extra-safe\" ||\n // biome-ignore lint/suspicious/noExplicitAny: Workaround to make this package easier to use in a project that otherwise only uses ES2022.)\n (argCharacters as unknown as any).intersection(specialShellCharacters)\n .size > 0\n ) {\n // Use single quote to reduce the need to escape (and therefore reduce the chance for bugs/security issues).\n const escaped = arg.replaceAll(\"\\\\\", \"\\\\\\\\\").replaceAll(\"'\", \"\\\\'\");\n return `'${escaped}'`;\n }\n return arg;\n}\n"],
|
|
5
|
+
"mappings": ";AAAA,OAAO,YAAY;AAMnB,SAAS,wBAAwB;AACjC,SAAS,cAAc;AACvB,SAAS,UAAU,gBAAgB;AAEnC,SAAS,iBAAiB;AAC1B,SAAS,MAAM,uBAAuB;AActC,IAAM,2BAA2B;AACjC,IAAM,0BAA0B;AAChC,IAAM,iCAAiC;AAEvC,IAAM,mBAAmB;AACzB,IAAM,qBAAqB;AAI3B,IAAM,iBAAkC,CAAC,QAAQ,MAAM;AAGvD,SAAS,SAAS,GAAqB;AACrC,SAAO,OAAO,MAAM;AACtB;AAGA,SAAS,sBAAsB,SAA6C;AAC1E,aAAW,SAAS,SAAS;AAC3B,QAAI,SAAS,KAAK,GAAG;AACnB;AAAA,IACF;AACA,QAAI,iBAAiB,MAAM;AACzB;AAAA,IACF;AACA,WAAO;AAAA,EACT;AACA,SAAO;AACT;AAOA,IAAM,gCAAgC;AAAA,EACpC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF;AAgDA,IAAM,2BAA2B,oBAAI,IAAI;AAAA,EACvC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,E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AAO,IAAI,OAAO;AAAA,EACpB;AACA,SAAO;AACT;",
|
|
6
6
|
"names": ["stream"]
|
|
7
7
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "printable-shell-command",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "5.0.1",
|
|
4
4
|
"description": "A helper class to construct shell commands in a way that allows printing them.",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "Lucas Garron",
|
|
@@ -21,14 +21,15 @@
|
|
|
21
21
|
},
|
|
22
22
|
"dependencies": {
|
|
23
23
|
"@types/node": ">=25.0.3",
|
|
24
|
-
"path-class": ">=0.
|
|
24
|
+
"path-class": ">=0.12.1"
|
|
25
25
|
},
|
|
26
26
|
"devDependencies": {
|
|
27
|
-
"@biomejs/biome": "^2.
|
|
28
|
-
"@cubing/dev-config": ">=0.
|
|
27
|
+
"@biomejs/biome": "^2.3.10",
|
|
28
|
+
"@cubing/dev-config": ">=0.8.3",
|
|
29
29
|
"@types/bun": "^1.3.5",
|
|
30
|
-
"
|
|
31
|
-
"
|
|
30
|
+
"bun-dx": "^0.1.4",
|
|
31
|
+
"esbuild": "^0.27.2",
|
|
32
|
+
"readme-cli-help": "^0.4.10",
|
|
32
33
|
"typescript": "^5.9.3"
|
|
33
34
|
},
|
|
34
35
|
"files": [
|