export async function getBatteryInfo(): Promise<BatteryInfo> {
const command = `
& {
$battery = Get-WmiObject Win32_Battery
if ($battery) {
$output = @{
isCharging = $battery.BatteryStatus -eq 2
chargeLevel = $battery.EstimatedChargeRemaining
timeRemaining = $battery.EstimatedRunTime
}
$output | ConvertTo-Json
} else {
Write-Output "null"
}
}`;
const abort = new AbortController();
const cmd = new Deno.Command("powershell", {
args: [
"-Command",
command,
],
signal: abort.signal,
});
const { code, stdout, stderr } = await cmd.output();
if (code) {
const errorString = decoder.decode(stderr);
throw new Error(`Failed to execute PowerShell command: ${errorString}`);
}
const output = decoder.decode(stdout);
const stats: BatteryInfo = parse(output);
abort.abort();
return stats;
}
export async function getBatteryInfo(): Promise<BatteryInfo> {
const command = `
& {
$battery = Get-WmiObject Win32_Battery
if ($battery) {
$output = @{
isCharging = $battery.BatteryStatus -eq 2
chargeLevel = $battery.EstimatedChargeRemaining
timeRemaining = $battery.EstimatedRunTime
}
$output | ConvertTo-Json
} else {
Write-Output "null"
}
}`;
const abort = new AbortController();
const cmd = new Deno.Command("powershell", {
args: [
"-Command",
command,
],
signal: abort.signal,
});
const { code, stdout, stderr } = await cmd.output();
if (code) {
const errorString = decoder.decode(stderr);
throw new Error(`Failed to execute PowerShell command: ${errorString}`);
}
const output = decoder.decode(stdout);
const stats: BatteryInfo = parse(output);
abort.abort();
return stats;
}