@tiens.nguyen/gu-cli 1.0.930 → 1.0.932

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/win-shell.mjs ADDED
@@ -0,0 +1,26 @@
1
+ /**
2
+ * DEP0190 (GH #557 follow-up): Node 24 warns "Passing args to a child process with shell option
3
+ * true can lead to security vulnerabilities, as the arguments are not escaped, only concatenated"
4
+ * whenever an args array goes with `shell: true` — and the warning landed in the middle of gu's
5
+ * Windows output. A shell is still needed on Windows (npm is npm.cmd, and a .cmd runs only
6
+ * through cmd.exe), so the command is handed over the way the shell takes it anyway: ONE string,
7
+ * each part quoted where cmd.exe needs it, no args array.
8
+ *
9
+ * → { cmd, args, shell } for spawn/execFile. Off Windows nothing changes: no shell, args as given.
10
+ */
11
+ const PLAIN = /^[\w\-.=:\\/@,+]+$/;
12
+
13
+ /** One argument for a cmd.exe command line. */
14
+ export function cmdQuote(arg) {
15
+ const s = String(arg);
16
+ if (s !== "" && PLAIN.test(s)) return s;
17
+ // Inside quotes cmd.exe still treats `"` as the end, so a double quote is doubled. `%` and `!`
18
+ // would still expand; nothing gu passes here contains them except, possibly, a home folder,
19
+ // and that is no worse than the plain concatenation this replaces.
20
+ return `"${s.replace(/"/g, '""')}"`;
21
+ }
22
+
23
+ export function shellCommand(cmd, args = [], { platform = process.platform } = {}) {
24
+ if (platform !== "win32") return { cmd, args: [...args], shell: false };
25
+ return { cmd: [cmd, ...args].map(cmdQuote).join(" "), args: [], shell: true };
26
+ }