Skip to content

Commit 1fec0d8

Browse files
committed
Add powershell helpers
1 parent 5769b07 commit 1fec0d8

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

src/lib.mjs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,39 @@ globalThis.setInterval = (fn, interval) => {
1212
};
1313
// TODO: clearInterval
1414

15+
globalThis.$quote = arg => {
16+
// Sources:
17+
// - https://stackoverflow.com/a/47469792
18+
// - https://docs.microsoft.com/en-gb/archive/blogs/twistylittlepassagesallalike/everyone-quotes-command-line-arguments-the-wrong-way
19+
if (!(/[ \t\n\v]/.exec(arg))) {
20+
return arg;
21+
}
22+
let str = '"';
23+
for (let i = 0; i < arg.length; i++) {
24+
let slashes = 0;
25+
while (i < arg.length && arg[i] === '\\') {
26+
slashes++;
27+
i++;
28+
}
29+
if (i === arg.length) {
30+
str += '\\'.repeat(slashes * 2);
31+
break;
32+
} else if (arg[i] === '"') {
33+
str += '\\'.repeat(slashes * 2 + 1) + arg[i];
34+
} else {
35+
str += '\\'.repeat(slashes) + arg[i];
36+
}
37+
}
38+
return str + '"';
39+
};
40+
41+
globalThis.$ps = async cmd => await $(`powershell -Command ${globalThis.$quote(cmd)}`);
42+
43+
globalThis.$psFetch = async url => {
44+
const cmd = `$ProgressPreference='SilentlyContinue';$(Invoke-WebRequest '${url.replace(/'/g, "''")}').Content`;
45+
return await globalThis.$ps(cmd);
46+
};
47+
1548
setInterval(__wbc.yieldToC, 10);
1649
setInterval(__wbc.checkBarSize, 100);
1750

0 commit comments

Comments
 (0)