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+
6+ const fs = require ( 'fs' ) ;
7+ const path = require ( 'path' ) ;
8+ const os = require ( 'os' ) ;
9+ const { remote } = require ( 'electron' ) ;
10+ const dialog = remote . dialog ;
11+
12+ const builtInExtensionsPath = path . join ( __dirname , '..' , 'builtInExtensions.json' ) ;
13+ const controlFilePath = path . join ( os . homedir ( ) , '.vscode-oss-dev' , 'extensions' , 'control.json' ) ;
14+
15+ function readJson ( filePath ) {
16+ return JSON . parse ( fs . readFileSync ( filePath ) ) ;
17+ }
18+
19+ function writeJson ( filePath , obj ) {
20+ fs . writeFileSync ( filePath , JSON . stringify ( obj , null , 2 ) ) ;
21+ }
22+
23+ function renderOption ( form , id , title , value , checked ) {
24+ const input = document . createElement ( 'input' ) ;
25+ input . type = 'radio' ;
26+ input . id = id ;
27+ input . name = 'choice' ;
28+ input . value = value ;
29+ input . checked = ! ! checked ;
30+ form . appendChild ( input ) ;
31+
32+ const label = document . createElement ( 'label' ) ;
33+ label . setAttribute ( 'for' , id ) ;
34+ label . textContent = title ;
35+ form . appendChild ( label ) ;
36+
37+ return input ;
38+ }
39+
40+ function render ( el , state ) {
41+ function setState ( state ) {
42+ try {
43+ writeJson ( controlFilePath , state . control ) ;
44+ } catch ( err ) {
45+ console . error ( err ) ;
46+ }
47+
48+ el . innerHTML = '' ;
49+ render ( el , state ) ;
50+ }
51+
52+ const ul = document . createElement ( 'ul' ) ;
53+ const { builtin, control } = state ;
54+
55+ for ( const ext of builtin ) {
56+ const controlState = control [ ext . name ] || 'marketplace' ;
57+
58+ const li = document . createElement ( 'li' ) ;
59+ ul . appendChild ( li ) ;
60+
61+ const name = document . createElement ( 'code' ) ;
62+ name . textContent = ext . name ;
63+ li . appendChild ( name ) ;
64+
65+ const form = document . createElement ( 'form' ) ;
66+ li . appendChild ( form ) ;
67+
68+ const marketplaceInput = renderOption ( form , `marketplace-${ ext . name } ` , 'Marketplace' , 'marketplace' , controlState === 'marketplace' ) ;
69+ marketplaceInput . onchange = function ( ) {
70+ control [ ext . name ] = 'marketplace' ;
71+ setState ( { builtin, control } ) ;
72+ } ;
73+
74+ const disabledInput = renderOption ( form , `disabled-${ ext . name } ` , 'Disabled' , 'disabled' , controlState === 'disabled' ) ;
75+ disabledInput . onchange = function ( ) {
76+ control [ ext . name ] = 'disabled' ;
77+ setState ( { builtin, control } ) ;
78+ } ;
79+
80+ let local = undefined ;
81+
82+ if ( controlState !== 'marketplace' && controlState !== 'disabled' ) {
83+ local = controlState ;
84+ }
85+
86+ const localInput = renderOption ( form , `local-${ ext . name } ` , 'Local' , 'local' , ! ! local ) ;
87+ localInput . onchange = function ( ) {
88+ const result = dialog . showOpenDialog ( remote . getCurrentWindow ( ) , {
89+ title : 'Choose Folder' ,
90+ properties : [ 'openDirectory' ]
91+ } ) ;
92+
93+ if ( result && result . length >= 1 ) {
94+ control [ ext . name ] = result [ 0 ] ;
95+ }
96+
97+ setState ( { builtin, control } ) ;
98+ } ;
99+
100+ if ( local ) {
101+ const localSpan = document . createElement ( 'code' ) ;
102+ localSpan . className = 'local' ;
103+ localSpan . textContent = local ;
104+ form . appendChild ( localSpan ) ;
105+ }
106+ }
107+
108+ el . appendChild ( ul ) ;
109+ }
110+
111+ function main ( ) {
112+ const el = document . getElementById ( 'extensions' ) ;
113+ const builtin = readJson ( builtInExtensionsPath ) ;
114+ let control ;
115+
116+ try {
117+ control = readJson ( controlFilePath ) ;
118+ } catch ( err ) {
119+ control = { } ;
120+ }
121+
122+ render ( el , { builtin, control } ) ;
123+ }
124+
125+ window . onload = main ;
0 commit comments