Add validation to init and create commands
This commit is contained in:
Родитель
7768e5cbd7
Коммит
281042513a
|
@ -127,4 +127,4 @@
|
|||
"extensionDependencies": [
|
||||
"ms-vscode.azure-account"
|
||||
]
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,13 +3,24 @@
|
|||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import * as vscode from 'vscode';
|
||||
import * as fs from 'fs';
|
||||
import * as opn from 'opn';
|
||||
import * as path from 'path';
|
||||
import * as util from './util';
|
||||
import * as vscode from 'vscode';
|
||||
|
||||
import { INode } from './nodes';
|
||||
import { FunctionsCli } from './functions-cli';
|
||||
import { QuickPickItemWithData } from './util';
|
||||
|
||||
const _expectedFunctionAppFiles = [
|
||||
"host.json",
|
||||
"local.settings.json",
|
||||
path.join(".vscode", "launch.json")
|
||||
];
|
||||
const _yes = "Yes";
|
||||
const _no = "No";
|
||||
|
||||
export function openInPortal(node?: INode) {
|
||||
if (node && node.tenantId) {
|
||||
opn(`https://portal.azure.com/${node.tenantId}/#resource${node.id}`);
|
||||
|
@ -17,20 +28,67 @@ export function openInPortal(node?: INode) {
|
|||
}
|
||||
|
||||
export async function createFunction(functionsCli: FunctionsCli) {
|
||||
const templates = [
|
||||
new QuickPickItemWithData("BlobTrigger"),
|
||||
new QuickPickItemWithData("EventGridTrigger"),
|
||||
new QuickPickItemWithData("HttpTrigger"),
|
||||
new QuickPickItemWithData("QueueTrigger"),
|
||||
new QuickPickItemWithData("TimerTrigger")
|
||||
];
|
||||
const template = await util.showQuickPick(templates, "Select a function template");
|
||||
const rootPath = vscode.workspace.rootPath;
|
||||
if (!rootPath) {
|
||||
throw new util.NoWorkspaceError();
|
||||
} else {
|
||||
const missingFiles = getMissingFunctionAppFiles(rootPath);
|
||||
if (missingFiles.length !== 0) {
|
||||
const result = await vscode.window.showWarningMessage(`The current folder is not initialized as a function app. Add missing files: '${missingFiles.join("', '")}'?`, _yes, _no);
|
||||
if (result === _yes) {
|
||||
functionsCli.initFunctionApp(rootPath);
|
||||
} else {
|
||||
throw new util.UserCancelledError();
|
||||
}
|
||||
}
|
||||
|
||||
const name = await util.showInputBox("Function Name", "Provide a function name");
|
||||
const templates = [
|
||||
new QuickPickItemWithData("BlobTrigger"),
|
||||
new QuickPickItemWithData("EventGridTrigger"),
|
||||
new QuickPickItemWithData("HttpTrigger"),
|
||||
new QuickPickItemWithData("QueueTrigger"),
|
||||
new QuickPickItemWithData("TimerTrigger")
|
||||
];
|
||||
const template = await util.showQuickPick(templates, "Select a function template");
|
||||
|
||||
functionsCli.createFunction(template.label, name);
|
||||
const name = await util.showInputBox("Function Name", "Provide a function name", (s) => validateTemplateName(rootPath, s));
|
||||
|
||||
functionsCli.createFunction(rootPath, template.label, name);
|
||||
}
|
||||
}
|
||||
|
||||
export async function initFunctionApp(functionsCli: FunctionsCli) {
|
||||
functionsCli.initFunctionApp("TODO");
|
||||
const rootPath = vscode.workspace.rootPath;
|
||||
if (!rootPath) {
|
||||
throw new util.NoWorkspaceError();
|
||||
} else {
|
||||
let result: string | undefined;
|
||||
const missingFiles = getMissingFunctionAppFiles(rootPath);
|
||||
if (missingFiles.length === 0) {
|
||||
await vscode.window.showWarningMessage("The current folder is already initialized as a function app.");
|
||||
return;
|
||||
} else if (missingFiles.length !== _expectedFunctionAppFiles.length) {
|
||||
result = await vscode.window.showWarningMessage(`The current folder is partially intialized. Add missing files: '${missingFiles.join("', '")}'?`, _yes, _no);
|
||||
} else {
|
||||
result = await vscode.window.showInformationMessage(`Initialize a function app in the current folder?`, _yes, _no);
|
||||
}
|
||||
|
||||
if (result !== _yes) {
|
||||
throw new util.UserCancelledError();
|
||||
}
|
||||
|
||||
functionsCli.initFunctionApp(rootPath);
|
||||
}
|
||||
}
|
||||
|
||||
function getMissingFunctionAppFiles(rootPath: string) {
|
||||
return _expectedFunctionAppFiles.filter(file => !fs.existsSync(path.join(rootPath, file)));
|
||||
}
|
||||
|
||||
function validateTemplateName(rootPath: string, name: string): string | undefined {
|
||||
if (!name) {
|
||||
return "The template name cannot be empty."
|
||||
} else if (fs.existsSync(path.join(rootPath, name))) {
|
||||
return `A folder with the name "${name}" already exists.`;
|
||||
}
|
||||
}
|
|
@ -4,23 +4,21 @@
|
|||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import * as vscode from 'vscode';
|
||||
import * as util from './util';
|
||||
|
||||
export class FunctionsCli {
|
||||
constructor(private readonly _terminal: vscode.Terminal) {
|
||||
}
|
||||
|
||||
createFunction(templateName: string, name: string) {
|
||||
this.executeCommand(`func new --language JavaScript --template ${templateName} --name ${name}`);
|
||||
createFunction(rootPath: string, templateName: string, name: string) {
|
||||
this.executeCommand(rootPath, `func new --language JavaScript --template ${templateName} --name ${name}`);
|
||||
}
|
||||
|
||||
async initFunctionApp(name: string) {
|
||||
this.executeCommand(`func init`);
|
||||
async initFunctionApp(rootPath: string) {
|
||||
this.executeCommand(rootPath, `func init`);
|
||||
}
|
||||
|
||||
private executeCommand(command: string) {
|
||||
// TODO: Verify terminal is in current working folder
|
||||
this._terminal.show();
|
||||
private executeCommand(rootPath: string, command: string) {
|
||||
this._terminal.sendText(`cd "${rootPath}"`);
|
||||
this._terminal.sendText(command);
|
||||
}
|
||||
}
|
10
src/util.ts
10
src/util.ts
|
@ -45,11 +45,11 @@ export async function showQuickPick<T>(items: QuickPickItemWithData<T>[] | Thena
|
|||
return result;
|
||||
}
|
||||
|
||||
export async function showInputBox(placeHolder: string, prompt: string): Promise<string> {
|
||||
export async function showInputBox(placeHolder: string, prompt: string, validateInput?: (s: string) => string | undefined | null): Promise<string> {
|
||||
const options: vscode.InputBoxOptions = {
|
||||
placeHolder: placeHolder,
|
||||
prompt: prompt,
|
||||
// TODO: validateInput
|
||||
validateInput: validateInput,
|
||||
ignoreFocusOut: true
|
||||
}
|
||||
const result = await vscode.window.showInputBox(options);
|
||||
|
@ -68,4 +68,8 @@ export class QuickPickItemWithData<T> implements vscode.QuickPickItem {
|
|||
}
|
||||
}
|
||||
|
||||
export class UserCancelledError extends Error { }
|
||||
export class UserCancelledError extends Error { }
|
||||
|
||||
export class NoWorkspaceError extends Error {
|
||||
message = "You must have a workspace open to perform this operation."
|
||||
}
|
Загрузка…
Ссылка в новой задаче