@ac-kit/format-shell 0.1.0

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.
Files changed (68) hide show
  1. package/CHANGELOG.md +41 -0
  2. package/LICENSE +28 -0
  3. package/README.md +69 -0
  4. package/dist/command/escape-posix-sh-command-arg.d.ts +20 -0
  5. package/dist/command/escape-posix-sh-command-arg.d.ts.map +1 -0
  6. package/dist/command/escape-posix-sh-command-arg.js +26 -0
  7. package/dist/command/escape-posix-sh-command-arg.js.map +1 -0
  8. package/dist/command/escape-posix-sh-command.d.ts +19 -0
  9. package/dist/command/escape-posix-sh-command.d.ts.map +1 -0
  10. package/dist/command/escape-posix-sh-command.js +55 -0
  11. package/dist/command/escape-posix-sh-command.js.map +1 -0
  12. package/dist/command/escape-posix-sh-sqe.d.ts +13 -0
  13. package/dist/command/escape-posix-sh-sqe.d.ts.map +1 -0
  14. package/dist/command/escape-posix-sh-sqe.js +15 -0
  15. package/dist/command/escape-posix-sh-sqe.js.map +1 -0
  16. package/dist/command/escape-powershell-command-arg.d.ts +21 -0
  17. package/dist/command/escape-powershell-command-arg.d.ts.map +1 -0
  18. package/dist/command/escape-powershell-command-arg.js +26 -0
  19. package/dist/command/escape-powershell-command-arg.js.map +1 -0
  20. package/dist/command/escape-powershell-command.d.ts +16 -0
  21. package/dist/command/escape-powershell-command.d.ts.map +1 -0
  22. package/dist/command/escape-powershell-command.js +18 -0
  23. package/dist/command/escape-powershell-command.js.map +1 -0
  24. package/dist/command/escape-win32-cmd-command-arg.d.ts +20 -0
  25. package/dist/command/escape-win32-cmd-command-arg.d.ts.map +1 -0
  26. package/dist/command/escape-win32-cmd-command-arg.js +29 -0
  27. package/dist/command/escape-win32-cmd-command-arg.js.map +1 -0
  28. package/dist/command/escape-win32-cmd-command.d.ts +18 -0
  29. package/dist/command/escape-win32-cmd-command.d.ts.map +1 -0
  30. package/dist/command/escape-win32-cmd-command.js +20 -0
  31. package/dist/command/escape-win32-cmd-command.js.map +1 -0
  32. package/dist/command/shell-dialect.d.ts +43 -0
  33. package/dist/command/shell-dialect.d.ts.map +1 -0
  34. package/dist/command/shell-dialect.js +61 -0
  35. package/dist/command/shell-dialect.js.map +1 -0
  36. package/dist/command/unquote-posix-sh-word.d.ts +17 -0
  37. package/dist/command/unquote-posix-sh-word.d.ts.map +1 -0
  38. package/dist/command/unquote-posix-sh-word.js +57 -0
  39. package/dist/command/unquote-posix-sh-word.js.map +1 -0
  40. package/dist/env/_env-quoting.d.ts +11 -0
  41. package/dist/env/_env-quoting.d.ts.map +1 -0
  42. package/dist/env/_env-quoting.js +99 -0
  43. package/dist/env/_env-quoting.js.map +1 -0
  44. package/dist/env/parse-env-value.d.ts +20 -0
  45. package/dist/env/parse-env-value.d.ts.map +1 -0
  46. package/dist/env/parse-env-value.js +44 -0
  47. package/dist/env/parse-env-value.js.map +1 -0
  48. package/dist/env/parse-env.d.ts +36 -0
  49. package/dist/env/parse-env.d.ts.map +1 -0
  50. package/dist/env/parse-env.js +104 -0
  51. package/dist/env/parse-env.js.map +1 -0
  52. package/dist/env/print-env.d.ts +59 -0
  53. package/dist/env/print-env.d.ts.map +1 -0
  54. package/dist/env/print-env.js +120 -0
  55. package/dist/env/print-env.js.map +1 -0
  56. package/dist/env/process-env.d.ts +21 -0
  57. package/dist/env/process-env.d.ts.map +1 -0
  58. package/dist/env/process-env.js +34 -0
  59. package/dist/env/process-env.js.map +1 -0
  60. package/dist/env/types.d.ts +31 -0
  61. package/dist/env/types.d.ts.map +1 -0
  62. package/dist/env/types.js +2 -0
  63. package/dist/env/types.js.map +1 -0
  64. package/dist/index.d.ts +15 -0
  65. package/dist/index.d.ts.map +1 -0
  66. package/dist/index.js +15 -0
  67. package/dist/index.js.map +1 -0
  68. package/package.json +64 -0
package/CHANGELOG.md ADDED
@@ -0,0 +1,41 @@
1
+ # @ac-kit/format-shell
2
+
3
+ ## 0.1.0
4
+
5
+ ### Minor Changes
6
+
7
+ - a493b4a: New package: shell syntax as a format — command-line quoting and environment
8
+ variable parsing and printing, extracted from `@ac-kit/core` and redesigned.
9
+
10
+ - `escapeCommandArg(value, dialect)` and `escapeCommand(command, dialect)`
11
+ dispatch over a `ShellDialect` naming the shell language, not the operating
12
+ system: `posix-sh`, `win32-cmd` and `powershell` are all implemented, and
13
+ `shellDialectForPlatform(platform)` maps a platform string to the default
14
+ shell there without the package itself touching `node:process`.
15
+ - `unquotePosixShWord` reads a POSIX word back whichever way it was spelled,
16
+ inverting `escapePosixShCommandArg`.
17
+ - `printEnv`/`parseEnv` handle a whole document and `printEnvAssignment`/
18
+ `parseEnvAssignment` one line, in four syntaxes: the bare `NAME=value` of an
19
+ `execve` entry, a `.env` file, a POSIX `export` statement and a cmd.exe `set`.
20
+ Each quotes its values so that what was printed is what is read back, which
21
+ `stringifyEnvVariable` never did — a value holding a space, a quote or a
22
+ newline used to produce a broken line.
23
+ - Parsing yields strings, because that is all an environment holds.
24
+ `parseEnvValueAsBool` and `parseEnvValueAsNumber` read one as something else
25
+ at the call site that knows it means one.
26
+ - `toProcessEnv`/`fromProcessEnv` convert to and from the record
27
+ `child_process.spawn` accepts.
28
+
29
+ ### Patch Changes
30
+
31
+ - a493b4a: Store a parsed key named `__proto__` as an ordinary entry rather than letting it
32
+ reach the prototype setter: CBOR map keys in `dataValueToJson`, unrecognised
33
+ `.editorconfig` properties, and ASN.1 component names in the BER, CER, DER and
34
+ PER decoders. `format-shell` already did this through a local helper, which is
35
+ now `@ac-kit/core`'s `setRecordEntry`.
36
+ - Updated dependencies [a493b4a]
37
+ - Updated dependencies [a493b4a]
38
+ - Updated dependencies [a493b4a]
39
+ - Updated dependencies [2ab5593]
40
+ - Updated dependencies [a493b4a]
41
+ - @ac-kit/core@0.2.0
package/LICENSE ADDED
@@ -0,0 +1,28 @@
1
+ BSD 3-Clause License
2
+
3
+ Copyright (c) 2024-2026 Anthony Champagne and contributors
4
+
5
+ Redistribution and use in source and binary forms, with or without
6
+ modification, are permitted provided that the following conditions are met:
7
+
8
+ 1. Redistributions of source code must retain the above copyright notice, this
9
+ list of conditions and the following disclaimer.
10
+
11
+ 2. Redistributions in binary form must reproduce the above copyright notice,
12
+ this list of conditions and the following disclaimer in the documentation
13
+ and/or other materials provided with the distribution.
14
+
15
+ 3. Neither the name of the copyright holder nor the names of its
16
+ contributors may be used to endorse or promote products derived from
17
+ this software without specific prior written permission.
18
+
19
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package/README.md ADDED
@@ -0,0 +1,69 @@
1
+ # @ac-kit/format-shell
2
+
3
+ Shell syntax as a format: the quoting rules a command line needs, and the
4
+ parsing and printing of environment variables.
5
+
6
+ Portable — no Node.js built-in is used, so a dialect or a syntax is always named
7
+ explicitly rather than read off the running platform.
8
+
9
+ ## Installation
10
+
11
+ You can install the package via npm:
12
+
13
+ ```bash
14
+ npm install @ac-kit/format-shell
15
+ ```
16
+
17
+ Or via yarn:
18
+
19
+ ```bash
20
+ yarn add @ac-kit/format-shell
21
+ ```
22
+
23
+ ## Command quoting
24
+
25
+ `escapeCommandArg(value, dialect)` makes a value safe as one argument;
26
+ `escapeCommand(command, dialect)` neutralizes the metacharacters of a whole
27
+ command line. Three dialects are covered — `posix-sh`, `win32-cmd` and
28
+ `powershell` — and each one's rule is also exported on its own
29
+ (`escapePosixShCommandArg`, `escapeWin32CmdCommand`, …).
30
+
31
+ ```ts
32
+ import { escapeCommandArg, shellDialectForPlatform } from "@ac-kit/format-shell";
33
+
34
+ const dialect = shellDialectForPlatform(process.platform);
35
+
36
+ escapeCommandArg("It's a test", dialect); // 'It'\''s a test'
37
+ ```
38
+
39
+ `unquotePosixShWord` reads a POSIX word back, whichever way it was spelled.
40
+
41
+ ## Environment variables
42
+
43
+ `printEnv` and `parseEnv` handle a whole document; `printEnvAssignment` and
44
+ `parseEnvAssignment` handle one line. Four syntaxes are covered:
45
+
46
+ | syntax | looks like | for |
47
+ | -------------- | --------------------- | -------------------------------------- |
48
+ | `assignment` | `NAME=value` | one `execve` entry — `env`, `docker --env` |
49
+ | `dotenv` | `NAME="value"` | a `.env` file |
50
+ | `posix-export` | `export NAME='value'` | a script to `source` |
51
+ | `win32-set` | `set "NAME=value"` | a batch file |
52
+
53
+ ```ts
54
+ import { parseEnv, printEnv } from "@ac-kit/format-shell";
55
+
56
+ printEnv({ TOKEN: "a b", RETRIES: 3 }); // TOKEN="a b"\nRETRIES=3\n
57
+ parseEnv('TOKEN="a b"\n# comment\n'); // { TOKEN: "a b" }
58
+ ```
59
+
60
+ Parsing always yields strings, because that is all an environment holds.
61
+ `parseEnvValueAsBool` and `parseEnvValueAsNumber` read one as something else
62
+ when the caller decides it means something else. `toProcessEnv` and
63
+ `fromProcessEnv` convert to and from the record `child_process.spawn` takes.
64
+
65
+ ## Usage
66
+
67
+ See the
68
+ [generated API docs](https://anthochamp.github.io/node-essentials/api/format-shell/)
69
+ for the full reference.
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Escape a string to be safely used as a shell argument in POSIX-compliant
3
+ * shells
4
+ *
5
+ * It adds single quotes around a string and quotes/escapes any existing single
6
+ * quotes.
7
+ *
8
+ * Example:
9
+ *
10
+ * ```ts
11
+ * const unsafe = "It's a test";
12
+ * const safe = escapeShellArgPosix(unsafe);
13
+ * console.log(safe); // 'It'\''s a test'
14
+ * ```
15
+ *
16
+ * @param expr The string to escape
17
+ * @returns The escaped string
18
+ */
19
+ export declare function escapePosixShCommandArg(expr: string): string;
20
+ //# sourceMappingURL=escape-posix-sh-command-arg.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"escape-posix-sh-command-arg.d.ts","sourceRoot":"","sources":["../../src/command/escape-posix-sh-command-arg.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,uBAAuB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAM5D"}
@@ -0,0 +1,26 @@
1
+ import { escapePosixShSqe } from "./escape-posix-sh-sqe.js";
2
+ /**
3
+ * Escape a string to be safely used as a shell argument in POSIX-compliant
4
+ * shells
5
+ *
6
+ * It adds single quotes around a string and quotes/escapes any existing single
7
+ * quotes.
8
+ *
9
+ * Example:
10
+ *
11
+ * ```ts
12
+ * const unsafe = "It's a test";
13
+ * const safe = escapeShellArgPosix(unsafe);
14
+ * console.log(safe); // 'It'\''s a test'
15
+ * ```
16
+ *
17
+ * @param expr The string to escape
18
+ * @returns The escaped string
19
+ */
20
+ export function escapePosixShCommandArg(expr) {
21
+ if (expr === "") {
22
+ return "''";
23
+ }
24
+ return `'${escapePosixShSqe(expr)}'`;
25
+ }
26
+ //# sourceMappingURL=escape-posix-sh-command-arg.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"escape-posix-sh-command-arg.js","sourceRoot":"","sources":["../../src/command/escape-posix-sh-command-arg.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,0BAA0B,CAAC;AAE5D;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,UAAU,uBAAuB,CAAC,IAAY;IACnD,IAAI,IAAI,KAAK,EAAE,EAAE,CAAC;QACjB,OAAO,IAAI,CAAC;IACb,CAAC;IAED,OAAO,IAAI,gBAAgB,CAAC,IAAI,CAAC,GAAG,CAAC;AACtC,CAAC"}
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Escapes any characters in a string that might be used to trick a
3
+ * POSIX-compliant shell command into executing arbitrary commands.
4
+ *
5
+ * It escapes the following characters by adding a backslash before them:
6
+ * &#;`|*?~<>^()[]{}$, \n and \xFF.
7
+ *
8
+ * The characters ' and " are escaped only if they are not paired.
9
+ *
10
+ * Based on PHP escapeshellcmd:
11
+ * https://www.php.net/manual/en/function.escapeshellcmd.php
12
+ *
13
+ * Source code:
14
+ * https://github.com/php/php-src/blob/d85662d6cc2c6d5f69403f6fb2001ff78e1bd174/ext/standard/exec.c#L270
15
+ *
16
+ * @param cmd The command to escape @returns The escaped command
17
+ */
18
+ export declare function escapePosixShCommand(cmd: string): string;
19
+ //# sourceMappingURL=escape-posix-sh-command.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"escape-posix-sh-command.d.ts","sourceRoot":"","sources":["../../src/command/escape-posix-sh-command.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,oBAAoB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAoCxD"}
@@ -0,0 +1,55 @@
1
+ /**
2
+ * Escapes any characters in a string that might be used to trick a
3
+ * POSIX-compliant shell command into executing arbitrary commands.
4
+ *
5
+ * It escapes the following characters by adding a backslash before them:
6
+ * &#;`|*?~<>^()[]{}$, \n and \xFF.
7
+ *
8
+ * The characters ' and " are escaped only if they are not paired.
9
+ *
10
+ * Based on PHP escapeshellcmd:
11
+ * https://www.php.net/manual/en/function.escapeshellcmd.php
12
+ *
13
+ * Source code:
14
+ * https://github.com/php/php-src/blob/d85662d6cc2c6d5f69403f6fb2001ff78e1bd174/ext/standard/exec.c#L270
15
+ *
16
+ * @param cmd The command to escape @returns The escaped command
17
+ */
18
+ export function escapePosixShCommand(cmd) {
19
+ // Escape any of the special characters with a backslash
20
+ cmd = cmd.replace(/([&#;`|*?~<>^()[\]{}$\\\n\xFF])/g, "\\$1");
21
+ // Escape unpaired quotes by preceding them with a backslash
22
+ let result = "";
23
+ let lastMatchingQuoteIndex = -1;
24
+ for (let i = 0; i < cmd.length; i++) {
25
+ const c = cmd[i];
26
+ if (c === "'" || c === '"') {
27
+ if (lastMatchingQuoteIndex === -1) {
28
+ lastMatchingQuoteIndex = cmd.indexOf(c, i + 1);
29
+ if (lastMatchingQuoteIndex === -1) {
30
+ // No matching quote found, escape this one
31
+ result += `\\${c}`;
32
+ }
33
+ else {
34
+ result += c;
35
+ }
36
+ }
37
+ else {
38
+ if (cmd[lastMatchingQuoteIndex] === c) {
39
+ // Found the matching quote for the last opened quote
40
+ lastMatchingQuoteIndex = -1;
41
+ result += c;
42
+ }
43
+ else {
44
+ // The quote is not matching the last opened quote, escape it
45
+ result += `\\${c}`;
46
+ }
47
+ }
48
+ }
49
+ else {
50
+ result += c;
51
+ }
52
+ }
53
+ return result;
54
+ }
55
+ //# sourceMappingURL=escape-posix-sh-command.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"escape-posix-sh-command.js","sourceRoot":"","sources":["../../src/command/escape-posix-sh-command.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,UAAU,oBAAoB,CAAC,GAAW;IAC/C,wDAAwD;IACxD,GAAG,GAAG,GAAG,CAAC,OAAO,CAAC,kCAAkC,EAAE,MAAM,CAAC,CAAC;IAE9D,4DAA4D;IAC5D,IAAI,MAAM,GAAG,EAAE,CAAC;IAChB,IAAI,sBAAsB,GAAG,CAAC,CAAC,CAAC;IAEhC,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,GAAG,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;QACrC,MAAM,CAAC,GAAG,GAAG,CAAC,CAAC,CAAC,CAAC;QAEjB,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC,KAAK,GAAG,EAAE,CAAC;YAC5B,IAAI,sBAAsB,KAAK,CAAC,CAAC,EAAE,CAAC;gBACnC,sBAAsB,GAAG,GAAG,CAAC,OAAO,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC;gBAC/C,IAAI,sBAAsB,KAAK,CAAC,CAAC,EAAE,CAAC;oBACnC,2CAA2C;oBAC3C,MAAM,IAAI,KAAK,CAAC,EAAE,CAAC;gBACpB,CAAC;qBAAM,CAAC;oBACP,MAAM,IAAI,CAAC,CAAC;gBACb,CAAC;YACF,CAAC;iBAAM,CAAC;gBACP,IAAI,GAAG,CAAC,sBAAsB,CAAC,KAAK,CAAC,EAAE,CAAC;oBACvC,qDAAqD;oBACrD,sBAAsB,GAAG,CAAC,CAAC,CAAC;oBAC5B,MAAM,IAAI,CAAC,CAAC;gBACb,CAAC;qBAAM,CAAC;oBACP,6DAA6D;oBAC7D,MAAM,IAAI,KAAK,CAAC,EAAE,CAAC;gBACpB,CAAC;YACF,CAAC;QACF,CAAC;aAAM,CAAC;YACP,MAAM,IAAI,CAAC,CAAC;QACb,CAAC;IACF,CAAC;IAED,OAAO,MAAM,CAAC;AACf,CAAC"}
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Escape a string to be safely used in a single-quoted shell command.
3
+ *
4
+ * Example:
5
+ *
6
+ * ```ts
7
+ * const unsafe = "It's a test";
8
+ * const safe = escapePosixShSqe(unsafe);
9
+ * console.log(safe); // It'\''s a test
10
+ * ```
11
+ */
12
+ export declare function escapePosixShSqe(expr: string): string;
13
+ //# sourceMappingURL=escape-posix-sh-sqe.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"escape-posix-sh-sqe.d.ts","sourceRoot":"","sources":["../../src/command/escape-posix-sh-sqe.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AACH,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAErD"}
@@ -0,0 +1,15 @@
1
+ /**
2
+ * Escape a string to be safely used in a single-quoted shell command.
3
+ *
4
+ * Example:
5
+ *
6
+ * ```ts
7
+ * const unsafe = "It's a test";
8
+ * const safe = escapePosixShSqe(unsafe);
9
+ * console.log(safe); // It'\''s a test
10
+ * ```
11
+ */
12
+ export function escapePosixShSqe(expr) {
13
+ return expr.replace(/'/g, "'\\''");
14
+ }
15
+ //# sourceMappingURL=escape-posix-sh-sqe.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"escape-posix-sh-sqe.js","sourceRoot":"","sources":["../../src/command/escape-posix-sh-sqe.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AACH,MAAM,UAAU,gBAAgB,CAAC,IAAY;IAC5C,OAAO,IAAI,CAAC,OAAO,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;AACpC,CAAC"}
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Escape a string to be safely used as a PowerShell command argument.
3
+ *
4
+ * It wraps the string in single quotes — inside which PowerShell performs no
5
+ * variable expansion, no subexpression evaluation and no backtick escaping —
6
+ * and doubles any single quote the string already contains, which is the only
7
+ * escape the single-quoted form recognises.
8
+ *
9
+ * Example:
10
+ *
11
+ * ```ts
12
+ * const unsafe = "It's $(a) `test`";
13
+ * const safe = escapePowerShellCommandArg(unsafe);
14
+ * console.log(safe); // 'It''s $(a) `test`'
15
+ * ```
16
+ *
17
+ * @param expr The string to escape
18
+ * @returns The escaped string
19
+ */
20
+ export declare function escapePowerShellCommandArg(expr: string): string;
21
+ //# sourceMappingURL=escape-powershell-command-arg.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"escape-powershell-command-arg.d.ts","sourceRoot":"","sources":["../../src/command/escape-powershell-command-arg.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,0BAA0B,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAM/D"}
@@ -0,0 +1,26 @@
1
+ /**
2
+ * Escape a string to be safely used as a PowerShell command argument.
3
+ *
4
+ * It wraps the string in single quotes — inside which PowerShell performs no
5
+ * variable expansion, no subexpression evaluation and no backtick escaping —
6
+ * and doubles any single quote the string already contains, which is the only
7
+ * escape the single-quoted form recognises.
8
+ *
9
+ * Example:
10
+ *
11
+ * ```ts
12
+ * const unsafe = "It's $(a) `test`";
13
+ * const safe = escapePowerShellCommandArg(unsafe);
14
+ * console.log(safe); // 'It''s $(a) `test`'
15
+ * ```
16
+ *
17
+ * @param expr The string to escape
18
+ * @returns The escaped string
19
+ */
20
+ export function escapePowerShellCommandArg(expr) {
21
+ if (expr === "") {
22
+ return "''";
23
+ }
24
+ return `'${expr.replace(/'/g, "''")}'`;
25
+ }
26
+ //# sourceMappingURL=escape-powershell-command-arg.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"escape-powershell-command-arg.js","sourceRoot":"","sources":["../../src/command/escape-powershell-command-arg.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,UAAU,0BAA0B,CAAC,IAAY;IACtD,IAAI,IAAI,KAAK,EAAE,EAAE,CAAC;QACjB,OAAO,IAAI,CAAC;IACb,CAAC;IAED,OAAO,IAAI,IAAI,CAAC,OAAO,CAAC,IAAI,EAAE,IAAI,CAAC,GAAG,CAAC;AACxC,CAAC"}
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Escape any characters in a string that might be used to trick a PowerShell
3
+ * command into executing arbitrary commands.
4
+ *
5
+ * It escapes the following characters by adding a backtick (`) before them:
6
+ * '"`$#&|;,(){}[]<>@, \n and \xFF.
7
+ *
8
+ * The backtick is PowerShell's escape character, the counterpart of the
9
+ * backslash `escapePosixShCommand` uses and the caret `escapeWin32CmdCommand`
10
+ * uses.
11
+ *
12
+ * @param cmd The command to escape
13
+ * @returns The escaped command
14
+ */
15
+ export declare function escapePowerShellCommand(cmd: string): string;
16
+ //# sourceMappingURL=escape-powershell-command.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"escape-powershell-command.d.ts","sourceRoot":"","sources":["../../src/command/escape-powershell-command.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AACH,wBAAgB,uBAAuB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAE3D"}
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Escape any characters in a string that might be used to trick a PowerShell
3
+ * command into executing arbitrary commands.
4
+ *
5
+ * It escapes the following characters by adding a backtick (`) before them:
6
+ * '"`$#&|;,(){}[]<>@, \n and \xFF.
7
+ *
8
+ * The backtick is PowerShell's escape character, the counterpart of the
9
+ * backslash `escapePosixShCommand` uses and the caret `escapeWin32CmdCommand`
10
+ * uses.
11
+ *
12
+ * @param cmd The command to escape
13
+ * @returns The escaped command
14
+ */
15
+ export function escapePowerShellCommand(cmd) {
16
+ return cmd.replace(/(['"`$#&|;,(){}[\]<>@\n\xFF])/g, "`$1");
17
+ }
18
+ //# sourceMappingURL=escape-powershell-command.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"escape-powershell-command.js","sourceRoot":"","sources":["../../src/command/escape-powershell-command.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AACH,MAAM,UAAU,uBAAuB,CAAC,GAAW;IAClD,OAAO,GAAG,CAAC,OAAO,CAAC,gCAAgC,EAAE,KAAK,CAAC,CAAC;AAC7D,CAAC"}
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Escape a string to be safely used as a shell argument in Windows cmd.exe
3
+ *
4
+ * It adds double quotes around a string and escapes any existing problematic
5
+ * characters (such as %, !, and ") by replacing them with spaces. Backslashes
6
+ * are escaped by doubling them.
7
+ *
8
+ * Example:
9
+ *
10
+ * ```ts
11
+ * const unsafe = 'A "complex" %string% with !special! chars \\';
12
+ * const safe = escapeShellArgWindows(unsafe);
13
+ * console.log(safe); // "A complex string with special chars \\"
14
+ * ```
15
+ *
16
+ * @param expr The string to escape
17
+ * @returns The escaped string
18
+ */
19
+ export declare function escapeWin32CmdCommandArg(expr: string): string;
20
+ //# sourceMappingURL=escape-win32-cmd-command-arg.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"escape-win32-cmd-command-arg.d.ts","sourceRoot":"","sources":["../../src/command/escape-win32-cmd-command-arg.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,wBAAwB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAY7D"}
@@ -0,0 +1,29 @@
1
+ /**
2
+ * Escape a string to be safely used as a shell argument in Windows cmd.exe
3
+ *
4
+ * It adds double quotes around a string and escapes any existing problematic
5
+ * characters (such as %, !, and ") by replacing them with spaces. Backslashes
6
+ * are escaped by doubling them.
7
+ *
8
+ * Example:
9
+ *
10
+ * ```ts
11
+ * const unsafe = 'A "complex" %string% with !special! chars \\';
12
+ * const safe = escapeShellArgWindows(unsafe);
13
+ * console.log(safe); // "A complex string with special chars \\"
14
+ * ```
15
+ *
16
+ * @param expr The string to escape
17
+ * @returns The escaped string
18
+ */
19
+ export function escapeWin32CmdCommandArg(expr) {
20
+ if (expr === "") {
21
+ return '""';
22
+ }
23
+ // Replace problematic characters with spaces
24
+ expr = expr.replace(/[%!"]/g, " ");
25
+ // Escape backslashes by double them
26
+ expr = expr.replace(/\\/g, "\\\\");
27
+ return `"${expr}"`;
28
+ }
29
+ //# sourceMappingURL=escape-win32-cmd-command-arg.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"escape-win32-cmd-command-arg.js","sourceRoot":"","sources":["../../src/command/escape-win32-cmd-command-arg.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,UAAU,wBAAwB,CAAC,IAAY;IACpD,IAAI,IAAI,KAAK,EAAE,EAAE,CAAC;QACjB,OAAO,IAAI,CAAC;IACb,CAAC;IAED,6CAA6C;IAC7C,IAAI,GAAG,IAAI,CAAC,OAAO,CAAC,QAAQ,EAAE,GAAG,CAAC,CAAC;IAEnC,oCAAoC;IACpC,IAAI,GAAG,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;IAEnC,OAAO,IAAI,IAAI,GAAG,CAAC;AACpB,CAAC"}
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Escape any characters in a string that might be used to trick a Windows
3
+ * cmd.exe command into executing arbitrary commands.
4
+ *
5
+ * It escapes the following characters by adding a caret (^) before them:
6
+ * '"%!&#;`|*?~<>^()[]{}$, \n and \xFF.
7
+ *
8
+ * Based on PHP escapeshellcmd:
9
+ * https://www.php.net/manual/en/function.escapeshellcmd.php
10
+ *
11
+ * Source code:
12
+ * https://github.com/php/php-src/blob/d85662d6cc2c6d5f69403f6fb2001ff78e1bd174/ext/standard/exec.c#L270
13
+ *
14
+ * @param cmd The command to escape
15
+ * @returns The escaped command
16
+ */
17
+ export declare function escapeWin32CmdCommand(cmd: string): string;
18
+ //# sourceMappingURL=escape-win32-cmd-command.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"escape-win32-cmd-command.d.ts","sourceRoot":"","sources":["../../src/command/escape-win32-cmd-command.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,qBAAqB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAEzD"}
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Escape any characters in a string that might be used to trick a Windows
3
+ * cmd.exe command into executing arbitrary commands.
4
+ *
5
+ * It escapes the following characters by adding a caret (^) before them:
6
+ * '"%!&#;`|*?~<>^()[]{}$, \n and \xFF.
7
+ *
8
+ * Based on PHP escapeshellcmd:
9
+ * https://www.php.net/manual/en/function.escapeshellcmd.php
10
+ *
11
+ * Source code:
12
+ * https://github.com/php/php-src/blob/d85662d6cc2c6d5f69403f6fb2001ff78e1bd174/ext/standard/exec.c#L270
13
+ *
14
+ * @param cmd The command to escape
15
+ * @returns The escaped command
16
+ */
17
+ export function escapeWin32CmdCommand(cmd) {
18
+ return cmd.replace(/(['"%!&#;`|*?~<>^()[\]{}$\\\n\xFF])/g, "^$1");
19
+ }
20
+ //# sourceMappingURL=escape-win32-cmd-command.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"escape-win32-cmd-command.js","sourceRoot":"","sources":["../../src/command/escape-win32-cmd-command.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AACH,MAAM,UAAU,qBAAqB,CAAC,GAAW;IAChD,OAAO,GAAG,CAAC,OAAO,CAAC,sCAAsC,EAAE,KAAK,CAAC,CAAC;AACnE,CAAC"}
@@ -0,0 +1,43 @@
1
+ /**
2
+ * The shell language a command line is written in.
3
+ *
4
+ * It names the shell's grammar, not the operating system running it: PowerShell
5
+ * is the default shell on Windows Server but also runs on Linux and macOS, and
6
+ * a POSIX shell is what runs under WSL, Cygwin and Git Bash on Windows.
7
+ */
8
+ export type ShellDialect = "posix-sh" | "win32-cmd" | "powershell";
9
+ /**
10
+ * Escape a string to be safely used as a single argument of a command line
11
+ * written in the given dialect.
12
+ *
13
+ * @param expr The string to escape
14
+ * @param dialect The shell language the command line is written in
15
+ * @returns The escaped string
16
+ */
17
+ export declare function escapeCommandArg(expr: string, dialect: ShellDialect): string;
18
+ /**
19
+ * Escape any characters in a string that might be used to trick a command line
20
+ * written in the given dialect into executing arbitrary commands.
21
+ *
22
+ * Prefer `escapeCommandArg` whenever the string is one argument rather than a
23
+ * whole command: neutralizing metacharacters leaves a string that the shell
24
+ * still splits on whitespace.
25
+ *
26
+ * @param cmd The command to escape
27
+ * @param dialect The shell language the command is written in
28
+ * @returns The escaped command
29
+ */
30
+ export declare function escapeCommand(cmd: string, dialect: ShellDialect): string;
31
+ /**
32
+ * Map an operating system identifier, as `process.platform` spells it, to the
33
+ * shell dialect a command runs under by default there.
34
+ *
35
+ * A platform does not determine a shell — it only names the default one, which
36
+ * is what `child_process.exec` and its equivalents spawn. A caller that knows
37
+ * it is driving a different shell passes that dialect instead.
38
+ *
39
+ * @param platform The operating system identifier
40
+ * @returns The dialect of that platform's default shell
41
+ */
42
+ export declare function shellDialectForPlatform(platform: string): ShellDialect;
43
+ //# sourceMappingURL=shell-dialect.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"shell-dialect.d.ts","sourceRoot":"","sources":["../../src/command/shell-dialect.ts"],"names":[],"mappings":"AAOA;;;;;;GAMG;AACH,MAAM,MAAM,YAAY,GAAG,UAAU,GAAG,WAAW,GAAG,YAAY,CAAC;AAEnE;;;;;;;GAOG;AACH,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,YAAY,GAAG,MAAM,CAW5E;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,aAAa,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,EAAE,YAAY,GAAG,MAAM,CAWxE;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,uBAAuB,CAAC,QAAQ,EAAE,MAAM,GAAG,YAAY,CAEtE"}
@@ -0,0 +1,61 @@
1
+ import { escapePosixShCommandArg } from "./escape-posix-sh-command-arg.js";
2
+ import { escapePosixShCommand } from "./escape-posix-sh-command.js";
3
+ import { escapePowerShellCommandArg } from "./escape-powershell-command-arg.js";
4
+ import { escapePowerShellCommand } from "./escape-powershell-command.js";
5
+ import { escapeWin32CmdCommandArg } from "./escape-win32-cmd-command-arg.js";
6
+ import { escapeWin32CmdCommand } from "./escape-win32-cmd-command.js";
7
+ /**
8
+ * Escape a string to be safely used as a single argument of a command line
9
+ * written in the given dialect.
10
+ *
11
+ * @param expr The string to escape
12
+ * @param dialect The shell language the command line is written in
13
+ * @returns The escaped string
14
+ */
15
+ export function escapeCommandArg(expr, dialect) {
16
+ switch (dialect) {
17
+ case "posix-sh":
18
+ return escapePosixShCommandArg(expr);
19
+ case "win32-cmd":
20
+ return escapeWin32CmdCommandArg(expr);
21
+ case "powershell":
22
+ return escapePowerShellCommandArg(expr);
23
+ }
24
+ }
25
+ /**
26
+ * Escape any characters in a string that might be used to trick a command line
27
+ * written in the given dialect into executing arbitrary commands.
28
+ *
29
+ * Prefer `escapeCommandArg` whenever the string is one argument rather than a
30
+ * whole command: neutralizing metacharacters leaves a string that the shell
31
+ * still splits on whitespace.
32
+ *
33
+ * @param cmd The command to escape
34
+ * @param dialect The shell language the command is written in
35
+ * @returns The escaped command
36
+ */
37
+ export function escapeCommand(cmd, dialect) {
38
+ switch (dialect) {
39
+ case "posix-sh":
40
+ return escapePosixShCommand(cmd);
41
+ case "win32-cmd":
42
+ return escapeWin32CmdCommand(cmd);
43
+ case "powershell":
44
+ return escapePowerShellCommand(cmd);
45
+ }
46
+ }
47
+ /**
48
+ * Map an operating system identifier, as `process.platform` spells it, to the
49
+ * shell dialect a command runs under by default there.
50
+ *
51
+ * A platform does not determine a shell — it only names the default one, which
52
+ * is what `child_process.exec` and its equivalents spawn. A caller that knows
53
+ * it is driving a different shell passes that dialect instead.
54
+ *
55
+ * @param platform The operating system identifier
56
+ * @returns The dialect of that platform's default shell
57
+ */
58
+ export function shellDialectForPlatform(platform) {
59
+ return platform === "win32" ? "win32-cmd" : "posix-sh";
60
+ }
61
+ //# sourceMappingURL=shell-dialect.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"shell-dialect.js","sourceRoot":"","sources":["../../src/command/shell-dialect.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,uBAAuB,EAAE,MAAM,kCAAkC,CAAC;AAC3E,OAAO,EAAE,oBAAoB,EAAE,MAAM,8BAA8B,CAAC;AACpE,OAAO,EAAE,0BAA0B,EAAE,MAAM,oCAAoC,CAAC;AAChF,OAAO,EAAE,uBAAuB,EAAE,MAAM,gCAAgC,CAAC;AACzE,OAAO,EAAE,wBAAwB,EAAE,MAAM,mCAAmC,CAAC;AAC7E,OAAO,EAAE,qBAAqB,EAAE,MAAM,+BAA+B,CAAC;AAWtE;;;;;;;GAOG;AACH,MAAM,UAAU,gBAAgB,CAAC,IAAY,EAAE,OAAqB;IACnE,QAAQ,OAAO,EAAE,CAAC;QACjB,KAAK,UAAU;YACd,OAAO,uBAAuB,CAAC,IAAI,CAAC,CAAC;QAEtC,KAAK,WAAW;YACf,OAAO,wBAAwB,CAAC,IAAI,CAAC,CAAC;QAEvC,KAAK,YAAY;YAChB,OAAO,0BAA0B,CAAC,IAAI,CAAC,CAAC;IAC1C,CAAC;AACF,CAAC;AAED;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,aAAa,CAAC,GAAW,EAAE,OAAqB;IAC/D,QAAQ,OAAO,EAAE,CAAC;QACjB,KAAK,UAAU;YACd,OAAO,oBAAoB,CAAC,GAAG,CAAC,CAAC;QAElC,KAAK,WAAW;YACf,OAAO,qBAAqB,CAAC,GAAG,CAAC,CAAC;QAEnC,KAAK,YAAY;YAChB,OAAO,uBAAuB,CAAC,GAAG,CAAC,CAAC;IACtC,CAAC;AACF,CAAC;AAED;;;;;;;;;;GAUG;AACH,MAAM,UAAU,uBAAuB,CAAC,QAAgB;IACvD,OAAO,QAAQ,KAAK,OAAO,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,UAAU,CAAC;AACxD,CAAC"}