-
Notifications
You must be signed in to change notification settings - Fork 133
/
createFunction.ts
85 lines (75 loc) · 4.38 KB
/
createFunction.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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { AzureWizard, type IActionContext } from '@microsoft/vscode-azext-utils';
import { type WorkspaceFolder } from 'vscode';
import { type FuncVersion } from '../../FuncVersion';
import { projectTemplateKeySetting, type ProjectLanguage } from '../../constants';
import { addLocalFuncTelemetry } from '../../funcCoreTools/getLocalFuncCoreToolsVersion';
import { type LocalProjectTreeItem } from '../../tree/localProject/LocalProjectTreeItem';
import { durableUtils } from '../../utils/durableUtils';
import { getContainingWorkspace, getRootWorkspaceFolder } from '../../utils/workspace';
import { getWorkspaceSetting } from '../../vsCodeConfig/settings';
import { verifyInitForVSCode } from '../../vsCodeConfig/verifyInitForVSCode';
import type * as api from '../../vscode-azurefunctions.api';
import { createNewProjectInternal } from '../createNewProject/createNewProject';
import { verifyProjectPath } from '../createNewProject/verifyIsProject';
import { FunctionListStep } from './FunctionListStep';
import { type IFunctionWizardContext } from './IFunctionWizardContext';
/**
* @deprecated Use AzureFunctionsExtensionApi.createFunction instead
*/
export async function createFunctionFromCommand(
context: IActionContext,
folderPath?: string | LocalProjectTreeItem | unknown,
templateId?: string | unknown[],
functionName?: string,
functionSettings?: { [key: string]: string | undefined },
language?: ProjectLanguage,
version?: FuncVersion): Promise<void> {
if (folderPath && typeof folderPath !== 'string') {
folderPath = undefined;
}
await createFunctionInternal(context, {
// if a tree element has been selected, it will be passed into the `folderProject` parameter as a BranchDataItemWrapper
folderPath: typeof folderPath === 'string' ? folderPath : undefined,
// if *multiple* tree elements are selected, they will be passed as an array as the `language` parameter as an array of BranchDataItemWrapper
templateId: typeof templateId === 'string' ? templateId : undefined,
functionName,
functionSettings,
language: <api.ProjectLanguage>language,
version: <api.ProjectVersion>version
});
}
export async function createFunctionInternal(context: IActionContext, options: api.ICreateFunctionOptions): Promise<void> {
let workspaceFolder: WorkspaceFolder | undefined;
let workspacePath: string | undefined = options.folderPath;
if (workspacePath === undefined) {
workspaceFolder = await getRootWorkspaceFolder();
workspacePath = workspaceFolder?.uri.fsPath;
} else {
workspaceFolder = getContainingWorkspace(workspacePath);
}
addLocalFuncTelemetry(context, workspacePath);
const projectPath: string | undefined = await verifyProjectPath(context, workspaceFolder || workspacePath);
if (!projectPath) {
// If we cannot find a valid Functions project, we need to put the user into the 'Create New Project' flow..
context.telemetry.properties.noWorkspaceResult = 'createNewProject';
await createNewProjectInternal(context, options);
return;
}
const { language, languageModel, version, templateSchemaVersion } = await verifyInitForVSCode(context, projectPath, options.language, options.languageModel, options.version);
const hasDurableStorage: boolean = await durableUtils.verifyHasDurableStorage(language, projectPath);
const projectTemplateKey: string | undefined = getWorkspaceSetting(projectTemplateKeySetting, projectPath);
const wizardContext: IFunctionWizardContext = Object.assign(context, options, { projectPath, workspacePath, workspaceFolder, version, language, languageModel, projectTemplateKey, hasDurableStorage, templateSchemaVersion });
const wizard: AzureWizard<IFunctionWizardContext> = new AzureWizard(wizardContext, {
promptSteps: [new FunctionListStep({
templateId: options.templateId,
functionSettings: options.functionSettings,
isProjectWizard: false,
})]
});
await wizard.prompt();
await wizard.execute();
}