|
1 | 1 | import inquirer from 'inquirer'; |
2 | 2 | import path from 'path'; |
3 | 3 | import { getGithubFolderNames } from './get-github-folder-names'; |
4 | | -import { ACCEPTED_EXAMPLE_FOLDERS, ACCEPTED_LANGUAGES, BANNED_FUNCTION_NAMES } from './constants'; |
5 | | -import { GenerateFunctionSettings, AcceptedFunctionExamples, SourceName, GenerateFunctionOptions, Language } from '../types'; |
| 4 | +import { ACCEPTED_LANGUAGES, BANNED_FUNCTION_NAMES } from './constants'; |
| 5 | +import { GenerateFunctionSettings, Language } from '../types'; |
6 | 6 | import ora from 'ora'; |
7 | 7 | import chalk from 'chalk'; |
8 | 8 | import { warn } from './logger'; |
9 | 9 |
|
10 | | -export async function buildGenerateFunctionSettings() : Promise<GenerateFunctionSettings> { |
| 10 | +export async function buildGenerateFunctionSettingsInteractive() : Promise<GenerateFunctionSettings> { |
11 | 11 | const baseSettings = await inquirer.prompt<GenerateFunctionSettings>([ |
12 | 12 | { |
13 | 13 | name: 'name', |
14 | 14 | message: `Function name (${path.basename(process.cwd())}):`, |
15 | 15 | }, |
16 | | - { |
17 | | - name: 'sourceType', |
18 | | - message: 'Do you want to start with a blank template or use one of our examples?', |
19 | | - type: 'list', |
20 | | - choices: [ |
21 | | - { name: 'Template', value: 'template' }, |
22 | | - { name: 'Example', value: 'example' }, |
23 | | - ], |
24 | | - default: 'template', |
25 | | - } |
26 | 16 | ]); |
27 | 17 | if (BANNED_FUNCTION_NAMES.includes(baseSettings.name)) { |
28 | 18 | throw new Error(`Invalid function name: ${baseSettings.name}`); |
29 | 19 | } |
30 | | - let sourceSpecificSettings : GenerateFunctionSettings; |
31 | | - if (baseSettings.sourceType === 'template') { |
32 | | - sourceSpecificSettings = await inquirer.prompt<GenerateFunctionSettings>([ |
33 | | - { |
34 | | - name: 'language', |
35 | | - message: 'Pick a template', |
36 | | - type: 'list', |
37 | | - choices: [ |
38 | | - { name: 'TypeScript', value: 'typescript' }, |
39 | | - { name: 'JavaScript', value: 'javascript' }, |
40 | | - ], |
41 | | - default: 'typescript', |
42 | | - } |
43 | | - ]) |
44 | | - sourceSpecificSettings.sourceName = sourceSpecificSettings.language.toLowerCase() as SourceName |
45 | | - } else { |
46 | | - const availableExamples = await getGithubFolderNames(); |
47 | | - const filteredExamples = availableExamples.filter( |
48 | | - (template) => |
49 | | - ACCEPTED_EXAMPLE_FOLDERS.includes(template as (typeof ACCEPTED_EXAMPLE_FOLDERS)[number]) |
50 | | - ); |
| 20 | + const filteredSources = await getGithubFolderNames(); |
51 | 21 |
|
52 | | - sourceSpecificSettings = await inquirer.prompt<GenerateFunctionSettings>([ |
53 | | - { |
54 | | - name: 'sourceName', |
55 | | - message: 'Select an example:', |
56 | | - type: 'list', |
57 | | - choices: filteredExamples, |
58 | | - }, |
59 | | - { |
60 | | - name: 'language', |
61 | | - message: 'Pick a template', |
62 | | - type: 'list', |
63 | | - choices: [ |
64 | | - { name: 'TypeScript', value: 'typescript' }, |
65 | | - { name: 'JavaScript', value: 'javascript' }, |
66 | | - ], |
67 | | - default: 'typescript', |
68 | | - } |
69 | | - ]) |
70 | | - } |
71 | | - baseSettings.sourceName = sourceSpecificSettings.sourceName |
72 | | - baseSettings.language = sourceSpecificSettings.language |
73 | | - return baseSettings |
| 22 | + const sourceSpecificSettings = await inquirer.prompt<GenerateFunctionSettings>([ |
| 23 | + { |
| 24 | + name: 'example', |
| 25 | + message: 'Select an example:', |
| 26 | + type: 'list', |
| 27 | + choices: filteredSources, |
| 28 | + }, |
| 29 | + { |
| 30 | + name: 'language', |
| 31 | + message: 'Select a language', |
| 32 | + type: 'list', |
| 33 | + choices: [ |
| 34 | + { name: 'TypeScript', value: 'typescript' }, |
| 35 | + { name: 'JavaScript', value: 'javascript' }, |
| 36 | + ], |
| 37 | + default: 'typescript', |
| 38 | + } |
| 39 | + ]) |
| 40 | + baseSettings.example = sourceSpecificSettings.example |
| 41 | + baseSettings.language = sourceSpecificSettings.language |
| 42 | + return baseSettings |
74 | 43 | } |
75 | 44 |
|
76 | | -function validateArguments(options: GenerateFunctionOptions) { |
77 | | - const templateRequired = ['name', 'template']; |
78 | | - const exampleRequired = ['name', 'example', 'language']; |
79 | | - if (BANNED_FUNCTION_NAMES.includes(options.name)) { |
| 45 | +function validateArguments(options: GenerateFunctionSettings) { |
| 46 | + const requiredParams = ['name', 'example', 'language']; |
| 47 | + if (!requiredParams.every((key) => key in options)) { |
| 48 | + throw new Error('You must specify a function name, an example, and a language'); |
| 49 | + } |
| 50 | + if (BANNED_FUNCTION_NAMES.includes(options.name)) { |
80 | 51 | throw new Error(`Invalid function name: ${options.name}`); |
81 | 52 | } |
82 | | - if ('template' in options) { |
83 | | - if (!templateRequired.every((key) => key in options)) { |
84 | | - throw new Error('You must specify a function name and a template'); |
| 53 | + // Convert options to lowercase and trim whitespace |
| 54 | + for (const key in options) { |
| 55 | + const optionKey = key as keyof GenerateFunctionSettings; |
| 56 | + const value = options[optionKey].toLowerCase().trim(); |
| 57 | + |
| 58 | + if (optionKey === 'language') { |
| 59 | + // Assert that the value is of type Language |
| 60 | + options[optionKey] = value as Language; |
| 61 | + } else { |
| 62 | + options[optionKey] = value; |
85 | 63 | } |
86 | | - } else if ('example' in options) { |
87 | | - if (!exampleRequired.every((key) => key in options)) { |
88 | | - throw new Error('You must specify a function name, an example, and a language'); |
89 | | - } |
90 | | - } else { |
91 | | - throw new Error('You must specify either --template or --example'); |
92 | 64 | } |
93 | 65 | } |
94 | 66 |
|
95 | | -export async function buildGenerateFunctionSettingsFromOptions(options: GenerateFunctionOptions) : Promise<GenerateFunctionSettings> { |
| 67 | +export async function buildGenerateFunctionSettingsCLI(options: GenerateFunctionSettings) : Promise<GenerateFunctionSettings> { |
96 | 68 | const validateSpinner = ora('Validating your input\n').start(); |
97 | 69 | const settings: GenerateFunctionSettings = {} as GenerateFunctionSettings; |
98 | 70 | try { |
99 | 71 | validateArguments(options); |
100 | | - for (const key in options) { // convert all options to lowercase and trim |
101 | | - const optionKey = key as keyof GenerateFunctionOptions; |
102 | | - options[optionKey] = options[optionKey].toLowerCase().trim(); |
103 | | - } |
104 | | - |
105 | | - if ('example' in options) { |
106 | | - if ('template' in options) { |
107 | | - throw new Error('Cannot specify both --template and --example'); |
108 | | - } |
109 | | - |
110 | | - if (!ACCEPTED_EXAMPLE_FOLDERS.includes(options.example as AcceptedFunctionExamples)) { |
111 | | - throw new Error(`Invalid example name: ${options.example}`); |
112 | | - } |
113 | 72 |
|
114 | | - if (!ACCEPTED_LANGUAGES.includes(options.language)) { |
115 | | - warn(`Invalid language: ${options.language}. Defaulting to TypeScript.`); |
116 | | - settings.language = 'typescript'; |
117 | | - } else { |
118 | | - settings.language = options.language; |
119 | | - } |
120 | | - settings.sourceType = 'example'; |
121 | | - settings.sourceName = options.example; |
122 | | - settings.name = options.name; |
123 | | - |
124 | | - } else if ('template' in options) { |
125 | | - if ('language' in options && options.language && options.language != options.template) { |
126 | | - console.warn(`Ignoring language option: ${options.language}. Defaulting to ${options.template}.`); |
127 | | - } |
128 | | - if (!ACCEPTED_LANGUAGES.includes(options.template as Language)) { |
129 | | - console.warn(`Invalid language: ${options.template}. Defaulting to TypeScript.`); |
130 | | - settings.language = 'typescript'; |
131 | | - settings.sourceName = 'typescript'; |
132 | | - } else { |
133 | | - settings.language = options.template as Language; |
134 | | - settings.sourceName = options.template; |
135 | | - } |
136 | | - settings.sourceType = 'template'; |
137 | | - settings.name = options.name; |
| 73 | + // Check if the source exists |
| 74 | + const filteredSources = await getGithubFolderNames(); |
| 75 | + if (!filteredSources.includes(options.example)) { |
| 76 | + throw new Error(`Invalid example name: ${options.example}. Please choose from: ${filteredSources.join(', ')}`); |
| 77 | + } |
| 78 | + |
| 79 | + // Check if the language is valid |
| 80 | + if (!ACCEPTED_LANGUAGES.includes(options.language)) { |
| 81 | + warn(`Invalid language: ${options.language}. Defaulting to TypeScript.`); |
| 82 | + settings.language = 'typescript'; |
| 83 | + } else { |
| 84 | + settings.language = options.language; |
138 | 85 | } |
139 | 86 |
|
| 87 | + settings.example = options.example; |
| 88 | + settings.name = options.name; |
140 | 89 | return settings; |
141 | 90 | } catch (err: any) { |
142 | 91 | console.log(` |
|
0 commit comments