Skip to content

Commit

Permalink
Expose programmatic initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
AstroCB committed Jul 21, 2020
1 parent bc37dd6 commit 2d10a2c
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 4 deletions.
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,25 @@ if (somethingHasGoneVeryWrong) {
}
```

This will send an alert to the pre-configured recipient from the sender's account, exactly as if triggered via CLI. If it is unable to deliver the notification due to a missing configuration file or failed login, the call to `notify` will throw, allowing you to fall back on other notification methods.
This will send an alert to the pre-configured recipient from the sender's account, exactly as if triggered via CLI. If it is unable to deliver the notification due to a missing configuration file or failed login, the call to `notify` will throw, allowing you to fall back on other notification methods.

As with the previous commands, you must initialize mnotify before this will work; if you've already done this via `mnotify --init`, you are good to go, but you can also do it programmatically:

> ### Note
> The code below is just an example for testing purposes; I don't recommend storing your login credentials in plaintext in any source file. In real world usage, you should store the required values below as environment variables or in some external gitignored file that won't be accidentally committed. When you call `init`, you should read in these values from the file or environment to pass as the first parameter.
```js
mnotify.init({
"senderEmail": "sender@example.com",
"senderPass": "*******",
"receiverEmail": "receiver@example.com",
"receiverPass": "*******",
"storeSenderCredentials": true
}, err => {
if (!err) {
mnotify.notify("Initialized successfully!");
} else {
console.error("Failed to initialize; oof");
}
});
```
11 changes: 9 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
// Expose a notification function for programmatic use
module.exports = require("./src/mnotify");
// Public API

module.exports = {
// Expose a notification function for programmatic use
"notify": require("./src/mnotify").notify,

// Expose initializastion for programmatic use
"init": require("./src/init").programmaticInit
}
13 changes: 13 additions & 0 deletions src/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,19 @@ exports.init = init;
exports.storePrefs = storePrefs;
exports.askToStore = askToStore;

exports.programmaticInit = (creds, callback) => {
storePrefs(creds.senderEmail,
creds.senderPass,
creds.receiverEmail,
creds.receiverPass,
creds.storeSenderCredentials,
getRecvApi,
(err, _) => {
callback(err);
}
);
};

if (require.main === module) {
init(() => { });
}
2 changes: 1 addition & 1 deletion src/mnotify.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ exports.notify = msg => {
loginWithConfig(tools.loadConfig(), msg, true);
} catch {
tools.printNoConfigError();
throw new Error("Unable to notify due to missing configuration");
throw new Error("Unable to notify due to missing configuration; you must initialize the package using `mnotify --init` or mnotify.init");
}
}

Expand Down

0 comments on commit 2d10a2c

Please sign in to comment.