-
Notifications
You must be signed in to change notification settings - Fork 427
Expand file tree
/
Copy pathenvVarSync.ts
More file actions
95 lines (87 loc) · 3.09 KB
/
Copy pathenvVarSync.ts
File metadata and controls
95 lines (87 loc) · 3.09 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.
import * as vscode from "vscode";
/**
* Helpers that update a {@link vscode.EnvironmentVariableCollection} only when
* the resulting mutation would actually change the collection.
*
* Background: VS Code shows a "Restart terminal to apply environment variable
* changes" prompt whenever an extension's collection differs from what has
* already been applied to running terminals. Calling `replace()` / `append()`
* with the same value, or calling `clear()` followed by re-adding identical
* entries, still counts as a change and re-triggers the prompt on every
* window reload. See issue #1647.
*
* These helpers compare the existing mutator (type + value + options) against
* the desired one and skip the write entirely when they match.
*/
const DEFAULT_OPTIONS: Required<vscode.EnvironmentVariableMutatorOptions> = {
applyAtProcessCreation: true,
applyAtShellIntegration: false,
};
function normalizeOptions(
options?: vscode.EnvironmentVariableMutatorOptions,
): Required<vscode.EnvironmentVariableMutatorOptions> {
return { ...DEFAULT_OPTIONS, ...options };
}
function sameOptions(
existing: vscode.EnvironmentVariableMutator,
desired?: vscode.EnvironmentVariableMutatorOptions,
): boolean {
const e = normalizeOptions(existing.options);
const d = normalizeOptions(desired);
return e.applyAtProcessCreation === d.applyAtProcessCreation
&& e.applyAtShellIntegration === d.applyAtShellIntegration;
}
/**
* Calls `collection.replace(variable, value, options)` only when the existing
* mutator (if any) does not already match.
*
* @returns `true` if the collection was actually written to.
*/
export function applyReplaceIfChanged(
collection: vscode.EnvironmentVariableCollection,
variable: string,
value: string,
options?: vscode.EnvironmentVariableMutatorOptions,
): boolean {
const existing = collection.get(variable);
if (existing
&& existing.type === vscode.EnvironmentVariableMutatorType.Replace
&& existing.value === value
&& sameOptions(existing, options)) {
return false;
}
if (options) {
collection.replace(variable, value, options);
} else {
collection.replace(variable, value);
}
return true;
}
/**
* Calls `collection.append(variable, value, options)` only when the existing
* mutator (if any) does not already match.
*
* @returns `true` if the collection was actually written to.
*/
export function applyAppendIfChanged(
collection: vscode.EnvironmentVariableCollection,
variable: string,
value: string,
options?: vscode.EnvironmentVariableMutatorOptions,
): boolean {
const existing = collection.get(variable);
if (existing
&& existing.type === vscode.EnvironmentVariableMutatorType.Append
&& existing.value === value
&& sameOptions(existing, options)) {
return false;
}
if (options) {
collection.append(variable, value, options);
} else {
collection.append(variable, value);
}
return true;
}