-
Notifications
You must be signed in to change notification settings - Fork 149
Expand file tree
/
Copy pathEventGridFileOpenStep.ts
More file actions
95 lines (77 loc) · 4.42 KB
/
EventGridFileOpenStep.ts
File metadata and controls
95 lines (77 loc) · 4.42 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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { AzExtFsExtra, AzureWizardExecuteStep, nonNullProp } from "@microsoft/vscode-azext-utils";
import * as os from 'os';
import * as path from "path";
import * as vscode from 'vscode';
import { type Progress } from "vscode";
import { ext } from "../../../extensionVariables";
import { localize } from "../../../localize";
import { feedUtils } from "../../../utils/feedUtils";
import { type EventGridExecuteFunctionContext } from "./EventGridExecuteFunctionContext";
export class EventGridFileOpenStep extends AzureWizardExecuteStep<EventGridExecuteFunctionContext> {
public priority: number;
public async execute(context: EventGridExecuteFunctionContext, progress: Progress<{ message?: string | undefined; increment?: number | undefined; }>): Promise<void> {
const eventSource = nonNullProp(context, 'eventSource');
const selectedFileName = nonNullProp(context, 'selectedFileName');
const selectedFileUrl = nonNullProp(context, 'selectedFileUrl');
// Get selected contents of sample request
const downloadingMsg: string = localize('downloadingSample', 'Downloading sample request...');
progress.report({ message: downloadingMsg });
const selectedFileContent = await feedUtils.getJsonFeed(context, selectedFileUrl);
// Create a temp file with the sample request & open in new window
const openingFileMsg: string = localize('openingFile', 'Opening file...');
progress.report({ message: openingFileMsg });
const tempFilePath: string = await createTempSampleFile(eventSource, selectedFileName, selectedFileContent);
const document: vscode.TextDocument = await vscode.workspace.openTextDocument(tempFilePath);
await vscode.window.showTextDocument(document, {
preview: false,
});
ext.fileToFunctionNodeMap.set(document.fileName, nonNullProp(ext, 'currentExecutingFunctionNode'));
context.fileOpened = true;
// Request will be sent when the user clicks on the button or on the codelens link
// Show the message only once per workspace
if (!ext.context.workspaceState.get('didShowEventGridFileOpenMsg')) {
const doneMsg = localize('modifyFile', "You can modify the file and then click the 'Save and execute' button to send the request.");
void vscode.window.showInformationMessage(doneMsg);
await ext.context.workspaceState.update('didShowEventGridFileOpenMsg', true);
}
// Set a listener to delete the temp file after it's closed
void new Promise<void>((resolve, reject) => {
const disposable = vscode.workspace.onDidCloseTextDocument(async (closedDocument) => {
if (closedDocument.fileName === document.fileName) {
try {
ext.fileToFunctionNodeMap.delete(document.fileName);
await AzExtFsExtra.deleteResource(tempFilePath);
resolve();
} catch (error) {
reject(error);
} finally {
disposable.dispose();
}
}
});
});
}
public shouldExecute(context: EventGridExecuteFunctionContext): boolean {
return !context.fileOpened
}
}
async function createTempSampleFile(eventSource: string, fileName: string, contents: {}): Promise<string> {
const samplesDirPath = await getSamplesDirPath(eventSource);
const sampleFileName = fileName.replace(/\.json$/, '.eventgrid.json');
const filePath: string = path.join(samplesDirPath, sampleFileName);
await AzExtFsExtra.writeJSON(filePath, contents);
return filePath;
}
async function getSamplesDirPath(eventSource: string): Promise<string> {
const baseDir: string = path.join(os.tmpdir(), 'vscode', 'azureFunctions', 'eventGridSamples');
// Create the path to the directory
const dirPath = path.join(baseDir, eventSource);
// Create the directory if it doesn't already exist
await AzExtFsExtra.ensureDir(dirPath);
// Return the path to the directory
return dirPath;
}