printable-shell-command 0.1.0 → 0.1.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/.github/workflows/publish-github-release.yaml +13 -0
- package/Makefile +6 -0
- package/README.md +25 -12
- package/package.json +1 -1
- package/src/index.ts +9 -0
- package/test/simple-test-bun.ts +1 -1
package/Makefile
CHANGED
package/README.md
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
# `printable-shell-command`
|
|
2
2
|
|
|
3
|
-
A helper class to construct shell
|
|
3
|
+
A helper class to construct shell commands in a way that allows printing them.
|
|
4
4
|
|
|
5
5
|
The goal is to make it easy to print commands that are bring run by a program, in a way that makes it easy and safe for a user to copy-and-paste.
|
|
6
6
|
|
|
7
|
-
Goals
|
|
7
|
+
## Goals
|
|
8
8
|
|
|
9
9
|
1. Security — the printed commands should be possible to use in all shells without injection vulnerabilities.
|
|
10
|
-
2. Fidelity —
|
|
11
|
-
3.
|
|
10
|
+
2. Fidelity — the printed command must match the arguments provided.
|
|
11
|
+
3. Aesthetics — the command is pretty-printed to make it easy to read and to avoid escaping/quoting simple arguments where humans usually would not.
|
|
12
12
|
|
|
13
13
|
Point 1 is difficult, and maybe even impossible. This library will do its best, but what you don't know can hurt you.
|
|
14
14
|
|
|
@@ -17,9 +17,10 @@ Point 1 is difficult, and maybe even impossible. This library will do its best,
|
|
|
17
17
|
Construct a command by providing a command string and a list of arguments. Each argument can either be an individual string, or a "pair" list containing two strings (usually a command flag and its argument). Pairs do not affect the semantics of the command, but they affect pretty-printing.
|
|
18
18
|
|
|
19
19
|
```typescript
|
|
20
|
+
// example.ts
|
|
20
21
|
import { PrintableShellCommand } from "printable-shell-command";
|
|
21
22
|
|
|
22
|
-
const command = new PrintableShellCommand("ffmpeg", [
|
|
23
|
+
export const command = new PrintableShellCommand("ffmpeg", [
|
|
23
24
|
["-i", "./test/My video.mp4"],
|
|
24
25
|
["-filter:v", "setpts=2.0*PTS"],
|
|
25
26
|
["-filter:a", "atempo=0.5"],
|
|
@@ -29,31 +30,43 @@ const command = new PrintableShellCommand("ffmpeg", [
|
|
|
29
30
|
command.print();
|
|
30
31
|
```
|
|
31
32
|
|
|
32
|
-
|
|
33
|
+
This prints:
|
|
34
|
+
|
|
35
|
+
```shell
|
|
36
|
+
ffmpeg \
|
|
37
|
+
-i './test/My video.mp4' \
|
|
38
|
+
-filter:v 'setpts=2.0*PTS' \
|
|
39
|
+
-filter:a atempo=0.5 \
|
|
40
|
+
'./test/My video (slow-mo).mov'
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
### Spawn a process in `node`
|
|
33
44
|
|
|
34
45
|
```typescript
|
|
46
|
+
import { command } from "./example";
|
|
35
47
|
import { spawn } from "node:child_process";
|
|
36
48
|
|
|
37
49
|
// Note the `...`
|
|
38
50
|
const child_process = spawn(...command.toCommandWithFlatArgs());
|
|
39
51
|
```
|
|
40
52
|
|
|
41
|
-
###
|
|
53
|
+
### Spawn a process in `bun`
|
|
42
54
|
|
|
43
55
|
```typescript
|
|
56
|
+
import { command } from "./example";
|
|
44
57
|
import { spawn } from "bun";
|
|
45
58
|
|
|
46
|
-
await spawn(
|
|
59
|
+
await spawn(command.toFlatCommand()).exited;
|
|
47
60
|
```
|
|
48
61
|
|
|
49
62
|
## Protections
|
|
50
63
|
|
|
51
64
|
Any command or argument containing the following characters is quoted and escaped:
|
|
52
65
|
|
|
53
|
-
- space character
|
|
54
|
-
- `"`
|
|
55
|
-
- `'`
|
|
56
|
-
-
|
|
66
|
+
- <code> </code> (space character)
|
|
67
|
+
- `"` (double quote)
|
|
68
|
+
- `'` (single quote)
|
|
69
|
+
- <code>`</code> (backtick)
|
|
57
70
|
- `|`
|
|
58
71
|
- `$`
|
|
59
72
|
- `*`
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -87,11 +87,20 @@ export class PrintableShellCommand {
|
|
|
87
87
|
return [this.commandName, ...this.args.flat()];
|
|
88
88
|
}
|
|
89
89
|
|
|
90
|
+
// Convenient alias for `toFlatCommand()`.
|
|
91
|
+
public forBun(): string[] {
|
|
92
|
+
return this.toFlatCommand();
|
|
93
|
+
}
|
|
90
94
|
// For use with `node:child_process`
|
|
91
95
|
public toCommandWithFlatArgs(): [string, string[]] {
|
|
92
96
|
return [this.commandName, this.args.flat()];
|
|
93
97
|
}
|
|
94
98
|
|
|
99
|
+
// Convenient alias for `toCommandWithFlatArgs()`.
|
|
100
|
+
public forNode(): [string, string[]] {
|
|
101
|
+
return this.toCommandWithFlatArgs();
|
|
102
|
+
}
|
|
103
|
+
|
|
95
104
|
#escapeArg(
|
|
96
105
|
arg: string,
|
|
97
106
|
isMainCommand: boolean,
|
package/test/simple-test-bun.ts
CHANGED