Skip to content

Commit

Permalink
feat: add '--no-attachment' option to 'email:send' (twilio#169)
Browse files Browse the repository at this point in the history
This option avoids an attachment prompt.
  • Loading branch information
childish-sambino committed Apr 14, 2020
1 parent 785c74c commit bb9de3d
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 123 deletions.
57 changes: 36 additions & 21 deletions src/commands/email/send.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ const { readFileOrStdIn, readFile } = require('../../services/file-io');
const sgMail = require('@sendgrid/mail');
const FileType = require('file-type');

const DEFAULT_ENCODING = 'base64';
const FLAGS = {
attachment: 'attachment',
noAttachment: 'no-attachment'
};

class Send extends BaseCommand {
async run() {
await super.run();
Expand Down Expand Up @@ -37,18 +43,21 @@ class Send extends BaseCommand {
html: '<p>' + this.emailText + '</p>'
};

const fileInfo = await readFileOrStdIn(this.flags.attachment);
if (!this.flags[FLAGS.noAttachment]) {
const fileInfo = await readFileOrStdIn(this.flags[FLAGS.attachment], DEFAULT_ENCODING);

if (fileInfo) {
sendInformation.attachments = await this.createAttachmentArray(fileInfo);
} else {
const attachmentVerdict = await this.askAttachment();
const attachment = await this.promptAttachment(attachmentVerdict);
if (fileInfo) {
sendInformation.attachments = await this.createAttachmentArray(fileInfo);
} else {
const attachmentVerdict = await this.askAttachment();
const attachment = await this.promptAttachment(attachmentVerdict);

if (attachment) {
sendInformation.attachments = await this.createAttachmentArray(readFile(attachment));
if (attachment) {
sendInformation.attachments = await this.createAttachmentArray(readFile(attachment, DEFAULT_ENCODING));
}
}
}

await this.sendEmail(sendInformation);
}

Expand Down Expand Up @@ -78,7 +87,7 @@ class Send extends BaseCommand {

async createAttachmentArray(fileInfo) {
// readFile and readFileOrStdIn return a base64 encoded string
const type = await FileType.fromBuffer(Buffer.from(fileInfo.content, 'base64'));
const type = await FileType.fromBuffer(Buffer.from(fileInfo.content, DEFAULT_ENCODING));

return [
{
Expand All @@ -92,26 +101,26 @@ class Send extends BaseCommand {
}

validateEmail(email) {
let emailList = [];
let emailList;
let validEmail = true;
const multipleEmail = email.includes(',');
if (multipleEmail === true) {
emailList = email.split(',').map(item => {
return item.trim();
});

if (email.includes(',')) {
emailList = email.split(',').map(item => item.trim());
} else {
emailList[0] = email;
emailList = [email];
}

emailList.forEach(emailAddress => {
if (emailUtilities.validateEmail(emailAddress) === false) {
this.logger.error(emailAddress + ' is not a valid email.');
if (!emailUtilities.validateEmail(emailAddress)) {
this.logger.error(`"${emailAddress}" is not a valid email.`);
validEmail = false;
}
});
if (validEmail === false) {

if (!validEmail) {
throw new TwilioCliError('Email could not be sent, please re-run the command with valid email addresses.');
}

return emailList;
}

Expand Down Expand Up @@ -185,8 +194,14 @@ Send.flags = Object.assign(
text: flags.string({
description: 'Text to send within the email body.'
}),
attachment: flags.string({
description: 'Path for the file that you want to attach.'
[FLAGS.attachment]: flags.string({
description: 'Path for the file that you want to attach.',
exclusive: [FLAGS.noAttachment]
}),
[FLAGS.noAttachment]: flags.boolean({
description: 'Do not include or prompt for an attachment.',
default: false,
exclusive: [FLAGS.attachment]
})
},
BaseCommand.flags
Expand Down
14 changes: 7 additions & 7 deletions src/services/file-io.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ const { logger } = require('@twilio/cli-core').services.logging;
const fs = require('fs');
const path = require('path');

async function readFileOrStdIn(filePath) {
async function readFileOrStdIn(filePath, encoding) {
if (filePath) {
return readFile(filePath);
return readFile(filePath, encoding);
}

const pipedInput = await readStream();
const pipedInput = await readStream(encoding);
if (pipedInput) {
return {
filename: 'piped.txt', // placeholder filename for attachment
Expand All @@ -17,21 +17,21 @@ async function readFileOrStdIn(filePath) {
}
}

function readFile(filePath) {
function readFile(filePath, encoding) {
try {
return {
filename: path.basename(filePath),
content: fs.readFileSync(filePath, 'base64')
content: fs.readFileSync(filePath, encoding)
};
} catch (error) {
logger.debug(error);
throw new TwilioCliError('Unable to read the file: ' + filePath);
}
}

async function readStream() {
async function readStream(encoding) {
const input = await getStdin();
return Buffer.from(input).toString('base64');
return Buffer.from(input).toString(encoding);
}

function getStdin() {
Expand Down
Loading

0 comments on commit bb9de3d

Please sign in to comment.