From 2d10a2ce0dc48572f7b19509b7323a135376701c Mon Sep 17 00:00:00 2001 From: Cameron Bernhardt Date: Tue, 21 Jul 2020 00:01:00 -0400 Subject: [PATCH] Expose programmatic initialization --- README.md | 23 ++++++++++++++++++++++- index.js | 11 +++++++++-- src/init.js | 13 +++++++++++++ src/mnotify.js | 2 +- 4 files changed, 45 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 72ccf0d..c89a744 100644 --- a/README.md +++ b/README.md @@ -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. \ No newline at end of file +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"); + } +}); +``` \ No newline at end of file diff --git a/index.js b/index.js index 758ca7c..e0f1a1b 100644 --- a/index.js +++ b/index.js @@ -1,2 +1,9 @@ -// Expose a notification function for programmatic use -module.exports = require("./src/mnotify"); \ No newline at end of file +// 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 +} \ No newline at end of file diff --git a/src/init.js b/src/init.js index ba50de5..f3fab77 100755 --- a/src/init.js +++ b/src/init.js @@ -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(() => { }); } \ No newline at end of file diff --git a/src/mnotify.js b/src/mnotify.js index 4cb181d..b0c390d 100755 --- a/src/mnotify.js +++ b/src/mnotify.js @@ -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"); } }