Skip to content

Commit

Permalink
Merge branch 'release/0.0.3'
Browse files Browse the repository at this point in the history
  • Loading branch information
DominusKelvin committed Jul 27, 2023
2 parents 6d021b1 + efeef38 commit e83d216
Show file tree
Hide file tree
Showing 11 changed files with 3,471 additions and 85 deletions.
1 change: 1 addition & 0 deletions .commitlintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = { extends: ['@commitlint/config-conventional'] }
22 changes: 22 additions & 0 deletions .github/workflows/prettier.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Prettier

on: [push, pull_request]

jobs:
prettier:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: 18

- name: Run npm ci
run: npm ci

- name: Run Prettier
run: npx prettier --config ./.prettierrc.js --write .
4 changes: 4 additions & 0 deletions .husky/commit-msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

npx --no -- commitlint --edit ${1}
4 changes: 4 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

npx lint-staged
42 changes: 29 additions & 13 deletions accessible/dry.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,39 @@
* Module dependencies
*/

const _ = require('@sailshq/lodash')
const LIBRARY_CONTENTS = require('../lib/private/LIBRARY_CONTENTS')

function reduceLibraryContents(libraryContents) {
const expandedPgInfoBySlug = {}

for (const [pgSlug, pgInfo] of Object.entries(libraryContents)) {
expandedPgInfoBySlug[pgSlug] = {
...pgInfo,
name: pgInfo.name || '.' + pgSlug,
defs: {}
}

if (pgSlug !== 'mail') {
throw new Error(
'Consistency violation: Encountered unrecognized pack/category: `' +
pgSlug +
'`. Please chose mail as pack/category'
)
}

for (const helperIdentity of libraryContents[pgSlug].methodIdts) {
expandedPgInfoBySlug[pgSlug].defs[
helperIdentity
] = require(`../lib/private/${pgSlug}/${helperIdentity}`)
}
}
return expandedPgInfoBySlug
}
const expandedPgInfoBySlug = reduceLibraryContents(LIBRARY_CONTENTS)

/**
* accessible/dry
*
* @type {Dictionary}
*/

module.exports = _.reduce(LIBRARY_CONTENTS, function (expandedPgInfoBySlug, pgInfo, pgSlug) {
expandedPgInfoBySlug[pgSlug] = _.extend({}, _.omit(pgInfo, 'methodIdts'), {
name: pgInfo.name || '.' + pgSlug,
defs: _.reduce(LIBRARY_CONTENTS[pgSlug].methodIdts, function (helpersByIdentity, helperIdentity) {
if (pgSlug !== 'mail') throw new Error('Consistency violation: Encountered unrecognized pack/category: `' + pgSlug + '`. Please chose mail as pack/category')
helpersByIdentity[helperIdentity] = require('../lib/private/' + pgSlug + '/' + helperIdentity)
return helpersByIdentity
}, {})// ∞
})// </ _.extend() >
return expandedPgInfoBySlug
}, {})// ∞
module.exports = expandedPgInfoBySlug
6 changes: 1 addition & 5 deletions lib/private/LIBRARY_CONTENTS.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,11 @@
*
* @type {Dictionary}
*/

module.exports = {

// for...
// Any Occasion
mail: {
description: 'Provides helpers to send emails in a Sails application',
methodIdts: [
'send',
]
methodIdts: ['send']
}
}
92 changes: 47 additions & 45 deletions lib/private/mail/send.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ module.exports = {
description: 'The mailer to used.',
extendedDescription:
'The mailer should be configured properly in config/mails.js. If not specified, the default mailer in sails.config.mail.default will be used',
defaultsTo: sails.config.mail.default,
isIn: ['log', 'smtp']
defaultsTo: sails.config.mail.default
},
template: {
description:
Expand Down Expand Up @@ -166,52 +165,55 @@ module.exports = {
return err
})

switch (mailer) {
case 'log':
const logMessage = `
Mailer is set to log so Sails is logging the email:
-=-=-=-=-=-=-=-=-=-=-=-=-= Email log -=-=-=-=-=-=-=-=-=-=-=-=-=
To: ${to}
Subject: ${subject}
if (
mailer == 'log' ||
sails.config.mail.mailers[mailer].transport == 'log'
) {
const logMessage = `
Mailer is set to log so Sails is logging the email:
-=-=-=-=-=-=-=-=-=-=-=-=-= Email log -=-=-=-=-=-=-=-=-=-=-=-=-=
To: ${to}
Subject: ${subject}
Body:
${html}
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
`
sails.log(logMessage)
break
case 'smtp':
const nodemailer = getModule('nodemailer')
var transporter = nodemailer.createTransport({
host: sails.config.smtp.host || sails.config.mail.mailers.smtp.host,
port: sails.config.smtp.port || sails.config.mail.mailers.smtp.port,
auth: {
user:
sails.config.smtp.username ||
sails.config.mail.mailers.smtp.username,
pass:
sails.config.smtp.password ||
sails.config.mail.mailers.smtp.password
}
})
Body:
${html}
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
`
sails.log(logMessage)
} else if (
mailer == 'smtp' ||
sails.config.mail.mailers[mailer].transport == 'smtp'
) {
const nodemailer = getModule('nodemailer')
var transporter = nodemailer.createTransport({
host:
sails.config[mailer].host || sails.config.mail.mailers[mailer].host,
port:
sails.config[mailer].port || sails.config.mail.mailers[mailer].port,
auth: {
user:
sails.config[mailer].username ||
sails.config.mail.mailers[mailer].username,
pass:
sails.config[mailer].password ||
sails.config.mail.mailers[mailer].password
}
})

const info = await transporter.sendMail({
from: {
name: fromName,
address: fromAddress
},
to,
subject,
text,
html
})
sails.log.debug('Message sent: %s', info.messageId)
break
default:
sails.log.error(`Unknown mailer: ${mailer}`)
break
const info = await transporter.sendMail({
from: {
name: fromName,
address: fromAddress
},
to,
subject,
text,
html
})
sails.log.debug('Message sent: %s', info.messageId)
} else {
sails.log.error(`Unknown mailer: ${mailer}`)
}

return {}
}
}
Expand Down
27 changes: 16 additions & 11 deletions lib/sails-hook-mail.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,29 @@
* sails-hook-mail
*
*/
const _ = require('@sailshq/lodash')
const DRY_PACKS_BY_SLUG = require('../accessible/dry')
module.exports = function (sails) {
return {
initialize: function (done) {
if (!sails.hooks.helpers) {
return done(new Error('Cannot load sails-hook-paystack without enabling the "helpers" hook!'))
return done(
new Error(
'Cannot load sails-hook-mail without enabling the "helpers" hook!'
)
)
}
sails.after('hook:helpers:loaded', function () {
try {
_.each(DRY_PACKS_BY_SLUG, (dryPack, slug) => {
_.each(dryPack.defs, (def, identity) => {
Object.entries(DRY_PACKS_BY_SLUG).forEach(([slug, dryPack]) => {
Object.entries(dryPack.defs).forEach(([identity, def]) => {
sails.hooks.helpers.furnishHelper(slug + '.' + identity, def)
})// ∞
})// ∞
} catch (error) { return done(error) }
})
})
} catch (error) {
return done(error)
}
return done()
})// _∏_
}//,
}// •
}// ƒ
})
}
}
}
Loading

0 comments on commit e83d216

Please sign in to comment.