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.
@@ -0,0 +1,13 @@
1
+ name: Publish GitHub release
2
+
3
+ on:
4
+ push:
5
+ tags:
6
+ - v*
7
+
8
+ jobs:
9
+ Publish:
10
+ permissions:
11
+ contents: write
12
+ if: startsWith(github.ref, 'refs/tags/v')
13
+ uses: cubing/actions-workflows/.github/workflows/publish-github-release.yaml@main
package/Makefile CHANGED
@@ -5,3 +5,9 @@ lint:
5
5
  .PHONY: format
6
6
  format:
7
7
  bun x @biomejs/biome check --write
8
+
9
+ # https://github.com/lgarron/repo
10
+ REPO_COMMANDS = setup publish
11
+
12
+ ${REPO_COMMANDS}:
13
+ repo $@
package/README.md CHANGED
@@ -1,14 +1,14 @@
1
1
  # `printable-shell-command`
2
2
 
3
- A helper class to construct shell command in a way that allows printing them.
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 — The printed command must match the arguments provided.
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
- ### In `node`
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
- ### With `bun`
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({ cmd: command.toFlatCommand() }).exited;
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "printable-shell-command",
3
- "version": "v0.1.0",
3
+ "version": "0.1.1",
4
4
  "main": "./src/index.ts",
5
5
  "type": "module",
6
6
  "exports": {
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,
@@ -9,4 +9,4 @@ const command = new PrintableShellCommand("ffmpeg", [
9
9
  ]);
10
10
 
11
11
  command.print();
12
- await spawn({ cmd: command.toFlatCommand() }).exited;
12
+ await spawn(command.toFlatCommand()).exited;