Skip to content

Commit

Permalink
feat: Makes file paths with tildes work for email:send (twilio#218)
Browse files Browse the repository at this point in the history
Supplying a file path for an email attachment with a tilde to mean the home directory when using email:send fails. Node.js doesn't support expanding ~ to os.homedir() by default, but the untildify package does create a safe way to achieve this.

I worked on this because I tried to send an attachment a number of times with files that definitely existed, but the CLI was telling me they couldn't be found. When I finally used an absolute path, it worked. I'd like to remove that frustration from anyone else that tries.
  • Loading branch information
philnash committed Sep 11, 2020
1 parent 457ae1d commit 4601971
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 3 deletions.
11 changes: 11 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@
"chalk": "^4.1.0",
"file-type": "^14.6.2",
"inquirer": "^7.3.0",
"twilio": "^3.48.2"
"twilio": "^3.48.2",
"untildify": "^4.0.0"
},
"devDependencies": {
"@oclif/dev-cli": "^1.22.2",
Expand All @@ -64,6 +65,7 @@
"nyc": "^15.1.0",
"proxyquire": "^2.1.3",
"sinon": "^9.0.2",
"tildify": "^2.0.0",
"tmp": "^0.2.1"
},
"optionalDependencies": {
Expand Down
6 changes: 4 additions & 2 deletions src/services/file-io.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const path = require('path');

const { TwilioCliError } = require('@twilio/cli-core').services.error;
const { logger } = require('@twilio/cli-core').services.logging;
const untildify = require('untildify');

function getStdin() {
return new Promise((resolve) => {
Expand All @@ -17,9 +18,10 @@ function getStdin() {

function readFile(filePath, encoding) {
try {
const resolvedFilePath = untildify(filePath);
return {
filename: path.basename(filePath),
content: fs.readFileSync(filePath, encoding),
filename: path.basename(resolvedFilePath),
content: fs.readFileSync(resolvedFilePath, encoding),
};
} catch (error) {
logger.debug(error);
Expand Down
12 changes: 12 additions & 0 deletions test/commands/email/send.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
const { resolve } = require('path');

const sinon = require('sinon');
const { expect, test } = require('@twilio/cli-test');
const { Config, ConfigData } = require('@twilio/cli-core').services.config;
const tildify = require('tildify');

const emailSend = require('../../../src/commands/email/send');

Expand Down Expand Up @@ -236,6 +239,15 @@ describe('commands', () => {
.catch(/Unable to read the file/)
.it('run email:send using flags to set information using invalid file path');

defaultSetup({
toEmail: 'jen@test.com',
flags: ['--attachment', tildify(resolve('test/commands/email/test.txt'))],
})
.do((ctx) => ctx.testCmd.run())
.it('run email:send using flags to set information using tilde in file path', (ctx) => {
expect(ctx.stderr).to.contain('test.txt');
});

defaultSetup({ toEmail: 'jen@test.com', attachmentVerdict: true })
.do((ctx) => ctx.testCmd.run())
.it(
Expand Down

0 comments on commit 4601971

Please sign in to comment.