UNPKG

shartjs

Version:

When your app crashes, it farts. That's it.

80 lines (77 loc) 2.14 kB
// src/index.ts import { join } from "path"; import { readdirSync } from "fs"; import { platform } from "os"; import chalk from "chalk"; import { execa } from "execa"; // src/dirname.ts import path from "path"; import { fileURLToPath } from "url"; var __dirnameShim; if (typeof __dirname !== "undefined") { __dirnameShim = __dirname; } else { const __filename = fileURLToPath(import.meta.url); __dirnameShim = path.dirname(__filename); } var __dirnameSafe = __dirnameShim; // src/index.ts async function playSound(filePath) { const os = platform(); try { if (os === "darwin") { await execa("afplay", [filePath]); } else if (os === "win32") { await execa("powershell", [ "-c", `Start-Process -FilePath "${filePath}" -WindowStyle Hidden` ]); } else { const players = ["paplay", "aplay", "mpg123", "mplayer", "play"]; let played = false; for (const cmd of players) { try { await execa(cmd, [filePath]); played = true; break; } catch { } } if (!played) { console.error("\u{1F507} No compatible audio player found."); } } } catch (err) { console.error("\u{1F4A9} Failed to play fart sound:", err); } } async function playFart() { const fartDir = join(__dirnameSafe, "..", "farts"); const files = readdirSync(fartDir).filter((file) => file.endsWith(".wav")); if (files.length === 0) { console.error("\u{1F4A8} No fart files found."); return; } const selected = files[Math.floor(Math.random() * files.length)]; const fartPath = join(fartDir, selected); await playSound(fartPath); } function printMessage(err) { console.error(chalk.redBright.bold("\u{1F4A9} UNHANDLED ERROR \u{1F4A9}")); console.error(chalk.red(err?.stack || err?.message || err)); } function initShart() { process.on("uncaughtException", async (err) => { await playFart(); printMessage(err); process.exit(1); }); process.on("unhandledRejection", async (reason) => { await playFart(); printMessage(reason); process.exit(1); }); } export { initShart };