Skip to content

Instantly share code, notes, and snippets.

@JosXa
Created June 19, 2024 21:22
Show Gist options
  • Save JosXa/7c005a168561e7c5fd890db8f99817fe to your computer and use it in GitHub Desktop.
Save JosXa/7c005a168561e7c5fd890db8f99817fe to your computer and use it in GitHub Desktop.
// Name: Kill
// Description: Immediately terminates a Windows process using `taskkill /F /im <name.exe>`
// Keyword: kill
// Pass: /.*\.exe/gm
import "@johnlindquist/kit"
import { showError } from "@josxa/kit-utils"
import fkill from "fkill"
const cache = await db({ recents: ["node.exe"] as string[] })
let process =
arg.pass ??
(await arg({ placeholder: "Type the name of a program to kill", choices: cache.recents, alwaysOnTop: true }))
process = process.replace(/\.exe$/, "")
cache.recents = Array.from(new Set(cache.recents).add(process))
cache.write().then()
const onSuccess = async () => {
await div({
alwaysOnTop: false,
width: 100,
html: md("## ✅☠"),
onInit() {
setName("")
setDescription("Success")
setTimeout(exit, 1500)
},
})
}
try {
await fkill(process, { force: true, tree: true, ignoreCase: true, forceAfterTimeout: 5000 })
await onSuccess()
} catch (err) {
if (err instanceof Error && err.message.includes("There is no running instance of the task.")) {
await div(md("## There is no running instance of the task."))
} else if (err instanceof Error && err.message.includes("Killing process node failed")) {
// Ok, this is expected: https://github.com/sindresorhus/fkill/pull/30
await onSuccess()
} else {
await showError(err, "Failed to kill process")
}
exit()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment