1
1
import * as fs from 'node:fs' ;
2
2
import * as path from 'node:path' ;
3
- import { computed , executeCommand , ref , useAllExtensions , useVscodeContext , watchEffect } from " reactive-vscode" ;
3
+ import { computed , executeCommand , useAllExtensions , useVscodeContext , watchEffect } from ' reactive-vscode' ;
4
4
import * as semver from 'semver' ;
5
5
import * as vscode from 'vscode' ;
6
6
import { incompatibleExtensions , unknownExtensions } from './compatibility' ;
7
7
import { config } from './config' ;
8
8
9
9
const extensions = useAllExtensions ( ) ;
10
10
11
- export const enabledHybridMode = ref < boolean > ( true ) ;
11
+ export const enabledHybridMode = computed ( ( ) => {
12
+ if ( config . server . hybridMode === 'typeScriptPluginOnly' ) {
13
+ return false ;
14
+ }
15
+ else if ( config . server . hybridMode === 'auto' ) {
16
+ if (
17
+ incompatibleExtensions . value . length ||
18
+ unknownExtensions . value . length
19
+ ) {
20
+ return false ;
21
+ }
22
+ else if (
23
+ ( vscodeTsdkVersion . value && ! semver . gte ( vscodeTsdkVersion . value , '5.3.0' ) ) ||
24
+ ( workspaceTsdkVersion . value && ! semver . gte ( workspaceTsdkVersion . value , '5.3.0' ) )
25
+ ) {
26
+ return false ;
27
+ }
28
+ return true ;
29
+ }
30
+ return config . server . hybridMode ;
31
+ } )
12
32
13
33
export const enabledTypeScriptPlugin = computed ( ( ) => {
14
34
return (
15
35
enabledHybridMode . value ||
16
- config . server . hybridMode === " typeScriptPluginOnly"
36
+ config . server . hybridMode === ' typeScriptPluginOnly'
17
37
) ;
18
38
} ) ;
19
39
20
40
const vscodeTsdkVersion = computed ( ( ) => {
21
41
const nightly = extensions . value . find (
22
- ( { id } ) => id === " ms-vscode.vscode-typescript-next"
42
+ ( { id } ) => id === ' ms-vscode.vscode-typescript-next'
23
43
) ;
24
44
if ( nightly ) {
25
45
const libPath = path . join (
26
- nightly . extensionPath . replace ( / \\ / g, "/" ) ,
27
- " node_modules/typescript/lib"
46
+ nightly . extensionPath . replace ( / \\ / g, '/' ) ,
47
+ ' node_modules/typescript/lib'
28
48
) ;
29
49
return getTsVersion ( libPath ) ;
30
50
}
31
51
32
52
if ( vscode . env . appRoot ) {
33
53
const libPath = path . join (
34
- vscode . env . appRoot . replace ( / \\ / g, "/" ) ,
35
- " extensions/node_modules/typescript/lib"
54
+ vscode . env . appRoot . replace ( / \\ / g, '/' ) ,
55
+ ' extensions/node_modules/typescript/lib'
36
56
) ;
37
57
return getTsVersion ( libPath ) ;
38
58
}
39
59
} ) ;
40
60
41
61
const workspaceTsdkVersion = computed ( ( ) => {
42
62
const libPath = vscode . workspace
43
- . getConfiguration ( " typescript" )
44
- . get < string > ( " tsdk" )
45
- ?. replace ( / \\ / g, "/" ) ;
63
+ . getConfiguration ( ' typescript' )
64
+ . get < string > ( ' tsdk' )
65
+ ?. replace ( / \\ / g, '/' ) ;
46
66
if ( libPath ) {
47
67
return getTsVersion ( libPath ) ;
48
68
}
49
69
} ) ;
50
70
51
71
export function useHybridModeTips ( ) {
52
- useVscodeContext ( " vueHybridMode" , enabledHybridMode ) ;
72
+ useVscodeContext ( ' vueHybridMode' , enabledHybridMode ) ;
53
73
54
74
watchEffect ( ( ) => {
55
- switch ( config . server . hybridMode ) {
56
- case "typeScriptPluginOnly" : {
57
- enabledHybridMode . value = false ;
58
- break ;
59
- }
60
- case "auto" : {
61
- if (
62
- incompatibleExtensions . value . length ||
63
- unknownExtensions . value . length
64
- ) {
65
- vscode . window
66
- . showInformationMessage (
67
- `Hybrid Mode is disabled automatically because there is a potentially incompatible ${ [
68
- ...incompatibleExtensions . value ,
69
- ...unknownExtensions . value ,
70
- ] . join ( ", " ) } TypeScript plugin installed.`,
71
- "Open Settings" ,
72
- "Report a false positive"
73
- )
74
- . then ( value => {
75
- if ( value === "Open Settings" ) {
76
- executeCommand (
77
- "workbench.action.openSettings" ,
78
- "vue.server.hybridMode"
79
- ) ;
80
- }
81
- else if ( value == "Report a false positive" ) {
82
- vscode . env . openExternal (
83
- vscode . Uri . parse (
84
- "https://github.com/vuejs/language-tools/pull/4206"
85
- )
86
- ) ;
87
- }
88
- } ) ;
89
- enabledHybridMode . value = false ;
90
- }
91
- else if (
92
- ( vscodeTsdkVersion . value && ! semver . gte ( vscodeTsdkVersion . value , "5.3.0" ) ) ||
93
- ( workspaceTsdkVersion . value && ! semver . gte ( workspaceTsdkVersion . value , "5.3.0" ) )
94
- ) {
95
- let msg = `Hybrid Mode is disabled automatically because TSDK >= 5.3.0 is required (VSCode TSDK: ${ vscodeTsdkVersion . value } ` ;
96
- if ( workspaceTsdkVersion . value ) {
97
- msg += `, Workspace TSDK: ${ workspaceTsdkVersion . value } ` ;
98
- }
99
- msg += `).` ;
100
- vscode . window
101
- . showInformationMessage ( msg , "Open Settings" )
102
- . then ( value => {
103
- if ( value === "Open Settings" ) {
104
- executeCommand (
105
- "workbench.action.openSettings" ,
106
- "vue.server.hybridMode"
107
- ) ;
108
- }
109
- } ) ;
110
- enabledHybridMode . value = false ;
111
- } else {
112
- enabledHybridMode . value = true ;
113
- }
114
- break ;
75
+ if ( config . server . hybridMode === 'auto' ) {
76
+ if (
77
+ incompatibleExtensions . value . length ||
78
+ unknownExtensions . value . length
79
+ ) {
80
+ vscode . window
81
+ . showInformationMessage (
82
+ `Hybrid Mode is disabled automatically because there is a potentially incompatible ${ [
83
+ ...incompatibleExtensions . value ,
84
+ ...unknownExtensions . value ,
85
+ ] . join ( ', ' ) } TypeScript plugin installed.`,
86
+ 'Open Settings' ,
87
+ 'Report a false positive'
88
+ )
89
+ . then ( value => {
90
+ if ( value === 'Open Settings' ) {
91
+ executeCommand (
92
+ 'workbench.action.openSettings' ,
93
+ 'vue.server.hybridMode'
94
+ ) ;
95
+ }
96
+ else if ( value == 'Report a false positive' ) {
97
+ vscode . env . openExternal (
98
+ vscode . Uri . parse (
99
+ 'https://github.com/vuejs/language-tools/pull/4206'
100
+ )
101
+ ) ;
102
+ }
103
+ } ) ;
115
104
}
116
- default : {
117
- if (
118
- config . server . hybridMode &&
119
- incompatibleExtensions . value . length
120
- ) {
121
- vscode . window
122
- . showWarningMessage (
123
- `You have explicitly enabled Hybrid Mode, but you have installed known incompatible extensions: ${ incompatibleExtensions . value . join (
124
- ", "
125
- ) } . You may want to change vue.server.hybridMode to "auto" to avoid compatibility issues.`,
126
- "Open Settings" ,
127
- "Report a false positive"
128
- )
129
- . then ( value => {
130
- if ( value === "Open Settings" ) {
131
- executeCommand (
132
- "workbench.action.openSettings" ,
133
- "vue.server.hybridMode"
134
- ) ;
135
- } else if ( value == "Report a false positive" ) {
136
- vscode . env . openExternal (
137
- vscode . Uri . parse (
138
- "https://github.com/vuejs/language-tools/pull/4206"
139
- )
140
- ) ;
141
- }
142
- } ) ;
105
+ else if (
106
+ ( vscodeTsdkVersion . value && ! semver . gte ( vscodeTsdkVersion . value , '5.3.0' ) ) ||
107
+ ( workspaceTsdkVersion . value && ! semver . gte ( workspaceTsdkVersion . value , '5.3.0' ) )
108
+ ) {
109
+ let msg = `Hybrid Mode is disabled automatically because TSDK >= 5.3.0 is required (VSCode TSDK: ${ vscodeTsdkVersion . value } ` ;
110
+ if ( workspaceTsdkVersion . value ) {
111
+ msg += `, Workspace TSDK: ${ workspaceTsdkVersion . value } ` ;
143
112
}
144
- enabledHybridMode . value = config . server . hybridMode ;
113
+ msg += `).` ;
114
+ vscode . window
115
+ . showInformationMessage ( msg , 'Open Settings' )
116
+ . then ( value => {
117
+ if ( value === 'Open Settings' ) {
118
+ executeCommand (
119
+ 'workbench.action.openSettings' ,
120
+ 'vue.server.hybridMode'
121
+ ) ;
122
+ }
123
+ } ) ;
145
124
}
146
125
}
126
+ else if ( config . server . hybridMode && incompatibleExtensions . value . length ) {
127
+ vscode . window
128
+ . showWarningMessage (
129
+ `You have explicitly enabled Hybrid Mode, but you have installed known incompatible extensions: ${ incompatibleExtensions . value . join (
130
+ ', '
131
+ ) } . You may want to change vue.server.hybridMode to "auto" to avoid compatibility issues.`,
132
+ 'Open Settings' ,
133
+ 'Report a false positive'
134
+ )
135
+ . then ( value => {
136
+ if ( value === 'Open Settings' ) {
137
+ executeCommand (
138
+ 'workbench.action.openSettings' ,
139
+ 'vue.server.hybridMode'
140
+ ) ;
141
+ }
142
+ else if ( value == 'Report a false positive' ) {
143
+ vscode . env . openExternal (
144
+ vscode . Uri . parse (
145
+ 'https://github.com/vuejs/language-tools/pull/4206'
146
+ )
147
+ ) ;
148
+ }
149
+ } ) ;
150
+ }
147
151
} ) ;
148
152
}
149
153
150
154
export function useHybridModeStatusItem ( ) {
151
155
const item = vscode . languages . createLanguageStatusItem (
152
- " vue-hybrid-mode" ,
156
+ ' vue-hybrid-mode' ,
153
157
config . server . includeLanguages
154
158
) ;
155
159
156
- item . text = " Hybrid Mode" ;
160
+ item . text = ' Hybrid Mode' ;
157
161
item . detail =
158
- ( enabledHybridMode . value ? " Enabled" : " Disabled" ) +
159
- ( config . server . hybridMode === " auto" ? " (Auto)" : "" ) ;
162
+ ( enabledHybridMode . value ? ' Enabled' : ' Disabled' ) +
163
+ ( config . server . hybridMode === ' auto' ? ' (Auto)' : '' ) ;
160
164
item . command = {
161
- title : " Open Setting" ,
162
- command : " workbench.action.openSettings" ,
163
- arguments : [ " vue.server.hybridMode" ] ,
165
+ title : ' Open Setting' ,
166
+ command : ' workbench.action.openSettings' ,
167
+ arguments : [ ' vue.server.hybridMode' ] ,
164
168
} ;
165
169
166
170
if ( ! enabledHybridMode . value ) {
@@ -170,11 +174,11 @@ export function useHybridModeStatusItem() {
170
174
171
175
function getTsVersion ( libPath : string ) {
172
176
try {
173
- const p = libPath . toString ( ) . split ( "/" ) ;
177
+ const p = libPath . toString ( ) . split ( '/' ) ;
174
178
const p2 = p . slice ( 0 , - 1 ) ;
175
- const modulePath = p2 . join ( "/" ) ;
176
- const filePath = modulePath + " /package.json" ;
177
- const contents = fs . readFileSync ( filePath , " utf-8" ) ;
179
+ const modulePath = p2 . join ( '/' ) ;
180
+ const filePath = modulePath + ' /package.json' ;
181
+ const contents = fs . readFileSync ( filePath , ' utf-8' ) ;
178
182
179
183
if ( contents === undefined ) {
180
184
return ;
@@ -183,7 +187,8 @@ function getTsVersion(libPath: string) {
183
187
let desc : any = null ;
184
188
try {
185
189
desc = JSON . parse ( contents ) ;
186
- } catch ( err ) {
190
+ }
191
+ catch ( err ) {
187
192
return ;
188
193
}
189
194
if ( ! desc || ! desc . version ) {
0 commit comments