Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextension.ts
More file actions
Latest commit
197 lines (170 loc) · 5.78 KB
/
Copy pathextension.ts
File metadata and controls
197 lines (170 loc) · 5.78 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/**
* lambda-lambda-lambda/vscode-extension
* VS Code extension to create a new L³ application.
*
* Copyright 2022-2025, Marc S. Brooks (https://mbrooks.info)
* Licensed under the MIT license:
* http://www.opensource.org/licenses/mit-license.php
*/
import{commands,window,workspace,ExtensionContext,Uri}from'vscode';
import{createFile,createFiles}from'@lambda-lambda-lambda/cli';
import{AppConfig}from'@lambda-lambda-lambda/types/cli';
// Local modules
import{InputBoxOpts,QuickPickOpts}from'./types';
/*
* Activate the VS Code extension.
*/
exportfunctionactivate(context: ExtensionContext){
context.subscriptions.push(
createApp(),
createResource('Middleware'),
createResource('Route')
);
}
/**
* Create new L³ application options.
*/
functioncreateApp(){
returncommands.registerCommand('lambda-lambda-lambda.createApp',async()=>{
constinputBoxTitle='L³: Create new application';
// Prompt application values.
constname: string|undefined=awaitpromptInputBox({
placeHolder: 'Application name (Example: restfulApiHandler)',
title: inputBoxTitle,
validateInput: value=>{
return(value&&/^[a-zA-Z0-9]{1,40}$/.test(value))
? undefined : 'Alphanumeric characters, no spaces';
},
step: 1
});
constdescription: string|undefined=awaitpromptInputBox({
placeHolder: 'Description',
title: inputBoxTitle,
validateInput: value=>{
return(value&&/^[\w-.,!?]{1,100}$/.test(value))
? undefined : 'Alphanumeric characters, .,!? punctuation';
},
step: 2
});
constprefix: string|undefined=awaitpromptInputBox({
placeHolder: 'Request prefix (Example: /api)',
title: inputBoxTitle,
validateInput: value=>{
return(value&&/^\/[\w-]{0,100}$/.test(value))
? undefined : 'Alphanumeric characters, must start with /';
},
step: 3
});
constasynchronous: boolean|undefined=(awaitpromptQuickPick({
placeHolder: 'Use asynchronous handler?',
title: inputBoxTitle,
items: [{label: 'Yes'},{label: 'No'}],
step: 4
})==='Yes');
consttimeout: string|undefined=awaitpromptInputBox({
placeHolder: 'Function timeout (in seconds)',
title: inputBoxTitle,
validateInput: value=>{
return(value&&/^[\d]{1,2}$/.test(value))
? undefined : 'Numbers only';
},
step: 5
});
constsdkVersion: string|undefined=awaitpromptQuickPick({
placeHolder: 'AWS SDK for JavaScript version',
title: inputBoxTitle,
items: [{label: '3'},{label: '2'}],
step: 6
});
construntime: string|undefined=awaitpromptQuickPick({
placeHolder: 'Node.js Lambda runtime identifier',
title: inputBoxTitle,
items: [{label: 'nodejs22.x'},{label: 'nodejs20.x'},{label: 'nodejs18.x'}],
step: 7
});
// Generate sources from templates.
if(description&&name&&prefix&&asynchronous&&timeout&&sdkVersion&&runtime){
constappConfig: AppConfig={
description, name, asynchronous, prefix, timeout, sdkVersion, runtime
};
awaitcreateFiles(appConfig,getWorkspace());
window.showInformationMessage('Created application sources.');
}else{
window.showErrorMessage('Failed to create application sources.');
}
});
}
/**
* Create new L³ application resource.
*/
functioncreateResource(type: string){
returncommands.registerCommand(`lambda-lambda-lambda.create${type}`,async(uri: Uri)=>{
constinputBoxTitle='L³: Create new application resource';
// Prompt application values.
constname: string|undefined=awaitpromptInputBox({
placeHolder: `${type} name (Example: ${type==='Route' ? 'Login' : 'BasicAuthHandler'})`,
title: inputBoxTitle,
validateInput: value=>{
return(value&&/^[a-zA-Z0-9]{1,40}$/.test(value))
? undefined : 'Alphanumeric characters, no spaces';
}
});
// Generate file from template.
if(name){
awaitcreateFile(name,uri.path,getWorkspace());
commands.executeCommand('workbench.files.action.refreshFilesExplorer');
}else{
window.showErrorMessage(`Failed to create ${type} resource`);
}
});
}
/**
* Create a new InputBox instance.
*/
functionpromptInputBox(opts: InputBoxOpts): Promise<string|undefined>{
returnnewPromise(resolve=>{
constinputBox=window.createInputBox();
inputBox.placeholder=opts.placeHolder;
inputBox.title=opts.title;
inputBox.value=opts.value||'';
inputBox.step=opts.step;
inputBox.totalSteps=5;
// Handle events.
inputBox.onDidAccept(()=>{
constvalue=inputBox.value;
consterror=opts.validateInput(value);
if(error){
inputBox.validationMessage=error;
}else{
inputBox.hide();
resolve(value);
}
});
inputBox.show();
});
}
/**
* Create a new QuickPick instance.
*/
functionpromptQuickPick(opts: QuickPickOpts): Promise<string|undefined>{
returnnewPromise(resolve=>{
constquickPick=window.createQuickPick();
quickPick.placeholder=opts.placeHolder;
quickPick.title=opts.title;
quickPick.items=opts.items;
quickPick.step=opts.step;
quickPick.totalSteps=5;
// Handle events.
quickPick.onDidChangeSelection(items=>resolve(items[0].label));
quickPick.onDidAccept(()=>quickPick.dispose());
quickPick.onDidHide(()=>quickPick.dispose());
quickPick.show();
});
}
/**
* Return the Workspace root path.
*/
functiongetWorkspace(): string{
return(workspace.workspaceFolders)
? workspace.workspaceFolders[0].uri.fsPath : './';
}