Skip to content
This repository was archived by the owner on Feb 3, 2022. It is now read-only.

Commit be48350

Browse files
committed
Import credentials from environment if they are present.
Completes #5.
1 parent 9592945 commit be48350

File tree

5 files changed

+130
-5
lines changed

5 files changed

+130
-5
lines changed

src/cli.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,14 @@ function cli(cwd) {
3030
type: 'string'
3131
},
3232
'skip-credentials': {
33-
describe: "Don't ask for Twilio account credentials",
33+
describe:
34+
"Don't ask for Twilio account credentials or import them from the environment",
35+
type: 'boolean',
36+
default: false
37+
},
38+
'import-credentials': {
39+
describe:
40+
'Import credentials from the environment variables TWILIO_ACCOUNT_SID and TWILIO_AUTH_TOKEN',
3441
type: 'boolean',
3542
default: false
3643
}

src/create-twilio-function.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const {
66
createPackageJSON
77
} = require('./create-twilio-function/create-files');
88
const createGitignore = require('./create-twilio-function/create-gitignore');
9+
const importCredentials = require('./create-twilio-function/import-credentials');
910
const {
1011
installDependencies
1112
} = require('./create-twilio-function/install-dependencies');
@@ -17,7 +18,7 @@ async function createTwilioFunction(config) {
1718
try {
1819
await createDirectory(config.path, config.name);
1920
} catch (e) {
20-
console.log(
21+
console.error(
2122
`A directory called '${
2223
config.name
2324
}' already exists. Please create your function in a new directory.`
@@ -26,7 +27,10 @@ async function createTwilioFunction(config) {
2627
}
2728

2829
// Get account sid and auth token
29-
accountDetails = await promptForAccountDetails(config);
30+
let accountDetails = await importCredentials(config);
31+
if (Object.keys(accountDetails).length === 0) {
32+
accountDetails = await promptForAccountDetails(config);
33+
}
3034
config = { ...accountDetails, ...config };
3135

3236
// Scaffold project
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const inquirer = require('inquirer');
2+
3+
async function importCredentials(config) {
4+
if (
5+
config.skipCredentials ||
6+
(typeof process.env.TWILIO_ACCOUNT_SID === 'undefined' &&
7+
typeof process.env.TWILIO_AUTH_TOKEN === 'undefined')
8+
) {
9+
return {};
10+
}
11+
12+
const credentials = {
13+
accountSid: process.env.TWILIO_ACCOUNT_SID,
14+
authToken: process.env.TWILIO_AUTH_TOKEN
15+
};
16+
if (config.importCredentials) {
17+
return credentials;
18+
}
19+
const { importCredentials } = await inquirer.prompt([
20+
{
21+
type: 'confirm',
22+
name: 'importCredentials',
23+
message:
24+
'Your account credentials have been found in your environment variables. Import them?',
25+
default: true
26+
}
27+
]);
28+
if (importCredentials) {
29+
return credentials;
30+
} else {
31+
return {};
32+
}
33+
}
34+
35+
module.exports = importCredentials;

tests/create-twilio-function.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,13 @@ describe('createTwilioFunction', () => {
7575
it("doesn't scaffold if the target folder name already exists", async () => {
7676
const name = 'test-function';
7777
await mkdir('./scratch/test-function');
78-
console.log = jest.fn();
78+
console.error = jest.fn();
7979

8080
await createTwilioFunction({ name, path: './scratch' });
8181

8282
expect.assertions(2);
8383

84-
expect(console.log).toHaveBeenCalledTimes(1);
84+
expect(console.error).toHaveBeenCalledTimes(1);
8585

8686
try {
8787
await stat(`./scratch/${name}/package.json`);

tests/import-credentials.test.js

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
const importCredentials = require('../src/create-twilio-function/import-credentials');
2+
const inquirer = require('inquirer');
3+
4+
describe('importCredentials', () => {
5+
beforeEach(() => jest.clearAllMocks());
6+
7+
describe('if credentials are present in the env', () => {
8+
afterEach(() => {
9+
delete process.env.TWILIO_ACCOUNT_SID;
10+
delete process.env.TWILIO_AUTH_TOKEN;
11+
});
12+
13+
test('it should prompt to ask if to use credentials and return them if affirmative', async () => {
14+
process.env.TWILIO_ACCOUNT_SID = 'AC1234';
15+
process.env.TWILIO_AUTH_TOKEN = 'auth-token';
16+
17+
inquirer.prompt = jest.fn(() =>
18+
Promise.resolve({
19+
importCredentials: true
20+
})
21+
);
22+
23+
const credentials = await importCredentials({});
24+
expect(inquirer.prompt).toHaveBeenCalledTimes(1);
25+
expect(inquirer.prompt).toHaveBeenCalledWith(expect.any(Array));
26+
expect(credentials.accountSid).toBe('AC1234');
27+
expect(credentials.authToken).toBe('auth-token');
28+
});
29+
30+
test('it should prompt to ask if to use credentials and return an empty object if negative', async () => {
31+
process.env.TWILIO_ACCOUNT_SID = 'AC1234';
32+
process.env.TWILIO_AUTH_TOKEN = 'auth-token';
33+
34+
inquirer.prompt = jest.fn(() =>
35+
Promise.resolve({
36+
importCredentials: false
37+
})
38+
);
39+
40+
const credentials = await importCredentials({});
41+
expect(inquirer.prompt).toHaveBeenCalledTimes(1);
42+
expect(inquirer.prompt).toHaveBeenCalledWith(expect.any(Array));
43+
expect(credentials.accountSid).toBe(undefined);
44+
expect(credentials.authToken).toBe(undefined);
45+
});
46+
47+
test('it should return credentials if the option importCredentials is true', async () => {
48+
process.env.TWILIO_ACCOUNT_SID = 'AC1234';
49+
process.env.TWILIO_AUTH_TOKEN = 'auth-token';
50+
51+
const credentials = await importCredentials({ importCredentials: true });
52+
expect(inquirer.prompt).not.toHaveBeenCalled();
53+
expect(credentials.accountSid).toBe('AC1234');
54+
expect(credentials.authToken).toBe('auth-token');
55+
});
56+
57+
test('it should not return credentials if skipCredentials is true', async () => {
58+
process.env.TWILIO_ACCOUNT_SID = 'AC1234';
59+
process.env.TWILIO_AUTH_TOKEN = 'auth-token';
60+
61+
const credentials = await importCredentials({
62+
skipCredentials: true,
63+
importCredentials: true
64+
});
65+
expect(inquirer.prompt).not.toHaveBeenCalled();
66+
expect(credentials.accountSid).toBe(undefined);
67+
expect(credentials.authToken).toBe(undefined);
68+
});
69+
});
70+
71+
describe('if there are no credentials in the env', () => {
72+
test('it should not ask about importing credentials', async () => {
73+
delete process.env.TWILIO_ACCOUNT_SID;
74+
delete process.env.TWILIO_AUTH_TOKEN;
75+
await importCredentials({});
76+
expect(inquirer.prompt).not.toHaveBeenCalled();
77+
});
78+
});
79+
});

0 commit comments

Comments
 (0)