diff --git a/commands/hello.js b/commands/hello.js index c61c6e4..0f82e7e 100644 --- a/commands/hello.js +++ b/commands/hello.js @@ -7,14 +7,14 @@ module.exports = team => { .prompt([ { type: 'list', - name: 'user', + name: 'name', message: 'Who the heck are you?', choices: team.map(({ name }) => name) } ]) - .then(({ user }) => { + .then(({ name }) => { console.log( - `Yay, it's ${chalk.bold.green(user)}! My favourite team member.` + `Yay, it's ${chalk.bold.green(name)}! My favourite team member.` ) }) } else { diff --git a/commands/notify.js b/commands/notify.js new file mode 100644 index 0000000..2b48c4d --- /dev/null +++ b/commands/notify.js @@ -0,0 +1,81 @@ +const chalk = require('chalk') +const inquirer = require('inquirer') +const nodemailer = require('nodemailer') + +const notifications = [ + { + subject: 'Reminder: production changes', + message: `It is important to remember that changes that have even the slightest chance of causing errors, should not be run directly on a production server (at least without testing somewhere else first). + +This includes changes such as: + - updating plugins + - changing config + - etc.`, + } +] + +module.exports = (team, email) => { + if (team.length) { + inquirer + .prompt([ + { + type: 'list', + name: 'name', + message: 'Who would you like to notify?', + choices: team.map(({ name }) => name), + }, + { + type: 'list', + name: 'service', + message: 'How would you like to notify them?', + choices: [ 'Email' ], + }, + { + type: 'list', + name: 'subject', + message: 'What nofification would you like to send?', + choices: notifications.map(({ subject }) => subject), + } + ]) + .then(({ name, service, subject }) => { + console.log(chalk.dim(`Sending notification to ${name}...`)) + + const mailgun = email[0] + const notification = notifications.filter(n => n.subject === subject)[0] + const to = team.filter(m => m.name === name)[0].email + + const transporter = nodemailer.createTransport({ + host: mailgun.host, + port: mailgun.ports[0], + secure: false, + auth: { + user: mailgun.user, + pass: mailgun.password, + }, + }) + + const options = { + from: '"S8 bot 🤖" ', + to, + subject: notification.subject, + text: `Hi ${name}! + +${notification.message} + +- +❤️ +S8 bot 🤖`, + } + + transporter.sendMail(options, (err, info) => { + if (err) { + return console.error(err) + } + + console.log(chalk.bold.green(`Notification sent!`)) + }) + }) + } else { + console.log(`${chalk.bold.red('No team members to notify.')}`) + } +} diff --git a/index.js b/index.js index fec4d8b..b527393 100755 --- a/index.js +++ b/index.js @@ -4,6 +4,7 @@ const chalk = require('chalk') const helloCmd = require('./commands/hello') const helpCmd = require('./commands/help') const installCmd = require('./commands/install') +const notifyCmd = require('./commands/notify') const setupCmd = require('./commands/setup') const teamCmd = require('./commands/team') const upgradeCmd = require('./commands/upgrade') @@ -18,8 +19,10 @@ try { config = require('@serieseight/config') hasSetup = true } catch (err) { - console.log(`${chalk.yellow(`You still need to run ${chalk.bold('s8 setup')} to complete installation. + if (cmd !== setup) { + console.log(`${chalk.yellow(`You still need to run ${chalk.bold('s8 setup')} to complete installation. Currently ${chalk.bold('s8')} does not have all the data it requires to function.`)}\n`) + } } switch(cmd) { @@ -42,6 +45,10 @@ switch(cmd) { installCmd(args) break + case 'notify': + notifyCmd(config.team, config.email) + break + case 'setup': setupCmd(hasSetup) break diff --git a/package-lock.json b/package-lock.json index 1c48e3f..e8a2085 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5586,6 +5586,11 @@ "semver": "^5.3.0" } }, + "nodemailer": { + "version": "4.6.8", + "resolved": "https://registry.npmjs.org/nodemailer/-/nodemailer-4.6.8.tgz", + "integrity": "sha512-A3s7EM/426OBIZbLHXq2KkgvmKbn2Xga4m4G+ZUA4IaZvG8PcZXrFh+2E4VaS2o+emhuUVRnzKN2YmpkXQ9qwA==" + }, "noop-logger": { "version": "0.1.1", "resolved": "https://registry.npmjs.org/noop-logger/-/noop-logger-0.1.1.tgz", diff --git a/package.json b/package.json index f3e95c1..d02afca 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,8 @@ }, "dependencies": { "chalk": "^2.4.1", - "inquirer": "^6.2.0" + "inquirer": "^6.2.0", + "nodemailer": "^4.6.8" }, "description": "A pointless CLI", "devDependencies": {