-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathEnvFileListStep.ts
More file actions
147 lines (124 loc) · 7.63 KB
/
EnvFileListStep.ts
File metadata and controls
147 lines (124 loc) · 7.63 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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { type EnvironmentVar } from "@azure/arm-appcontainers";
import { AzExtFsExtra, AzureWizardPromptStep, GenericTreeItem, activitySuccessContext, activitySuccessIcon, createUniversallyUniqueContextValue } from "@microsoft/vscode-azext-utils";
import { parse, type DotenvParseOutput } from "dotenv";
import { RelativePattern, workspace, type Uri, type WorkspaceFolder } from "vscode";
import { ImageSource, envFileGlobPattern } from "../../../constants";
import { ext } from "../../../extensionVariables";
import { type EnvironmentVariableTelemetryProps as TelemetryProps } from "../../../telemetry/ImageSourceTelemetryProps";
import { type SetTelemetryProps } from "../../../telemetry/SetTelemetryProps";
import { localize } from "../../../utils/localize";
import { selectWorkspaceFile } from "../../../utils/workspaceUtils";
import { type EnvironmentVariablesContext } from "../../environmentVariables/EnvironmentVariablesContext";
export interface EnvFileListStepOptions {
suppressSkipPick?: boolean;
}
export enum SetEnvironmentVariableOption {
NoDotEnv = 'noDotEnv',
SkipForNow = 'skipForNow',
ProvideFile = 'provideFile',
UseExisting = 'useExisting'
}
type EnvFileListContext = EnvironmentVariablesContext & SetTelemetryProps<TelemetryProps>;
const allEnvFilesGlobPattern: string = `**/${envFileGlobPattern}`;
export class EnvFileListStep<T extends EnvFileListContext> extends AzureWizardPromptStep<T> {
private _setEnvironmentVariableOption?: SetEnvironmentVariableOption;
constructor(public readonly options?: EnvFileListStepOptions) {
super();
}
public async prompt(context: T): Promise<void> {
const existingData = context.template?.containers?.[context.containersIdx ?? 0].env ?? context.containerApp?.template?.containers?.[context.containersIdx ?? 0].env;
context.envPath ??= await this.promptForEnvPath(context, !!existingData /** showHasExistingData */);
if (!context.envPath && existingData) {
context.environmentVariables = existingData;
this._setEnvironmentVariableOption = SetEnvironmentVariableOption.UseExisting;
} else {
context.environmentVariables = await this.parseEnvironmentVariablesFromEnvPath(context.envPath);
}
if (this._setEnvironmentVariableOption) {
this.outputLogs(context, this._setEnvironmentVariableOption);
}
}
public async configureBeforePrompt(context: T): Promise<void> {
if (context.environmentVariables?.length === 0) {
context.telemetry.properties.environmentVariableFileCount = '0';
this._setEnvironmentVariableOption = SetEnvironmentVariableOption.NoDotEnv;
}
if (this._setEnvironmentVariableOption) {
this.outputLogs(context, this._setEnvironmentVariableOption);
}
}
public shouldPrompt(context: T): boolean {
return context.imageSource !== ImageSource.QuickstartImage && context.environmentVariables === undefined;
}
private async promptForEnvPath(context: T, showHasExistingData?: boolean): Promise<string | undefined> {
const placeHolder: string = localize('setEnvVar', 'Select a {0} file to set the environment variables for the container instance', '.env');
const skipLabel: string | undefined = showHasExistingData ? localize('useExisting', 'Use existing configuration') : undefined;
return await selectWorkspaceFile(context, placeHolder,
{ filters: { 'env file': ['env', 'env.*'] }, allowSkip: !this.options?.suppressSkipPick, skipLabel }, allEnvFilesGlobPattern);
}
private async parseEnvironmentVariablesFromEnvPath(envPath: string | undefined): Promise<EnvironmentVar[]> {
if (!envPath || !await AzExtFsExtra.pathExists(envPath)) {
this._setEnvironmentVariableOption = SetEnvironmentVariableOption.SkipForNow;
return [];
}
this._setEnvironmentVariableOption = SetEnvironmentVariableOption.ProvideFile;
const data: string = await AzExtFsExtra.readFile(envPath);
const envData: DotenvParseOutput = parse(data);
return Object.keys(envData).map(name => { return { name, value: envData[name] } });
}
public static async workspaceHasEnvFile(rootFolder?: WorkspaceFolder): Promise<boolean> {
let envFileUris: Uri[];
if (rootFolder) {
const relativePattern: RelativePattern = new RelativePattern(rootFolder, allEnvFilesGlobPattern);
envFileUris = await workspace.findFiles(relativePattern);
} else {
envFileUris = await workspace.findFiles(allEnvFilesGlobPattern);
}
return !!envFileUris.length;
}
private outputLogs(context: T, setEnvironmentVariableOption: SetEnvironmentVariableOption): void {
context.telemetry.properties.setEnvironmentVariableOption = setEnvironmentVariableOption;
if (
setEnvironmentVariableOption === SetEnvironmentVariableOption.NoDotEnv ||
setEnvironmentVariableOption === SetEnvironmentVariableOption.SkipForNow
) {
context.activityChildren?.push(
new GenericTreeItem(undefined, {
contextValue: createUniversallyUniqueContextValue(['envFileListStepSuccessItem', setEnvironmentVariableOption, activitySuccessContext]),
label: localize('skipEnvVarsLabel',
'Skip environment variable configuration' +
(setEnvironmentVariableOption === SetEnvironmentVariableOption.NoDotEnv ? ' (no .env files found)' : '')
),
iconPath: activitySuccessIcon
})
);
const logMessage: string = localize('skippedEnvVarsMessage',
'Skipped environment variable configuration for the container app' +
(setEnvironmentVariableOption === SetEnvironmentVariableOption.NoDotEnv ? ' because no .env files were detected.' : '.')
);
ext.outputChannel.appendLog(logMessage);
} else if (setEnvironmentVariableOption === SetEnvironmentVariableOption.ProvideFile) {
context.activityChildren?.push(
new GenericTreeItem(undefined, {
contextValue: createUniversallyUniqueContextValue(['environmentVariablesListStepSuccessItem', activitySuccessContext]),
label: localize('saveEnvVarsFileLabel', 'Save environment variables using provided .env file'),
iconPath: activitySuccessIcon
})
);
ext.outputChannel.appendLog(localize('savedEnvVarsFileMessage', 'Saved environment variables using provided .env file "{0}".', context.envPath));
} else if (setEnvironmentVariableOption === SetEnvironmentVariableOption.UseExisting) {
context.activityChildren?.push(
new GenericTreeItem(undefined, {
contextValue: createUniversallyUniqueContextValue(['environmentVariablesListStepSuccessItem', activitySuccessContext]),
label: localize('useExistingEnvVarsLabel', 'Use existing environment variable configuration'),
iconPath: activitySuccessIcon
})
);
ext.outputChannel.appendLog(localize('useExistingEnvVarsMessage', 'Used existing environment variable configuration.'));
}
}
}