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

Commit 1434a77

Browse files
committed
Adds command to list available templates
1 parent 2dec54d commit 1434a77

File tree

4 files changed

+64
-0
lines changed

4 files changed

+64
-0
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"inquirer": "^6.2.2",
3535
"ora": "^3.2.0",
3636
"pkg-install": "^1.0.0",
37+
"twilio-run": "^2.0.0-beta.12",
3738
"yargs": "^12.0.5"
3839
},
3940
"engines": {

src/cli.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const yargs = require('yargs');
22
const { handler, describe, cliInfo } = require('./command');
3+
const listTemplates = require('./commands/list-templates');
34

45
function cli(cwd) {
56
yargs.help();
@@ -22,6 +23,11 @@ function cli(cwd) {
2223
},
2324
argv => handler(argv)
2425
);
26+
yargs.command({
27+
command: 'list-templates',
28+
desc: 'List the available templates you can create a project with.',
29+
handler: argv => listTemplates(argv)
30+
});
2531

2632
return yargs;
2733
}

src/commands/list-templates.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const { fetchListOfTemplates } = require('twilio-run/dist/templating/actions');
2+
const ora = require('ora');
3+
const chalk = require('chalk');
4+
5+
const listTemplates = async function() {
6+
const spinner = ora();
7+
spinner.start('Fetching available templates');
8+
9+
try {
10+
const templates = await fetchListOfTemplates();
11+
spinner.stop();
12+
templates.forEach(template =>
13+
console.log(
14+
chalk`‣ ${template.name} ({cyan ${template.id}})\n {dim ${
15+
template.description
16+
}}`
17+
)
18+
);
19+
} catch (err) {
20+
spinner.fail('Could not retrieve templates');
21+
process.exit(1);
22+
return;
23+
}
24+
};
25+
26+
module.exports = listTemplates;

tests/list-templates.test.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const templateActions = require('twilio-run/dist/templating/actions');
2+
const listTemplates = require('../src/commands/list-templates');
3+
4+
jest.mock('twilio-run/dist/templating/actions');
5+
6+
describe('listTemplates', () => {
7+
it('gets templates from twilio-run and displays them', async () => {
8+
const consoleSpy = jest
9+
.spyOn(global.console, 'log')
10+
.mockImplementation(() => {});
11+
const template = {
12+
name: 'Test template',
13+
id: 'test-template',
14+
description: 'A template to test with'
15+
};
16+
templateActions.fetchListOfTemplates.mockResolvedValue([template]);
17+
18+
await listTemplates();
19+
20+
expect(consoleSpy).toHaveBeenCalledTimes(1);
21+
expect(consoleSpy).toHaveBeenCalledWith(
22+
expect.stringContaining(template.name)
23+
);
24+
expect(consoleSpy).toHaveBeenCalledWith(
25+
expect.stringContaining(template.id)
26+
);
27+
expect(consoleSpy).toHaveBeenCalledWith(
28+
expect.stringContaining(template.description)
29+
);
30+
});
31+
});

0 commit comments

Comments
 (0)