UNPKG

node-notifier

Version:

A Node.js module for sending notifications on native Mac, Windows (post and pre 8) and Linux (or Growl as fallback)

101 lines (100 loc) • 3.13 kB
import callableClass from "../lib/callableClass.js"; import checkGrowl from "../lib/checkGrowl.js"; import utils from "../lib/utils.js"; import { vendorPath } from "../lib/vendor.js"; import Notifier$1 from "./growl.js"; import Notifier$2 from "./toaster.js"; import os from "node:os"; import { EventEmitter } from "node:events"; // src/notifiers/balloon.ts const notifier = vendorPath("notifu", "notifu"); let hasGrowl; var WindowsBalloon = class extends EventEmitter { options; #notify; constructor(options) { super(); this.options = utils.clone(options || {}); } get notify() { this.#notify ??= notifyRaw.bind(this); return this.#notify; } }; const Notifier = callableClass(WindowsBalloon); function noop() {} function notifyRaw(notification, userCallback) { const notifierOptions = this.options; const options = typeof notification === "string" ? { title: "node-notifier", message: notification } : utils.clone(notification || {}); const callback = userCallback || noop; const actionJackedCallback = utils.actionJackerDecorator(this, options, callback, (data) => { if (data === "activate") return "click"; if (data === "timeout") return "timeout"; return false; }); if (this.options.withFallback && utils.isWin8()) return new Notifier$2(notifierOptions).notify(options, callback); if (this.options.withFallback && (!utils.isLessThanWin8() || hasGrowl === true)) return new Notifier$1(notifierOptions).notify(options, callback); if (!this.options.withFallback || hasGrowl === false) { doNotification(options, notifierOptions, actionJackedCallback); return this; } checkGrowl(notifierOptions, (_, hasGrowlResult) => { hasGrowl = hasGrowlResult; if (hasGrowl) return new Notifier$1(notifierOptions).notify(options, callback); doNotification(options, notifierOptions, actionJackedCallback); }); return this; } const allowedArguments = [ "t", "d", "p", "m", "i", "e", "q", "w", "xp" ]; function doNotification(options, notifierOptions, callback) { const is64Bit = os.arch() === "x64"; options = utils.mapToNotifu(options); options.p = options.p || "Node Notification:"; const fullNotifierPath = `${notifier + (is64Bit ? "64" : "")}.exe`; const localNotifier = notifierOptions.customPath || fullNotifierPath; if (!options.m) { callback(/* @__PURE__ */ new Error("Message is required.")); return; } const argsList = utils.constructArgumentList(options, { wrapper: "", noEscape: true, explicitTrue: true, allowedArguments }); if (options.wait) { utils.fileCommand(localNotifier, argsList, (error, data) => { const code = error?.code; const action = fromErrorCodeToAction(code); if (action === "error") return callback(error, data); return callback(null, action); }); return; } utils.immediateFileCommand(localNotifier, argsList, callback); } function fromErrorCodeToAction(errorCode) { switch (errorCode) { case 2: return "timeout"; case 3: case 6: case 7: return "activate"; case 4: return "close"; default: return "error"; } } export { Notifier as default, Notifier as "module.exports" }; //# sourceMappingURL=balloon.js.map