-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.js
109 lines (94 loc) · 2.9 KB
/
extension.js
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
const vscode = require("vscode");
const settings = require("./settings.json");
async function activate(context) {
const newVersion = isNewVersion(context);
if (newVersion) {
updateVersionState(context, newVersion);
await updateSettings();
}
const disposable = vscode.commands.registerCommand(
"angular-enterprise.clearVersionState",
async () => {
clearVersionState(context);
}
);
context.subscriptions.push(disposable);
}
function deactivate() {}
module.exports = {
activate,
deactivate,
};
/**
* Checks if a new version is available.
* @param {vscode.ExtensionContext} context The extension context.
* @returns { string | null } The new version number if found, null if not found.
*/
function isNewVersion(context) {
const version = context.extension.packageJSON.version;
const versionState = context.globalState.get(context.extension.id);
return version === versionState ? null : version;
}
/**
* Updates the global extension version state.
* @param {vscode.ExtensionContext} context The extension context.
* @param {string} version The new version to be set.
*/
function updateVersionState(context, version) {
context.globalState.update(context.extension.id, version);
}
/**
* Clears the global extension version state.
* @param {vscode.ExtensionContext} context The extension context.
*/
async function clearVersionState(context) {
context.globalState.update(context.extension.id, undefined).then(
async () => {
await vscode.window.showInformationMessage(
"✅ Angular Enterprise cleared its version state."
);
},
async () => {
await vscode.window.showInformationMessage(
"❌ Angular Enterprise failed to clear its version state. See the Debug Console for more information."
);
}
);
}
/**
* Updates the global settings. Will append to Object configs and overwrite specific id configuration.
*/
async function updateSettings() {
const ids = Object.keys(settings);
let failed = [];
ids.forEach(async (id) => {
let appendTo = null;
if (isObject(settings[id])) {
appendTo = { ...vscode.workspace.getConfiguration(id), ...settings[id] };
}
await vscode.workspace
.getConfiguration()
.update(id, appendTo ?? settings[id], vscode.ConfigurationTarget.Global)
.then(
() => {},
() => {
failed.push(id);
}
);
});
if (failed.length) {
await vscode.window.showErrorMessage(
`❌ Angular Enterprise failed to configure ${failed.length} setting(s). See the Debug Console for more information.`
);
} else {
await vscode.window.showInformationMessage(
"✅ Angular Enterprise successfully updated global settings. 🚀"
);
}
}
/**
*
* @param { any } obj
* @returns { boolean } Returns true if input is an Object. Otherwise, returns false.
*/
const isObject = (obj) => (obj ?? false)?.constructor?.name === "Object";