Skip to content

Instantly share code, notes, and snippets.

@JosXa
Created May 23, 2024 18:19
Show Gist options
  • Save JosXa/c6bd4e6357b05289b7d4900d2a161554 to your computer and use it in GitHub Desktop.
Save JosXa/c6bd4e6357b05289b7d4900d2a161554 to your computer and use it in GitHub Desktop.
// Watch: ~/Downloads/*.{zip,7z}
import "@johnlindquist/kit"
import { extractArchive } from "../lib/sevenzip"
import "../lib/globals"
import fs from "node:fs"
import notifier from "node-notifier"
export const metadata: Metadata = {
name: "Extract archives in ~/Downlaods",
}
if (args.length < 2) {
exit()
}
const [filePath, event] = args
if (event !== "add") {
console.log({ filePath, event })
}
if (!filePath || event !== "add") {
exit()
}
const parsed = path.parse(filePath)
const targetDir = path.join(parsed.dir, parsed.name)
const started = performance.now()
await extractArchive(filePath, targetDir, {
onProgress(percent) {
// Show notification only if the extraction already took a while
if (percent < 20 && performance.now() - started > 2000) {
notifier.notify({ message: `Extracting ${path.basename(filePath)}`, timeout: 2 })
}
},
})
await new Promise<void>((resolve) => {
let busy = false
const notificationTimeoutSeconds = 8
notifier.notify(
{
message: "Extracted successfully.",
actions: ["Open and delete zip", "Open"],
timeout: notificationTimeoutSeconds,
},
(_, choice) => {
busy = true
if (choice === "open and delete zip") {
openInExplorer(targetDir).then(() => {
fs.unlinkSync(filePath)
resolve()
})
} else if (choice === "open") {
openInExplorer(targetDir).then(resolve)
}
},
)
wait(notificationTimeoutSeconds * 1000).then(() => {
if (!busy) {
resolve()
}
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment