-
Notifications
You must be signed in to change notification settings - Fork 1
/
templates.ts
105 lines (91 loc) · 2.36 KB
/
templates.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import prompts from 'prompts';
import got from 'got';
import { checkGithubAuth } from './utils';
const TEMPLATES_REPO_ID='270079803';
// https://github.com/aws-samples/aws-cdk-examples?files=1
const exampleLanguages = [
'typescript',
'python',
'java',
'csharp'
];
async function listRepoPath(repoId: string, path: string): Promise<any> {
const options = {};
const auth = checkGithubAuth();
if (auth) {
Object.assign(options, auth);
}
const endpoint = `https://api.github.com/repositories/${repoId}/${path}`;
const res = await got(endpoint, options);
return JSON.parse(res.body);
}
async function listTemplates(): Promise<any> {
try {
return listRepoPath(TEMPLATES_REPO_ID, `contents/templates`);
} catch (e) {
console.error(`Failed to list templates`);
process.exit(1);
}
}
async function chooseTemplate() {
const choices = await listTemplates();
const example = await prompts({
type: 'select',
name: 'value',
message: 'Select template',
choices: choices.map((choice: any) => {
return {
title: choice.name,
value: choice.name
}
})
});
if (!example.value) {
console.log('Please specify a template');
process.exit(1);
}
return example.value;
}
export async function chooseLanguage(): Promise<string> {
const language = await prompts({
type: 'select',
name: 'value',
message: 'Pick a language',
choices: exampleLanguages.map(language => {
return {
title: language,
value: language
}
})
});
if (!language.value) {
console.log('Please specify the language');
process.exit(1);
}
return language.value;
}
export async function promptForTemplate(): Promise<string> {
const template = await prompts({
type: 'select',
name: 'value',
message: 'Pick a template',
choices: [
{ title: 'Default cdk app', value: 'cdk-init' },
{ title: 'Example from the cdk-tools/templates repo', value: 'templates' }
],
});
if (!template.value) {
console.log('Please specify a template');
process.exit(1);
}
let choice: string;
if (template.value === 'cdk-init') {
choice = 'cdk-init';
} else if (template.value === 'templates') {
choice = `templates/${await chooseTemplate()}`;
} else {
console.error(`Unknown template: ${template.value}`);
process.exit(0);
}
return choice;
}