From 46019716dc701d8974718029dc38d62b97cab8b8 Mon Sep 17 00:00:00 2001 From: Phil Nash Date: Sat, 12 Sep 2020 01:14:39 +1000 Subject: [PATCH] feat: Makes file paths with tildes work for email:send (#218) 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. --- package-lock.json | 11 +++++++++++ package.json | 4 +++- src/services/file-io.js | 6 ++++-- test/commands/email/send.test.js | 12 ++++++++++++ 4 files changed, 30 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index e8d613897..12874f229 100644 --- a/package-lock.json +++ b/package-lock.json @@ -6021,6 +6021,12 @@ "resolved": "https://registry.npmjs.org/through/-/through-2.3.8.tgz", "integrity": "sha1-DdTJ/6q8NXlgsbckEV1+Doai4fU=" }, + "tildify": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/tildify/-/tildify-2.0.0.tgz", + "integrity": "sha512-Cc+OraorugtXNfs50hU9KS369rFXCfgGLpfCfvlc+Ud5u6VWmUQsOAa9HbTvheQdYnrdJqqv1e5oIqXppMYnSw==", + "dev": true + }, "tmp": { "version": "0.2.1", "resolved": "https://registry.npmjs.org/tmp/-/tmp-0.2.1.tgz", @@ -6187,6 +6193,11 @@ "resolved": "https://registry.npmjs.org/universalify/-/universalify-1.0.0.tgz", "integrity": "sha512-rb6X1W158d7pRQBg5gkR8uPaSfiids68LTJQYOtEUhoJUWBdaQHsuT/EUduxXYxcrt4r5PJ4fuHW1MHT6p0qug==" }, + "untildify": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/untildify/-/untildify-4.0.0.tgz", + "integrity": "sha512-KK8xQ1mkzZeg9inewmFVDNkg3l5LUhoq9kN6iWYB/CC9YMG8HA+c1Q8HwDe6dEX7kErrEVNVBO3fWsVq5iDgtw==" + }, "uri-js": { "version": "4.4.0", "resolved": "https://registry.npmjs.org/uri-js/-/uri-js-4.4.0.tgz", diff --git a/package.json b/package.json index 3d7f8af19..1a592971c 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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": { diff --git a/src/services/file-io.js b/src/services/file-io.js index 377a36802..4fdcd6ebc 100644 --- a/src/services/file-io.js +++ b/src/services/file-io.js @@ -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) => { @@ -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); diff --git a/test/commands/email/send.test.js b/test/commands/email/send.test.js index 0b1be5d37..a225e6813 100644 --- a/test/commands/email/send.test.js +++ b/test/commands/email/send.test.js @@ -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'); @@ -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(