@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
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Read back the value of a POSIX shell word, undoing single quotes, double
3
+ * quotes and backslash escapes.
4
+ *
5
+ * The inverse of `escapePosixShCommandArg`, and of any other spelling of the
6
+ * same word: a shell word concatenates quoted and unquoted segments freely, so
7
+ * `'it'\''s'` and `it\'s` both carry `it's`.
8
+ *
9
+ * Expansion is not performed. `$HOME` and `` `id` `` are returned verbatim,
10
+ * which is what the single-quoted form means and the only reading available
11
+ * without a shell to run.
12
+ *
13
+ * @param word The word to read.
14
+ * @returns The value it carries.
15
+ */
16
+ export declare function unquotePosixShWord(word: string): string;
17
+ //# sourceMappingURL=unquote-posix-sh-word.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"unquote-posix-sh-word.d.ts","sourceRoot":"","sources":["../../src/command/unquote-posix-sh-word.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CA4CvD"}
@@ -0,0 +1,57 @@
1
+ /**
2
+ * Read back the value of a POSIX shell word, undoing single quotes, double
3
+ * quotes and backslash escapes.
4
+ *
5
+ * The inverse of `escapePosixShCommandArg`, and of any other spelling of the
6
+ * same word: a shell word concatenates quoted and unquoted segments freely, so
7
+ * `'it'\''s'` and `it\'s` both carry `it's`.
8
+ *
9
+ * Expansion is not performed. `$HOME` and `` `id` `` are returned verbatim,
10
+ * which is what the single-quoted form means and the only reading available
11
+ * without a shell to run.
12
+ *
13
+ * @param word The word to read.
14
+ * @returns The value it carries.
15
+ */
16
+ export function unquotePosixShWord(word) {
17
+ let result = "";
18
+ let index = 0;
19
+ while (index < word.length) {
20
+ const char = word[index];
21
+ if (char === "'") {
22
+ const end = word.indexOf("'", index + 1);
23
+ if (end === -1) {
24
+ // Unterminated: take the rest rather than reject, so a hand-written
25
+ // line missing its closing quote still yields its value.
26
+ result += word.slice(index + 1);
27
+ break;
28
+ }
29
+ result += word.slice(index + 1, end);
30
+ index = end + 1;
31
+ }
32
+ else if (char === '"') {
33
+ index++;
34
+ while (index < word.length && word[index] !== '"') {
35
+ // Inside double quotes a backslash only escapes these four.
36
+ if (word[index] === "\\" &&
37
+ index + 1 < word.length &&
38
+ ['"', "\\", "$", "`"].includes(word[index + 1])) {
39
+ index++;
40
+ }
41
+ result += word[index];
42
+ index++;
43
+ }
44
+ index++;
45
+ }
46
+ else if (char === "\\" && index + 1 < word.length) {
47
+ result += word[index + 1];
48
+ index += 2;
49
+ }
50
+ else {
51
+ result += char;
52
+ index++;
53
+ }
54
+ }
55
+ return result;
56
+ }
57
+ //# sourceMappingURL=unquote-posix-sh-word.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"unquote-posix-sh-word.js","sourceRoot":"","sources":["../../src/command/unquote-posix-sh-word.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AACH,MAAM,UAAU,kBAAkB,CAAC,IAAY;IAC9C,IAAI,MAAM,GAAG,EAAE,CAAC;IAChB,IAAI,KAAK,GAAG,CAAC,CAAC;IAEd,OAAO,KAAK,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC;QAC5B,MAAM,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC;QAEzB,IAAI,IAAI,KAAK,GAAG,EAAE,CAAC;YAClB,MAAM,GAAG,GAAG,IAAI,CAAC,OAAO,CAAC,GAAG,EAAE,KAAK,GAAG,CAAC,CAAC,CAAC;YACzC,IAAI,GAAG,KAAK,CAAC,CAAC,EAAE,CAAC;gBAChB,oEAAoE;gBACpE,yDAAyD;gBACzD,MAAM,IAAI,IAAI,CAAC,KAAK,CAAC,KAAK,GAAG,CAAC,CAAC,CAAC;gBAChC,MAAM;YACP,CAAC;YAED,MAAM,IAAI,IAAI,CAAC,KAAK,CAAC,KAAK,GAAG,CAAC,EAAE,GAAG,CAAC,CAAC;YACrC,KAAK,GAAG,GAAG,GAAG,CAAC,CAAC;QACjB,CAAC;aAAM,IAAI,IAAI,KAAK,GAAG,EAAE,CAAC;YACzB,KAAK,EAAE,CAAC;YACR,OAAO,KAAK,GAAG,IAAI,CAAC,MAAM,IAAI,IAAI,CAAC,KAAK,CAAC,KAAK,GAAG,EAAE,CAAC;gBACnD,4DAA4D;gBAC5D,IACC,IAAI,CAAC,KAAK,CAAC,KAAK,IAAI;oBACpB,KAAK,GAAG,CAAC,GAAG,IAAI,CAAC,MAAM;oBACvB,CAAC,GAAG,EAAE,IAAI,EAAE,GAAG,EAAE,GAAG,CAAC,CAAC,QAAQ,CAAC,IAAI,CAAC,KAAK,GAAG,CAAC,CAAW,CAAC,EACxD,CAAC;oBACF,KAAK,EAAE,CAAC;gBACT,CAAC;gBAED,MAAM,IAAI,IAAI,CAAC,KAAK,CAAC,CAAC;gBACtB,KAAK,EAAE,CAAC;YACT,CAAC;YACD,KAAK,EAAE,CAAC;QACT,CAAC;aAAM,IAAI,IAAI,KAAK,IAAI,IAAI,KAAK,GAAG,CAAC,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC;YACrD,MAAM,IAAI,IAAI,CAAC,KAAK,GAAG,CAAC,CAAC,CAAC;YAC1B,KAAK,IAAI,CAAC,CAAC;QACZ,CAAC;aAAM,CAAC;YACP,MAAM,IAAI,IAAI,CAAC;YACf,KAAK,EAAE,CAAC;QACT,CAAC;IACF,CAAC;IAED,OAAO,MAAM,CAAC;AACf,CAAC"}
@@ -0,0 +1,11 @@
1
+ export declare function quoteDotenvValue(value: string): string;
2
+ /**
3
+ * Reports the quote a `.env` value opens and never closes, so a reader can keep
4
+ * appending lines to it. A value spanning lines is how a PEM key or a JSON blob
5
+ * reaches a `.env` file.
6
+ */
7
+ export declare function unterminatedDotenvQuote(rawValue: string): string | null;
8
+ export declare function unquoteDotenvValue(rawValue: string): string;
9
+ export declare function quoteWin32SetValue(value: string): string;
10
+ export declare function unquoteWin32SetValue(rawValue: string): string;
11
+ //# sourceMappingURL=_env-quoting.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_env-quoting.d.ts","sourceRoot":"","sources":["../../src/env/_env-quoting.ts"],"names":[],"mappings":"AAmBA,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAYtD;AAED;;;;GAIG;AACH,wBAAgB,uBAAuB,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAQvE;AAgBD,wBAAgB,kBAAkB,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,CAe3D;AA4BD,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAUxD;AAED,wBAAgB,oBAAoB,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,CAE7D"}
@@ -0,0 +1,99 @@
1
+ import { UnsupportedError } from "@ac-kit/core";
2
+ /**
3
+ * A `.env` value made only of these characters needs no quotes: none of them
4
+ * can start a comment, end the value early, or be eaten by the leading and
5
+ * trailing whitespace a reader strips.
6
+ */
7
+ const DOTENV_BARE_VALUE_RE_ = /^[A-Za-z0-9_@%+=:,./-]*$/;
8
+ /** The escapes a double-quoted `.env` value recognises, keyed by their letter. */
9
+ const DOTENV_UNESCAPES_ = new Map([
10
+ ["n", "\n"],
11
+ ["r", "\r"],
12
+ ["t", "\t"],
13
+ ["f", "\f"],
14
+ ["b", "\b"],
15
+ ["v", "\v"],
16
+ ]);
17
+ export function quoteDotenvValue(value) {
18
+ if (DOTENV_BARE_VALUE_RE_.test(value)) {
19
+ return value;
20
+ }
21
+ const escaped = value
22
+ .replace(/[\\"$`]/g, "\\$&")
23
+ .replace(/\n/g, "\\n")
24
+ .replace(/\r/g, "\\r")
25
+ .replace(/\t/g, "\\t");
26
+ return `"${escaped}"`;
27
+ }
28
+ /**
29
+ * Reports the quote a `.env` value opens and never closes, so a reader can keep
30
+ * appending lines to it. A value spanning lines is how a PEM key or a JSON blob
31
+ * reaches a `.env` file.
32
+ */
33
+ export function unterminatedDotenvQuote(rawValue) {
34
+ const trimmed = rawValue.trimStart();
35
+ const quote = trimmed[0];
36
+ if (quote !== '"' && quote !== "'") {
37
+ return null;
38
+ }
39
+ return isDotenvQuoteClosed_(trimmed, quote) ? null : quote;
40
+ }
41
+ function isDotenvQuoteClosed_(trimmed, quote) {
42
+ for (let i = 1; i < trimmed.length; i++) {
43
+ if (quote === '"' && trimmed[i] === "\\") {
44
+ i++;
45
+ continue;
46
+ }
47
+ if (trimmed[i] === quote) {
48
+ return true;
49
+ }
50
+ }
51
+ return false;
52
+ }
53
+ export function unquoteDotenvValue(rawValue) {
54
+ const trimmed = rawValue.trim();
55
+ if (trimmed.length >= 2) {
56
+ // Single quotes are literal in POSIX and in every `.env` reader built on
57
+ // it: there is nothing to unescape inside them.
58
+ if (trimmed.startsWith("'") && trimmed.endsWith("'")) {
59
+ return trimmed.slice(1, -1);
60
+ }
61
+ if (trimmed.startsWith('"') && trimmed.endsWith('"')) {
62
+ return unescapeDotenvValue_(trimmed.slice(1, -1));
63
+ }
64
+ }
65
+ return stripDotenvComment_(trimmed);
66
+ }
67
+ function unescapeDotenvValue_(quoted) {
68
+ let result = "";
69
+ for (let i = 0; i < quoted.length; i++) {
70
+ const char = quoted[i];
71
+ if (char !== "\\" || i === quoted.length - 1) {
72
+ result += char;
73
+ continue;
74
+ }
75
+ i++;
76
+ const escaped = quoted[i];
77
+ result += DOTENV_UNESCAPES_.get(escaped) ?? escaped;
78
+ }
79
+ return result;
80
+ }
81
+ /**
82
+ * A `#` only starts a comment when whitespace precedes it: an unquoted value is
83
+ * allowed to contain one, which is how a URL fragment or a colour survives.
84
+ */
85
+ function stripDotenvComment_(trimmed) {
86
+ return trimmed.replace(/\s+#.*$/s, "").trimEnd();
87
+ }
88
+ export function quoteWin32SetValue(value) {
89
+ if (/["\r\n]/.test(value)) {
90
+ // `set "NAME=value"` ends at the first quote and cmd.exe offers no escape
91
+ // for one inside the assignment, so there is no spelling to fall back on.
92
+ throw new UnsupportedError('cmd.exe "set" value containing a quote or a newline');
93
+ }
94
+ return value.replace(/%/g, "%%");
95
+ }
96
+ export function unquoteWin32SetValue(rawValue) {
97
+ return rawValue.replace(/%%/g, "%");
98
+ }
99
+ //# sourceMappingURL=_env-quoting.js.map
@@ -0,0 +1 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,EAAE,CAAC;QACxC,MAAM,IAAI,GAAG,MAAM,CAAC,CAAC,CAAC,CAAC;QACvB,IAAI,IAAI,KAAK,IAAI,IAAI,CAAC,KAAK,MAAM,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;YAC9C,MAAM,IAAI,IAAI,CAAC;YACf,SAAS;QACV,CAAC;QAED,CAAC,EAAE,CAAC;QACJ,MAAM,OAAO,GAAG,MAAM,CAAC,CAAC,CAAW,CAAC;QACpC,MAAM,IAAI,iBAAiB,CAAC,GAAG,CAAC,OAAO,CAAC,IAAI,OAAO,CAAC;IACrD,CAAC;IAED,OAAO,MAAM,CAAC;AACf,CAAC;AAED;;;GAGG;AACH,SAAS,mBAAmB,CAAC,OAAe;IAC3C,OAAO,OAAO,CAAC,OAAO,CAAC,UAAU,EAAE,EAAE,CAAC,CAAC,OAAO,EAAE,CAAC;AAClD,CAAC;AAED,MAAM,UAAU,kBAAkB,CAAC,KAAa;IAC/C,IAAI,SAAS,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC;QAC3B,0EAA0E;QAC1E,0EAA0E;QAC1E,MAAM,IAAI,gBAAgB,CACzB,qDAAqD,CACrD,CAAC;IACH,CAAC;IAED,OAAO,KAAK,CAAC,OAAO,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;AAClC,CAAC;AAED,MAAM,UAAU,oBAAoB,CAAC,QAAgB;IACpD,OAAO,QAAQ,CAAC,OAAO,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC;AACrC,CAAC"}
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Read an environment variable value as a number.
3
+ *
4
+ * An integer beyond the safe range is returned as a `bigint`, so a value is
5
+ * never silently rounded.
6
+ *
7
+ * @param value The value to read.
8
+ * @returns The number or bigint it spells, or null if it spells neither.
9
+ */
10
+ export declare function parseEnvValueAsNumber(value: string | undefined | null): number | bigint | null;
11
+ /**
12
+ * Read an environment variable value as a boolean.
13
+ *
14
+ * Recognises every flavor `printEnvBoolValue` writes, case-insensitively.
15
+ *
16
+ * @param value The value to read.
17
+ * @returns The boolean it spells, or null if it spells none.
18
+ */
19
+ export declare function parseEnvValueAsBool(value: string | undefined | null): boolean | null;
20
+ //# sourceMappingURL=parse-env-value.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"parse-env-value.d.ts","sourceRoot":"","sources":["../../src/env/parse-env-value.ts"],"names":[],"mappings":"AAEA;;;;;;;;GAQG;AACH,wBAAgB,qBAAqB,CACpC,KAAK,EAAE,MAAM,GAAG,SAAS,GAAG,IAAI,GAC9B,MAAM,GAAG,MAAM,GAAG,IAAI,CAMxB;AAED;;;;;;;GAOG;AACH,wBAAgB,mBAAmB,CAClC,KAAK,EAAE,MAAM,GAAG,SAAS,GAAG,IAAI,GAC9B,OAAO,GAAG,IAAI,CAqBhB"}
@@ -0,0 +1,44 @@
1
+ import { parseNumberOrBigInt } from "@ac-kit/core";
2
+ /**
3
+ * Read an environment variable value as a number.
4
+ *
5
+ * An integer beyond the safe range is returned as a `bigint`, so a value is
6
+ * never silently rounded.
7
+ *
8
+ * @param value The value to read.
9
+ * @returns The number or bigint it spells, or null if it spells neither.
10
+ */
11
+ export function parseEnvValueAsNumber(value) {
12
+ if (!value) {
13
+ return null;
14
+ }
15
+ return parseNumberOrBigInt(value);
16
+ }
17
+ /**
18
+ * Read an environment variable value as a boolean.
19
+ *
20
+ * Recognises every flavor `printEnvBoolValue` writes, case-insensitively.
21
+ *
22
+ * @param value The value to read.
23
+ * @returns The boolean it spells, or null if it spells none.
24
+ */
25
+ export function parseEnvValueAsBool(value) {
26
+ if (!value) {
27
+ return null;
28
+ }
29
+ switch (value.toLowerCase()) {
30
+ case "1":
31
+ case "true":
32
+ case "yes":
33
+ case "on":
34
+ return true;
35
+ case "0":
36
+ case "false":
37
+ case "no":
38
+ case "off":
39
+ return false;
40
+ default:
41
+ return null;
42
+ }
43
+ }
44
+ //# sourceMappingURL=parse-env-value.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"parse-env-value.js","sourceRoot":"","sources":["../../src/env/parse-env-value.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,cAAc,CAAC;AAEnD;;;;;;;;GAQG;AACH,MAAM,UAAU,qBAAqB,CACpC,KAAgC;IAEhC,IAAI,CAAC,KAAK,EAAE,CAAC;QACZ,OAAO,IAAI,CAAC;IACb,CAAC;IAED,OAAO,mBAAmB,CAAC,KAAK,CAAC,CAAC;AACnC,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,mBAAmB,CAClC,KAAgC;IAEhC,IAAI,CAAC,KAAK,EAAE,CAAC;QACZ,OAAO,IAAI,CAAC;IACb,CAAC;IAED,QAAQ,KAAK,CAAC,WAAW,EAAE,EAAE,CAAC;QAC7B,KAAK,GAAG,CAAC;QACT,KAAK,MAAM,CAAC;QACZ,KAAK,KAAK,CAAC;QACX,KAAK,IAAI;YACR,OAAO,IAAI,CAAC;QAEb,KAAK,GAAG,CAAC;QACT,KAAK,OAAO,CAAC;QACb,KAAK,IAAI,CAAC;QACV,KAAK,KAAK;YACT,OAAO,KAAK,CAAC;QAEd;YACC,OAAO,IAAI,CAAC;IACd,CAAC;AACF,CAAC"}
@@ -0,0 +1,36 @@
1
+ import type { EnvAssignment, EnvSyntax } from "./types.js";
2
+ export type ParseEnvAssignmentOptions = {
3
+ /** The surface syntax to read. Defaults to "assignment". */
4
+ syntax?: EnvSyntax;
5
+ };
6
+ /**
7
+ * Parse one environment variable assignment.
8
+ *
9
+ * A name with no `=` after it — `env(1)` and `docker --env` both accept one, to
10
+ * mean "inherit this variable" — parses to an empty value.
11
+ *
12
+ * @param line The assignment to parse.
13
+ * @param options Options for parsing the assignment.
14
+ * @returns The parsed assignment, or `null` when the line carries none: a blank
15
+ * line or a comment in the syntaxes that have them.
16
+ */
17
+ export declare function parseEnvAssignment(line: string, options?: ParseEnvAssignmentOptions): EnvAssignment | null;
18
+ export type ParseEnvOptions = ParseEnvAssignmentOptions & {
19
+ /** The surface syntax to read. Defaults to "dotenv". */
20
+ syntax?: EnvSyntax;
21
+ };
22
+ /**
23
+ * Parse a whole environment document into its variables, keyed by name.
24
+ *
25
+ * A later assignment to the same name wins, as it does in a shell. In the
26
+ * quoted syntaxes a value may span lines, which is how a PEM key or a JSON blob
27
+ * reaches a `.env` file.
28
+ *
29
+ * @param source The document to parse.
30
+ * @param options Options for parsing the document.
31
+ * @returns The parsed variables. Values are always strings; see
32
+ * `parseEnvValueAsNumber` and `parseEnvValueAsBool` to read one as something
33
+ * else.
34
+ */
35
+ export declare function parseEnv(source: string, options?: ParseEnvOptions): Record<string, string>;
36
+ //# sourceMappingURL=parse-env.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"parse-env.d.ts","sourceRoot":"","sources":["../../src/env/parse-env.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,aAAa,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAE3D,MAAM,MAAM,yBAAyB,GAAG;IACvC,4DAA4D;IAC5D,MAAM,CAAC,EAAE,SAAS,CAAC;CACnB,CAAC;AAOF;;;;;;;;;;GAUG;AACH,wBAAgB,kBAAkB,CACjC,IAAI,EAAE,MAAM,EACZ,OAAO,CAAC,EAAE,yBAAyB,GACjC,aAAa,GAAG,IAAI,CAwCtB;AAeD,MAAM,MAAM,eAAe,GAAG,yBAAyB,GAAG;IACzD,wDAAwD;IACxD,MAAM,CAAC,EAAE,SAAS,CAAC;CACnB,CAAC;AAMF;;;;;;;;;;;;GAYG;AACH,wBAAgB,QAAQ,CACvB,MAAM,EAAE,MAAM,EACd,OAAO,CAAC,EAAE,eAAe,GACvB,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CA8BxB"}
@@ -0,0 +1,104 @@
1
+ import { defaults, setRecordEntry } from "@ac-kit/core";
2
+ import { unquotePosixShWord } from "../command/unquote-posix-sh-word.js";
3
+ import { unquoteDotenvValue, unquoteWin32SetValue, unterminatedDotenvQuote, } from "./_env-quoting.js";
4
+ const PARSE_ENV_ASSIGNMENT_DEFAULT_OPTIONS_ = {
5
+ syntax: "assignment",
6
+ };
7
+ /**
8
+ * Parse one environment variable assignment.
9
+ *
10
+ * A name with no `=` after it — `env(1)` and `docker --env` both accept one, to
11
+ * mean "inherit this variable" — parses to an empty value.
12
+ *
13
+ * @param line The assignment to parse.
14
+ * @param options Options for parsing the assignment.
15
+ * @returns The parsed assignment, or `null` when the line carries none: a blank
16
+ * line or a comment in the syntaxes that have them.
17
+ */
18
+ export function parseEnvAssignment(line, options) {
19
+ const { syntax } = defaults(options, PARSE_ENV_ASSIGNMENT_DEFAULT_OPTIONS_);
20
+ // The bare form is one `execve` entry, so every byte after the first `=` is
21
+ // part of the value — including whitespace, quotes and `#`.
22
+ if (syntax === "assignment") {
23
+ const eqIndex = line.indexOf("=");
24
+ return eqIndex === -1
25
+ ? { name: line, value: "" }
26
+ : { name: line.slice(0, eqIndex), value: line.slice(eqIndex + 1) };
27
+ }
28
+ let rest = line.trim();
29
+ if (rest.length === 0 || rest.startsWith("#")) {
30
+ return null;
31
+ }
32
+ switch (syntax) {
33
+ case "dotenv":
34
+ case "posix-export":
35
+ rest = rest.replace(/^export\s+/, "");
36
+ break;
37
+ case "win32-set":
38
+ rest = rest.replace(/^set\s+/i, "");
39
+ if (rest.length >= 2 && rest.startsWith('"') && rest.endsWith('"')) {
40
+ rest = rest.slice(1, -1);
41
+ }
42
+ break;
43
+ }
44
+ const eqIndex = rest.indexOf("=");
45
+ if (eqIndex === -1) {
46
+ return { name: rest, value: "" };
47
+ }
48
+ const name = rest.slice(0, eqIndex).trimEnd();
49
+ const rawValue = rest.slice(eqIndex + 1);
50
+ return { name, value: unquoteEnvValue_(rawValue, syntax) };
51
+ }
52
+ function unquoteEnvValue_(rawValue, syntax) {
53
+ switch (syntax) {
54
+ case "posix-export":
55
+ return unquotePosixShWord(rawValue.trim());
56
+ case "win32-set":
57
+ return unquoteWin32SetValue(rawValue);
58
+ default:
59
+ return unquoteDotenvValue(rawValue);
60
+ }
61
+ }
62
+ const PARSE_ENV_DEFAULT_OPTIONS_ = {
63
+ syntax: "dotenv",
64
+ };
65
+ /**
66
+ * Parse a whole environment document into its variables, keyed by name.
67
+ *
68
+ * A later assignment to the same name wins, as it does in a shell. In the
69
+ * quoted syntaxes a value may span lines, which is how a PEM key or a JSON blob
70
+ * reaches a `.env` file.
71
+ *
72
+ * @param source The document to parse.
73
+ * @param options Options for parsing the document.
74
+ * @returns The parsed variables. Values are always strings; see
75
+ * `parseEnvValueAsNumber` and `parseEnvValueAsBool` to read one as something
76
+ * else.
77
+ */
78
+ export function parseEnv(source, options) {
79
+ const effectiveOptions = defaults(options, PARSE_ENV_DEFAULT_OPTIONS_);
80
+ const result = {};
81
+ const lines = source.split(/\r?\n/);
82
+ for (let i = 0; i < lines.length; i++) {
83
+ let line = lines[i];
84
+ if (effectiveOptions.syntax !== "assignment") {
85
+ const eqIndex = line.indexOf("=");
86
+ if (eqIndex !== -1) {
87
+ const quote = unterminatedDotenvQuote(line.slice(eqIndex + 1));
88
+ while (quote !== null && i < lines.length - 1) {
89
+ i++;
90
+ line += `\n${lines[i]}`;
91
+ if (unterminatedDotenvQuote(line.slice(eqIndex + 1)) === null) {
92
+ break;
93
+ }
94
+ }
95
+ }
96
+ }
97
+ const assignment = parseEnvAssignment(line, effectiveOptions);
98
+ if (assignment !== null && assignment.name.length > 0) {
99
+ setRecordEntry(result, assignment.name, assignment.value);
100
+ }
101
+ }
102
+ return result;
103
+ }
104
+ //# sourceMappingURL=parse-env.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"parse-env.js","sourceRoot":"","sources":["../../src/env/parse-env.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAExD,OAAO,EAAE,kBAAkB,EAAE,MAAM,qCAAqC,CAAC;AACzE,OAAO,EACN,kBAAkB,EAClB,oBAAoB,EACpB,uBAAuB,GACvB,MAAM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,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,KAAK,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;QACvC,IAAI,IAAI,GAAG,KAAK,CAAC,CAAC,CAAW,CAAC;QAE9B,IAAI,gBAAgB,CAAC,MAAM,KAAK,YAAY,EAAE,CAAC;YAC9C,MAAM,OAAO,GAAG,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;YAClC,IAAI,OAAO,KAAK,CAAC,CAAC,EAAE,CAAC;gBACpB,MAAM,KAAK,GAAG,uBAAuB,CAAC,IAAI,CAAC,KAAK,CAAC,OAAO,GAAG,CAAC,CAAC,CAAC,CAAC;gBAC/D,OAAO,KAAK,KAAK,IAAI,IAAI,CAAC,GAAG,KAAK,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;oBAC/C,CAAC,EAAE,CAAC;oBACJ,IAAI,IAAI,KAAK,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC;oBACxB,IAAI,uBAAuB,CAAC,IAAI,CAAC,KAAK,CAAC,OAAO,GAAG,CAAC,CAAC,CAAC,KAAK,IAAI,EAAE,CAAC;wBAC/D,MAAM;oBACP,CAAC;gBACF,CAAC;YACF,CAAC;QACF,CAAC;QAED,MAAM,UAAU,GAAG,kBAAkB,CAAC,IAAI,EAAE,gBAAgB,CAAC,CAAC;QAC9D,IAAI,UAAU,KAAK,IAAI,IAAI,UAAU,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;YACvD,cAAc,CAAC,MAAM,EAAE,UAAU,CAAC,IAAI,EAAE,UAAU,CAAC,KAAK,CAAC,CAAC;QAC3D,CAAC;IACF,CAAC;IAED,OAAO,MAAM,CAAC;AACf,CAAC"}
@@ -0,0 +1,59 @@
1
+ import type { EnvBoolFlavor, EnvSyntax, EnvValue, EnvVariables } from "./types.js";
2
+ /**
3
+ * Print a boolean environment variable value in the given flavor.
4
+ *
5
+ * @param value The boolean value to print.
6
+ * @param flavor How the boolean is spelled.
7
+ * @returns The printed boolean value.
8
+ */
9
+ export declare function printEnvBoolValue(value: boolean, flavor: EnvBoolFlavor): string;
10
+ export type PrintEnvValueOptions = {
11
+ /** How booleans are spelled. Defaults to "1/0". */
12
+ boolFlavor?: EnvBoolFlavor;
13
+ };
14
+ /**
15
+ * Print an environment variable value, unquoted.
16
+ *
17
+ * `null` prints as the empty string: an environment variable that exists with
18
+ * no value is the closest thing the environment has to an absent one.
19
+ *
20
+ * @param value The value to print.
21
+ * @param options Options for printing the value.
22
+ * @returns The printed value, with no quoting applied.
23
+ */
24
+ export declare function printEnvValue(value: EnvValue, options?: PrintEnvValueOptions): string;
25
+ export type PrintEnvAssignmentOptions = PrintEnvValueOptions & {
26
+ /** The surface syntax to print. Defaults to "assignment". */
27
+ syntax?: EnvSyntax;
28
+ };
29
+ /**
30
+ * Print one environment variable assignment in the given syntax, quoting the
31
+ * value as that syntax requires so that `parseEnvAssignment` reads back exactly
32
+ * the value given.
33
+ *
34
+ * @param name The environment variable name.
35
+ * @param value The environment variable value.
36
+ * @param options Options for printing the assignment.
37
+ * @returns The printed assignment.
38
+ * @throws {UnsupportedError} If the name contains `=`, NUL or a line break, or
39
+ * if the value has no representation in the requested syntax.
40
+ */
41
+ export declare function printEnvAssignment(name: string, value: EnvValue, options?: PrintEnvAssignmentOptions): string;
42
+ export type PrintEnvOptions = PrintEnvValueOptions & {
43
+ /** The surface syntax to print. Defaults to "dotenv". */
44
+ syntax?: EnvSyntax;
45
+ /** The line separator between assignments. Defaults to "\n". */
46
+ newline?: string;
47
+ };
48
+ /**
49
+ * Print a whole set of environment variables as a document, one assignment per
50
+ * line, terminated by a trailing line separator.
51
+ *
52
+ * @param variables The environment variables to print, keyed by name.
53
+ * @param options Options for printing the document.
54
+ * @returns The printed document, empty when there is no variable to print.
55
+ * @throws {UnsupportedError} If a name or a value has no representation in the
56
+ * requested syntax.
57
+ */
58
+ export declare function printEnv(variables: Readonly<EnvVariables>, options?: PrintEnvOptions): string;
59
+ //# sourceMappingURL=print-env.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"print-env.d.ts","sourceRoot":"","sources":["../../src/env/print-env.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EACX,aAAa,EACb,SAAS,EACT,QAAQ,EACR,YAAY,EACZ,MAAM,YAAY,CAAC;AASpB;;;;;;GAMG;AACH,wBAAgB,iBAAiB,CAChC,KAAK,EAAE,OAAO,EACd,MAAM,EAAE,aAAa,GACnB,MAAM,CAcR;AAED,MAAM,MAAM,oBAAoB,GAAG;IAClC,mDAAmD;IACnD,UAAU,CAAC,EAAE,aAAa,CAAC;CAC3B,CAAC;AAMF;;;;;;;;;GASG;AACH,wBAAgB,aAAa,CAC5B,KAAK,EAAE,QAAQ,EACf,OAAO,CAAC,EAAE,oBAAoB,GAC5B,MAAM,CAuBR;AAED,MAAM,MAAM,yBAAyB,GAAG,oBAAoB,GAAG;IAC9D,6DAA6D;IAC7D,MAAM,CAAC,EAAE,SAAS,CAAC;CACnB,CAAC;AAQF;;;;;;;;;;;GAWG;AACH,wBAAgB,kBAAkB,CACjC,IAAI,EAAE,MAAM,EACZ,KAAK,EAAE,QAAQ,EACf,OAAO,CAAC,EAAE,yBAAyB,GACjC,MAAM,CA4BR;AAED,MAAM,MAAM,eAAe,GAAG,oBAAoB,GAAG;IACpD,yDAAyD;IACzD,MAAM,CAAC,EAAE,SAAS,CAAC;IAEnB,gEAAgE;IAChE,OAAO,CAAC,EAAE,MAAM,CAAC;CACjB,CAAC;AAQF;;;;;;;;;GASG;AACH,wBAAgB,QAAQ,CACvB,SAAS,EAAE,QAAQ,CAAC,YAAY,CAAC,EACjC,OAAO,CAAC,EAAE,eAAe,GACvB,MAAM,CAYR"}
@@ -0,0 +1,120 @@
1
+ import { defaults, UnsupportedError } from "@ac-kit/core";
2
+ import { escapePosixShCommandArg } from "../command/escape-posix-sh-command-arg.js";
3
+ import { quoteDotenvValue, quoteWin32SetValue } from "./_env-quoting.js";
4
+ /**
5
+ * A name is rejected rather than escaped: every syntax here reads the name up
6
+ * to the first `=`, and a NUL cannot cross `execve` at all, so no spelling of
7
+ * either would survive a round trip.
8
+ */
9
+ const INVALID_ENV_NAME_RE_ = /[=\0\r\n]/;
10
+ /**
11
+ * Print a boolean environment variable value in the given flavor.
12
+ *
13
+ * @param value The boolean value to print.
14
+ * @param flavor How the boolean is spelled.
15
+ * @returns The printed boolean value.
16
+ */
17
+ export function printEnvBoolValue(value, flavor) {
18
+ switch (flavor) {
19
+ case "1/0":
20
+ return value ? "1" : "0";
21
+ case "true/false":
22
+ return value ? "true" : "false";
23
+ case "yes/no":
24
+ return value ? "yes" : "no";
25
+ case "on/off":
26
+ return value ? "on" : "off";
27
+ }
28
+ }
29
+ const PRINT_ENV_VALUE_DEFAULT_OPTIONS_ = {
30
+ boolFlavor: "1/0",
31
+ };
32
+ /**
33
+ * Print an environment variable value, unquoted.
34
+ *
35
+ * `null` prints as the empty string: an environment variable that exists with
36
+ * no value is the closest thing the environment has to an absent one.
37
+ *
38
+ * @param value The value to print.
39
+ * @param options Options for printing the value.
40
+ * @returns The printed value, with no quoting applied.
41
+ */
42
+ export function printEnvValue(value, options) {
43
+ const effectiveOptions = defaults(options, PRINT_ENV_VALUE_DEFAULT_OPTIONS_);
44
+ switch (typeof value) {
45
+ case "string":
46
+ return value;
47
+ case "number":
48
+ case "bigint":
49
+ return `${value}`;
50
+ case "boolean":
51
+ return printEnvBoolValue(value, effectiveOptions.boolFlavor);
52
+ case "object":
53
+ if (value === null) {
54
+ return "";
55
+ }
56
+ // else, fall through to throw
57
+ default:
58
+ throw new UnsupportedError(`value type "${typeof value}"`);
59
+ }
60
+ }
61
+ const PRINT_ENV_ASSIGNMENT_DEFAULT_OPTIONS_ = {
62
+ ...PRINT_ENV_VALUE_DEFAULT_OPTIONS_,
63
+ syntax: "assignment",
64
+ };
65
+ /**
66
+ * Print one environment variable assignment in the given syntax, quoting the
67
+ * value as that syntax requires so that `parseEnvAssignment` reads back exactly
68
+ * the value given.
69
+ *
70
+ * @param name The environment variable name.
71
+ * @param value The environment variable value.
72
+ * @param options Options for printing the assignment.
73
+ * @returns The printed assignment.
74
+ * @throws {UnsupportedError} If the name contains `=`, NUL or a line break, or
75
+ * if the value has no representation in the requested syntax.
76
+ */
77
+ export function printEnvAssignment(name, value, options) {
78
+ const { syntax, ...valueOptions } = defaults(options, PRINT_ENV_ASSIGNMENT_DEFAULT_OPTIONS_);
79
+ if (name.length === 0 || INVALID_ENV_NAME_RE_.test(name)) {
80
+ throw new UnsupportedError(`environment variable name "${name}"`);
81
+ }
82
+ const printedValue = printEnvValue(value, valueOptions);
83
+ switch (syntax) {
84
+ case "assignment":
85
+ if (printedValue.includes("\0")) {
86
+ throw new UnsupportedError("environment value containing NUL");
87
+ }
88
+ return `${name}=${printedValue}`;
89
+ case "dotenv":
90
+ return `${name}=${quoteDotenvValue(printedValue)}`;
91
+ case "posix-export":
92
+ return `export ${name}=${escapePosixShCommandArg(printedValue)}`;
93
+ case "win32-set":
94
+ return `set "${name}=${quoteWin32SetValue(printedValue)}"`;
95
+ }
96
+ }
97
+ const PRINT_ENV_DEFAULT_OPTIONS_ = {
98
+ ...PRINT_ENV_VALUE_DEFAULT_OPTIONS_,
99
+ syntax: "dotenv",
100
+ newline: "\n",
101
+ };
102
+ /**
103
+ * Print a whole set of environment variables as a document, one assignment per
104
+ * line, terminated by a trailing line separator.
105
+ *
106
+ * @param variables The environment variables to print, keyed by name.
107
+ * @param options Options for printing the document.
108
+ * @returns The printed document, empty when there is no variable to print.
109
+ * @throws {UnsupportedError} If a name or a value has no representation in the
110
+ * requested syntax.
111
+ */
112
+ export function printEnv(variables, options) {
113
+ const { newline, ...assignmentOptions } = defaults(options, PRINT_ENV_DEFAULT_OPTIONS_);
114
+ const lines = [];
115
+ for (const [name, value] of Object.entries(variables)) {
116
+ lines.push(printEnvAssignment(name, value, assignmentOptions));
117
+ }
118
+ return lines.length === 0 ? "" : `${lines.join(newline)}${newline}`;
119
+ }
120
+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kBAAkB,CAAC,YAAY,CAAC,GAAG,CAAC;IAC7D,CAAC;AACF,CAAC;AAUD,MAAM,0BAA0B,GAA8B;IAC7D,GAAG,gCAAgC;IACnC,MAAM,EAAE,QAAQ;IAChB,OAAO,EAAE,IAAI;CACb,CAAC;AAEF;;;;;;;;;GASG;AACH,MAAM,UAAU,QAAQ,CACvB,SAAiC,EACjC,OAAyB;IAEzB,MAAM,EAAE,OAAO,EAAE,GAAG,iBAAiB,EAAE,GAAG,QAAQ,CACjD,OAAO,EACP,0BAA0B,CAC1B,CAAC;IAEF,MAAM,KAAK,GAAa,EAAE,CAAC;IAC3B,KAAK,MAAM,CAAC,IAAI,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,SAAS,CAAC,EAAE,CAAC;QACvD,KAAK,CAAC,IAAI,CAAC,kBAAkB,CAAC,IAAI,EAAE,KAAK,EAAE,iBAAiB,CAAC,CAAC,CAAC;IAChE,CAAC;IAED,OAAO,KAAK,CAAC,MAAM,KAAK,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,GAAG,OAAO,EAAE,CAAC;AACrE,CAAC"}
@@ -0,0 +1,21 @@
1
+ import { type PrintEnvValueOptions } from "./print-env.js";
2
+ import type { EnvVariables } from "./types.js";
3
+ export type ProcessEnv = Record<string, string>;
4
+ /**
5
+ * Convert printable environment variables to the string record
6
+ * `child_process.spawn` and its siblings accept.
7
+ *
8
+ * @param variables The environment variables to convert.
9
+ * @param options Options for printing each value.
10
+ * @returns The variables with every value printed as a string.
11
+ */
12
+ export declare function toProcessEnv(variables: Readonly<EnvVariables>, options?: PrintEnvValueOptions): ProcessEnv;
13
+ /**
14
+ * Convert a `process.env`-shaped record to a string record, dropping the names
15
+ * that are present but unset.
16
+ *
17
+ * @param env The environment to convert.
18
+ * @returns The environment with every unset name removed.
19
+ */
20
+ export declare function fromProcessEnv(env: Readonly<Record<string, string | undefined>>): ProcessEnv;
21
+ //# sourceMappingURL=process-env.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"process-env.d.ts","sourceRoot":"","sources":["../../src/env/process-env.ts"],"names":[],"mappings":"AAEA,OAAO,EAAiB,KAAK,oBAAoB,EAAE,MAAM,gBAAgB,CAAC;AAC1E,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAE/C,MAAM,MAAM,UAAU,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;AAEhD;;;;;;;GAOG;AACH,wBAAgB,YAAY,CAC3B,SAAS,EAAE,QAAQ,CAAC,YAAY,CAAC,EACjC,OAAO,CAAC,EAAE,oBAAoB,GAC5B,UAAU,CAQZ;AAED;;;;;;GAMG;AACH,wBAAgB,cAAc,CAC7B,GAAG,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC,CAAC,GAC/C,UAAU,CAUZ"}