Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/build/builtin/browser-main.js
3520 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
//@ts-check
6
7
const fs = require('fs');
8
const path = require('path');
9
const os = require('os');
10
const { ipcRenderer } = require('electron');
11
12
const builtInExtensionsPath = path.join(__dirname, '..', '..', 'product.json');
13
const controlFilePath = path.join(os.homedir(), '.vscode-oss-dev', 'extensions', 'control.json');
14
15
/**
16
* @param {string} filePath
17
*/
18
function readJson(filePath) {
19
return JSON.parse(fs.readFileSync(filePath, { encoding: 'utf8' }));
20
}
21
22
/**
23
* @param {string} filePath
24
* @param {any} obj
25
*/
26
function writeJson(filePath, obj) {
27
fs.writeFileSync(filePath, JSON.stringify(obj, null, 2));
28
}
29
30
/**
31
* @param {HTMLFormElement} form
32
* @param {string} id
33
* @param {string} title
34
* @param {string} value
35
* @param {boolean} checked
36
*/
37
function renderOption(form, id, title, value, checked) {
38
const input = document.createElement('input');
39
input.type = 'radio';
40
input.id = id;
41
input.name = 'choice';
42
input.value = value;
43
input.checked = !!checked;
44
form.appendChild(input);
45
46
const label = document.createElement('label');
47
label.setAttribute('for', id);
48
label.textContent = title;
49
form.appendChild(label);
50
51
return input;
52
}
53
54
/**
55
* @param {HTMLElement} el
56
* @param {any} state
57
*/
58
function render(el, state) {
59
/**
60
* @param {any} state
61
*/
62
function setState(state) {
63
try {
64
writeJson(controlFilePath, state.control);
65
} catch (err) {
66
console.error(err);
67
}
68
69
el.innerHTML = '';
70
render(el, state);
71
}
72
73
const ul = document.createElement('ul');
74
const { builtin, control } = state;
75
76
for (const ext of builtin) {
77
const controlState = control[ext.name] || 'marketplace';
78
79
const li = document.createElement('li');
80
ul.appendChild(li);
81
82
const name = document.createElement('code');
83
name.textContent = ext.name;
84
li.appendChild(name);
85
86
const form = document.createElement('form');
87
li.appendChild(form);
88
89
const marketplaceInput = renderOption(form, `marketplace-${ext.name}`, 'Marketplace', 'marketplace', controlState === 'marketplace');
90
marketplaceInput.onchange = function () {
91
control[ext.name] = 'marketplace';
92
setState({ builtin, control });
93
};
94
95
const disabledInput = renderOption(form, `disabled-${ext.name}`, 'Disabled', 'disabled', controlState === 'disabled');
96
disabledInput.onchange = function () {
97
control[ext.name] = 'disabled';
98
setState({ builtin, control });
99
};
100
101
let local = undefined;
102
103
if (controlState !== 'marketplace' && controlState !== 'disabled') {
104
local = controlState;
105
}
106
107
const localInput = renderOption(form, `local-${ext.name}`, 'Local', 'local', !!local);
108
localInput.onchange = async function () {
109
const result = await ipcRenderer.invoke('pickdir');
110
111
if (result) {
112
control[ext.name] = result;
113
setState({ builtin, control });
114
}
115
};
116
117
if (local) {
118
const localSpan = document.createElement('code');
119
localSpan.className = 'local';
120
localSpan.textContent = local;
121
form.appendChild(localSpan);
122
}
123
}
124
125
el.appendChild(ul);
126
}
127
128
function main() {
129
const el = document.getElementById('extensions');
130
const builtin = readJson(builtInExtensionsPath).builtInExtensions;
131
let control;
132
133
try {
134
control = readJson(controlFilePath);
135
} catch (err) {
136
control = {};
137
}
138
139
if (el) {
140
render(el, { builtin, control });
141
}
142
}
143
144
window.onload = main;
145
146