node-notifier
Version:
A Node.js module for sending notifications on native Mac, Windows (post and pre 8) and Linux (or Growl as fallback)
73 lines (72 loc) • 2.25 kB
JavaScript
import callableClass from "../lib/callableClass.js";
import checkGrowl from "../lib/checkGrowl.js";
import utils from "../lib/utils.js";
import { EventEmitter } from "node:events";
import growly from "growly";
// src/notifiers/growl.ts
/**
* Wrapper for the growly module
*/
const errorMessageNotFound = "Couldn't connect to growl (might be used as a fallback). Make sure it is running";
let hasGrowl;
var Growl = class extends EventEmitter {
options;
#notify;
constructor(options) {
super();
options = utils.clone(options || {});
growly.appname = stripCarriageReturns(options.name) || "Node";
this.options = options;
}
get notify() {
this.#notify ??= notifyRaw.bind(this);
return this.#notify;
}
};
const Notifier = callableClass(Growl);
function notifyRaw(notification, userCallback) {
growly.setHost(this.options.host, this.options.port);
let options = typeof notification === "string" ? {
title: "node-notifier",
message: notification
} : utils.clone(notification || {});
const callback = utils.actionJackerDecorator(this, options, userCallback, (data) => {
if (data === "click") return "click";
if (data === "timedout") return "timeout";
return false;
});
options = utils.mapToGrowl(options);
if (!options.message) {
callback(/* @__PURE__ */ new Error("Message is required."));
return this;
}
options.title = options.title || "Node Notification:";
for (const key of GNTP_HEADER_OPTIONS) options[key] = stripCarriageReturns(options[key]);
if (hasGrowl || options.wait) {
const localCallback = options.wait ? callback : noop;
growly.notify(options.message, options, localCallback);
if (!options.wait) callback();
return this;
}
checkGrowl(growly, (_, didHaveGrowl) => {
hasGrowl = didHaveGrowl;
if (!didHaveGrowl) return callback(/* @__PURE__ */ new Error(errorMessageNotFound));
growly.notify(options.message, options);
callback();
});
return this;
}
const GNTP_HEADER_OPTIONS = [
"title",
"message",
"label",
"priority",
"coalescingId"
];
function stripCarriageReturns(value) {
if (typeof value !== "string") return value;
return value.replace(/\r\n?/g, "\n");
}
function noop() {}
export { Notifier as default, Notifier as "module.exports" };
//# sourceMappingURL=growl.js.map