Skip to content

Commit

Permalink
Add powershell helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
JerwuQu committed Apr 16, 2022
1 parent 5769b07 commit 1fec0d8
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/lib.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,39 @@ globalThis.setInterval = (fn, interval) => {
};
// TODO: clearInterval

globalThis.$quote = arg => {
// Sources:
// - https://stackoverflow.com/a/47469792
// - https://docs.microsoft.com/en-gb/archive/blogs/twistylittlepassagesallalike/everyone-quotes-command-line-arguments-the-wrong-way
if (!(/[ \t\n\v]/.exec(arg))) {
return arg;
}
let str = '"';
for (let i = 0; i < arg.length; i++) {
let slashes = 0;
while (i < arg.length && arg[i] === '\\') {
slashes++;
i++;
}
if (i === arg.length) {
str += '\\'.repeat(slashes * 2);
break;
} else if (arg[i] === '"') {
str += '\\'.repeat(slashes * 2 + 1) + arg[i];
} else {
str += '\\'.repeat(slashes) + arg[i];
}
}
return str + '"';
};

globalThis.$ps = async cmd => await $(`powershell -Command ${globalThis.$quote(cmd)}`);

globalThis.$psFetch = async url => {
const cmd = `$ProgressPreference='SilentlyContinue';$(Invoke-WebRequest '${url.replace(/'/g, "''")}').Content`;
return await globalThis.$ps(cmd);
};

setInterval(__wbc.yieldToC, 10);
setInterval(__wbc.checkBarSize, 100);

Expand Down

0 comments on commit 1fec0d8

Please sign in to comment.